agb/website/src/App.tsx

120 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-07-05 11:59:32 +01:00
import { useRef, useState } from "react";
import { Mgba, MgbaHandle } from "./mgba";
2023-07-05 11:23:59 +01:00
import { BindingsControl, DefaultBindingsSet, Bindings } from "./bindings";
import { styled } from "styled-components";
import { useOnKeyUp } from "./useOnKeyUp.hook";
2023-07-05 11:39:14 +01:00
import { useLocalStorage } from "./useLocalStorage.hook";
2023-07-05 11:23:59 +01:00
const BindingsDialog = styled.dialog`
border-radius: 5px;
margin-top: 20px;
`;
const VolumeLabel = styled.label`
display: flex;
gap: 10px;
margin-bottom: 20px;
`;
2023-07-05 11:59:32 +01:00
const ActionButton = styled.button`
2023-07-05 11:23:59 +01:00
width: 100%;
margin-top: 20px;
`;
2023-04-13 20:12:45 +01:00
2023-07-05 12:04:27 +01:00
const AppContainer = styled.main`
height: calc(100vh - 20px);
padding: 10px;
`;
2023-04-13 20:12:45 +01:00
function App() {
2023-07-05 11:59:32 +01:00
const [{ volume, bindings }, setState] = useLocalStorage(
{ volume: 1.0, bindings: DefaultBindingsSet() },
2023-07-05 11:39:14 +01:00
"agbrswebplayer"
);
2023-07-05 11:59:32 +01:00
const setVolume = (newVolume: number) =>
setState({ volume: newVolume, bindings });
const setBindings = (newBindings: Bindings) =>
setState({ volume, bindings: newBindings });
const [paused, setPaused] = useState(false);
2023-04-13 21:32:40 +01:00
2023-07-05 11:23:59 +01:00
const [showBindings, setShowBindings] = useState(false);
2023-07-05 11:59:32 +01:00
const mgbaRef = useRef<MgbaHandle>(null);
2023-07-05 11:23:59 +01:00
useOnKeyUp("Escape", () => {
setShowBindings(!showBindings);
});
2023-04-13 20:12:45 +01:00
return (
2023-07-05 12:04:27 +01:00
<AppContainer>
2023-07-05 11:23:59 +01:00
{showBindings && (
<BindingsWindow
bindings={bindings}
setBindings={setBindings}
setPaused={setPaused}
volume={volume}
setVolume={setVolume}
hide={() => setShowBindings(false)}
2023-07-05 11:59:32 +01:00
restart={() => mgbaRef.current?.restart()}
2023-07-05 11:23:59 +01:00
/>
)}
2023-07-05 10:07:10 +01:00
<Mgba
2023-07-05 11:59:32 +01:00
ref={mgbaRef}
2023-07-05 10:07:10 +01:00
gameUrl="/game.gba"
volume={volume}
controls={bindings.Actual}
paused={paused}
/>
2023-07-05 12:04:27 +01:00
</AppContainer>
2023-07-05 11:23:59 +01:00
);
}
2023-07-05 10:07:10 +01:00
2023-07-05 11:23:59 +01:00
function BindingsWindow({
bindings,
setBindings,
setPaused,
volume,
setVolume,
hide,
2023-07-05 11:59:32 +01:00
restart,
2023-07-05 11:23:59 +01:00
}: {
bindings: Bindings;
setBindings: (b: Bindings) => void;
setPaused: (paused: boolean) => void;
volume: number;
setVolume: (v: number) => void;
hide: () => void;
2023-07-05 11:59:32 +01:00
restart: () => void;
2023-07-05 11:23:59 +01:00
}) {
return (
<BindingsDialog open onClose={hide}>
<VolumeLabel>
Volume:
<input
type="range"
value={volume}
min="0"
max="1"
step="0.05"
onChange={(e) => {
console.log("e.target.value", e.target.value);
console.log("volume", volume);
setVolume(Number(e.target.value));
}}
/>
</VolumeLabel>
<BindingsControl
bindings={bindings}
setBindings={setBindings}
setPaused={setPaused}
/>
2023-07-05 11:59:32 +01:00
<ActionButton onClick={restart}>Restart</ActionButton>
<ActionButton onClick={hide}>Close</ActionButton>
2023-07-05 11:23:59 +01:00
</BindingsDialog>
2023-04-13 20:12:45 +01:00
);
}
export default App;