-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement basic touch controls with input visualization
- Loading branch information
Showing
9 changed files
with
276 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Placeholder } from "./Placeholder"; | ||
import { TouchButton } from "./TouchButton"; | ||
import { GameBoyButton } from "./useControls"; | ||
|
||
export const ABStartSelect = () => { | ||
return ( | ||
<div className="ml-4 flex flex-col"> | ||
<div className="flex flex-row"> | ||
<Placeholder /> | ||
<TouchButton button={GameBoyButton.SELECT}>SELECT</TouchButton> | ||
<Placeholder /> | ||
</div> | ||
<div className="flex flex-row"> | ||
<TouchButton button={GameBoyButton.START}>START</TouchButton> | ||
<Placeholder /> | ||
<TouchButton button={GameBoyButton.A}>A</TouchButton> | ||
</div> | ||
<div className="flex flex-row"> | ||
<Placeholder /> | ||
<TouchButton button={GameBoyButton.B}>B</TouchButton> | ||
<Placeholder /> | ||
</div> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { | ||
FaArrowDown, | ||
FaArrowLeft, | ||
FaArrowRight, | ||
FaArrowUp, | ||
} from "react-icons/fa"; | ||
import { TouchButton } from "./TouchButton"; | ||
import { GameBoyButton } from "./useControls"; | ||
import { Placeholder } from "./Placeholder"; | ||
|
||
export const DPad = () => { | ||
return ( | ||
<div className="mr-4 flex flex-col"> | ||
<div className="flex flex-row"> | ||
<Placeholder /> | ||
<TouchButton button={GameBoyButton.UP}> | ||
<FaArrowUp /> | ||
</TouchButton> | ||
<Placeholder /> | ||
</div> | ||
<div className="flex flex-row"> | ||
<TouchButton button={GameBoyButton.LEFT}> | ||
<FaArrowLeft /> | ||
</TouchButton> | ||
<Placeholder /> | ||
<TouchButton button={GameBoyButton.RIGHT}> | ||
<FaArrowRight /> | ||
</TouchButton> | ||
</div> | ||
<div className="flex flex-row"> | ||
<Placeholder /> | ||
<TouchButton button={GameBoyButton.DOWN}> | ||
<FaArrowDown /> | ||
</TouchButton> | ||
<Placeholder /> | ||
</div> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export const Placeholder = () => { | ||
return ( | ||
<div | ||
className="h-16 w-16" | ||
onGotPointerCapture={(event) => { | ||
const targetElement = event.target as HTMLDivElement; | ||
targetElement.releasePointerCapture(event.pointerId); | ||
}} | ||
></div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { type GameBoyButton } from "./useControls"; | ||
import { useDisplayedButtonsStore } from "./useDisplayedButtonsStore"; | ||
import { useTouchButtonsStore } from "./useTouchButtonsStore"; | ||
|
||
interface Props { | ||
button: GameBoyButton; | ||
children: React.ReactNode; | ||
} | ||
|
||
export const TouchButton: React.FC<Props> = ({ button, children }) => { | ||
const { addTouchButton, removeTouchButton } = useTouchButtonsStore( | ||
(state) => ({ | ||
addTouchButton: state.addTouchButton, | ||
removeTouchButton: state.removeTouchButton, | ||
}), | ||
); | ||
const { displayedButtons, addDisplayedButton, removeDisplayedButton } = | ||
useDisplayedButtonsStore((state) => ({ | ||
displayedButtons: state.displayedButtons, | ||
addDisplayedButton: state.addDisplayedButton, | ||
removeDisplayedButton: state.removeDisplayedButton, | ||
})); | ||
|
||
return ( | ||
<button | ||
className="pointer-events-auto flex h-16 w-16 touch-none select-none items-center justify-center" | ||
style={{ | ||
backgroundColor: (displayedButtons & button) > 0 ? "blue" : "white", | ||
}} | ||
onPointerEnter={() => { | ||
addTouchButton(button); | ||
addDisplayedButton(button); | ||
}} | ||
onGotPointerCapture={(event) => { | ||
const targetElement = event.target as HTMLButtonElement; | ||
targetElement.releasePointerCapture(event.pointerId); | ||
}} | ||
onPointerLeave={() => { | ||
removeTouchButton(button); | ||
removeDisplayedButton(button); | ||
}} | ||
> | ||
{children} | ||
</button> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { create } from "zustand"; | ||
import { type GameBoyButton } from "./useControls"; | ||
|
||
interface DisplayedButtonsState { | ||
displayedButtons: number; | ||
addDisplayedButton: (button: GameBoyButton) => void; | ||
removeDisplayedButton: (button: GameBoyButton) => void; | ||
} | ||
|
||
export const useDisplayedButtonsStore = create<DisplayedButtonsState>( | ||
(set, get) => ({ | ||
displayedButtons: 0, | ||
addDisplayedButton: (button) => { | ||
set({ displayedButtons: get().displayedButtons | button }); | ||
}, | ||
removeDisplayedButton: (button) => { | ||
set({ displayedButtons: get().displayedButtons & ~button }); | ||
}, | ||
}), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { create } from "zustand"; | ||
import { type GameBoyButton } from "./useControls"; | ||
|
||
interface TouchButtonsState { | ||
touchButtons: number; | ||
addTouchButton: (button: GameBoyButton) => void; | ||
removeTouchButton: (button: GameBoyButton) => void; | ||
} | ||
|
||
export const useTouchButtonsStore = create<TouchButtonsState>((set, get) => ({ | ||
touchButtons: 0, | ||
addTouchButton: (button) => { | ||
set({ touchButtons: get().touchButtons | button }); | ||
}, | ||
removeTouchButton: (button) => { | ||
set({ touchButtons: get().touchButtons & ~button }); | ||
}, | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters