Spaces:
Running
Running
add hover
Browse files
src/components/rotation-panel.tsx
CHANGED
|
@@ -3,18 +3,25 @@
|
|
| 3 |
import { useLoader } from "@react-three/fiber";
|
| 4 |
import { TextureLoader } from "three";
|
| 5 |
import { Rotations, FacingDirection } from "./consts";
|
|
|
|
| 6 |
|
| 7 |
type RotationPanelProps = {
|
| 8 |
direction: "clockwise" | "counter-clockwise";
|
| 9 |
facingDirection: FacingDirection;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
};
|
| 11 |
|
| 12 |
export const RotationPanel = ({
|
| 13 |
direction,
|
| 14 |
facingDirection,
|
|
|
|
| 15 |
}: RotationPanelProps) => {
|
| 16 |
const clockwise = direction === "clockwise";
|
| 17 |
const texture = useLoader(TextureLoader, `/textures/${direction}.png`);
|
|
|
|
| 18 |
|
| 19 |
const position: Record<FacingDirection, [number, number, number]> = {
|
| 20 |
front: clockwise ? [0.5, 0, 1.01] : [-0.5, 0, 1.01],
|
|
@@ -25,16 +32,29 @@ export const RotationPanel = ({
|
|
| 25 |
bottom: clockwise ? [0.5, -1.01, 0] : [-0.5, -1.01, 0],
|
| 26 |
};
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
return (
|
| 29 |
<mesh
|
| 30 |
position={position[facingDirection]}
|
| 31 |
rotation={Rotations[facingDirection]}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
>
|
| 33 |
<planeGeometry args={[0.8, 1.6]} />
|
| 34 |
<meshStandardMaterial
|
| 35 |
color={"#aaaaaa"}
|
| 36 |
roughness={0.5}
|
| 37 |
-
opacity={
|
| 38 |
map={texture}
|
| 39 |
transparent
|
| 40 |
/>
|
|
|
|
| 3 |
import { useLoader } from "@react-three/fiber";
|
| 4 |
import { TextureLoader } from "three";
|
| 5 |
import { Rotations, FacingDirection } from "./consts";
|
| 6 |
+
import { useState } from "react";
|
| 7 |
|
| 8 |
type RotationPanelProps = {
|
| 9 |
direction: "clockwise" | "counter-clockwise";
|
| 10 |
facingDirection: FacingDirection;
|
| 11 |
+
onClick?: (
|
| 12 |
+
facingDirection: FacingDirection,
|
| 13 |
+
direction: "clockwise" | "counter-clockwise",
|
| 14 |
+
) => void;
|
| 15 |
};
|
| 16 |
|
| 17 |
export const RotationPanel = ({
|
| 18 |
direction,
|
| 19 |
facingDirection,
|
| 20 |
+
onClick,
|
| 21 |
}: RotationPanelProps) => {
|
| 22 |
const clockwise = direction === "clockwise";
|
| 23 |
const texture = useLoader(TextureLoader, `/textures/${direction}.png`);
|
| 24 |
+
const [opacity, setOpacity] = useState(0);
|
| 25 |
|
| 26 |
const position: Record<FacingDirection, [number, number, number]> = {
|
| 27 |
front: clockwise ? [0.5, 0, 1.01] : [-0.5, 0, 1.01],
|
|
|
|
| 32 |
bottom: clockwise ? [0.5, -1.01, 0] : [-0.5, -1.01, 0],
|
| 33 |
};
|
| 34 |
|
| 35 |
+
const handleClick = () => {
|
| 36 |
+
onClick?.(facingDirection, direction);
|
| 37 |
+
};
|
| 38 |
+
|
| 39 |
return (
|
| 40 |
<mesh
|
| 41 |
position={position[facingDirection]}
|
| 42 |
rotation={Rotations[facingDirection]}
|
| 43 |
+
onPointerEnter={() => {
|
| 44 |
+
setOpacity(1);
|
| 45 |
+
document.body.style.cursor = "pointer";
|
| 46 |
+
}}
|
| 47 |
+
onPointerLeave={() => {
|
| 48 |
+
setOpacity(0);
|
| 49 |
+
document.body.style.cursor = "default";
|
| 50 |
+
}}
|
| 51 |
+
onClick={handleClick}
|
| 52 |
>
|
| 53 |
<planeGeometry args={[0.8, 1.6]} />
|
| 54 |
<meshStandardMaterial
|
| 55 |
color={"#aaaaaa"}
|
| 56 |
roughness={0.5}
|
| 57 |
+
opacity={opacity}
|
| 58 |
map={texture}
|
| 59 |
transparent
|
| 60 |
/>
|