parent
9fd4868d05
commit
bfdde852f6
@ -1,27 +1,17 @@
|
|||||||
import React, {RefObject, useRef} from "react"
|
|
||||||
|
|
||||||
import "../../style/ball.css"
|
import "../../style/ball.css"
|
||||||
|
|
||||||
import Ball from "../../assets/icon/ball.svg?react"
|
import BallSvg from "../../assets/icon/ball.svg?react"
|
||||||
import Draggable from "react-draggable"
|
import {Ball} from "../../tactic/CourtObjects";
|
||||||
|
|
||||||
export interface CourtBallProps {
|
export interface CourtBallProps {
|
||||||
onDrop: (pos: DOMRect) => void
|
onMoved: (rect: DOMRect) => void
|
||||||
|
ball: Ball
|
||||||
}
|
}
|
||||||
|
|
||||||
export function CourtBall({ onDrop }: CourtBallProps) {
|
|
||||||
const ref = useRef<HTMLElement>()
|
|
||||||
return (
|
|
||||||
<Draggable onStop={() => onDrop(ref.current!.getBoundingClientRect())} nodeRef={ref}>
|
|
||||||
<BallPiece pieceRef={ref}/>
|
|
||||||
</Draggable>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function BallPiece({pieceRef}: {pieceRef: RefObject<HTMLElement>}) {
|
export function BallPiece() {
|
||||||
return (
|
return (
|
||||||
<div ref={pieceRef} className={`ball-div`} >
|
<BallSvg className={"ball"}/>
|
||||||
<Ball className={"ball"} />
|
|
||||||
</div>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,28 @@
|
|||||||
|
import React, {useRef} from "react";
|
||||||
|
import Draggable from "react-draggable";
|
||||||
|
import {BallPiece, CourtBallProps} from "./BallPiece";
|
||||||
|
|
||||||
|
export function CourtBall({onMoved, ball}: CourtBallProps) {
|
||||||
|
const pieceRef = useRef<HTMLDivElement>(null)
|
||||||
|
|
||||||
|
const x = ball.rightRatio
|
||||||
|
const y = ball.bottomRatio
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Draggable
|
||||||
|
onStop={() => onMoved(pieceRef.current!.getBoundingClientRect())}
|
||||||
|
nodeRef={pieceRef}
|
||||||
|
>
|
||||||
|
<div className={"ball-div"}
|
||||||
|
ref={pieceRef}
|
||||||
|
style={{
|
||||||
|
position: "absolute",
|
||||||
|
left: `${x * 100}%`,
|
||||||
|
top: `${y * 100}%`,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<BallPiece/>
|
||||||
|
</div>
|
||||||
|
</Draggable>
|
||||||
|
)
|
||||||
|
}
|
@ -1,11 +1,20 @@
|
|||||||
|
|
||||||
|
|
||||||
|
export type CourtObject = {type: "ball"} & Ball
|
||||||
|
|
||||||
|
|
||||||
export interface Ball {
|
export interface Ball {
|
||||||
/**
|
/**
|
||||||
* Percentage of the player's position to the bottom (0 means top, 1 means bottom, 0.5 means middle)
|
* The ball is a "ball" court object
|
||||||
*/
|
*/
|
||||||
bottomRatio: number
|
readonly type: "ball",
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Percentage of the player's position to the bottom (0 means top, 1 means bottom, 0.5 means middle)
|
||||||
|
*/
|
||||||
|
readonly bottomRatio: number
|
||||||
/**
|
/**
|
||||||
* Percentage of the player's position to the right (0 means left, 1 means right, 0.5 means middle)
|
* Percentage of the player's position to the right (0 means left, 1 means right, 0.5 means middle)
|
||||||
*/
|
*/
|
||||||
rightRatio: number
|
readonly rightRatio: number
|
||||||
}
|
}
|
Loading…
Reference in new issue