mirror of
https://github.com/italicsjenga/agb.git
synced 2024-12-23 08:11:33 +11:00
add controller support
This commit is contained in:
parent
5f37e9f442
commit
c29a9a4a2b
|
@ -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;
|
||||
|
|
98
website/agb/src/app/mgba/useController.hook.ts
Normal file
98
website/agb/src/app/mgba/useController.hook.ts
Normal 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]);
|
||||
}
|
Loading…
Reference in a new issue