parent
a7ddad4c67
commit
2a5ece19a9
After Width: | Height: | Size: 427 B |
@ -1,33 +1,50 @@
|
|||||||
import React, {useEffect, useRef} from "react";
|
import React, {useRef} from "react";
|
||||||
import "../../style/player.css";
|
import "../../style/player.css";
|
||||||
|
import RemoveIcon from "../../assets/icon/remove.svg";
|
||||||
import Draggable, {DraggableBounds} from "react-draggable";
|
import Draggable, {DraggableBounds} from "react-draggable";
|
||||||
|
|
||||||
export default function Player({id, x, y, bounds}: {
|
export interface PlayerOptions {
|
||||||
id: number,
|
id: number,
|
||||||
x: number,
|
x: number,
|
||||||
y: number,
|
y: number,
|
||||||
bounds: DraggableBounds
|
bounds: DraggableBounds,
|
||||||
}) {
|
onRemove: () => void
|
||||||
|
}
|
||||||
|
|
||||||
const ref = useRef<HTMLDivElement>(null);
|
export default function Player({id, x, y, bounds, onRemove}: PlayerOptions) {
|
||||||
useEffect(() => {
|
|
||||||
const playerRect = ref.current!.getBoundingClientRect();
|
|
||||||
bounds.bottom! -= playerRect.height / 2;
|
|
||||||
bounds.right! -= playerRect.width / 2;
|
|
||||||
}, [ref])
|
|
||||||
|
|
||||||
|
const ref = useRef<HTMLDivElement>(null);
|
||||||
return (
|
return (
|
||||||
<Draggable
|
<Draggable
|
||||||
|
handle={".player-piece"}
|
||||||
nodeRef={ref}
|
nodeRef={ref}
|
||||||
bounds={bounds}
|
bounds={bounds}
|
||||||
defaultPosition={{x: x, y: y}}>
|
defaultPosition={{x: x, y: y}}
|
||||||
|
>
|
||||||
<div ref={ref}
|
<div ref={ref}
|
||||||
className="player"
|
className={"player"}
|
||||||
style={{
|
style={{
|
||||||
position: "absolute",
|
position: "absolute",
|
||||||
}}>
|
}}>
|
||||||
<p>{id}</p>
|
|
||||||
|
<div tabIndex={0}
|
||||||
|
className="player-content"
|
||||||
|
onKeyUp={e => {
|
||||||
|
if (e.key == "Delete")
|
||||||
|
onRemove()
|
||||||
|
}}>
|
||||||
|
<div className="player-selection-tab">
|
||||||
|
<RemoveIcon
|
||||||
|
className="player-selection-tab-remove"
|
||||||
|
onClick={() => onRemove()}/>
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
className="player-piece">
|
||||||
|
<p>{id}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</Draggable>
|
</Draggable>
|
||||||
|
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in new issue