diff --git a/frontend/src/console/Layout.vue b/frontend/src/console/Layout.vue index e3366b237..17cc4c626 100644 --- a/frontend/src/console/Layout.vue +++ b/frontend/src/console/Layout.vue @@ -4,10 +4,30 @@ diff --git a/frontend/src/console/composables/useInputScope.ts b/frontend/src/console/composables/useInputScope.ts new file mode 100644 index 000000000..bfd49fb99 --- /dev/null +++ b/frontend/src/console/composables/useInputScope.ts @@ -0,0 +1,13 @@ +import { inject, onMounted, onUnmounted } from 'vue'; +import { InputBus, InputBusSymbol } from '@/console/input/bus'; +import type { InputListener } from '@/console/input/actions'; + +export function useInputScope() { + const bus = inject(InputBusSymbol)!; + let pop: (() => void) | null = null; + onMounted(() => { pop = bus.pushScope(); }); + onUnmounted(() => { pop?.(); pop = null; }); + + const on = (listener: InputListener) => bus.subscribe(listener); + return { bus, on }; +} diff --git a/frontend/src/console/composables/useRovingFocus.ts b/frontend/src/console/composables/useRovingFocus.ts new file mode 100644 index 000000000..a5655cfc9 --- /dev/null +++ b/frontend/src/console/composables/useRovingFocus.ts @@ -0,0 +1,14 @@ +import { onMounted, ref } from 'vue'; + +export function useRovingFocus(countRef: () => number) { + const index = ref(0); + const set = (i: number) => { index.value = Math.max(0, Math.min(countRef() - 1, i)); }; + const next = () => set(index.value + 1); + const prev = () => set(index.value - 1); + + onMounted(() => { + // no-op placeholder for possible DOM sync + }); + + return { index, set, next, prev }; +} diff --git a/frontend/src/console/composables/useSpatialNav.ts b/frontend/src/console/composables/useSpatialNav.ts new file mode 100644 index 000000000..5a4c48b0c --- /dev/null +++ b/frontend/src/console/composables/useSpatialNav.ts @@ -0,0 +1,9 @@ +import type { Ref } from 'vue'; + +export function useSpatialNav(selectedIndex: Ref, getCols: () => number, getCount: () => number) { + const moveLeft = () => { selectedIndex.value = Math.max(0, selectedIndex.value - 1); }; + const moveRight = () => { const cols = getCols(); const last = getCount() - 1; const atRight = (selectedIndex.value + 1) % cols === 0; if(!atRight) selectedIndex.value = Math.min(last, selectedIndex.value + 1); }; + const moveUp = () => { const cols = getCols(); const ni = selectedIndex.value - cols; if (ni >= 0) selectedIndex.value = ni; }; + const moveDown = () => { const cols = getCols(); const ni = selectedIndex.value + cols; if (ni < getCount()) selectedIndex.value = ni; }; + return { moveLeft, moveRight, moveUp, moveDown }; +} diff --git a/frontend/src/console/input/actions.ts b/frontend/src/console/input/actions.ts new file mode 100644 index 000000000..4b42c93cc --- /dev/null +++ b/frontend/src/console/input/actions.ts @@ -0,0 +1,12 @@ +export type InputAction = + | 'moveUp' + | 'moveDown' + | 'moveLeft' + | 'moveRight' + | 'confirm' + | 'back' + | 'menu' + | 'tabNext' + | 'tabPrev'; + +export type InputListener = (action: InputAction) => boolean | void; diff --git a/frontend/src/console/input/bus.ts b/frontend/src/console/input/bus.ts new file mode 100644 index 000000000..d6a7326b5 --- /dev/null +++ b/frontend/src/console/input/bus.ts @@ -0,0 +1,46 @@ +import type { InputAction, InputListener } from './actions'; + +export class InputBus { + private scopes: Array> = []; + private globalShortcuts: Set = new Set(); + + pushScope(): () => void { + const set = new Set(); + this.scopes.push(set); + return () => this.popScope(set); + } + + private popScope(scope: Set) { + const idx = this.scopes.lastIndexOf(scope); + if (idx >= 0) this.scopes.splice(idx, 1); + } + + subscribe(listener: InputListener): () => void { + const top = this.scopes[this.scopes.length - 1]; + if (!top) throw new Error('No active input scope. Call pushScope() first.'); + top.add(listener); + return () => top.delete(listener); + } + + subscribeGlobal(listener: InputListener): () => void { + this.globalShortcuts.add(listener); + return () => this.globalShortcuts.delete(listener); + } + + dispatch(action: InputAction): boolean { + for (let i = this.scopes.length - 1; i >= 0; i--) { + const scope = this.scopes[i]; + for (const l of Array.from(scope).reverse()) { + const handled = l(action); + if (handled) return true; + } + } + for (const l of Array.from(this.globalShortcuts).reverse()) { + const handled = l(action); + if (handled) return true; + } + return false; + } +} + +export const InputBusSymbol = Symbol('InputBus'); diff --git a/frontend/src/console/input/config.ts b/frontend/src/console/input/config.ts new file mode 100644 index 000000000..65d27f38c --- /dev/null +++ b/frontend/src/console/input/config.ts @@ -0,0 +1,46 @@ +export interface InputConfig { + rtl: boolean; + repeat: { initialDelayMs: number; repeatEveryMs: number }; + deadzone: number; + keyMap: Record; + gamepad: { + axesThreshold: number; + axisToAction: { + x: { neg: import('./actions').InputAction; pos: import('./actions').InputAction }; + y: { neg: import('./actions').InputAction; pos: import('./actions').InputAction }; + }; + buttons: Record; + }; +} + +import type { InputAction } from './actions'; + +export const defaultInputConfig: InputConfig = { + rtl: false, + repeat: { initialDelayMs: 350, repeatEveryMs: 120 }, + deadzone: 0.2, + keyMap: { + ArrowUp: 'moveUp', + ArrowDown: 'moveDown', + ArrowLeft: 'moveLeft', + ArrowRight: 'moveRight', + Enter: 'confirm', + ' ': 'confirm', + // Escape: 'back', + Backspace: 'back', + Tab: undefined, + }, + gamepad: { + axesThreshold: 0.5, + axisToAction: { + x: { neg: 'moveLeft' as InputAction, pos: 'moveRight' as InputAction }, + y: { neg: 'moveUp' as InputAction, pos: 'moveDown' as InputAction }, + }, + buttons: { + 0: 'confirm', + 1: 'back', + 8: 'back', + 9: 'menu', + }, + }, +}; diff --git a/frontend/src/console/input/gamepad.ts b/frontend/src/console/input/gamepad.ts new file mode 100644 index 000000000..b0f6d83f5 --- /dev/null +++ b/frontend/src/console/input/gamepad.ts @@ -0,0 +1,61 @@ +import { InputBus } from './bus'; +import { defaultInputConfig } from './config'; +import type { InputAction } from './actions'; + +interface ButtonState { pressed: boolean; nextRepeatAt: number; } + +export function attachGamepad(bus: InputBus) { + const states: Record; axesRepeatAt: Record<'x'|'y', number>; }> = {}; + const now = () => performance.now(); + + const onConnect = () => {}; + const onDisconnect = () => {}; + window.addEventListener('gamepadconnected', onConnect); + window.addEventListener('gamepaddisconnected', onDisconnect); + + let rafId = 0; + const loop = () => { + const pads = navigator.getGamepads?.() || []; + const t = now(); + for (const pad of pads) { + if (!pad) continue; + const key = `${pad.index}:${pad.id}`; + const st = (states[key] ||= { buttons: {}, axesRepeatAt: { x: 0, y: 0 } }); + + const x = pad.axes[0] || 0; + const y = pad.axes[1] || 0; + const thr = defaultInputConfig.gamepad.axesThreshold; + const delay = defaultInputConfig.repeat.initialDelayMs; + const rep = defaultInputConfig.repeat.repeatEveryMs; + const fireAxis = (axis: 'x'|'y', v: number) => { + if (Math.abs(v) < thr) { st.axesRepeatAt[axis] = 0; return; } + const dirAction: InputAction = v < 0 + ? (axis === 'x' ? defaultInputConfig.gamepad.axisToAction.x.neg : defaultInputConfig.gamepad.axisToAction.y.neg) + : (axis === 'x' ? defaultInputConfig.gamepad.axisToAction.x.pos : defaultInputConfig.gamepad.axisToAction.y.pos); + if (st.axesRepeatAt[axis] === 0) { + bus.dispatch(dirAction); + st.axesRepeatAt[axis] = t + delay; + } else if (t >= st.axesRepeatAt[axis]) { + bus.dispatch(dirAction); + st.axesRepeatAt[axis] = t + rep; + } + }; + fireAxis('x', x); fireAxis('y', y); + + for (let i = 0; i < pad.buttons.length; i++) { + const b = pad.buttons[i]; + const action = defaultInputConfig.gamepad.buttons[i]; + if (!action) continue; + const prev = (st.buttons[i] ||= { pressed: false, nextRepeatAt: 0 }); + if (b.pressed) { + if (!prev.pressed) { bus.dispatch(action); prev.pressed = true; prev.nextRepeatAt = t + delay; } + else if (t >= prev.nextRepeatAt) { bus.dispatch(action); prev.nextRepeatAt = t + rep; } + } else { prev.pressed = false; prev.nextRepeatAt = 0; } + } + } + rafId = requestAnimationFrame(loop); + }; + rafId = requestAnimationFrame(loop); + + return () => { cancelAnimationFrame(rafId); window.removeEventListener('gamepadconnected', onConnect); window.removeEventListener('gamepaddisconnected', onDisconnect); }; +} diff --git a/frontend/src/console/input/keyboard.ts b/frontend/src/console/input/keyboard.ts new file mode 100644 index 000000000..fa53a60c2 --- /dev/null +++ b/frontend/src/console/input/keyboard.ts @@ -0,0 +1,17 @@ +import { InputBus } from './bus'; +import { defaultInputConfig } from './config'; + +export function attachKeyboard(bus: InputBus) { + const onKey = (e: KeyboardEvent) => { + const target = e.target as HTMLElement | null; + if (target && (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA' || target.isContentEditable)) { + return; + } + const action = defaultInputConfig.keyMap[e.key]; + if (!action) return; + const handled = bus.dispatch(action); + if (handled) e.preventDefault(); + }; + window.addEventListener('keydown', onKey); + return () => window.removeEventListener('keydown', onKey); +} diff --git a/frontend/src/console/views/Game.vue b/frontend/src/console/views/Game.vue index 074680b09..77db79c30 100644 --- a/frontend/src/console/views/Game.vue +++ b/frontend/src/console/views/Game.vue @@ -25,7 +25,7 @@ v-else class="relative w-full h-full overflow-y-auto overflow-x-hidden pb-28 md:pb-32" > - +
@@ -290,12 +290,14 @@