mirror of
https://github.com/rommapp/romm.git
synced 2026-11-30 02:22:44 +01:00
fix(v2): read left-stick motion as pad input
The stick is translated into synthetic Arrow keydowns, which bubble to useInputModality and read as a keyboard press. Buttons re-assert the pad right after dispatching; the stick never did, so a stick-only user settled in "key" modality and the grid-nav autofocus, which only claims a page for "pad", never fired. Nothing was focused, so the arrows had nowhere to go. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
120
frontend/src/v2/composables/useGamepad/index.test.ts
Normal file
120
frontend/src/v2/composables/useGamepad/index.test.ts
Normal file
@@ -0,0 +1,120 @@
|
||||
import { mount } from "@vue/test-utils";
|
||||
import { createPinia, setActivePinia } from "pinia";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { defineComponent } from "vue";
|
||||
import storePlaying from "@/stores/playing";
|
||||
import { useInputModality } from "@/v2/composables/useInputModality";
|
||||
import { AXIS_THRESHOLD, useGamepad } from "./index";
|
||||
|
||||
vi.mock("vue-router", () => ({
|
||||
useRoute: () => ({ path: "/platforms" }),
|
||||
useRouter: () => ({ push: vi.fn(), back: vi.fn() }),
|
||||
}));
|
||||
|
||||
const PUSHED = AXIS_THRESHOLD + 0.2;
|
||||
|
||||
function padWithStick(x: number, y: number): Gamepad {
|
||||
return {
|
||||
index: 0,
|
||||
id: "test-pad",
|
||||
connected: true,
|
||||
mapping: "standard",
|
||||
axes: [x, y],
|
||||
buttons: [],
|
||||
timestamp: 0,
|
||||
} as unknown as Gamepad;
|
||||
}
|
||||
|
||||
describe("useGamepad", () => {
|
||||
const { modality, setModality } = useInputModality();
|
||||
let wrapper: ReturnType<typeof mount> | null = null;
|
||||
let frame: FrameRequestCallback | null = null;
|
||||
let keys: string[] = [];
|
||||
|
||||
function onKeydown(e: KeyboardEvent) {
|
||||
keys.push(e.key);
|
||||
}
|
||||
|
||||
// Runs one poll of the RAF loop install() armed; the loop re-arms itself.
|
||||
function step() {
|
||||
const pending = frame;
|
||||
frame = null;
|
||||
pending?.(0);
|
||||
}
|
||||
|
||||
// Seeing a pad at install flips the modality, so hand the session back to
|
||||
// the mouse: these tests are about the user who launched with a click.
|
||||
function installOnMouse(pad: Gamepad) {
|
||||
Object.defineProperty(navigator, "getGamepads", {
|
||||
value: () => [pad],
|
||||
configurable: true,
|
||||
});
|
||||
wrapper = mount(
|
||||
defineComponent({
|
||||
setup() {
|
||||
useGamepad().install();
|
||||
return () => null;
|
||||
},
|
||||
}),
|
||||
);
|
||||
setModality("mouse");
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
setActivePinia(createPinia());
|
||||
keys = [];
|
||||
frame = null;
|
||||
vi.stubGlobal("requestAnimationFrame", (cb: FrameRequestCallback) => {
|
||||
frame = cb;
|
||||
return 1;
|
||||
});
|
||||
vi.stubGlobal("cancelAnimationFrame", () => {});
|
||||
window.addEventListener("keydown", onKeydown);
|
||||
// The listener that reads a synthetic arrow as a keyboard press is half
|
||||
// of what this suite exercises, so the real modality tracker is live.
|
||||
useInputModality().install();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
window.removeEventListener("keydown", onKeydown);
|
||||
wrapper?.unmount();
|
||||
wrapper = null;
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it("steers with the left stick", () => {
|
||||
installOnMouse(padWithStick(PUSHED, 0));
|
||||
|
||||
step();
|
||||
|
||||
expect(keys).toEqual(["ArrowRight"]);
|
||||
});
|
||||
|
||||
it("reads stick motion as pad input, not as the keypress it synthesises", () => {
|
||||
installOnMouse(padWithStick(0, -PUSHED));
|
||||
|
||||
step();
|
||||
|
||||
expect(keys).toEqual(["ArrowUp"]);
|
||||
expect(modality.value).toBe("pad");
|
||||
});
|
||||
|
||||
it("still claims the pad while the emulator owns the stick", () => {
|
||||
installOnMouse(padWithStick(PUSHED, 0));
|
||||
storePlaying().setPlaying(true);
|
||||
|
||||
step();
|
||||
|
||||
expect(keys).toEqual([]);
|
||||
expect(modality.value).toBe("pad");
|
||||
});
|
||||
|
||||
it("ignores a stick resting below the threshold", () => {
|
||||
installOnMouse(padWithStick(AXIS_THRESHOLD - 0.1, 0));
|
||||
|
||||
step();
|
||||
|
||||
expect(keys).toEqual([]);
|
||||
expect(modality.value).toBe("mouse");
|
||||
});
|
||||
});
|
||||
@@ -288,24 +288,30 @@ export function useGamepad() {
|
||||
axisNextAt: { x: 0, y: 0 },
|
||||
});
|
||||
|
||||
// Left stick → ArrowKey equivalents.
|
||||
// Left stick → ArrowKey equivalents. The synthetic arrow reads as a
|
||||
// keyboard press on its way through useInputModality, so re-assert the
|
||||
// pad after it: "key" modality never trips the grid-nav autofocus.
|
||||
const x = pad.axes[0] ?? 0;
|
||||
const y = pad.axes[1] ?? 0;
|
||||
tickAxis(st, "x", x, t, (dir) => {
|
||||
if (gameOwnsInput) return;
|
||||
dispatchKey(
|
||||
dir < 0
|
||||
? { key: "ArrowLeft", code: "ArrowLeft" }
|
||||
: { key: "ArrowRight", code: "ArrowRight" },
|
||||
);
|
||||
if (!gameOwnsInput) {
|
||||
dispatchKey(
|
||||
dir < 0
|
||||
? { key: "ArrowLeft", code: "ArrowLeft" }
|
||||
: { key: "ArrowRight", code: "ArrowRight" },
|
||||
);
|
||||
}
|
||||
onAnyInput();
|
||||
});
|
||||
tickAxis(st, "y", y, t, (dir) => {
|
||||
if (gameOwnsInput) return;
|
||||
dispatchKey(
|
||||
dir < 0
|
||||
? { key: "ArrowUp", code: "ArrowUp" }
|
||||
: { key: "ArrowDown", code: "ArrowDown" },
|
||||
);
|
||||
if (!gameOwnsInput) {
|
||||
dispatchKey(
|
||||
dir < 0
|
||||
? { key: "ArrowUp", code: "ArrowUp" }
|
||||
: { key: "ArrowDown", code: "ArrowDown" },
|
||||
);
|
||||
}
|
||||
onAnyInput();
|
||||
});
|
||||
|
||||
// Buttons. Three tracks, evaluated in order:
|
||||
|
||||
Reference in New Issue
Block a user