2021-06-21 21:35:28 +00:00
|
|
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
2021-07-12 11:27:30 +00:00
|
|
|
import { MutableRefObject, useCallback, useEffect } from 'react'
|
2021-06-06 10:50:01 +00:00
|
|
|
import state from 'state'
|
2021-06-16 12:09:45 +00:00
|
|
|
import {
|
|
|
|
fastBrushSelect,
|
|
|
|
fastDrawUpdate,
|
2021-07-11 07:03:20 +00:00
|
|
|
fastPanUpdate,
|
2021-06-16 12:09:45 +00:00
|
|
|
fastTransform,
|
|
|
|
fastTranslate,
|
|
|
|
} from 'state/hacks'
|
2021-06-06 10:50:01 +00:00
|
|
|
import inputs from 'state/inputs'
|
2021-06-30 20:56:42 +00:00
|
|
|
import Vec from 'utils/vec'
|
2021-06-06 10:50:01 +00:00
|
|
|
|
|
|
|
export default function useCanvasEvents(
|
|
|
|
rCanvas: MutableRefObject<SVGGElement>
|
|
|
|
) {
|
2021-07-11 12:51:01 +00:00
|
|
|
const handlePointerDown = useCallback(
|
|
|
|
(e: React.PointerEvent<SVGSVGElement>) => {
|
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
2021-07-02 10:48:49 +00:00
|
|
|
|
2021-07-11 12:51:01 +00:00
|
|
|
rCanvas.current.setPointerCapture(e.pointerId)
|
2021-06-06 10:50:01 +00:00
|
|
|
|
2021-07-11 12:51:01 +00:00
|
|
|
const info = inputs.pointerDown(e, 'canvas')
|
2021-06-06 10:50:01 +00:00
|
|
|
|
2021-07-11 12:51:01 +00:00
|
|
|
if (e.button === 0) {
|
|
|
|
if (inputs.isDoubleClick() && !(info.altKey || info.metaKey)) {
|
|
|
|
state.send('DOUBLE_POINTED_CANVAS', info)
|
|
|
|
}
|
2021-06-30 20:56:42 +00:00
|
|
|
|
2021-07-11 12:51:01 +00:00
|
|
|
state.send('POINTED_CANVAS', info)
|
|
|
|
} else if (e.button === 2) {
|
|
|
|
state.send('RIGHT_POINTED', info)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
|
|
|
|
const handlePointerMove = useCallback(
|
|
|
|
(e: React.PointerEvent<SVGSVGElement>) => {
|
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
|
|
|
|
|
|
|
const prev = inputs.pointer?.point
|
|
|
|
const info = inputs.pointerMove(e)
|
|
|
|
|
|
|
|
if (prev && state.isIn('selecting') && inputs.keys[' ']) {
|
|
|
|
const delta = Vec.sub(prev, info.point)
|
|
|
|
fastPanUpdate(delta)
|
|
|
|
state.send('KEYBOARD_PANNED_CAMERA', {
|
|
|
|
delta: Vec.sub(prev, info.point),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2021-06-06 10:50:01 +00:00
|
|
|
|
2021-07-11 12:51:01 +00:00
|
|
|
if (state.isIn('draw.editing')) {
|
|
|
|
fastDrawUpdate(info)
|
|
|
|
} else if (state.isIn('brushSelecting')) {
|
|
|
|
fastBrushSelect(info.point)
|
|
|
|
} else if (state.isIn('translatingSelection')) {
|
|
|
|
fastTranslate(info)
|
|
|
|
} else if (state.isIn('transformingSelection')) {
|
|
|
|
fastTransform(info)
|
|
|
|
}
|
2021-06-23 14:39:14 +00:00
|
|
|
|
2021-07-11 12:51:01 +00:00
|
|
|
state.send('MOVED_POINTER', info)
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
)
|
2021-06-23 14:39:14 +00:00
|
|
|
|
2021-07-11 12:51:01 +00:00
|
|
|
const handlePointerUp = useCallback(
|
|
|
|
(e: React.PointerEvent<SVGSVGElement>) => {
|
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
2021-06-06 10:50:01 +00:00
|
|
|
|
2021-07-11 12:51:01 +00:00
|
|
|
rCanvas.current.releasePointerCapture(e.pointerId)
|
2021-06-18 14:39:07 +00:00
|
|
|
|
2021-07-11 12:51:01 +00:00
|
|
|
state.send('STOPPED_POINTING', {
|
|
|
|
id: 'canvas',
|
|
|
|
...inputs.pointerUp(e, 'canvas'),
|
|
|
|
})
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
)
|
2021-07-09 20:39:29 +00:00
|
|
|
|
2021-07-11 12:51:01 +00:00
|
|
|
const handleTouchStart = useCallback((e: React.TouchEvent<SVGSVGElement>) => {
|
|
|
|
if ('safari' in window) {
|
|
|
|
e.preventDefault()
|
2021-07-09 20:39:29 +00:00
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
2021-07-12 11:27:30 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const preventGestureNavigation = (event: TouchEvent) => {
|
|
|
|
event.preventDefault()
|
|
|
|
}
|
|
|
|
|
|
|
|
const preventNavigation = (event: TouchEvent) => {
|
|
|
|
// Center point of the touch area
|
|
|
|
const touchXPosition = event.touches[0].pageX
|
|
|
|
// Size of the touch area
|
|
|
|
const touchXRadius = event.touches[0].radiusX || 0
|
|
|
|
|
|
|
|
// We set a threshold (10px) on both sizes of the screen,
|
|
|
|
// if the touch area overlaps with the screen edges
|
|
|
|
// it's likely to trigger the navigation. We prevent the
|
|
|
|
// touchstart event in that case.
|
|
|
|
if (
|
|
|
|
touchXPosition - touchXRadius < 10 ||
|
|
|
|
touchXPosition + touchXRadius > window.innerWidth - 10
|
|
|
|
) {
|
|
|
|
event.preventDefault()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rCanvas.current.addEventListener('gestureend', preventGestureNavigation)
|
|
|
|
rCanvas.current.addEventListener('gesturechange', preventGestureNavigation)
|
|
|
|
rCanvas.current.addEventListener('gesturestart', preventGestureNavigation)
|
|
|
|
rCanvas.current.addEventListener('touchstart', preventNavigation)
|
|
|
|
|
|
|
|
return () => {
|
2021-07-14 08:04:07 +00:00
|
|
|
if (rCanvas.current) {
|
|
|
|
rCanvas.current.removeEventListener(
|
|
|
|
'gestureend',
|
|
|
|
preventGestureNavigation
|
|
|
|
)
|
|
|
|
rCanvas.current.removeEventListener(
|
|
|
|
'gesturechange',
|
|
|
|
preventGestureNavigation
|
|
|
|
)
|
|
|
|
rCanvas.current.removeEventListener(
|
|
|
|
'gesturestart',
|
|
|
|
preventGestureNavigation
|
|
|
|
)
|
|
|
|
rCanvas.current.removeEventListener('touchstart', preventNavigation)
|
|
|
|
}
|
2021-07-12 11:27:30 +00:00
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
2021-06-06 10:50:01 +00:00
|
|
|
return {
|
|
|
|
onPointerDown: handlePointerDown,
|
|
|
|
onPointerMove: handlePointerMove,
|
|
|
|
onPointerUp: handlePointerUp,
|
2021-06-18 14:39:07 +00:00
|
|
|
onTouchStart: handleTouchStart,
|
2021-06-06 10:50:01 +00:00
|
|
|
}
|
|
|
|
}
|