Open History to see the last commit
1import { useEffect, useRef, useState } from 'react'
2import { ArrowDown, ArrowLeft, ArrowRight, ArrowUp } from 'lucide-react'
3import { setLingbotAxis } from '@/lib/reactor-session'
4import type { ReactNode } from 'react'
5import type { Reactor } from '@reactor-team/js-sdk'
6import type { ReactorWorldModel } from '@tanstack/ai-reactor'
7import type { LingbotAxis } from '@/lib/reactor-session'
8
9interface Control {
10 axis: LingbotAxis
11 value: string
12 key: string
13 label: string
14 cap: ReactNode
15}
16
17const ICON = 'h-4 w-4'
18
19// Order in each pad: up, left, down, right.
20const MOVE: Array<Control> = [
21 {
22 axis: 'move_longitudinal',
23 value: 'forward',
24 key: 'w',
25 label: 'Forward',
26 cap: 'W',
27 },
28 {
29 axis: 'move_lateral',
30 value: 'strafe_left',
31 key: 'a',
32 label: 'Strafe left',
33 cap: 'A',
34 },
35 {
36 axis: 'move_longitudinal',
37 value: 'back',
38 key: 's',
39 label: 'Back',
40 cap: 'S',
41 },
42 {
43 axis: 'move_lateral',
44 value: 'strafe_right',
45 key: 'd',
46 label: 'Strafe right',
47 cap: 'D',
48 },
49]
50
51const LOOK: Array<Control> = [
52 {
53 axis: 'look_vertical',
54 value: 'up',
55 key: 'ArrowUp',
56 label: 'Look up',
57 cap: <ArrowUp className={ICON} />,
58 },
59 {
60 axis: 'look_horizontal',
61 value: 'left',
62 key: 'ArrowLeft',
63 label: 'Look left',
64 cap: <ArrowLeft className={ICON} />,
65 },
66 {
67 axis: 'look_vertical',
68 value: 'down',
69 key: 'ArrowDown',
70 label: 'Look down',
71 cap: <ArrowDown className={ICON} />,
72 },
73 {
74 axis: 'look_horizontal',
75 value: 'right',
76 key: 'ArrowRight',
77 label: 'Look right',
78 cap: <ArrowRight className={ICON} />,
79 },
80]
81
82const ALL = [...MOVE, ...LOOK]
83
84function controlForKey(event: KeyboardEvent): Control | undefined {
85 const target = event.target
86 if (
87 target instanceof HTMLElement &&
88 target.closest('input, textarea, select')
89 ) {
90 return undefined
91 }
92 const key = event.key.length === 1 ? event.key.toLowerCase() : event.key
93 return ALL.find((control) => control.key === key)
94}
95
96/**
97 * Hold-to-move pads for LingBot, laid over the video. Buttons, WASD, and
98 * arrow keys drive the same held axes. A button lights while its axis runs.
99 */
100export function LingbotControls(props: {
101 reactor: Reactor
102 model: ReactorWorldModel
103 onError: (message: string) => void
104}) {
105 const { reactor, model, onError } = props
106 const held = useRef(new Map<LingbotAxis, string>())
107 const [active, setActive] = useState<ReadonlyMap<LingbotAxis, string>>(
108 () => new Map(),
109 )
110
111 function send(axis: LingbotAxis, value: string) {
112 setActive(new Map(held.current))
113 setLingbotAxis(reactor, model, axis, value).catch((error: unknown) =>
114 onError(String(error)),
115 )
116 }
117
118 function press(control: Control) {
119 if (held.current.get(control.axis) === control.value) return
120 held.current.set(control.axis, control.value)
121 send(control.axis, control.value)
122 }
123
124 function release(control: Control) {
125 // Another key on the same axis took over. Leave it running.
126 if (held.current.get(control.axis) !== control.value) return
127 held.current.delete(control.axis)
128 send(control.axis, 'idle')
129 }
130
131 const pressRef = useRef(press)
132 const releaseRef = useRef(release)
133 pressRef.current = press
134 releaseRef.current = release
135
136 useEffect(() => {
137 const onDown = (event: KeyboardEvent) => {
138 const control = controlForKey(event)
139 if (!control) return
140 event.preventDefault()
141 pressRef.current(control)
142 }
143 const onUp = (event: KeyboardEvent) => {
144 const control = controlForKey(event)
145 if (control) releaseRef.current(control)
146 }
147 // A key released outside the window never fires keyup. Stop everything.
148 const releaseAll = () => {
149 for (const control of ALL) releaseRef.current(control)
150 }
151 window.addEventListener('keydown', onDown)
152 window.addEventListener('keyup', onUp)
153 window.addEventListener('blur', releaseAll)
154 return () => {
155 window.removeEventListener('keydown', onDown)
156 window.removeEventListener('keyup', onUp)
157 window.removeEventListener('blur', releaseAll)
158 }
159 }, [])
160
161 function pad(title: string, controls: Array<Control>, corner: string) {
162 const areas = ['up', 'left', 'down', 'right']
163 return (
164 <div
165 className={`absolute bottom-3 flex flex-col items-center gap-1 ${corner}`}
166 >
167 <div
168 className="grid grid-cols-3 gap-1"
169 style={{ gridTemplateAreas: '". up ." "left down right"' }}
170 >
171 {controls.map((control, index) => (
172 <button
173 key={control.label}
174 type="button"
175 aria-label={control.label}
176 aria-pressed={active.get(control.axis) === control.value}
177 title={control.label}
178 style={{ gridArea: areas[index] }}
179 onPointerDown={(event) => {
180 event.currentTarget.setPointerCapture(event.pointerId)
181 press(control)
182 }}
183 onPointerUp={() => release(control)}
184 onPointerCancel={() => release(control)}
185 className="flex h-10 w-10 touch-none select-none items-center justify-center rounded-lg border border-white/20 bg-black/50 font-mono text-sm font-semibold text-white backdrop-blur-sm hover:bg-black/70 aria-pressed:border-purple-400 aria-pressed:bg-purple-600"
186 >
187 {control.cap}
188 </button>
189 ))}
190 </div>
191 <span className="rounded bg-black/50 px-1.5 text-xs text-gray-200">
192 {title}
193 </span>
194 </div>
195 )
196 }
197
198 return (
199 <>
200 {pad('Move', MOVE, 'left-3')}
201 {pad('Look', LOOK, 'right-3')}
202 </>
203 )
204}