mirror of
https://github.com/mileswolfallen2/OmniEmu2.0.git
synced 2026-12-06 16:43:54 +00:00
feat: enhance emulator configuration and controller support
- Refactored built-in presets in configurator.ts to improve organization and added new configurations for various emulators. - Implemented caching for scraped cover URLs in scraper.ts and added functionality to apply cached covers to game entries. - Updated IPC handlers in ipc.ts to support caching of cover URLs. - Enhanced gamepad navigation in useGamepadNav.ts for better user experience with controller inputs. - Improved controller detection and button labeling in ControllerPage.tsx to support PlayStation, Xbox, and Switch controllers. - Added accessibility features to game cards in Dashboard.tsx and LibraryPage.tsx for keyboard navigation. - Introduced a new presets.json file with recommended configurations for various emulators. - Updated styles.css to enhance focus visibility for keyboard and controller navigation.
This commit is contained in:
1 parent
9a3002010d
commit
bd8b83a02f
11 files changed
+477
-158
No files matched your search
@@ -1,11 +1,11 @@
|
||||
import { useEffect, useRef } from 'react';
|
||||
|
||||
const DEBOUNCE_MS = 200;
|
||||
const DEBOUNCE_MS = 300;
|
||||
|
||||
type NavDir = 'up' | 'down' | 'left' | 'right';
|
||||
const SIDEBAR_SELECTOR = '.nav-item';
|
||||
|
||||
export function useGamepadNav() {
|
||||
const lastInput = useRef<Record<string, number>>({});
|
||||
const lastTime = useRef<Record<string, number>>({});
|
||||
|
||||
useEffect(() => {
|
||||
let raf = 0;
|
||||
@@ -17,76 +17,69 @@ export function useGamepadNav() {
|
||||
for (const gp of gamepads) {
|
||||
if (!gp || !gp.connected) continue;
|
||||
|
||||
// D-Pad buttons (indices 12-15)
|
||||
const dpadUp = gp.buttons[12]?.pressed;
|
||||
const dpadDown = gp.buttons[13]?.pressed;
|
||||
const dpadLeft = gp.buttons[14]?.pressed;
|
||||
const dpadRight = gp.buttons[15]?.pressed;
|
||||
const debounced = (key: string): boolean => {
|
||||
if (now - (lastTime.current[key] || 0) < DEBOUNCE_MS) return false;
|
||||
lastTime.current[key] = now;
|
||||
return true;
|
||||
};
|
||||
|
||||
// Left stick axes (indices 0, 1)
|
||||
const axisX = gp.axes[0] || 0;
|
||||
const axisY = gp.axes[1] || 0;
|
||||
// D-Pad buttons (12-15) and left stick
|
||||
const dpadUp = gp.buttons[12]?.pressed || gp.axes[1] < -0.6;
|
||||
const dpadDown = gp.buttons[13]?.pressed || gp.axes[1] > 0.6;
|
||||
const dpadLeft = gp.buttons[14]?.pressed || gp.axes[0] < -0.6;
|
||||
const dpadRight = gp.buttons[15]?.pressed || gp.axes[0] > 0.6;
|
||||
|
||||
const threshold = 0.5;
|
||||
|
||||
const dirs: NavDir[] = [];
|
||||
if (dpadUp) dirs.push('up');
|
||||
if (dpadDown) dirs.push('down');
|
||||
if (dpadLeft) dirs.push('left');
|
||||
if (dpadRight) dirs.push('right');
|
||||
|
||||
if (axisY < -threshold) dirs.push('up');
|
||||
if (axisY > threshold) dirs.push('down');
|
||||
if (axisX < -threshold) dirs.push('left');
|
||||
if (axisX > threshold) dirs.push('right');
|
||||
|
||||
for (const dir of dirs) {
|
||||
const key = `dir:${dir}`;
|
||||
if (now - (lastInput.current[key] || 0) < DEBOUNCE_MS) continue;
|
||||
lastInput.current[key] = now;
|
||||
|
||||
const keyMap: Record<NavDir, string> = {
|
||||
up: 'ArrowUp',
|
||||
down: 'ArrowDown',
|
||||
left: 'ArrowLeft',
|
||||
right: 'ArrowRight',
|
||||
};
|
||||
document.dispatchEvent(new KeyboardEvent('keydown', {
|
||||
key: keyMap[dir],
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
}));
|
||||
// --- Sidebar navigation with Left/Right ---
|
||||
if (dpadLeft && debounced('left')) {
|
||||
cycleSidebar(-1);
|
||||
}
|
||||
if (dpadRight && debounced('right')) {
|
||||
cycleSidebar(1);
|
||||
}
|
||||
|
||||
// A button (0) → Enter
|
||||
if (gp.buttons[0]?.pressed) {
|
||||
if (now - (lastInput.current['a'] || 0) < DEBOUNCE_MS) continue;
|
||||
lastInput.current['a'] = now;
|
||||
// --- Page content navigation with Up/Down (Tab/Shift+Tab) ---
|
||||
if (dpadUp && debounced('up')) {
|
||||
const focused = document.activeElement;
|
||||
if (focused) {
|
||||
focused.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true }));
|
||||
const sidebar = focused?.closest('.sidebar');
|
||||
if (sidebar) {
|
||||
cycleSidebar(-1);
|
||||
} else {
|
||||
document.dispatchEvent(new KeyboardEvent('keydown', {
|
||||
key: 'Enter', bubbles: true, cancelable: true,
|
||||
}));
|
||||
// Focus previous focusable element
|
||||
const focusable = getFocusableElements();
|
||||
const idx = focusable.indexOf(focused as HTMLElement);
|
||||
if (idx > 0) focusable[idx - 1]?.focus();
|
||||
}
|
||||
}
|
||||
if (dpadDown && debounced('down')) {
|
||||
const focused = document.activeElement;
|
||||
const sidebar = focused?.closest('.sidebar');
|
||||
if (sidebar) {
|
||||
cycleSidebar(1);
|
||||
} else {
|
||||
const focusable = getFocusableElements();
|
||||
const idx = focusable.indexOf(focused as HTMLElement);
|
||||
if (idx < focusable.length - 1) focusable[idx + 1]?.focus();
|
||||
else focusable[0]?.focus();
|
||||
}
|
||||
}
|
||||
|
||||
// B button (1) → Escape
|
||||
if (gp.buttons[1]?.pressed) {
|
||||
if (now - (lastInput.current['b'] || 0) < DEBOUNCE_MS) continue;
|
||||
lastInput.current['b'] = now;
|
||||
document.dispatchEvent(new KeyboardEvent('keydown', {
|
||||
key: 'Escape', bubbles: true, cancelable: true,
|
||||
}));
|
||||
// A button (0) → click
|
||||
if (gp.buttons[0]?.pressed && debounced('a')) {
|
||||
const el = document.activeElement as HTMLElement;
|
||||
if (el) el.click();
|
||||
}
|
||||
|
||||
// Start (9) → Enter on first focusable
|
||||
if (gp.buttons[9]?.pressed) {
|
||||
if (now - (lastInput.current['start'] || 0) < DEBOUNCE_MS) continue;
|
||||
lastInput.current['start'] = now;
|
||||
const firstBtn = document.querySelector<HTMLElement>('button, [tabindex]:not([tabindex="-1"]), a, input');
|
||||
firstBtn?.focus();
|
||||
// B button (1) → focus sidebar (return to nav)
|
||||
if (gp.buttons[1]?.pressed && debounced('b')) {
|
||||
const sideItems = document.querySelectorAll<HTMLElement>(SIDEBAR_SELECTOR);
|
||||
const active = document.querySelector('.nav-item.active') as HTMLElement;
|
||||
(active || sideItems[0])?.focus();
|
||||
}
|
||||
|
||||
// Start (9) → focus first interactive element on page
|
||||
if (gp.buttons[9]?.pressed && debounced('start')) {
|
||||
const all = getFocusableElements();
|
||||
all[0]?.focus();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,3 +90,24 @@ export function useGamepadNav() {
|
||||
return () => cancelAnimationFrame(raf);
|
||||
}, []);
|
||||
}
|
||||
|
||||
function getFocusableElements(): HTMLElement[] {
|
||||
return Array.from(
|
||||
document.querySelectorAll<HTMLElement>(
|
||||
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"]), .game-card, .nav-item'
|
||||
)
|
||||
).filter(el => el.offsetParent !== null); // only visible
|
||||
}
|
||||
|
||||
function cycleSidebar(dir: 1 | -1): void {
|
||||
const items = document.querySelectorAll<HTMLElement>(SIDEBAR_SELECTOR);
|
||||
if (items.length === 0) return;
|
||||
const activeIdx = Array.from(items).findIndex(el =>
|
||||
el.classList.contains('active') || el === document.activeElement
|
||||
);
|
||||
let next = activeIdx + dir;
|
||||
if (next < 0) next = items.length - 1;
|
||||
if (next >= items.length) next = 0;
|
||||
items[next]?.click();
|
||||
items[next]?.focus();
|
||||
}
|
||||
@@ -9,11 +9,42 @@ interface ControllerState {
|
||||
connected: boolean;
|
||||
}
|
||||
|
||||
const buttonLabels = [
|
||||
'A', 'B', 'X', 'Y', 'LB', 'RB', 'LT', 'RT',
|
||||
'Back', 'Start', 'L3', 'R3', 'DPad-Up', 'DPad-Down', 'DPad-Left', 'DPad-Right',
|
||||
'Home',
|
||||
];
|
||||
type ControllerVendor = 'xbox' | 'playstation' | 'switch' | 'generic';
|
||||
|
||||
function detectVendor(id: string): ControllerVendor {
|
||||
const lower = id.toLowerCase();
|
||||
if (lower.includes('ps4') || lower.includes('ps5') || lower.includes('dualsense')
|
||||
|| lower.includes('dualshock') || lower.includes('sony') || lower.includes('wireless controller')
|
||||
|| lower.includes('playstation')) return 'playstation';
|
||||
if (lower.includes('xbox') || lower.includes('xinput') || lower.includes('x-box')
|
||||
|| lower.includes('microsoft')) return 'xbox';
|
||||
if (lower.includes('switch') || lower.includes('pro controller') || lower.includes('nintendo'))
|
||||
return 'switch';
|
||||
return 'generic';
|
||||
}
|
||||
|
||||
const vendorLabels: Record<ControllerVendor, string[]> = {
|
||||
playstation: [
|
||||
'Cross', 'Circle', 'Square', 'Triangle', 'L1', 'R1', 'L2', 'R2',
|
||||
'Share', 'Options', 'L3', 'R3', 'DPad-Up', 'DPad-Down', 'DPad-Left', 'DPad-Right',
|
||||
'PS',
|
||||
],
|
||||
xbox: [
|
||||
'A', 'B', 'X', 'Y', 'LB', 'RB', 'LT', 'RT',
|
||||
'Back', 'Start', 'L3', 'R3', 'DPad-Up', 'DPad-Down', 'DPad-Left', 'DPad-Right',
|
||||
'Home',
|
||||
],
|
||||
switch: [
|
||||
'B', 'A', 'Y', 'X', 'L', 'R', 'ZL', 'ZR',
|
||||
'-', '+', 'L3', 'R3', 'DPad-Up', 'DPad-Down', 'DPad-Left', 'DPad-Right',
|
||||
'Home',
|
||||
],
|
||||
generic: [
|
||||
'A', 'B', 'X', 'Y', 'LB', 'RB', 'LT', 'RT',
|
||||
'Back', 'Start', 'L3', 'R3', 'DPad-Up', 'DPad-Down', 'DPad-Left', 'DPad-Right',
|
||||
'Home',
|
||||
],
|
||||
};
|
||||
|
||||
export function ControllerPage() {
|
||||
const [controllers, setControllers] = useState<ControllerState[]>([]);
|
||||
@@ -95,51 +126,58 @@ export function ControllerPage() {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{controllers.map((ctrl) => (
|
||||
<div key={ctrl.index} className="card mb-4">
|
||||
<div className="card-header">
|
||||
<h3>Controller {ctrl.index + 1}</h3>
|
||||
<span className="badge badge-installed">Connected</span>
|
||||
</div>
|
||||
<p className="text-sm text-muted mb-2">{ctrl.id}</p>
|
||||
{controllers.map((ctrl) => {
|
||||
const vendor = detectVendor(ctrl.id);
|
||||
const labels = vendorLabels[vendor];
|
||||
|
||||
<div className="controller-buttons">
|
||||
{ctrl.buttons.length > 0 && buttonLabels.map((label, i) => {
|
||||
const btn = ctrl.buttons[i];
|
||||
if (!btn) return null;
|
||||
return (
|
||||
<div
|
||||
key={i}
|
||||
className={`controller-btn ${btn.pressed ? 'pressed' : ''}`}
|
||||
>
|
||||
<span className="controller-btn-label">{label}</span>
|
||||
<span className="controller-btn-value">{btn.value.toFixed(2)}</span>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
|
||||
{ctrl.axes.length > 0 && (
|
||||
<div className="mt-2">
|
||||
<p className="text-sm text-muted mb-2">Axes</p>
|
||||
<div className="controller-axes">
|
||||
{ctrl.axes.map((val, i) => (
|
||||
<div key={i} className="axis-bar">
|
||||
<span className="axis-label">Axis {i}</span>
|
||||
<div className="axis-track">
|
||||
<div
|
||||
className="axis-fill"
|
||||
style={{ left: `${((val + 1) / 2) * 100}%` }}
|
||||
/>
|
||||
</div>
|
||||
<span className="axis-value">{val.toFixed(2)}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
return (
|
||||
<div key={ctrl.index} className="card mb-4">
|
||||
<div className="card-header">
|
||||
<h3>
|
||||
{vendor === 'playstation' ? '🎮 PlayStation' : vendor === 'xbox' ? '🎮 Xbox' : '🎮 Controller'} {ctrl.index + 1}
|
||||
</h3>
|
||||
<span className="badge badge-installed">Connected</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
<p className="text-sm text-muted mb-2">{ctrl.id}</p>
|
||||
|
||||
<div className="controller-buttons">
|
||||
{ctrl.buttons.length > 0 && labels.map((label, i) => {
|
||||
const btn = ctrl.buttons[i];
|
||||
if (!btn) return null;
|
||||
return (
|
||||
<div
|
||||
key={i}
|
||||
className={`controller-btn ${btn.pressed ? 'pressed' : ''}`}
|
||||
>
|
||||
<span className="controller-btn-label">{label}</span>
|
||||
<span className="controller-btn-value">{btn.value.toFixed(2)}</span>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
|
||||
{ctrl.axes.length > 0 && (
|
||||
<div className="mt-2">
|
||||
<p className="text-sm text-muted mb-2">Axes</p>
|
||||
<div className="controller-axes">
|
||||
{ctrl.axes.map((val, i) => (
|
||||
<div key={i} className="axis-bar">
|
||||
<span className="axis-label">Axis {i}</span>
|
||||
<div className="axis-track">
|
||||
<div
|
||||
className="axis-fill"
|
||||
style={{ left: `${((val + 1) / 2) * 100}%` }}
|
||||
/>
|
||||
</div>
|
||||
<span className="axis-value">{val.toFixed(2)}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
|
||||
{emulators.length > 0 && (
|
||||
<div>
|
||||
|
||||
@@ -100,6 +100,8 @@ export function Dashboard({ onNavigate }: { onNavigate?: (tab: string) => void }
|
||||
<div
|
||||
key={game.id}
|
||||
className="game-card"
|
||||
tabIndex={0}
|
||||
role="button"
|
||||
onClick={async () => {
|
||||
await window.omni.game.launch(game.emulatorId, game.romPath);
|
||||
const updated = await window.omni.game.recent();
|
||||
|
||||
@@ -28,13 +28,17 @@ export function LibraryPage() {
|
||||
}, []);
|
||||
|
||||
const scrapeMissingArt = async (list: GameEntry[]): Promise<GameEntry[]> => {
|
||||
return Promise.all(list.map(async (g) => {
|
||||
const updated = await Promise.all(list.map(async (g) => {
|
||||
if (!g.coverUrl) {
|
||||
const url = await window.omni.game.scrapeArt(g.title, g.platform);
|
||||
if (url) g.coverUrl = url;
|
||||
}
|
||||
return g;
|
||||
}));
|
||||
// Persist found covers
|
||||
const toCache = updated.filter(g => g.coverUrl).map(g => ({ romPath: g.romPath, coverUrl: g.coverUrl! }));
|
||||
if (toCache.length > 0) window.omni.game.cacheCovers(toCache);
|
||||
return updated;
|
||||
};
|
||||
|
||||
const scanDirectory = useCallback(async (dir: string) => {
|
||||
@@ -118,6 +122,9 @@ export function LibraryPage() {
|
||||
className="game-card"
|
||||
key={game.id}
|
||||
onClick={() => handleLaunch(game)}
|
||||
onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') handleLaunch(game); }}
|
||||
tabIndex={0}
|
||||
role="button"
|
||||
title={`Launch ${game.title} via ${game.emulatorId}`}
|
||||
>
|
||||
<div className="game-card-cover">
|
||||
|
||||
@@ -6,6 +6,19 @@
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
/* Focus ring for controller/keyboard navigation */
|
||||
:focus-visible {
|
||||
outline: 2px solid var(--accent) !important;
|
||||
outline-offset: 2px !important;
|
||||
}
|
||||
|
||||
/* Game cards need special treatment since they use div click handlers */
|
||||
.game-card:focus-visible {
|
||||
outline: 2px solid var(--accent) !important;
|
||||
outline-offset: 2px !important;
|
||||
border-color: var(--accent);
|
||||
}
|
||||
|
||||
:root {
|
||||
--bg-primary: #0f0f1a;
|
||||
--bg-secondary: #1a1a2e;
|
||||
|
||||
Reference in new issue
Block a user