Skip to content

Commit cfc430f

Browse files
committed
Gamepads gui states
1 parent f55ec40 commit cfc430f

File tree

3 files changed

+63
-85
lines changed

3 files changed

+63
-85
lines changed

src/Gamepads.tsx

+62-54
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
11
import * as THREE from "three";
2-
import { useEffect, useState } from "react";
2+
import { useCallback, useEffect, useState } from "react";
33
import { useFrame, useThree } from "@react-three/fiber";
44
import { useController, useXR } from "@react-three/xr";
55
import nipplejs from "nipplejs";
6-
7-
import FaceText from "./components/FaceText";
6+
import { folder, useControls } from "leva";
87

98
type Vec2 = { x: number; y: number };
109

11-
type GamepadsProps = {
12-
nipples: boolean;
13-
};
14-
function Gamepads({ nipples = true }: GamepadsProps) {
15-
const [leftpad] = useState<Vec2>({ x: 0, y: 0 });
16-
const [rightpad] = useState<Vec2>({ x: 0, y: 0 });
10+
function Gamepads() {
11+
const [gui, setGui] = useControls(() => ({
12+
Gamepads: folder(
13+
{
14+
leftpad: { x: 0, y: 0 },
15+
rightpad: { x: 0, y: 0 },
16+
nipples: false,
17+
}
18+
// { collapsed: true }
19+
),
20+
}));
21+
22+
const leftpad = gui.leftpad;
23+
const setLeftpad = useCallback(
24+
(v: typeof leftpad) => setGui({ leftpad: v }),
25+
[setGui]
26+
);
27+
28+
const rightpad = gui.rightpad;
29+
const setRightpad = useCallback(
30+
(v: typeof rightpad) => setGui({ rightpad: v }),
31+
[setGui]
32+
);
1733

1834
const player = useXR((state) => state.player);
1935

20-
const [text1, setText1] = useState("left");
21-
const [text2, setText2] = useState("right");
22-
2336
//
2437
// Update `player` position and rotation
2538
//
@@ -30,8 +43,6 @@ function Gamepads({ nipples = true }: GamepadsProps) {
3043
};
3144

3245
useFrame(() => {
33-
setText1(`leftpad: [ ${leftpad.x.toFixed(3)}, ${leftpad.y.toFixed(3)} ]`);
34-
3546
player.position.add(
3647
new THREE.Vector3(
3748
leftpad.x * sensitivity.left.x,
@@ -40,21 +51,17 @@ function Gamepads({ nipples = true }: GamepadsProps) {
4051
).applyEuler(player.rotation)
4152
);
4253

43-
setText2(
44-
`rightpad: [ ${rightpad.x.toFixed(3)}, ${rightpad.y.toFixed(3)} ]`
45-
);
4654
player.rotation.y -= rightpad.x * sensitivity.right.x;
4755
player.position.y -= rightpad.y * sensitivity.right.y;
4856
});
4957

5058
return (
5159
<>
52-
<FaceText position={[0, 6, 0]}>{text1}</FaceText>
53-
<FaceText position={[0, 5, 0]}>{text2}</FaceText>
54-
55-
<NormalGamepad leftpad={leftpad} rightpad={rightpad} />
56-
<XRGamepads leftpad={leftpad} rightpad={rightpad} />
57-
{nipples && <NipplesGamepads leftpad={leftpad} rightpad={rightpad} />}
60+
<NormalGamepad setLeftpad={setLeftpad} setRightpad={setRightpad} />
61+
<XRGamepads setLeftpad={setLeftpad} setRightpad={setRightpad} />
62+
{gui.nipples && (
63+
<NipplesGamepads setLeftpad={setLeftpad} setRightpad={setRightpad} />
64+
)}
5865
</>
5966
);
6067
}
@@ -68,11 +75,11 @@ export default Gamepads;
6875
//
6976

7077
function NormalGamepad({
71-
leftpad,
72-
rightpad,
78+
setLeftpad,
79+
setRightpad,
7380
}: {
74-
leftpad: Vec2;
75-
rightpad: Vec2;
81+
setLeftpad: (v: Vec2) => void;
82+
setRightpad: (v: Vec2) => void;
7683
}) {
7784
const [gamepadIndex, setGamepadIndex] = useState<number | null>(null);
7885

@@ -106,11 +113,8 @@ function NormalGamepad({
106113
const gp = navigator.getGamepads()[gamepadIndex];
107114
if (!gp) return;
108115

109-
leftpad.x = threshold(gp.axes[0]);
110-
leftpad.y = threshold(gp.axes[1]);
111-
112-
rightpad.x = threshold(gp.axes[2]);
113-
rightpad.y = threshold(gp.axes[3]);
116+
setLeftpad({ x: threshold(gp.axes[0]), y: threshold(gp.axes[1]) });
117+
setRightpad({ x: threshold(gp.axes[2]), y: threshold(gp.axes[3]) });
114118
});
115119

116120
return <></>;
@@ -120,7 +124,13 @@ function NormalGamepad({
120124
// XR Gamepads (see: https://github.com/pmndrs/react-xr/issues/218)
121125
//
122126

123-
function XRGamepads({ leftpad, rightpad }: { leftpad: Vec2; rightpad: Vec2 }) {
127+
function XRGamepads({
128+
setLeftpad,
129+
setRightpad,
130+
}: {
131+
setLeftpad: (v: Vec2) => void;
132+
setRightpad: (v: Vec2) => void;
133+
}) {
124134
const leftController = useController("left");
125135
const rightController = useController("right");
126136

@@ -130,15 +140,19 @@ function XRGamepads({ leftpad, rightpad }: { leftpad: Vec2; rightpad: Vec2 }) {
130140
if (leftController) {
131141
const XRLeftGamepad = leftController.inputSource?.gamepad;
132142

133-
leftpad.x = XRLeftGamepad?.axes[2] || 0;
134-
leftpad.y = XRLeftGamepad?.axes[3] || 0;
143+
setLeftpad({
144+
x: XRLeftGamepad?.axes[2] || 0,
145+
y: XRLeftGamepad?.axes[3] || 0,
146+
});
135147
}
136148

137149
if (rightController) {
138150
const XRRightGamepad = rightController.inputSource?.gamepad;
139151

140-
rightpad.x = XRRightGamepad?.axes[2] || 0;
141-
rightpad.y = XRRightGamepad?.axes[3] || 0;
152+
setRightpad({
153+
x: XRRightGamepad?.axes[2] || 0,
154+
y: XRRightGamepad?.axes[3] || 0,
155+
});
142156
}
143157
});
144158

@@ -152,11 +166,11 @@ function XRGamepads({ leftpad, rightpad }: { leftpad: Vec2; rightpad: Vec2 }) {
152166
//
153167

154168
function NipplesGamepads({
155-
leftpad,
156-
rightpad,
169+
setLeftpad,
170+
setRightpad,
157171
}: {
158-
leftpad: Vec2;
159-
rightpad: Vec2;
172+
setLeftpad: (v: Vec2) => void;
173+
setRightpad: (v: Vec2) => void;
160174
}) {
161175
const { size, gl } = useThree();
162176

@@ -174,12 +188,10 @@ function NipplesGamepads({
174188
// Only one joystick
175189
if (position.x < size.width / 2) {
176190
// left part of the screen
177-
leftpad.x = vector.x;
178-
leftpad.y = -vector.y;
191+
setLeftpad({ x: vector.x, y: -vector.y });
179192
} else {
180193
// right part of the screen
181-
rightpad.x = vector.x;
182-
rightpad.y = -vector.y;
194+
setRightpad({ x: vector.x, y: -vector.y });
183195
}
184196
} else {
185197
// Two joysticks
@@ -188,20 +200,16 @@ function NipplesGamepads({
188200

189201
if (position.x < otherJoystick.position.x) {
190202
// I'm on the left of the other joystick
191-
leftpad.x = vector.x;
192-
leftpad.y = -vector.y;
203+
setLeftpad({ x: vector.x, y: -vector.y });
193204
} else {
194205
// I'm on the right of the other joystick
195-
rightpad.x = vector.x;
196-
rightpad.y = -vector.y;
206+
setRightpad({ x: vector.x, y: -vector.y });
197207
}
198208
}
199209
});
200210
manager.on("end", (evt, nipple) => {
201-
leftpad.x = 0;
202-
leftpad.y = 0;
203-
rightpad.x = 0;
204-
rightpad.y = 0;
211+
setLeftpad({ x: 0, y: 0 });
212+
setRightpad({ x: 0, y: 0 });
205213
});
206214

207215
//
@@ -211,7 +219,7 @@ function NipplesGamepads({
211219
return () => {
212220
manager.destroy();
213221
};
214-
}, [gl, size, leftpad, rightpad]);
222+
}, [gl, size, setLeftpad, setRightpad]);
215223

216224
return <></>;
217225
}

src/Layout.tsx

+1-4
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ function Layout({
2828
step: 0.1,
2929
},
3030
}),
31-
gamepads: folder({
32-
nipples: true,
33-
}),
3431
},
3532
{ collapsed: true }
3633
),
@@ -41,7 +38,7 @@ function Layout({
4138
<>
4239
<Camera position={gui.player} lookAt={gui.lookAt} fov={gui.fov} />
4340

44-
<Gamepads nipples={gui.nipples} />
41+
<Gamepads />
4542

4643
<Environment background>
4744
<mesh scale={100}>

src/components/FaceText.tsx

-27
This file was deleted.

0 commit comments

Comments
 (0)