Skip to content

Commit

Permalink
Make import error workaround more visible
Browse files Browse the repository at this point in the history
  • Loading branch information
neftaly committed May 2, 2023
1 parent e4418ce commit 2a8cb56
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@ import { invalidate } from '@react-three/fiber'
import { PerspectiveCamera } from '@react-three/drei'

export const SphericalCamera = ({
origin = [0, 0, 0], // Target position
coords: [
r, // Distance to origin
theta, // Polar (up-down) angle
phi // Azimuthal (left-right) angle
] = [0, 0, 0],
makeDefault = true, // Make this the default three camera
updateStream, // Stream of { origin, coords } updates
// TODO: Depreciate origin/coords in this type (hopefully we can recommend them in the future)
origin = [0, 0, 0], // Target position
coords = [0, 0, 0], // Camera rotation
...cameraProps
}) => {
const groupRef = useRef()
const cameraRef = useRef()
// TODO: Show a development-mode console depreciation warning if `origin`/`coords` is truthy
useEffect(
() =>
// Take a stream of {origin,coords} updates and imperatively move camera.
// Escape hatch for bypassing the React update system (i.e. for animations).
// Subscribe to a stream of {origin,coords} updates and imperatively move camera.
// Escape hatch for bypassing React diffing.
updateStream &&
updateStream(({ origin, coords }) => {
if (!groupRef.current || !cameraRef.current) return
Expand All @@ -31,8 +29,10 @@ export const SphericalCamera = ({
}
if (origin || coords) invalidate()
}),
[updateStream, invalidate]
[updateStream, invalidate] // TODO: Invalidate probably shouldn't be a dependency
)
const [r, theta, phi] = coords
// TODO: Use maths, not a <group> https://github.com/garbo-succus/control-kit/issues/6
return (
<group ref={groupRef} position={origin} rotation={[0, phi, theta]}>
<PerspectiveCamera
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
// Convert a PointerEvent "buttons" prop into an array of booleans
export const bitmaskToArray = (i: number) => [
!!(i & 1),
!!(i & 2),
!!(i & 4),
!!(i & 8),
!!(i & 16)
]

// Add event listeners to a list of events on a target; return a function to remove them
export const addEventListeners = (
target: undefined | HTMLElement,
Expand All @@ -22,6 +13,15 @@ export const addEventListeners = (
)
}

// Convert a PointerEvent "buttons" prop into an array of booleans
export const bitmaskToArray = (i: number) => [
!!(i & 1),
!!(i & 2),
!!(i & 4),
!!(i & 8),
!!(i & 16)
]

// PreventDefault on a list of events
export const addPreventDefaults = (
target: undefined | HTMLElement,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export { ControlRig } from './ControlRig'
// DOM API
export { bitmaskToArray, addEventListeners, addPreventDefaults } from './dom'
export { ControlRig } from './ControlRig'

export { SphericalCamera } from './SphericalCamera'
// Camera API
export { normalizeCoords, getScreenXY } from './camera'
export { SphericalCamera } from './SphericalCamera'
3 changes: 2 additions & 1 deletion examples/r3f-zustand/src/Controls/eventHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { normalizeCoords, bitmaskToArray, getScreenXY } from '../control-kit'
// TODO: Remove `TEMPORARY_control-kit` - https://github.com/garbo-succus/control-kit/issues/7
import { normalizeCoords, bitmaskToArray, getScreenXY } from './TEMPORARY_control-kit'
import { useCameraConfig, updateCamera } from './cameraState'

export const eventHandler = (event) => {
Expand Down
3 changes: 2 additions & 1 deletion examples/r3f-zustand/src/Controls/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect } from 'react'
import { useThree } from '@react-three/fiber'
import { ControlRig, SphericalCamera } from '../control-kit'
// TODO: Remove `TEMPORARY_control-kit` - https://github.com/garbo-succus/control-kit/issues/7
import { ControlRig, SphericalCamera } from './TEMPORARY_control-kit'
import { useCamera, updateCamera } from './cameraState'
import { eventHandler } from './eventHandler'

Expand Down
4 changes: 2 additions & 2 deletions src/SphericalCamera.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export const SphericalCamera = ({
makeDefault = true, // Make this the default three camera
updateStream, // Stream of { origin, coords } updates
// TODO: Depreciate origin/coords in this type (hopefully we can recommend them in the future)
origin, // Target position
coords, // Camera rotation
origin = [0, 0, 0], // Target position
coords = [0, 0, 0], // Camera rotation
...cameraProps
}) => {
const groupRef = useRef()
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// DOM API
export { bitmaskToArray, addEventListeners, addPreventDefaults } from './dom'
export { ControlRig } from './ControlRig'

export { normalizeCoords } from './camera'
// Camera API
export { normalizeCoords, getScreenXY } from './camera'
export { SphericalCamera } from './SphericalCamera'

0 comments on commit 2a8cb56

Please sign in to comment.