|
1 | 1 | import React, { useState, useRef, useCallback, useEffect, useLayoutEffect } from 'react'
|
2 | 2 | import { useDrag } from '../../hooks'
|
3 | 3 | import { clamp, multiplyStep } from '../../utils'
|
4 |
| -import { JoystickTrigger, JoystickPlayground } from './StyledJoystick' |
| 4 | +import { JoystickTrigger, JoystickPlayground, JoystickGrid } from './StyledJoystick' |
5 | 5 | import { useTh } from '../../styles'
|
6 |
| -import { Portal } from '../UI' |
| 6 | +import { Portal } from '.' |
7 | 7 | import { useTransform } from '../../hooks'
|
8 |
| -import type { Vector2d } from '../../types' |
9 |
| -import type { Vector2dProps } from './vector2d-types' |
| 8 | +import type { Vector2d, Vector3d } from '../../types' |
| 9 | +import type { Vector2dProps } from '../Vector2d/vector2d-types' |
10 | 10 |
|
11 |
| -type JoystickProps = { value: Vector2d } & Pick<Vector2dProps, 'onUpdate' | 'settings'> |
| 11 | +type JoystickProps = { value: Vector2d | Vector3d } & Pick<Vector2dProps, 'onUpdate' | 'settings'> & { children?: any } |
12 | 12 |
|
13 |
| -export function Joystick({ value, settings, onUpdate }: JoystickProps) { |
| 13 | +export function Joystick({ value, settings, onUpdate, children }: JoystickProps) { |
14 | 14 | const timeout = useRef<number | undefined>()
|
15 | 15 | const outOfBoundsX = useRef(0)
|
16 | 16 | const outOfBoundsY = useRef(0)
|
@@ -52,13 +52,14 @@ export function Joystick({ value, settings, onUpdate }: JoystickProps) {
|
52 | 52 | if (outOfBoundsX.current) set({ x: outOfBoundsX.current * w })
|
53 | 53 | if (outOfBoundsY.current) set({ y: outOfBoundsY.current * -h })
|
54 | 54 | timeout.current = window.setInterval(() => {
|
55 |
| - onUpdate((v: Vector2d) => { |
| 55 | + onUpdate((v: Vector2d | Vector3d) => { |
56 | 56 | const incX = stepV1 * outOfBoundsX.current * stepMultiplier.current
|
57 | 57 | const incY = yFactor * stepV2 * outOfBoundsY.current * stepMultiplier.current
|
| 58 | + |
58 | 59 | return Array.isArray(v)
|
59 | 60 | ? {
|
60 |
| - [v1]: v[0] + incX, |
61 |
| - [v2]: v[1] + incY, |
| 61 | + [v1]: v[['x', 'y', 'z'].indexOf(v1)] + incX, |
| 62 | + [v2]: v[['x', 'y', 'z'].indexOf(v2)] + incY, |
62 | 63 | }
|
63 | 64 | : {
|
64 | 65 | [v1]: v[v1] + incX,
|
@@ -128,7 +129,7 @@ export function Joystick({ value, settings, onUpdate }: JoystickProps) {
|
128 | 129 | {joystickShown && (
|
129 | 130 | <Portal>
|
130 | 131 | <JoystickPlayground ref={playgroundRef} isOutOfBounds={isOutOfBounds}>
|
131 |
| - <div /> |
| 132 | + {children ? children : <JoystickGrid />} |
132 | 133 | <span ref={spanRef} />
|
133 | 134 | </JoystickPlayground>
|
134 | 135 | </Portal>
|
|
0 commit comments