Pause the game while you're choosing key bindings

This commit is contained in:
Gwilym Inzani 2023-04-25 21:45:41 +01:00 committed by Corwin
parent 2106c4cefd
commit 845a4b9244
No known key found for this signature in database
4 changed files with 31 additions and 13 deletions

View file

@ -6,6 +6,7 @@ function App() {
const [onGame, setOnGame] = useState(false); const [onGame, setOnGame] = useState(false);
const [volume, setVolume] = useState(1.0); const [volume, setVolume] = useState(1.0);
const [bindings, setBindings] = useState(DefaultBindingsSet()); const [bindings, setBindings] = useState(DefaultBindingsSet());
const [paused, setPaused] = useState(false);
return ( return (
<div> <div>
@ -15,6 +16,7 @@ function App() {
gameUrl="/game.gba" gameUrl="/game.gba"
volume={volume} volume={volume}
controls={bindings.Actual} controls={bindings.Actual}
paused={paused}
/> />
<input <input
type="range" type="range"
@ -29,7 +31,11 @@ function App() {
<button onClick={() => setOnGame(!onGame)}> <button onClick={() => setOnGame(!onGame)}>
{onGame ? "End Game" : "Start Game"} {onGame ? "End Game" : "Start Game"}
</button> </button>
<BindingsControl bindings={bindings} setBindings={setBindings} /> <BindingsControl
bindings={bindings}
setBindings={setBindings}
setPaused={setPaused}
/>
</div> </div>
); );
} }

View file

@ -75,7 +75,8 @@ const toHumanName = (keyName: string) => {
export const BindingsControl: FC<{ export const BindingsControl: FC<{
bindings: Bindings; bindings: Bindings;
setBindings: (a: Bindings) => void; setBindings: (a: Bindings) => void;
}> = ({ bindings, setBindings }) => { setPaused: (pause: boolean) => void;
}> = ({ bindings, setBindings, setPaused }) => {
const [buttonToChange, setButtonToChange] = useState<GbaKey | null>(null); const [buttonToChange, setButtonToChange] = useState<GbaKey | null>(null);
const setKey = (key: string) => { const setKey = (key: string) => {
@ -91,13 +92,19 @@ export const BindingsControl: FC<{
setButtonToChange(null); setButtonToChange(null);
setBindings(nextBindings); setBindings(nextBindings);
setPaused(false);
};
const onSelectButtonClick = (key: GbaKey) => {
setPaused(true);
setButtonToChange(key);
}; };
return ( return (
<ButtonWrapper onKeyDown={(evt) => setKey(evt.key)}> <ButtonWrapper onKeyUp={(evt) => setKey(evt.key)}>
{BindingsOrder.map((x) => ( {BindingsOrder.map((x) => (
<SelectButton <SelectButton
onClick={() => setButtonToChange(x)} onClick={() => onSelectButtonClick(x)}
key={x} key={x}
selected={buttonToChange === x} selected={buttonToChange === x}
> >

View file

@ -8,6 +8,7 @@ interface MgbaProps {
gameUrl: string; gameUrl: string;
volume?: Number; volume?: Number;
controls: KeyBindings; controls: KeyBindings;
paused: boolean;
} }
enum MgbaState { enum MgbaState {
@ -18,7 +19,7 @@ enum MgbaState {
const MGBA_ROM_DIRECTORY = "/data/games"; const MGBA_ROM_DIRECTORY = "/data/games";
export const Mgba: FC<MgbaProps> = ({ gameUrl, volume, controls }) => { export const Mgba: FC<MgbaProps> = ({ gameUrl, volume, controls, paused }) => {
const canvas = useRef(null); const canvas = useRef(null);
const mgbaModule = useRef<Module>({}); const mgbaModule = useRef<Module>({});
@ -92,6 +93,16 @@ export const Mgba: FC<MgbaProps> = ({ gameUrl, volume, controls }) => {
mgbaModule.current.setVolume(volume ?? 1.0); mgbaModule.current.setVolume(volume ?? 1.0);
}, [state, volume]); }, [state, volume]);
useEffect(() => {
if (state !== MgbaState.Initialised) return;
if (paused) {
mgbaModule.current.pauseGame();
} else {
mgbaModule.current.resumeGame();
}
}, [state, paused]);
return ( return (
<> <>
<canvas ref={canvas}></canvas> <canvas ref={canvas}></canvas>

View file

@ -1,11 +1,7 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "es5", "target": "es5",
"lib": [ "lib": ["dom", "dom.iterable", "esnext"],
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true, "allowJs": true,
"skipLibCheck": true, "skipLibCheck": true,
"esModuleInterop": true, "esModuleInterop": true,
@ -20,7 +16,5 @@
"noEmit": true, "noEmit": true,
"jsx": "react-jsx" "jsx": "react-jsx"
}, },
"include": [ "include": ["src"]
"src"
]
} }