diff --git a/front/Utils.ts b/front/Utils.ts new file mode 100644 index 0000000..5b604a0 --- /dev/null +++ b/front/Utils.ts @@ -0,0 +1,9 @@ +export function calculateRatio(it: { x: number, y: number }, parent: DOMRect): { x: number, y: number } { + const relativeXPixels = it.x - parent.x; + const relativeYPixels = it.y - parent.y; + + const xRatio = relativeXPixels / parent.width; + const yRatio = relativeYPixels / parent.height; + + return {x: xRatio, y: yRatio} +} diff --git a/front/components/editor/BasketCourt.tsx b/front/components/editor/BasketCourt.tsx index 7eab38c..4ae8c5f 100644 --- a/front/components/editor/BasketCourt.tsx +++ b/front/components/editor/BasketCourt.tsx @@ -21,6 +21,7 @@ export function BasketCourt({players, onPlayerRemove, onPlayerChange}: BasketCou player={player} onChange={onPlayerChange} onRemove={() => onPlayerRemove(player)} + parentRef={divRef} /> })} diff --git a/front/components/editor/CourtPlayer.tsx b/front/components/editor/CourtPlayer.tsx index b8ff341..a94f957 100644 --- a/front/components/editor/CourtPlayer.tsx +++ b/front/components/editor/CourtPlayer.tsx @@ -1,52 +1,70 @@ -import {useRef} from "react"; -import "../../style/player.css"; -import RemoveIcon from "../../assets/icon/remove.svg?react"; -import Draggable from "react-draggable"; -import {PlayerPiece} from "./PlayerPiece"; -import {Player} from "../../tactic/Player"; +import { MutableRefObject, useEffect, useRef, useState } from "react" +import "../../style/player.css" +import RemoveIcon from "../../assets/icon/remove.svg?react" +import Draggable from "react-draggable" +import { PlayerPiece } from "./PlayerPiece" +import { Player } from "../../tactic/Player" +import { calculateRatio } from "../../Utils" export interface PlayerProps { player: Player, onChange: (p: Player) => void, onRemove: () => void + parentRef: MutableRefObject } /** * A player that is placed on the court, which can be selected, and moved in the associated bounds * */ +export default function CourtPlayer({ + player, + onChange, + onRemove, + parentRef, +}: PlayerProps) { + const pieceRef = useRef(null) -export default function CourtPlayer({player, onChange, onRemove}: PlayerProps) { - - const x = player.rightRatio - const y = player.bottomRatio + const [x, setX] = useState(player.rightRatio) + const [y, setY] = useState(player.bottomRatio) return ( onChange({ - id: player.id, - rightRatio: player.rightRatio, - bottomRatio: player.bottomRatio, - team: player.team, - role: player.role - })} - > -
- -
{ - if (e.key == "Delete") - onRemove() - }}> + position={{ x, y }} + onStop={() => { + const pieceBounds = pieceRef.current!.getBoundingClientRect() + const parentBounds = parentRef.current!.getBoundingClientRect() + + const { x, y } = calculateRatio(pieceBounds, parentBounds) + + setX(x) + setY(y) + + onChange({ + id: player.id, + rightRatio: x, + bottomRatio: y, + team: player.team, + role: player.role, + }) + }}> +
+
{ + if (e.key == "Delete") onRemove() + }}>
{ - return [ - ...players, - { - id: players.length, - team: element.team, - role: element.key, - rightRatio: xRatio, - bottomRatio: yRatio, - }, - ] + + const {x, y} = calculateRatio(refBounds, courtBounds) + + setPlayers(players => { + return [...players, { + id: players.length, + team: element.team, + role: element.key, + rightRatio: x, + bottomRatio: y + }] }) }