can assign ball to a player, player with ball is surrounded
continuous-integration/drone/push Build is passing Details

pull/40/head
Vivien DUFOUR 1 year ago
parent 7ab18786cc
commit 7177c07ca0

@ -1,13 +1,26 @@
import React from "react"; import React, {RefObject} from "react";
import "../../style/ball.css"; import "../../style/ball.css";
import Ball from "../../assets/icon/ball.svg?react"; import Ball from "../../assets/icon/ball.svg?react";
import Draggable from "react-draggable";
export function BallPiece() { export interface BallPieceProps {
onDrop: () => void
pieceRef: RefObject<HTMLDivElement>
}
export function BallPiece({onDrop, pieceRef}: BallPieceProps) {
return ( return (
<div className={`ball-div`}> <Draggable
<Ball id={'ball'}/> onStop={onDrop}
nodeRef={pieceRef}
position={{x: 0, y: 0}}
>
<div className={`ball-div`} ref={pieceRef}>
<Ball className={'ball'}/>
</div> </div>
</Draggable>
) )
} }

@ -8,12 +8,14 @@ import { Player } from "../../tactic/Player"
export interface BasketCourtProps { export interface BasketCourtProps {
players: Player[] players: Player[]
onPlayerRemove: (p: Player) => void onPlayerRemove: (p: Player) => void
onBallDrop: (ref : HTMLDivElement) => void
onPlayerChange: (p: Player) => void onPlayerChange: (p: Player) => void
} }
export function BasketCourt({ export function BasketCourt({
players, players,
onPlayerRemove, onPlayerRemove,
onBallDrop,
onPlayerChange, onPlayerChange,
}: BasketCourtProps) { }: BasketCourtProps) {
const divRef = useRef<HTMLDivElement>(null) const divRef = useRef<HTMLDivElement>(null)
@ -28,6 +30,7 @@ export function BasketCourt({
player={player} player={player}
onChange={onPlayerChange} onChange={onPlayerChange}
onRemove={() => onPlayerRemove(player)} onRemove={() => onPlayerRemove(player)}
onBallDrop={onBallDrop}
parentRef={divRef} parentRef={divRef}
/> />
) )

@ -1,7 +1,7 @@
import { RefObject, useRef, useState } from "react" import { RefObject, 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 BallIcon from "../../assets/icon/ball.svg?react" import {BallPiece} from "./BallPiece";
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"
@ -11,6 +11,7 @@ export interface PlayerProps {
player: Player player: Player
onChange: (p: Player) => void onChange: (p: Player) => void
onRemove: () => void onRemove: () => void
onBallDrop: (ref: HTMLDivElement) => void
parentRef: RefObject<HTMLDivElement> parentRef: RefObject<HTMLDivElement>
} }
@ -21,12 +22,15 @@ export default function CourtPlayer({
player, player,
onChange, onChange,
onRemove, onRemove,
onBallDrop,
parentRef, parentRef,
}: PlayerProps) { }: PlayerProps) {
const pieceRef = useRef<HTMLDivElement>(null) const pieceRef = useRef<HTMLDivElement>(null)
const ballPiece = useRef<HTMLDivElement>(null)
const x = player.rightRatio const x = player.rightRatio
const y = player.bottomRatio const y = player.bottomRatio
const hasBall = player.hasBall
return ( return (
<Draggable <Draggable
@ -40,6 +44,7 @@ export default function CourtPlayer({
const { x, y } = calculateRatio(pieceBounds, parentBounds) const { x, y } = calculateRatio(pieceBounds, parentBounds)
onChange({ onChange({
id : player.id, id : player.id,
rightRatio: x, rightRatio: x,
@ -68,8 +73,9 @@ export default function CourtPlayer({
className="player-selection-tab-remove" className="player-selection-tab-remove"
onClick={onRemove} onClick={onRemove}
/> />
{hasBall && <BallPiece onDrop={() => onBallDrop(ballPiece.current!)} pieceRef={ballPiece}/>}
</div> </div>
<PlayerPiece team={player.team} text={player.role} /> <PlayerPiece team={player.team} text={player.role} hasBall={hasBall}/>
</div> </div>
</div> </div>
</Draggable> </Draggable>

@ -2,9 +2,20 @@ import React from "react"
import "../../style/player.css" import "../../style/player.css"
import { Team } from "../../tactic/Team" import { Team } from "../../tactic/Team"
export function PlayerPiece({ team, text }: { team: Team; text: string }) { export interface PlayerPieceProps {
team: Team
text: string
hasBall : boolean
}
export function PlayerPiece({ team, text, hasBall}: PlayerPieceProps) {
let className = `player-piece ${team}`
if (hasBall) {
className += ` player-piece-has-ball`
}
return ( return (
<div className={`player-piece ${team}`}> <div className={className}>
<p>{text}</p> <p>{text}</p>
</div> </div>
) )

@ -1,8 +1,10 @@
#ball * { .ball * {
fill: #c5520d; fill: #c5520d;
} }
#ball { .ball {
pointer-events: all;
width: 20px; width: 20px;
height: 20px; height: 20px;
cursor: pointer;
} }

@ -24,10 +24,6 @@ on the court.
color: var(--selected-team-secondarycolor); color: var(--selected-team-secondarycolor);
border-radius: 100px; border-radius: 100px;
/*
border-width: 2px;
border-style: solid;
*/
width: 20px; width: 20px;
height: 20px; height: 20px;
@ -40,6 +36,11 @@ on the court.
user-select: none; user-select: none;
} }
.player-piece-has-ball {
border-width: 2px;
border-style: solid;
}
.player-selection-tab { .player-selection-tab {
display: flex; display: flex;

@ -114,6 +114,8 @@ function EditorView({
getRackPlayers(Team.Opponents, content.players), getRackPlayers(Team.Opponents, content.players),
) )
const [showBall, setShowBall] = useState(true)
const ballPiece = useRef<HTMLDivElement>(null) const ballPiece = useRef<HTMLDivElement>(null)
const courtDivContentRef = useRef<HTMLDivElement>(null) const courtDivContentRef = useRef<HTMLDivElement>(null)
@ -155,22 +157,29 @@ function EditorView({
}) })
} }
const onElementDetachBall = () => { const onBallDrop = (ref : HTMLDivElement) => {
const ballBounds = ballPiece.current!.getBoundingClientRect() const ballBounds = ref.getBoundingClientRect()
let showBall = true
for (const player of content.players) { setContent(content => {
const players = content.players.map(player => {
const playerBounds = document.getElementById(player.id)!.getBoundingClientRect() const playerBounds = document.getElementById(player.id)!.getBoundingClientRect()
const doesNotOverlap = ( const doesOverlap = !(
ballBounds.top > playerBounds.bottom || ballBounds.top > playerBounds.bottom ||
ballBounds.right < playerBounds.left || ballBounds.right < playerBounds.left ||
ballBounds.bottom < playerBounds.top || ballBounds.bottom < playerBounds.top ||
ballBounds.left > playerBounds.right ballBounds.left > playerBounds.right
) )
if (doesNotOverlap) { if(doesOverlap) {
continue showBall = false
}
player.hasBall = true
} }
return {...player, hasBall: doesOverlap}
})
setShowBall(showBall)
return {players: players}
})
} }
@ -203,17 +212,11 @@ function EditorView({
canDetach={canDetach} canDetach={canDetach}
onElementDetached={onPieceDetach} onElementDetached={onPieceDetach}
render={({team, key}) => ( render={({team, key}) => (
<PlayerPiece team={team} text={key} key={key}/> <PlayerPiece team={team} text={key} key={key} hasBall={false}/>
)} )}
/> />
<Draggable {showBall && <BallPiece onDrop={() => onBallDrop(ballPiece.current!)} pieceRef={ballPiece}/>}
onStop={onElementDetachBall}
>
<div ref={ballPiece}>
<BallPiece/>
</div>
</Draggable>
<Rack <Rack
id="opponent-rack" id="opponent-rack"
@ -222,7 +225,7 @@ function EditorView({
canDetach={canDetach} canDetach={canDetach}
onElementDetached={onPieceDetach} onElementDetached={onPieceDetach}
render={({team, key}) => ( render={({team, key}) => (
<PlayerPiece team={team} text={key} key={key}/> <PlayerPiece team={team} text={key} key={key} hasBall={false}/>
)} )}
/> />
</div> </div>
@ -230,6 +233,7 @@ function EditorView({
<div id="court-div-bounds" ref={courtDivContentRef}> <div id="court-div-bounds" ref={courtDivContentRef}>
<BasketCourt <BasketCourt
players={content.players} players={content.players}
onBallDrop={onBallDrop}
onPlayerChange={(player) => { onPlayerChange={(player) => {
setContent((content) => ({ setContent((content) => ({
players: toSplicedPlayers( players: toSplicedPlayers(
@ -264,6 +268,7 @@ function EditorView({
}, },
]) ])
}} }}
/> />
</div> </div>
</div> </div>

Loading…
Cancel
Save