diff --git a/website/agb/src/app/mgba/useController.hook.ts b/website/agb/src/app/mgba/useController.hook.ts index ff874e7c..4555d526 100644 --- a/website/agb/src/app/mgba/useController.hook.ts +++ b/website/agb/src/app/mgba/useController.hook.ts @@ -3,96 +3,92 @@ import { mGBAEmulator } from "./vendor/mgba"; import { GbaKey } from "./bindings"; export function useController(mgbaModule: MutableRefObject) { - useEffect(() => { - let stopped = false; + useEffect(() => { + let stopped = false; + let previouslyPressedButtons = new Set(); - let previouslyPressedButtons = new Set(); + function raf(time: DOMHighResTimeStamp) { + const controllers = navigator.getGamepads(); + const currentlyPressed = new Set(); + for (let controller of controllers) { + if (!controller) continue; - function raf(time: DOMHighResTimeStamp) { - - const controllers = navigator.getGamepads(); - const currentlyPressed = new Set(); - 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); - } + 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); } - function gamepadConnectedEvent() { - + if (controller.buttons[8].pressed) { + currentlyPressed.add(GbaKey.Select); } - window.addEventListener("gamepadconnected", gamepadConnectedEvent); + 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] < -0.5) { + currentlyPressed.add(GbaKey.Left); + } + if (controller.axes[0] > 0.5) { + currentlyPressed.add(GbaKey.Right); + } + if (controller.axes[1] < -0.5) { + currentlyPressed.add(GbaKey.Up); + } + if (controller.axes[1] > 0.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); - return () => { stopped = true; window.removeEventListener("gamepadconnected", gamepadConnectedEvent); }; - }, [mgbaModule]); -} \ No newline at end of file + } + } + + function gamepadConnectedEvent() {} + + window.addEventListener("gamepadconnected", gamepadConnectedEvent); + + window.requestAnimationFrame(raf); + return () => { + stopped = true; + window.removeEventListener("gamepadconnected", gamepadConnectedEvent); + }; + }, [mgbaModule]); +} diff --git a/website/agb/src/app/mgba/useFrameSkip.hook.ts b/website/agb/src/app/mgba/useFrameSkip.hook.ts index 9f96d447..8f75309f 100644 --- a/website/agb/src/app/mgba/useFrameSkip.hook.ts +++ b/website/agb/src/app/mgba/useFrameSkip.hook.ts @@ -1,53 +1,48 @@ import { MutableRefObject, useEffect } from "react"; import { mGBAEmulator } from "./vendor/mgba"; - export function useFrameSkip(mgbaModule: MutableRefObject) { - useEffect(() => { - let previous: number | undefined = undefined; - let stopped = false; - let smoothedFrameTime = 60; + useEffect(() => { + let previous: number | undefined = undefined; + let stopped = false; + let smoothedFrameTime = 60; - let totalTime = 0; - let paused = false; + let totalTime = 0; + let paused = false; - function raf(time: DOMHighResTimeStamp) { - if (previous) { - const delta = time - previous; + function raf(time: DOMHighResTimeStamp) { + if (previous) { + const delta = time - previous; - smoothedFrameTime = (smoothedFrameTime * 3 + delta) / 4; + smoothedFrameTime = (smoothedFrameTime * 3 + delta) / 4; - const smoothedFrameRate = Math.round(1 / (smoothedFrameTime / 1000)); + const smoothedFrameRate = Math.round(1 / (smoothedFrameTime / 1000)); + totalTime += 1 / smoothedFrameRate; - totalTime += 1 / smoothedFrameRate; - - if (totalTime >= 1 / 60) { - totalTime -= 1 / 60; - if (paused) { - mgbaModule.current.resumeGame(); - paused = false; - } - } else { - if (!paused) { - mgbaModule.current.pauseGame(); - paused = true; - } - } - - - } - previous = time; - - if (!stopped) { - window.requestAnimationFrame(raf); - } + if (totalTime >= 1 / 60) { + totalTime -= 1 / 60; + if (paused) { + mgbaModule.current.resumeGame(); + paused = false; + } + } else { + if (!paused) { + mgbaModule.current.pauseGame(); + paused = true; + } } + } + previous = time; + if (!stopped) { window.requestAnimationFrame(raf); - return () => { stopped = true; }; - }, [mgbaModule]); + } + } - - -} \ No newline at end of file + window.requestAnimationFrame(raf); + return () => { + stopped = true; + }; + }, [mgbaModule]); +} diff --git a/website/agb/src/app/mgba/useLocalStorage.hook.ts b/website/agb/src/app/mgba/useLocalStorage.hook.ts index 5d5e247c..5a13148e 100644 --- a/website/agb/src/app/mgba/useLocalStorage.hook.ts +++ b/website/agb/src/app/mgba/useLocalStorage.hook.ts @@ -1,7 +1,9 @@ import { useCallback, useState } from "react"; -export function useLocalStorage(defaultValue: T, - appName: string): [T, (newValue: T) => void] { +export function useLocalStorage( + defaultValue: T, + appName: string +): [T, (newValue: T) => void] { const [value, setValue] = useState(() => { try { const storageValue = localStorage.getItem(appName); @@ -19,7 +21,7 @@ export function useLocalStorage(defaultValue: T, setValue(newValue); try { localStorage.setItem(appName, JSON.stringify(newValue)); - } catch { } + } catch {} // eslint-disable-next-line react-hooks/exhaustive-deps }, []); diff --git a/website/agb/src/app/useClientValue.hook.ts b/website/agb/src/app/useClientValue.hook.ts index 79f6d95e..cb0c395b 100644 --- a/website/agb/src/app/useClientValue.hook.ts +++ b/website/agb/src/app/useClientValue.hook.ts @@ -1,10 +1,10 @@ import { useEffect, useState } from "react"; export function useClientValue(fn: () => T) { - const [value, setValue] = useState(); - useEffect(() => { - setValue(fn()); - }, [fn]); + const [value, setValue] = useState(); + useEffect(() => { + setValue(fn()); + }, [fn]); - return value; -} \ No newline at end of file + return value; +}