2021-06-18 15:19:10 +00:00
|
|
|
import { useRef } from 'react'
|
2021-05-28 16:25:43 +00:00
|
|
|
import state from 'state'
|
|
|
|
import inputs from 'state/inputs'
|
2021-06-16 12:09:45 +00:00
|
|
|
import vec from 'utils/vec'
|
2021-05-30 13:14:35 +00:00
|
|
|
import { useGesture } from 'react-use-gesture'
|
2021-06-18 15:19:10 +00:00
|
|
|
import { fastPanUpdate, fastPinchCamera, fastZoomUpdate } from 'state/hacks'
|
2021-05-09 13:04:42 +00:00
|
|
|
|
2021-05-09 21:22:25 +00:00
|
|
|
/**
|
|
|
|
* Capture zoom gestures (pinches, wheels and pans) and send to the state.
|
|
|
|
* @param ref
|
|
|
|
* @returns
|
|
|
|
*/
|
2021-05-30 21:24:31 +00:00
|
|
|
export default function useZoomEvents() {
|
2021-05-28 13:08:51 +00:00
|
|
|
const rPinchDa = useRef<number[] | undefined>(undefined)
|
|
|
|
const rPinchPoint = useRef<number[] | undefined>(undefined)
|
|
|
|
|
2021-05-30 21:24:31 +00:00
|
|
|
useGesture(
|
2021-05-30 13:14:35 +00:00
|
|
|
{
|
|
|
|
onWheel: ({ event, delta }) => {
|
|
|
|
if (event.ctrlKey) {
|
2021-06-06 10:50:01 +00:00
|
|
|
const { point } = inputs.wheel(event as WheelEvent)
|
|
|
|
fastZoomUpdate(point, delta[1])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-13 13:24:03 +00:00
|
|
|
if (state.isInAny('pointing', 'drawing')) {
|
2021-06-06 10:50:01 +00:00
|
|
|
fastPanUpdate(delta)
|
2021-05-30 13:14:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
state.send('PANNED_CAMERA', {
|
|
|
|
delta,
|
|
|
|
...inputs.wheel(event as WheelEvent),
|
|
|
|
})
|
|
|
|
},
|
|
|
|
onPinch: ({ pinching, da, origin }) => {
|
|
|
|
if (!pinching) {
|
|
|
|
state.send('STOPPED_PINCHING')
|
|
|
|
rPinchDa.current = undefined
|
|
|
|
rPinchPoint.current = undefined
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rPinchPoint.current === undefined) {
|
|
|
|
state.send('STARTED_PINCHING')
|
|
|
|
rPinchDa.current = da
|
|
|
|
rPinchPoint.current = origin
|
|
|
|
}
|
|
|
|
|
|
|
|
const [distanceDelta, angleDelta] = vec.sub(rPinchDa.current, da)
|
|
|
|
|
2021-06-06 10:50:01 +00:00
|
|
|
fastPinchCamera(
|
|
|
|
origin,
|
|
|
|
vec.sub(rPinchPoint.current, origin),
|
2021-05-30 13:14:35 +00:00
|
|
|
distanceDelta,
|
2021-06-06 10:50:01 +00:00
|
|
|
angleDelta
|
|
|
|
)
|
|
|
|
|
2021-05-30 13:14:35 +00:00
|
|
|
rPinchDa.current = da
|
|
|
|
rPinchPoint.current = origin
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
domTarget: document.body,
|
|
|
|
eventOptions: { passive: false },
|
2021-05-28 13:08:51 +00:00
|
|
|
}
|
2021-05-30 13:14:35 +00:00
|
|
|
)
|
2021-05-09 13:04:42 +00:00
|
|
|
}
|