2021-06-21 21:35:28 +00:00
|
|
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
2021-06-06 10:50:01 +00:00
|
|
|
import { MutableRefObject, useCallback } from 'react'
|
|
|
|
import state from 'state'
|
2021-06-16 12:09:45 +00:00
|
|
|
import {
|
|
|
|
fastBrushSelect,
|
|
|
|
fastDrawUpdate,
|
|
|
|
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>
|
|
|
|
) {
|
|
|
|
const handlePointerDown = useCallback((e: React.PointerEvent) => {
|
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
2021-06-10 09:49:16 +00:00
|
|
|
|
2021-06-06 10:50:01 +00:00
|
|
|
rCanvas.current.setPointerCapture(e.pointerId)
|
2021-06-10 09:49:16 +00:00
|
|
|
|
2021-07-02 10:48:49 +00:00
|
|
|
const info = inputs.pointerDown(e, 'canvas')
|
|
|
|
|
2021-06-10 09:49:16 +00:00
|
|
|
if (e.button === 0) {
|
2021-07-02 10:48:49 +00:00
|
|
|
if (inputs.isDoubleClick() && !(info.altKey || info.metaKey)) {
|
|
|
|
state.send('DOUBLE_POINTED_CANVAS', info)
|
|
|
|
}
|
|
|
|
|
|
|
|
state.send('POINTED_CANVAS', info)
|
2021-06-10 09:49:16 +00:00
|
|
|
} else if (e.button === 2) {
|
2021-07-02 10:48:49 +00:00
|
|
|
state.send('RIGHT_POINTED', info)
|
2021-06-10 09:49:16 +00:00
|
|
|
}
|
2021-06-06 10:50:01 +00:00
|
|
|
}, [])
|
|
|
|
|
|
|
|
const handlePointerMove = useCallback((e: React.PointerEvent) => {
|
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
|
|
|
|
2021-06-30 20:56:42 +00:00
|
|
|
const prev = inputs.pointer?.point
|
2021-06-13 14:12:59 +00:00
|
|
|
const info = inputs.pointerMove(e)
|
|
|
|
|
2021-06-30 20:56:42 +00:00
|
|
|
if (prev && state.isIn('selecting') && inputs.keys[' ']) {
|
|
|
|
state.send('KEYBOARD_PANNED_CAMERA', { delta: Vec.sub(prev, info.point) })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-06 10:50:01 +00:00
|
|
|
if (state.isIn('draw.editing')) {
|
2021-06-13 14:12:59 +00:00
|
|
|
fastDrawUpdate(info)
|
2021-06-30 20:31:29 +00:00
|
|
|
} else if (state.isIn('brushSelecting')) {
|
2021-06-06 10:50:01 +00:00
|
|
|
fastBrushSelect(info.point)
|
2021-06-30 20:31:29 +00:00
|
|
|
} else if (state.isIn('translatingSelection')) {
|
2021-06-13 14:12:59 +00:00
|
|
|
fastTranslate(info)
|
2021-06-30 20:31:29 +00:00
|
|
|
} else if (state.isIn('transformingSelection')) {
|
2021-06-16 12:09:45 +00:00
|
|
|
fastTransform(info)
|
|
|
|
}
|
|
|
|
|
2021-06-13 14:12:59 +00:00
|
|
|
state.send('MOVED_POINTER', info)
|
2021-06-06 10:50:01 +00:00
|
|
|
}, [])
|
|
|
|
|
|
|
|
const handlePointerUp = useCallback((e: React.PointerEvent) => {
|
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
2021-06-23 14:39:14 +00:00
|
|
|
e.stopPropagation()
|
|
|
|
|
2021-06-06 10:50:01 +00:00
|
|
|
rCanvas.current.releasePointerCapture(e.pointerId)
|
2021-06-23 14:39:14 +00:00
|
|
|
|
|
|
|
state.send('STOPPED_POINTING', {
|
|
|
|
id: 'canvas',
|
|
|
|
...inputs.pointerUp(e, 'canvas'),
|
|
|
|
})
|
2021-06-06 10:50:01 +00:00
|
|
|
}, [])
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
const handleTouchStart = useCallback(() => {
|
2021-06-18 14:39:07 +00:00
|
|
|
// if (isMobile()) {
|
|
|
|
// if (e.touches.length === 2) {
|
|
|
|
// state.send('TOUCH_UNDO')
|
|
|
|
// } else state.send('TOUCHED_CANVAS')
|
|
|
|
// }
|
|
|
|
}, [])
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|