mirror of
https://github.com/rommapp/romm.git
synced 2026-12-03 10:33:22 +01:00
created new input handling, updated home view to use it
This commit is contained in:
@@ -4,10 +4,30 @@
|
||||
</div>
|
||||
</template>
|
||||
<script setup lang="ts">
|
||||
import { onMounted, onUnmounted } from 'vue';
|
||||
import { onMounted, onUnmounted, provide } from 'vue';
|
||||
// Import Tailwind v4 entry so utilities are available when this layout renders
|
||||
import './index.css';
|
||||
import { InputBus, InputBusSymbol } from '@/console/input/bus';
|
||||
import { attachKeyboard } from '@/console/input/keyboard';
|
||||
import { attachGamepad } from '@/console/input/gamepad';
|
||||
|
||||
const bus = new InputBus();
|
||||
provide(InputBusSymbol, bus);
|
||||
|
||||
let detachKeyboard: (() => void) | null = null;
|
||||
let detachGamepad: (() => void) | null = null;
|
||||
onMounted(() => document.body.classList.add('console-mode'));
|
||||
onUnmounted(() => document.body.classList.remove('console-mode'));
|
||||
onMounted(() => {
|
||||
// Establish a root input scope so child views can subscribe safely
|
||||
bus.pushScope();
|
||||
detachKeyboard = attachKeyboard(bus);
|
||||
detachGamepad = attachGamepad(bus);
|
||||
});
|
||||
onUnmounted(() => {
|
||||
document.body.classList.remove('console-mode');
|
||||
// teardown input attachments
|
||||
detachKeyboard?.();
|
||||
detachGamepad?.();
|
||||
});
|
||||
</script>
|
||||
<style></style>
|
||||
|
||||
13
frontend/src/console/composables/useInputScope.ts
Normal file
13
frontend/src/console/composables/useInputScope.ts
Normal file
@@ -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<InputBus>(InputBusSymbol)!;
|
||||
let pop: (() => void) | null = null;
|
||||
onMounted(() => { pop = bus.pushScope(); });
|
||||
onUnmounted(() => { pop?.(); pop = null; });
|
||||
|
||||
const on = (listener: InputListener) => bus.subscribe(listener);
|
||||
return { bus, on };
|
||||
}
|
||||
14
frontend/src/console/composables/useRovingFocus.ts
Normal file
14
frontend/src/console/composables/useRovingFocus.ts
Normal file
@@ -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 };
|
||||
}
|
||||
9
frontend/src/console/composables/useSpatialNav.ts
Normal file
9
frontend/src/console/composables/useSpatialNav.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import type { Ref } from 'vue';
|
||||
|
||||
export function useSpatialNav(selectedIndex: Ref<number>, 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 };
|
||||
}
|
||||
12
frontend/src/console/input/actions.ts
Normal file
12
frontend/src/console/input/actions.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
export type InputAction =
|
||||
| 'moveUp'
|
||||
| 'moveDown'
|
||||
| 'moveLeft'
|
||||
| 'moveRight'
|
||||
| 'confirm'
|
||||
| 'back'
|
||||
| 'menu'
|
||||
| 'tabNext'
|
||||
| 'tabPrev';
|
||||
|
||||
export type InputListener = (action: InputAction) => boolean | void;
|
||||
46
frontend/src/console/input/bus.ts
Normal file
46
frontend/src/console/input/bus.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import type { InputAction, InputListener } from './actions';
|
||||
|
||||
export class InputBus {
|
||||
private scopes: Array<Set<InputListener>> = [];
|
||||
private globalShortcuts: Set<InputListener> = new Set();
|
||||
|
||||
pushScope(): () => void {
|
||||
const set = new Set<InputListener>();
|
||||
this.scopes.push(set);
|
||||
return () => this.popScope(set);
|
||||
}
|
||||
|
||||
private popScope(scope: Set<InputListener>) {
|
||||
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');
|
||||
46
frontend/src/console/input/config.ts
Normal file
46
frontend/src/console/input/config.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
export interface InputConfig {
|
||||
rtl: boolean;
|
||||
repeat: { initialDelayMs: number; repeatEveryMs: number };
|
||||
deadzone: number;
|
||||
keyMap: Record<string, import('./actions').InputAction | undefined>;
|
||||
gamepad: {
|
||||
axesThreshold: number;
|
||||
axisToAction: {
|
||||
x: { neg: import('./actions').InputAction; pos: import('./actions').InputAction };
|
||||
y: { neg: import('./actions').InputAction; pos: import('./actions').InputAction };
|
||||
};
|
||||
buttons: Record<number, import('./actions').InputAction | undefined>;
|
||||
};
|
||||
}
|
||||
|
||||
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',
|
||||
},
|
||||
},
|
||||
};
|
||||
61
frontend/src/console/input/gamepad.ts
Normal file
61
frontend/src/console/input/gamepad.ts
Normal file
@@ -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<string, { buttons: Record<number, ButtonState>; 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); };
|
||||
}
|
||||
17
frontend/src/console/input/keyboard.ts
Normal file
17
frontend/src/console/input/keyboard.ts
Normal file
@@ -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);
|
||||
}
|
||||
@@ -25,7 +25,7 @@
|
||||
v-else
|
||||
class="relative w-full h-full overflow-y-auto overflow-x-hidden pb-28 md:pb-32"
|
||||
>
|
||||
<BackButton />
|
||||
<BackButton />
|
||||
|
||||
<!-- Backdrop -->
|
||||
<div class="absolute inset-0 z-0 overflow-hidden">
|
||||
@@ -290,12 +290,14 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref } from 'vue';
|
||||
import { computed, onMounted, onUnmounted, ref } from 'vue';
|
||||
import { useRoute, useRouter } from 'vue-router';
|
||||
import romApi from '@/services/api/rom';
|
||||
import type { DetailedRomSchema } from '@/__generated__/models/DetailedRomSchema';
|
||||
import ScreenshotLightbox from '@/console/components/ScreenshotLightbox.vue';
|
||||
import BackButton from '@/console/components/BackButton.vue';
|
||||
import { useInputScope } from '@/console/composables/useInputScope';
|
||||
import type { InputAction } from '@/console/input/actions';
|
||||
|
||||
const route = useRoute();
|
||||
const router = useRouter();
|
||||
@@ -340,24 +342,31 @@ const coverUrl = computed(() =>
|
||||
|
||||
const coreMap: Record<string,string> = { nes:'nes', snes:'snes', n64:'n64', gb:'gb', gba:'gba', gbc:'gbc', genesis:'segaMD', megadrive:'segaMD', master_system:'sms', sms:'sms', gg:'gg', gamecube:'gc', saturn:'sat', psx:'psx', ps1:'psx', ps2:'ps2', ps3:'ps3', psp:'psp', atari2600:'a26', atari7800:'a78', lynx:'lynx', vb:'vb', wonderswan:'ws', wonderswancolor:'wsc', ngp:'ngp', ngpc:'ngpc', pce:'pce', pcfx:'pcfx', tg16:'pce', sgx:'sgx', msx:'msx', msx2:'msx2' };
|
||||
|
||||
function handleKey(e: KeyboardEvent){
|
||||
const { on } = useInputScope();
|
||||
function handleAction(action: InputAction): boolean {
|
||||
// Modal handling first
|
||||
if(showDescription.value || showDetails.value){
|
||||
if(e.key==='Escape') { e.preventDefault(); e.stopPropagation(); showDescription.value=false; showDetails.value=false; return; }
|
||||
if(e.key==='ArrowUp' || e.key==='ArrowDown'){
|
||||
e.preventDefault(); const body = document.querySelector('.modal-body') as HTMLElement|null; if(!body) return; const amt=40; if(e.key==='ArrowUp') body.scrollTop -= amt; else body.scrollTop += amt; return;
|
||||
if(action==='back'){ showDescription.value=false; showDetails.value=false; return true; }
|
||||
if(action==='moveUp' || action==='moveDown'){
|
||||
const body = document.querySelector('.modal-body') as HTMLElement|null; if(!body) return true;
|
||||
const amt = 40; if(action==='moveUp') body.scrollTop -= amt; else body.scrollTop += amt; return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
keyboardMode.value = true; window.setTimeout(()=> keyboardMode.value=false, 2000);
|
||||
if(['ArrowUp','ArrowDown','ArrowLeft','ArrowRight'].includes(e.key)) e.preventDefault();
|
||||
if(['ArrowUp','ArrowDown','ArrowLeft','ArrowRight'].includes(e.key)) {
|
||||
if(action==='moveLeft' || action==='moveRight' || action==='moveUp' || action==='moveDown'){
|
||||
// toggle main selection; also move screenshot focus when lightbox closed
|
||||
selected.value = selected.value==='play' ? 'description' : 'play';
|
||||
if(!showLightbox.value && screenshotUrls.value.length){
|
||||
if(e.key==='ArrowRight') selectedShot.value = (selectedShot.value + 1) % screenshotUrls.value.length;
|
||||
if(e.key==='ArrowLeft') selectedShot.value = (selectedShot.value - 1 + screenshotUrls.value.length) % screenshotUrls.value.length;
|
||||
if(action==='moveRight') selectedShot.value = (selectedShot.value + 1) % screenshotUrls.value.length;
|
||||
if(action==='moveLeft') selectedShot.value = (selectedShot.value - 1 + screenshotUrls.value.length) % screenshotUrls.value.length;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if(e.key==='Enter') { if(selected.value==='play') play(); else showDescription.value = true; }
|
||||
if(action==='confirm'){ if(selected.value==='play') play(); else showDescription.value = true; return true; }
|
||||
if(action==='back'){ router.back(); return true; }
|
||||
return false;
|
||||
}
|
||||
|
||||
function play(){
|
||||
@@ -408,8 +417,10 @@ onMounted(async () => {
|
||||
rom.value = data;
|
||||
}catch(err: unknown){ error.value = err instanceof Error ? err.message : 'Failed to load game'; }
|
||||
finally{ loading.value = false; }
|
||||
window.addEventListener('keydown', handleKey);
|
||||
off = on(handleAction);
|
||||
});
|
||||
let off: (() => void) | null = null;
|
||||
onUnmounted(() => { off?.(); off = null; });
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
@@ -60,8 +60,8 @@
|
||||
:index="i"
|
||||
:selected="navigationMode==='systems' && i===selectedIndex"
|
||||
@click="goPlatform(p.id)"
|
||||
@mouseenter="selectedIndex=i"
|
||||
@focus="selectedIndex=i"
|
||||
@mouseenter="focusSystem(i)"
|
||||
@focus="focusSystem(i)"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
@@ -103,8 +103,8 @@
|
||||
:selected="navigationMode==='recent' && i===recentIndex"
|
||||
:loaded="true"
|
||||
@click="goGame(g)"
|
||||
@mouseenter="recentIndex=i"
|
||||
@focus="recentIndex=i"
|
||||
@mouseenter="focusRecent(i)"
|
||||
@focus="focusRecent(i)"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
@@ -134,7 +134,7 @@
|
||||
</div>
|
||||
</template>
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref, watch } from 'vue';
|
||||
import { onMounted, onUnmounted, ref, watch } from 'vue';
|
||||
import { useRouter } from 'vue-router';
|
||||
import platformApi from '@/services/api/platform';
|
||||
import romApi from '@/services/api/rom';
|
||||
@@ -143,6 +143,8 @@ import GameCard from '@/console/components/GameCard.vue';
|
||||
import RIsotipo from '@/components/common/RIsotipo.vue';
|
||||
import type { PlatformSchema } from '@/__generated__/models/PlatformSchema';
|
||||
import type { SimpleRomSchema } from '@/__generated__/models/SimpleRomSchema';
|
||||
import { useInputScope } from '@/console/composables/useInputScope';
|
||||
import type { InputAction } from '@/console/input/actions';
|
||||
|
||||
const router = useRouter();
|
||||
const platforms = ref<PlatformSchema[]>([]);
|
||||
@@ -157,6 +159,9 @@ const systemsRef = ref<HTMLDivElement>();
|
||||
const recentRef = ref<HTMLDivElement>();
|
||||
const currentBackground = ref('');
|
||||
|
||||
function focusSystem(i: number){ navigationMode.value='systems'; selectedIndex.value=i; }
|
||||
function focusRecent(i: number){ navigationMode.value='recent'; recentIndex.value=i; }
|
||||
|
||||
function onWheelSystems(e: WheelEvent){
|
||||
const el = systemsRef.value; if(!el) return;
|
||||
el.scrollLeft += (e.deltaY || e.deltaX);
|
||||
@@ -181,32 +186,50 @@ function nextRecent(){ recentIndex.value = (recentIndex.value + 1) % recent.valu
|
||||
function tickScroll(){ const w = window as unknown as { systemCardElements?: HTMLElement[] }; const el = w.systemCardElements?.[selectedIndex.value]; scrollToSelected(systemsRef.value!, el); }
|
||||
function tickRecentScroll(){ const w = window as unknown as { recentGameElements?: HTMLElement[] }; const el = w.recentGameElements?.[recentIndex.value]; scrollToSelected(recentRef.value!, el); }
|
||||
|
||||
function handleKeyDown(e: KeyboardEvent){
|
||||
if(['ArrowLeft','ArrowRight','ArrowUp','ArrowDown','Enter'].includes(e.key)) e.preventDefault();
|
||||
switch(e.key){
|
||||
case 'ArrowLeft':
|
||||
if(navigationMode.value==='systems'){ prevSystem(); }
|
||||
else if(navigationMode.value==='recent'){ prevRecent(); }
|
||||
else if(navigationMode.value==='controls'){ controlIndex.value=(controlIndex.value-1+2)%2; }
|
||||
break;
|
||||
case 'ArrowRight':
|
||||
if(navigationMode.value==='systems'){ nextSystem(); }
|
||||
else if(navigationMode.value==='recent'){ nextRecent(); }
|
||||
else if(navigationMode.value==='controls'){ controlIndex.value=(controlIndex.value+1)%2; }
|
||||
break;
|
||||
case 'ArrowUp':
|
||||
if(navigationMode.value==='recent') navigationMode.value='systems';
|
||||
else if(navigationMode.value==='controls') navigationMode.value= recent.value.length>0 ? 'recent' : 'systems';
|
||||
break;
|
||||
case 'ArrowDown':
|
||||
if(navigationMode.value==='systems') navigationMode.value= recent.value.length>0 ? 'recent' : 'controls';
|
||||
else if(navigationMode.value==='recent') navigationMode.value='controls';
|
||||
break;
|
||||
case 'Enter':
|
||||
if(navigationMode.value==='systems' && platforms.value[selectedIndex.value]) router.push({ name:'console-platform', params: { id: platforms.value[selectedIndex.value].id } });
|
||||
else if(navigationMode.value==='recent' && recent.value[recentIndex.value]) router.push({ name:'console-rom', params:{ rom: recent.value[recentIndex.value].id }, query:{ id: recent.value[recentIndex.value].platform_id } });
|
||||
else if(navigationMode.value==='controls') { if(controlIndex.value===0) doLogout(); else toggleFullscreen(); }
|
||||
break;
|
||||
const { on } = useInputScope();
|
||||
function handleAction(action: InputAction): boolean {
|
||||
switch(action){
|
||||
case 'moveLeft':
|
||||
if(navigationMode.value==='systems'){ prevSystem(); return true; }
|
||||
if(navigationMode.value==='recent'){ prevRecent(); return true; }
|
||||
if(navigationMode.value==='controls'){ controlIndex.value=(controlIndex.value-1+2)%2; return true; }
|
||||
return false;
|
||||
case 'moveRight':
|
||||
if(navigationMode.value==='systems'){ nextSystem(); return true; }
|
||||
if(navigationMode.value==='recent'){ nextRecent(); return true; }
|
||||
if(navigationMode.value==='controls'){ controlIndex.value=(controlIndex.value+1)%2; return true; }
|
||||
return false;
|
||||
case 'moveUp':
|
||||
if(navigationMode.value==='recent') { navigationMode.value='systems'; return true; }
|
||||
if(navigationMode.value==='controls') { navigationMode.value= recent.value.length>0 ? 'recent' : 'systems'; return true; }
|
||||
return false;
|
||||
case 'moveDown':
|
||||
if(navigationMode.value==='systems') { navigationMode.value= recent.value.length>0 ? 'recent' : 'controls'; return true; }
|
||||
if(navigationMode.value==='recent') { navigationMode.value='controls'; return true; }
|
||||
return false;
|
||||
case 'confirm':
|
||||
{
|
||||
// Controls take precedence if active
|
||||
if(navigationMode.value==='controls') { if(controlIndex.value===0) doLogout(); else toggleFullscreen(); return true; }
|
||||
|
||||
// If recent is active or focused, open recent
|
||||
const active = document.activeElement as HTMLElement | null;
|
||||
const recentFocused = !!(active && recentRef.value && recentRef.value.contains(active));
|
||||
if(navigationMode.value==='recent' || recentFocused) {
|
||||
const r = recent.value[recentIndex.value];
|
||||
if(r) { router.push({ name:'console-rom', params:{ rom: r.id }, query:{ id: r.platform_id } }); return true; }
|
||||
}
|
||||
|
||||
// Default/fallback: open currently selected system
|
||||
const sys = platforms.value[selectedIndex.value];
|
||||
if(sys) { router.push({ name:'console-platform', params: { id: sys.id } }); return true; }
|
||||
}
|
||||
return false;
|
||||
case 'back':
|
||||
router.back();
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -246,8 +269,10 @@ onMounted(async () => {
|
||||
recent.value = recents.items ?? [];
|
||||
}catch(err: unknown){ error.value = err instanceof Error ? err.message : 'Failed to load'; }
|
||||
finally{ loading.value = false; }
|
||||
window.addEventListener('keydown', handleKeyDown);
|
||||
off = on(handleAction);
|
||||
});
|
||||
let off: (() => void) | null = null;
|
||||
onUnmounted(() => { off?.(); off = null; });
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
@@ -78,13 +78,16 @@
|
||||
</div>
|
||||
</template>
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref, watch } from 'vue';
|
||||
import { computed, onMounted, onUnmounted, ref, watch } from 'vue';
|
||||
import { useRoute, useRouter } from 'vue-router';
|
||||
import romApi from '@/services/api/rom';
|
||||
import GameCard from '@/console/components/GameCard.vue';
|
||||
import NavigationHint from '@/console/components/NavigationHint.vue';
|
||||
import BackButton from '@/console/components/BackButton.vue';
|
||||
import type { SimpleRomSchema } from '@/__generated__/models/SimpleRomSchema';
|
||||
import { useInputScope } from '@/console/composables/useInputScope';
|
||||
import type { InputAction } from '@/console/input/actions';
|
||||
import { useSpatialNav } from '@/console/composables/useSpatialNav';
|
||||
|
||||
const route = useRoute();
|
||||
const router = useRouter();
|
||||
@@ -153,39 +156,43 @@ function scrollToSelected(){
|
||||
|
||||
watch(selectedIndex, () => { if(keyboardMode.value) scrollToSelected(); });
|
||||
|
||||
function handleKey(e: KeyboardEvent){
|
||||
if(!filtered.value.length) return;
|
||||
const { on } = useInputScope();
|
||||
const { moveLeft, moveRight, moveUp, moveDown } = useSpatialNav(selectedIndex, getCols, () => filtered.value.length);
|
||||
|
||||
function handleAction(action: InputAction): boolean {
|
||||
if(!filtered.value.length) return false;
|
||||
keyboardMode.value = true; window.clearTimeout(keyboardTimeout); keyboardTimeout = window.setTimeout(()=> keyboardMode.value=false, 3000);
|
||||
if(['ArrowLeft','ArrowRight','ArrowUp','ArrowDown','Enter'].includes(e.key)) e.preventDefault();
|
||||
|
||||
if(inAlphabet.value){
|
||||
if(e.key==='ArrowLeft'){ inAlphabet.value=false; return; }
|
||||
if(e.key==='ArrowUp'){ alphaIndex.value = Math.max(0, alphaIndex.value-1); return; }
|
||||
if(e.key==='ArrowDown'){ alphaIndex.value = Math.min(letters.length-1, alphaIndex.value+1); return; }
|
||||
if(e.key==='Enter'){
|
||||
if(action==='moveLeft'){ inAlphabet.value=false; return true; }
|
||||
if(action==='moveUp'){ alphaIndex.value = Math.max(0, alphaIndex.value-1); return true; }
|
||||
if(action==='moveDown'){ alphaIndex.value = Math.min(letters.length-1, alphaIndex.value+1); return true; }
|
||||
if(action==='confirm'){
|
||||
const L = letters[alphaIndex.value];
|
||||
const idx = filtered.value.findIndex(r => (r.name||'').toUpperCase().startsWith(L));
|
||||
if(idx>=0){ selectedIndex.value = idx; inAlphabet.value=false; }
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
|
||||
const cols = getCols();
|
||||
switch(e.key){
|
||||
case 'ArrowRight':{
|
||||
switch(action){
|
||||
case 'moveRight':{
|
||||
const cols = getCols();
|
||||
const atRight = (selectedIndex.value + 1) % cols === 0;
|
||||
const last = selectedIndex.value === filtered.value.length-1;
|
||||
if(atRight || last){ inAlphabet.value = true; alphaIndex.value = 0; }
|
||||
else selectedIndex.value = Math.min(filtered.value.length-1, selectedIndex.value+1);
|
||||
break; }
|
||||
case 'ArrowLeft': selectedIndex.value = Math.max(0, selectedIndex.value-1); break;
|
||||
case 'ArrowUp': { const ni = selectedIndex.value - cols; if(ni>=0) selectedIndex.value = ni; break; }
|
||||
case 'ArrowDown': { const ni = selectedIndex.value + cols; if(ni < filtered.value.length) selectedIndex.value = ni; break; }
|
||||
case 'Enter': {
|
||||
else moveRight();
|
||||
return true; }
|
||||
case 'moveLeft': moveLeft(); return true;
|
||||
case 'moveUp': moveUp(); return true;
|
||||
case 'moveDown': moveDown(); return true;
|
||||
case 'back': router.back(); return true;
|
||||
case 'confirm': {
|
||||
const rom = filtered.value[selectedIndex.value];
|
||||
router.push({ name: 'console-rom', params: { rom: rom.id }, query: { id: platformId } });
|
||||
break; }
|
||||
return true; }
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,6 +201,7 @@ function onCardEnter(i:number){ hoverIndex.value = i; if(!keyboardMode.value) se
|
||||
function selectAndOpen(i:number, rom: SimpleRomSchema){ selectedIndex.value = i; router.push({ name: 'console-rom', params: { rom: rom.id }, query: { id: platformId } }); }
|
||||
function jumpToLetter(L:string){ const idx = filtered.value.findIndex(r => (r.name||'').toUpperCase().startsWith(L)); if(idx>=0){ selectedIndex.value = idx; inAlphabet.value=false; keyboardMode.value=true; window.clearTimeout(keyboardTimeout); keyboardTimeout = window.setTimeout(()=> keyboardMode.value=false, 3000); } }
|
||||
|
||||
let off: (() => void) | null = null;
|
||||
onMounted(async () => {
|
||||
try{
|
||||
const { data } = await romApi.getRoms({ platformId, limit: 500, orderBy: 'name', orderDir: 'asc' });
|
||||
@@ -206,8 +214,10 @@ onMounted(async () => {
|
||||
}
|
||||
}catch(err: unknown){ error.value = err instanceof Error ? err.message : 'Failed to load roms'; }
|
||||
finally{ loading.value = false; }
|
||||
window.addEventListener('keydown', handleKey);
|
||||
off = on(handleAction);
|
||||
});
|
||||
|
||||
onUnmounted(() => { off?.(); off = null; });
|
||||
|
||||
function markLoaded(id: number){ loadedMap.value[id] = true; }
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user