import { ReactNode, RefObject, useRef } from "react" import "../../style/player.css" import Draggable from "react-draggable" import { PlayerPiece } from "./PlayerPiece" import { Player } from "../../model/tactic/Player" import { NULL_POS, ratioWithinBase } from "../arrows/Pos" export interface PlayerProps { player: Player onDrag: () => void onChange: (p: Player) => void onRemove: () => void courtRef: RefObject availableActions: (ro: HTMLElement) => ReactNode[] } /** * A player that is placed on the court, which can be selected, and moved in the associated bounds * */ export default function CourtPlayer({ player, onDrag, onChange, onRemove, courtRef, availableActions, }: PlayerProps) { const hasBall = player.hasBall const x = player.rightRatio const y = player.bottomRatio const pieceRef = useRef(null) return ( { const pieceBounds = pieceRef.current!.getBoundingClientRect() const parentBounds = courtRef.current!.getBoundingClientRect() const { x, y } = ratioWithinBase(pieceBounds, parentBounds) onChange({ id: player.id, rightRatio: x, bottomRatio: y, team: player.team, role: player.role, hasBall: player.hasBall, }) }}>
{ if (e.key == "Delete") onRemove() }}>
{availableActions(pieceRef.current!)}
) }