Force interaction to start

This commit is contained in:
Gwilym Inzani 2023-07-05 12:37:55 +01:00 committed by Corwin
parent f944f35b61
commit 3323d6a4e7
No known key found for this signature in database
2 changed files with 42 additions and 13 deletions

View file

@ -25,6 +25,23 @@ const ActionButton = styled.button`
const AppContainer = styled.main` const AppContainer = styled.main`
height: calc(100vh - 20px); height: calc(100vh - 20px);
padding: 10px; padding: 10px;
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;
}
`; `;
function App() { function App() {
@ -50,6 +67,8 @@ function App() {
useAvoidItchIoScrolling(); useAvoidItchIoScrolling();
const [isPlaying, setIsPlaying] = useState(false);
return ( return (
<AppContainer> <AppContainer>
{showBindings && ( {showBindings && (
@ -63,6 +82,7 @@ function App() {
restart={() => mgbaRef.current?.restart()} restart={() => mgbaRef.current?.restart()}
/> />
)} )}
{isPlaying ? (
<Mgba <Mgba
ref={mgbaRef} ref={mgbaRef}
gameUrl="/game.gba" gameUrl="/game.gba"
@ -70,6 +90,9 @@ function App() {
controls={bindings.Actual} controls={bindings.Actual}
paused={paused} paused={paused}
/> />
) : (
<StartButton onClick={() => setIsPlaying(true)} />
)}
</AppContainer> </AppContainer>
); );
} }
@ -101,11 +124,7 @@ function BindingsWindow({
min="0" min="0"
max="1" max="1"
step="0.05" step="0.05"
onChange={(e) => { onChange={(e) => setVolume(Number(e.target.value))}
console.log("e.target.value", e.target.value);
console.log("volume", volume);
setVolume(Number(e.target.value));
}}
/> />
</VolumeLabel> </VolumeLabel>
<BindingsControl <BindingsControl
@ -119,4 +138,10 @@ function BindingsWindow({
); );
} }
function StartButton({ onClick }: { onClick: () => void }) {
return (
<StartButtonWrapper onClick={onClick}>Press to start</StartButtonWrapper>
);
}
export default App; export default App;

View file

@ -56,6 +56,8 @@ export const Mgba = forwardRef<MgbaHandle, MgbaProps>(
const gamePath = `${MGBA_ROM_DIRECTORY}/${gameUrl}`; const gamePath = `${MGBA_ROM_DIRECTORY}/${gameUrl}`;
mgbaModule.current.FS.writeFile(gamePath, new Uint8Array(gameData)); mgbaModule.current.FS.writeFile(gamePath, new Uint8Array(gameData));
mgbaModule.current.loadGame(gamePath); mgbaModule.current.loadGame(gamePath);
mgbaModule.current.setVolume(0.1); // for some reason you have to do this or you get no sound
console.log(mgbaModule.current.getVolume());
setGameLoaded(true); setGameLoaded(true);
})(); })();
}, [state, gameUrl]); }, [state, gameUrl]);
@ -63,7 +65,7 @@ export const Mgba = forwardRef<MgbaHandle, MgbaProps>(
// init mgba // init mgba
useEffect(() => { useEffect(() => {
(async () => { (async () => {
if (canvas === null) return; if (canvas.current === null) return;
if (state !== MgbaState.Uninitialised) return; if (state !== MgbaState.Uninitialised) return;
setState(MgbaState.Initialising); setState(MgbaState.Initialising);
@ -111,7 +113,9 @@ export const Mgba = forwardRef<MgbaHandle, MgbaProps>(
useEffect(() => { useEffect(() => {
if (!gameLoaded) return; if (!gameLoaded) return;
console.log("before", mgbaModule.current.getVolume());
mgbaModule.current.setVolume(volume ?? 1.0); mgbaModule.current.setVolume(volume ?? 1.0);
console.log("after", mgbaModule.current.getVolume());
}, [gameLoaded, volume]); }, [gameLoaded, volume]);
useEffect(() => { useEffect(() => {