From 984d14a55bf3e96a9c42327df5d14792f65bd58c Mon Sep 17 00:00:00 2001 From: Curt Grimes Date: Fri, 30 Sep 2022 23:49:44 -0700 Subject: [PATCH 1/2] Add ability to drag abilities into slots - Do not allow dragging an ability onto a slot that cannot accept that ability. - When dragging, dim slots that are not a valid target for the ability currently being dragged. - Do not make any changes to the existing click behavior to assign abilities. --- app/components/AbilitiesSelector.tsx | 132 ++++++++++++++++++++++----- app/components/Ability.tsx | 31 ++++++- app/components/Image.tsx | 1 + app/styles/common.css | 17 ++++ 4 files changed, 155 insertions(+), 26 deletions(-) diff --git a/app/components/AbilitiesSelector.tsx b/app/components/AbilitiesSelector.tsx index cfb059ce3..6920bb770 100644 --- a/app/components/AbilitiesSelector.tsx +++ b/app/components/AbilitiesSelector.tsx @@ -1,3 +1,5 @@ +import clsx from "clsx"; +import React from "react"; import invariant from "tiny-invariant"; import { abilities } from "~/modules/in-game-lists"; import type { BuildAbilitiesTupleWithUnknown } from "~/modules/in-game-lists/types"; @@ -40,6 +42,38 @@ export function AbilitiesSelector({ onChange(addAbility({ oldAbilities: selectedAbilities, ability })); }; + const [draggingAbility, setDraggingAbility] = React.useState< + typeof abilities[number] | undefined + >(); + + const onDragStart = + (ability: typeof abilities[number]) => (event: React.DragEvent) => { + setDraggingAbility(ability); + event.dataTransfer.setData("text/plain", JSON.stringify(ability)); + }; + + const onDragEnd = () => { + setDraggingAbility(undefined); + }; + + const onDrop = + (atRowIndex: number, atAbilityIndex: number) => + (event: React.DragEvent) => { + event.preventDefault(); + const ability = JSON.parse( + event.dataTransfer.getData("text/plain") + ) as typeof abilities[number]; + + onChange( + addAbility({ + oldAbilities: selectedAbilities, + ability, + atRowIndex, + atAbilityIndex, + }) + ); + }; + return (
@@ -50,6 +84,13 @@ export function AbilitiesSelector({ ability={ability} size={abilityI === 0 ? "MAIN" : "SUB"} onClick={() => onSlotClick({ rowI, abilityI })} + dragStarted={!!draggingAbility} + dropAllowed={canPlaceAbilityAtSlot( + rowI, + abilityI, + draggingAbility + )} + onDrop={onDrop(rowI, abilityI)} /> )) )} @@ -58,10 +99,15 @@ export function AbilitiesSelector({ {abilities.map((ability) => ( diff --git a/app/components/Image.tsx b/app/components/Image.tsx index c8c2631d6..5956b89f6 100644 --- a/app/components/Image.tsx +++ b/app/components/Image.tsx @@ -32,6 +32,7 @@ export function Image({ width={width} height={height} style={style} + draggable="false" /> ); diff --git a/app/styles/common.css b/app/styles/common.css index 86162dbba..8701f1c88 100644 --- a/app/styles/common.css +++ b/app/styles/common.css @@ -792,9 +792,22 @@ dialog::backdrop { background-size: 100%; border-radius: 50%; box-shadow: 0 0 0 1px var(--bg-ability); + transform: scale(1); + transition: all 0.1s ease; user-select: none; } +.build__ability.is-drag-target { + background: var(--abilities-button-bg); + transform: scale(1.15); +} + +.build__ability.drag-started:not(.drop-allowed) { + filter: grayscale(1); + opacity: 0.3; + pointer-events: none; +} + .build__bottom-row { display: flex; height: 100%; @@ -841,3 +854,7 @@ dialog::backdrop { background-color: var(--abilities-button-bg); border-radius: 50%; } + +.ability-selector__ability-button.is-dragging { + box-shadow: 0 0 100px inset rgb(255 255 255 / 25%); +} From ca1b1251cacf4645a45ca7f75f90cafe39d21367 Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Sat, 1 Oct 2022 11:22:45 +0300 Subject: [PATCH 2/2] Default values to Ability dragging related props --- app/components/Ability.tsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/components/Ability.tsx b/app/components/Ability.tsx index ffb1e2ee3..6870acfa1 100644 --- a/app/components/Ability.tsx +++ b/app/components/Ability.tsx @@ -13,15 +13,15 @@ const sizeMap = { export function Ability({ ability, size, - dragStarted, - dropAllowed, + dragStarted = false, + dropAllowed = false, onClick, onDrop, }: { ability: AbilityWithUnknown; size: keyof typeof sizeMap; - dragStarted: boolean; - dropAllowed: boolean; + dragStarted?: boolean; + dropAllowed?: boolean; onClick?: () => void; onDrop?: (event: React.DragEvent) => void; }) {