Skip to content

Commit 6572935

Browse files
committed
feat(app): floating menu
1 parent 853f12d commit 6572935

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

app/src/pages/debug/menu.tsx

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import type { JSX, PropsWithChildren } from 'react'
2+
import type { Group } from 'three'
3+
4+
import { useFrame, useThree } from '@react-three/fiber'
5+
import { Root } from '@react-three/uikit'
6+
import { colors } from '@react-three/uikit-default'
7+
import { useXRInputSourceState } from '@react-three/xr'
8+
import { useDebouncedState } from 'foxact/use-debounced-state'
9+
import { useMemo, useRef } from 'react'
10+
import { Vector3 } from 'three'
11+
12+
import { Settings } from '~/components/ui/settings'
13+
14+
export const FloatingMenu = ({ children, ...props }: PropsWithChildren<JSX.IntrinsicElements['group']>) => {
15+
const menuRef = useRef<Group>(null)
16+
const { camera } = useThree()
17+
18+
const localPosition = useMemo(() => new Vector3(), [])
19+
20+
useFrame(() => {
21+
if (!menuRef.current)
22+
return
23+
24+
localPosition.set(0, 0, -1)
25+
menuRef.current.position.copy(localPosition.applyMatrix4(camera.matrixWorld))
26+
menuRef.current.lookAt(camera.position)
27+
})
28+
29+
return (
30+
<group ref={menuRef} {...props}>
31+
{children}
32+
</group>
33+
)
34+
}
35+
36+
const DebugMenu = () => {
37+
const [visible, setVisible] = useDebouncedState(true, 100)
38+
39+
const controllerLeft = useXRInputSourceState('controller', 'left')
40+
const controllerRight = useXRInputSourceState('controller', 'right')
41+
42+
useFrame(() => {
43+
if (controllerLeft?.gamepad['y-button']?.state === 'pressed' || controllerRight?.gamepad['b-button']?.state === 'pressed')
44+
setVisible(!visible)
45+
})
46+
47+
return (
48+
<FloatingMenu visible={visible}>
49+
<Root
50+
alignItems="center"
51+
backgroundColor={colors.muted}
52+
borderRadius={12}
53+
height={768}
54+
justifyContent="center"
55+
pixelSize={0.0015}
56+
width={1024}
57+
>
58+
<Settings />
59+
</Root>
60+
</FloatingMenu>
61+
)
62+
}
63+
64+
export default DebugMenu

app/src/router.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export type Path =
88
| `/debug/animations`
99
| `/debug/illuminance`
1010
| `/debug/input`
11+
| `/debug/menu`
1112
| `/debug/planes`
1213
| `/debug/settings`
1314
| `/debug/tablet`

0 commit comments

Comments
 (0)