|
|
@ -12,22 +12,30 @@ const ERROR_STYLE: CSSProperties = {
|
|
|
|
borderColor: "red"
|
|
|
|
borderColor: "red"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* information about a player that is into a rack
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
interface RackedPlayer {
|
|
|
|
|
|
|
|
team: "allies" | "opponents",
|
|
|
|
|
|
|
|
key: string,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default function Editor({id, name}: { id: number, name: string }) {
|
|
|
|
export default function Editor({id, name}: { id: number, name: string }) {
|
|
|
|
const [style, setStyle] = useState<CSSProperties>({});
|
|
|
|
const [style, setStyle] = useState<CSSProperties>({});
|
|
|
|
|
|
|
|
|
|
|
|
const positions = ["1", "2", "3", "4", "5"]
|
|
|
|
const positions = ["1", "2", "3", "4", "5"]
|
|
|
|
const [allies, setAllies] = useState(
|
|
|
|
const [allies, setAllies] = useState(
|
|
|
|
positions.map(pos => <PlayerPiece team="allies" key={pos} text={pos}/>)
|
|
|
|
positions.map(key => ({team: "allies", key}))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
const [opponents, setOpponents] = useState(
|
|
|
|
const [opponents, setOpponents] = useState(
|
|
|
|
positions.map(pos => <PlayerPiece team="opponents" key={pos} text={pos}/>)
|
|
|
|
positions.map(key => ({team: "opponents", key}))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const [players, setPlayers] = useState<Player[]>([]);
|
|
|
|
const [players, setPlayers] = useState<Player[]>([]);
|
|
|
|
const courtDivContentRef = useRef<HTMLDivElement>(null);
|
|
|
|
const courtDivContentRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
|
|
|
|
const canDetach = (ref: RefObject<HTMLDivElement>) => {
|
|
|
|
const canDetach = (ref: HTMLDivElement) => {
|
|
|
|
const refBounds = ref.current!.getBoundingClientRect();
|
|
|
|
const refBounds = ref.getBoundingClientRect();
|
|
|
|
const courtBounds = courtDivContentRef.current!.getBoundingClientRect();
|
|
|
|
const courtBounds = courtDivContentRef.current!.getBoundingClientRect();
|
|
|
|
|
|
|
|
|
|
|
|
// check if refBounds overlaps courtBounds
|
|
|
|
// check if refBounds overlaps courtBounds
|
|
|
@ -39,23 +47,23 @@ export default function Editor({id, name}: { id: number, name: string }) {
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const onElementDetach = (ref: RefObject<HTMLDivElement>, element: ReactElement) => {
|
|
|
|
const onElementDetach = (ref: HTMLDivElement, element: RackedPlayer) => {
|
|
|
|
const refBounds = ref.current!.getBoundingClientRect();
|
|
|
|
const refBounds = ref.getBoundingClientRect();
|
|
|
|
const courtBounds = courtDivContentRef.current!.getBoundingClientRect();
|
|
|
|
const courtBounds = courtDivContentRef.current!.getBoundingClientRect();
|
|
|
|
|
|
|
|
|
|
|
|
const relativeXPixels = refBounds.x - courtBounds.x;
|
|
|
|
const relativeXPixels = refBounds.x - courtBounds.x;
|
|
|
|
const relativeYPixels = refBounds.y - courtBounds.y;
|
|
|
|
const relativeYPixels = refBounds.y - courtBounds.y;
|
|
|
|
|
|
|
|
|
|
|
|
const xPercent = relativeXPixels / courtBounds.width;
|
|
|
|
const xRatio = relativeXPixels / courtBounds.width;
|
|
|
|
const yPercent = relativeYPixels / courtBounds.height;
|
|
|
|
const yRatio = relativeYPixels / courtBounds.height;
|
|
|
|
|
|
|
|
|
|
|
|
setPlayers(players => {
|
|
|
|
setPlayers(players => {
|
|
|
|
return [...players, {
|
|
|
|
return [...players, {
|
|
|
|
id: players.length,
|
|
|
|
id: players.length,
|
|
|
|
team: element.props.team,
|
|
|
|
team: element.team,
|
|
|
|
position: element.props.text,
|
|
|
|
position: element.key,
|
|
|
|
right_percentage: xPercent,
|
|
|
|
rightRatio: xRatio,
|
|
|
|
bottom_percentage: yPercent
|
|
|
|
bottomRatio: yRatio
|
|
|
|
}]
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -87,13 +95,17 @@ export default function Editor({id, name}: { id: number, name: string }) {
|
|
|
|
<div id="edit-div">
|
|
|
|
<div id="edit-div">
|
|
|
|
<div id="racks">
|
|
|
|
<div id="racks">
|
|
|
|
<Rack id="allies-rack"
|
|
|
|
<Rack id="allies-rack"
|
|
|
|
objects={[allies, setAllies]}
|
|
|
|
objects={allies}
|
|
|
|
|
|
|
|
onChange={setAllies}
|
|
|
|
canDetach={canDetach}
|
|
|
|
canDetach={canDetach}
|
|
|
|
onElementDetached={onElementDetach}/>
|
|
|
|
onElementDetached={onElementDetach}
|
|
|
|
|
|
|
|
render={({team, key}) => <PlayerPiece team={team} text={key} key={key}/>}/>
|
|
|
|
<Rack id="opponent-rack"
|
|
|
|
<Rack id="opponent-rack"
|
|
|
|
objects={[opponents, setOpponents]}
|
|
|
|
objects={opponents}
|
|
|
|
|
|
|
|
onChange={setOpponents}
|
|
|
|
canDetach={canDetach}
|
|
|
|
canDetach={canDetach}
|
|
|
|
onElementDetached={onElementDetach}/>
|
|
|
|
onElementDetached={onElementDetach}
|
|
|
|
|
|
|
|
render={({team, key}) => <PlayerPiece team={team} text={key} key={key}/>}/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div id="court-div">
|
|
|
|
<div id="court-div">
|
|
|
|
<div id="court-div-bounds" ref={courtDivContentRef}>
|
|
|
|
<div id="court-div-bounds" ref={courtDivContentRef}>
|
|
|
@ -104,16 +116,15 @@ export default function Editor({id, name}: { id: number, name: string }) {
|
|
|
|
const idx = players.indexOf(player)
|
|
|
|
const idx = players.indexOf(player)
|
|
|
|
return players.toSpliced(idx, 1)
|
|
|
|
return players.toSpliced(idx, 1)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
const piece = <PlayerPiece team={player.team} key={player.position} text={player.position}/>
|
|
|
|
|
|
|
|
switch (player.team) {
|
|
|
|
switch (player.team) {
|
|
|
|
case "opponents":
|
|
|
|
case "opponents":
|
|
|
|
setOpponents(opponents => (
|
|
|
|
setOpponents(opponents => (
|
|
|
|
[...opponents, piece]
|
|
|
|
[...opponents, {team: player.team, pos: player.position, key: player.position}]
|
|
|
|
))
|
|
|
|
))
|
|
|
|
break
|
|
|
|
break
|
|
|
|
case "allies":
|
|
|
|
case "allies":
|
|
|
|
setAllies(allies => (
|
|
|
|
setAllies(allies => (
|
|
|
|
[...allies, piece]
|
|
|
|
[...allies, {team: player.team, pos: player.position, key: player.position}]
|
|
|
|
))
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}/>
|
|
|
|
}}/>
|
|
|
|