agb/website/src/App.tsx

148 lines
3.3 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 12:07:47 +01:00
import { useAvoidItchIoScrolling } from "./useAvoidItchIoScrolling";
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-07-05 12:37:55 +01:00
display: flex;
`;
const StartButtonWrapper = styled.button`
margin: auto;
font-size: 5em;
padding: 1em;
text-transform: uppercase;
background-color: black;
color: white;
border: none;
border-radius: 0.5em;
&:hover {
background-color: #222;
cursor: pointer;
}
2023-07-05 12:04:27 +01:00
`;
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-07-05 12:07:47 +01:00
useAvoidItchIoScrolling();
2023-07-05 12:37:55 +01:00
const [isPlaying, setIsPlaying] = useState(false);
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 12:37:55 +01:00
{isPlaying ? (
<Mgba
ref={mgbaRef}
gameUrl="/game.gba"
volume={volume}
controls={bindings.Actual}
paused={paused}
/>
) : (
<StartButton onClick={() => setIsPlaying(true)} />
)}
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"
2023-07-05 12:37:55 +01:00
onChange={(e) => setVolume(Number(e.target.value))}
2023-07-05 11:23:59 +01:00
/>
</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
);
}
2023-07-05 12:37:55 +01:00
function StartButton({ onClick }: { onClick: () => void }) {
return (
<StartButtonWrapper onClick={onClick}>Press to start</StartButtonWrapper>
);
}
2023-04-13 20:12:45 +01:00
export default App;