-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow joystick to control a non XY plane feat: Add hook to capture keypress chore: Create component for Joystick3d fix: Don’t use precision hotkey to set plane feat: Add children to Joystick feat: Add buttons to JoystickPlayground style: Styled joystick plane indicator feat: Add button key labels feat: Add cube rotation feat: Update cube fix: Don’t always show all joysticks ;) chore: Refactoring out JoystickPlayground3d chore: Add changeset fix: Joystick buttons should not be a button (remove nested button warn) chore: Simplify indexing chore: Rename for clarity chore: Remove comments style: Remove hardcoded size throughout
- Loading branch information
Showing
13 changed files
with
359 additions
and
43 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,5 @@ | ||
--- | ||
'leva': minor | ||
--- | ||
|
||
Add a joystick to Vector3d |
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
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,51 @@ | ||
import React from 'react' | ||
import { StyledJoyCubeFace, StyledJoyCube } from './StyledJoystick3d' | ||
|
||
export function JoyCube({ | ||
isTop, | ||
isRight, | ||
showFront = true, | ||
showMid = true, | ||
showRear = false, | ||
}: { | ||
isTop?: boolean | ||
isRight?: boolean | ||
showFront?: boolean | ||
showMid?: boolean | ||
showRear?: boolean | ||
}) { | ||
return ( | ||
<StyledJoyCube top={isTop} right={isRight}> | ||
{showFront && ( | ||
<> | ||
<StyledJoyCubeFace className="joycube-face--front" /> | ||
<StyledJoyCubeFace className="joycube-face--back" /> | ||
<StyledJoyCubeFace className="joycube-face--right" /> | ||
<StyledJoyCubeFace className="joycube-face--left" /> | ||
<StyledJoyCubeFace className="joycube-face--top" /> | ||
<StyledJoyCubeFace className="joycube-face--bottom" /> | ||
</> | ||
)} | ||
{showMid && ( | ||
<> | ||
<StyledJoyCubeFace className="joycube-face--front-mid" /> | ||
<StyledJoyCubeFace className="joycube-face--back-mid" /> | ||
<StyledJoyCubeFace className="joycube-face--right-mid" /> | ||
<StyledJoyCubeFace className="joycube-face--left-mid" /> | ||
<StyledJoyCubeFace className="joycube-face--top-mid" /> | ||
<StyledJoyCubeFace className="joycube-face--bottom-mid" /> | ||
</> | ||
)} | ||
{showRear && ( | ||
<> | ||
<StyledJoyCubeFace className="joycube-face--front-rear" /> | ||
<StyledJoyCubeFace className="joycube-face--back-rear" /> | ||
<StyledJoyCubeFace className="joycube-face--right-rear" /> | ||
<StyledJoyCubeFace className="joycube-face--left-rear" /> | ||
<StyledJoyCubeFace className="joycube-face--top-rear" /> | ||
<StyledJoyCubeFace className="joycube-face--bottom-rear" /> | ||
</> | ||
)} | ||
</StyledJoyCube> | ||
) | ||
} |
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,63 @@ | ||
import React from 'react' | ||
import { Joystick } from './Joystick' | ||
import { useKeyPress } from '../../hooks/useKeyPress' | ||
import { JoystickButtons, KeyLabel } from './StyledJoystick3d' | ||
import { Button } from '../Button' | ||
import type { InternalVector2dSettings } from '../Vector2d/vector2d-types' | ||
import type { Vector3d } from '../../types' | ||
import type { Vector3dProps } from '../Vector3d/vector3d-types' | ||
import { JoyCube } from './JoyCube' | ||
|
||
type Joystick3dProps = { value: Vector3d } & Pick<Vector3dProps, 'onUpdate' | 'settings'> | ||
|
||
const joystick3dKeyBindings = [ | ||
{ key: 'Control', keyLabel: 'ctrl', plane: 'xz', label: 'XZ' }, | ||
{ key: '', keyLabel: '', plane: 'xy', label: 'XY' }, | ||
{ key: 'Meta', keyLabel: 'meta', plane: 'zy', label: 'ZY' }, | ||
] | ||
|
||
export function Joystick3d({ value, settings, onUpdate }: Joystick3dProps) { | ||
const [plane, setPlane] = React.useState('xy') | ||
const keyPress0 = useKeyPress(joystick3dKeyBindings[0].key) | ||
// const keyPress1 = useKeyPress(joystick3dKeyBindings[1].key) | ||
const keyPress2 = useKeyPress(joystick3dKeyBindings[2].key) | ||
|
||
React.useEffect(() => { | ||
if (keyPress0) setPlane(joystick3dKeyBindings[0].plane) | ||
else if (keyPress2) setPlane(joystick3dKeyBindings[2].plane) | ||
else setPlane(joystick3dKeyBindings[1].plane) | ||
}, [keyPress0, keyPress2]) | ||
|
||
const settings2d = React.useMemo(() => { | ||
const { keys, ...rest } = settings | ||
return { keys: plane, ...rest } as unknown as InternalVector2dSettings | ||
}, [settings, plane]) | ||
|
||
return ( | ||
<> | ||
<Joystick value={value} settings={settings2d} onUpdate={onUpdate}> | ||
<JoyCube isTop={keyPress0} isRight={keyPress2} /> | ||
|
||
<JoystickButtons> | ||
{joystick3dKeyBindings.map((kb) => ( | ||
<Button | ||
key={kb.label} | ||
label={<ButtonLabel label={kb.label} keyLabel={kb.keyLabel || kb.key} />} | ||
onClick={() => ''} | ||
settings={{ disabled: plane !== kb.plane }} | ||
/> | ||
))} | ||
</JoystickButtons> | ||
</Joystick> | ||
</> | ||
) | ||
} | ||
|
||
function ButtonLabel({ label, keyLabel }: { label: string; keyLabel: string }) { | ||
return ( | ||
<div> | ||
<KeyLabel>{keyLabel}</KeyLabel> | ||
<span>{label}</span> | ||
</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
Oops, something went wrong.