add controller support

This commit is contained in:
Corwin 2024-04-10 18:30:12 +01:00
parent 5f37e9f442
commit c29a9a4a2b
No known key found for this signature in database
2 changed files with 100 additions and 0 deletions

View file

@ -9,6 +9,7 @@ import mGBA, { mGBAEmulator } from "./vendor/mgba";
import { GbaKey, KeyBindings } from "./bindings";
import { styled } from "styled-components";
import { useFrameSkip } from "./useFrameSkip.hook";
import { useController } from "./useController.hook";
type Module = any;
@ -105,6 +106,7 @@ export const Mgba = forwardRef<MgbaHandle, MgbaProps>(
}, [state]);
useFrameSkip(mgbaModule);
useController(mgbaModule);
useEffect(() => {
if (!gameLoaded) return;

View file

@ -0,0 +1,98 @@
import { MutableRefObject, useEffect } from "react";
import { mGBAEmulator } from "./vendor/mgba";
import { GbaKey } from "./bindings";
export function useController(mgbaModule: MutableRefObject<mGBAEmulator>) {
useEffect(() => {
let stopped = false;
let previouslyPressedButtons = new Set<GbaKey>();
function raf(time: DOMHighResTimeStamp) {
const controllers = navigator.getGamepads();
const currentlyPressed = new Set<GbaKey>();
for (let controller of controllers) {
if (!controller) continue;
if (controller.buttons[1].pressed) {
currentlyPressed.add(GbaKey.A);
}
if (controller.buttons[0].pressed) {
currentlyPressed.add(GbaKey.B);
}
if (controller.buttons[5].pressed) {
currentlyPressed.add(GbaKey.R);
}
if (controller.buttons[4].pressed) {
currentlyPressed.add(GbaKey.L);
}
if (controller.buttons[8].pressed) {
currentlyPressed.add(GbaKey.Select);
}
if (controller.buttons[9].pressed) {
currentlyPressed.add(GbaKey.Start);
}
if (controller.buttons[12].pressed) {
currentlyPressed.add(GbaKey.Up);
}
if (controller.buttons[13].pressed) {
currentlyPressed.add(GbaKey.Down);
}
if (controller.buttons[14].pressed) {
currentlyPressed.add(GbaKey.Left);
}
if (controller.buttons[15].pressed) {
currentlyPressed.add(GbaKey.Right);
}
if (controller.axes[0] < -.5) {
currentlyPressed.add(GbaKey.Left);
}
if (controller.axes[0] > .5) {
currentlyPressed.add(GbaKey.Right);
}
if (controller.axes[1] < -.5) {
currentlyPressed.add(GbaKey.Up);
}
if (controller.axes[1] > .5) {
currentlyPressed.add(GbaKey.Down);
}
}
for (let oldButton of previouslyPressedButtons) {
if (!currentlyPressed.has(oldButton)) {
mgbaModule.current.buttonUnpress(oldButton);
}
}
for (let newButton of currentlyPressed) {
if (!previouslyPressedButtons.has(newButton)) {
mgbaModule.current.buttonPress(newButton);
}
}
previouslyPressedButtons = currentlyPressed;
if (!stopped) {
window.requestAnimationFrame(raf);
}
}
function gamepadConnectedEvent() {
}
window.addEventListener("gamepadconnected", gamepadConnectedEvent);
window.requestAnimationFrame(raf);
return () => { stopped = true; window.removeEventListener("gamepadconnected", gamepadConnectedEvent); };
}, [mgbaModule]);
}