|
|
@ -1,51 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
import {useRef} from "react";
|
|
|
|
import { MutableRefObject, useEffect, useRef, useState } from "react"
|
|
|
|
import "../../style/player.css";
|
|
|
|
import "../../style/player.css"
|
|
|
|
import RemoveIcon from "../../assets/icon/remove.svg?react";
|
|
|
|
import RemoveIcon from "../../assets/icon/remove.svg?react"
|
|
|
|
import Draggable from "react-draggable";
|
|
|
|
import Draggable from "react-draggable"
|
|
|
|
import {PlayerPiece} from "./PlayerPiece";
|
|
|
|
import { PlayerPiece } from "./PlayerPiece"
|
|
|
|
import {Player} from "../../tactic/Player";
|
|
|
|
import { Player } from "../../tactic/Player"
|
|
|
|
|
|
|
|
import { calculateRatio } from "../../Utils"
|
|
|
|
|
|
|
|
|
|
|
|
export interface PlayerProps {
|
|
|
|
export interface PlayerProps {
|
|
|
|
player: Player,
|
|
|
|
player: Player,
|
|
|
|
onChange: (p: Player) => void,
|
|
|
|
onChange: (p: Player) => void,
|
|
|
|
onRemove: () => void
|
|
|
|
onRemove: () => void
|
|
|
|
|
|
|
|
parentRef: MutableRefObject<HTMLElement>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* A player that is placed on the court, which can be selected, and moved in the associated bounds
|
|
|
|
* 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<HTMLDivElement>(null)
|
|
|
|
|
|
|
|
|
|
|
|
export default function CourtPlayer({player, onChange, onRemove}: PlayerProps) {
|
|
|
|
const [x, setX] = useState(player.rightRatio)
|
|
|
|
|
|
|
|
const [y, setY] = useState(player.bottomRatio)
|
|
|
|
const x = player.rightRatio
|
|
|
|
|
|
|
|
const y = player.bottomRatio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<Draggable
|
|
|
|
<Draggable
|
|
|
|
handle={".player-piece"}
|
|
|
|
handle={".player-piece"}
|
|
|
|
|
|
|
|
nodeRef={pieceRef}
|
|
|
|
bounds="parent"
|
|
|
|
bounds="parent"
|
|
|
|
defaultPosition={{x, y}}
|
|
|
|
position={{ x, y }}
|
|
|
|
onStop={() => onChange({
|
|
|
|
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,
|
|
|
|
id: player.id,
|
|
|
|
rightRatio: player.rightRatio,
|
|
|
|
rightRatio: x,
|
|
|
|
bottomRatio: player.bottomRatio,
|
|
|
|
bottomRatio: y,
|
|
|
|
team: player.team,
|
|
|
|
team: player.team,
|
|
|
|
role: player.role
|
|
|
|
role: player.role,
|
|
|
|
})}
|
|
|
|
})
|
|
|
|
>
|
|
|
|
}}>
|
|
|
|
<div className={"player"}
|
|
|
|
<div
|
|
|
|
|
|
|
|
ref={pieceRef}
|
|
|
|
|
|
|
|
className={"player"}
|
|
|
|
style={{
|
|
|
|
style={{
|
|
|
|
position: "absolute",
|
|
|
|
position: "absolute",
|
|
|
|
left: `${x * 100}%`,
|
|
|
|
left: `${x * 100}%`,
|
|
|
|
top: `${y * 100}%`,
|
|
|
|
top: `${y * 100}%`,
|
|
|
|
}}>
|
|
|
|
}}>
|
|
|
|
|
|
|
|
<div
|
|
|
|
<div tabIndex={0}
|
|
|
|
tabIndex={0}
|
|
|
|
className="player-content"
|
|
|
|
className="player-content"
|
|
|
|
onKeyUp={e => {
|
|
|
|
onKeyUp={(e) => {
|
|
|
|
if (e.key == "Delete")
|
|
|
|
if (e.key == "Delete") onRemove()
|
|
|
|
onRemove()
|
|
|
|
|
|
|
|
}}>
|
|
|
|
}}>
|
|
|
|
<div className="player-selection-tab">
|
|
|
|
<div className="player-selection-tab">
|
|
|
|
<RemoveIcon
|
|
|
|
<RemoveIcon
|
|
|
|