-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathContextMenu.js
More file actions
88 lines (77 loc) · 2.01 KB
/
Copy pathContextMenu.js
File metadata and controls
88 lines (77 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { useEffect, useRef, useState } from 'react'
import { css } from '@firebolt-dev/css'
import { Menu, MenuItemBtn, MenuItemNumber } from './Menu'
export function ContextMenu({ world, visible, position, onClose }) {
const [fov, setFov] = useState(world?.settings?.fov || 70)
const menuRef = useRef()
useEffect(() => {
if (visible && world?.settings) {
setFov(world.settings.fov)
}
}, [visible, world?.settings?.fov])
useEffect(() => {
if (!visible) return
const handleClickOutside = (e) => {
if (menuRef.current && !menuRef.current.contains(e.target)) {
onClose()
}
}
const handleEscape = (e) => {
if (e.key === 'Escape') {
onClose()
}
}
document.addEventListener('mousedown', handleClickOutside)
document.addEventListener('keydown', handleEscape)
return () => {
document.removeEventListener('mousedown', handleClickOutside)
document.removeEventListener('keydown', handleEscape)
}
}, [visible, onClose])
if (!visible || !world) return null
const handleFovChange = (newFov) => {
setFov(newFov)
// Update settings which will update the camera
world.settings.set('fov', newFov, true)
// Also directly update camera for immediate feedback
if (world.camera) {
world.camera.fov = newFov
world.camera.updateProjectionMatrix()
}
}
const resetFov = () => {
handleFovChange(70)
}
return (
<div
ref={menuRef}
className="context-menu"
css={css`
position: fixed;
top: ${position.y}px;
left: ${position.x}px;
z-index: 1000;
pointer-events: auto;
border-radius: 1.375rem;
overflow: hidden;
`}
>
<Menu title="Camera Settings" blur={false}>
<MenuItemNumber
label="Field of View"
hint="Adjust the camera's field of view (30-120 degrees)"
min={30}
max={120}
step={1}
value={fov}
onChange={handleFovChange}
/>
<MenuItemBtn
label="Reset to Default"
hint="Reset FOV to default 70 degrees"
onClick={resetFov}
/>
</Menu>
</div>
)
}