2021-05-30 13:14:35 +00:00
|
|
|
import { useCallback, useRef } from 'react'
|
|
|
|
import inputs from 'state/inputs'
|
|
|
|
import { Edge, Corner } from 'types'
|
2021-05-22 15:45:24 +00:00
|
|
|
|
2021-05-30 13:14:35 +00:00
|
|
|
import state from '../state'
|
2021-05-22 15:45:24 +00:00
|
|
|
|
|
|
|
export default function useBoundsHandleEvents(
|
2021-05-30 13:14:35 +00:00
|
|
|
handle: Edge | Corner | 'rotate'
|
2021-05-22 15:45:24 +00:00
|
|
|
) {
|
|
|
|
const onPointerDown = useCallback(
|
|
|
|
(e) => {
|
|
|
|
if (e.buttons !== 1) return
|
2021-05-30 13:14:35 +00:00
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
2021-05-22 15:45:24 +00:00
|
|
|
e.stopPropagation()
|
|
|
|
e.currentTarget.setPointerCapture(e.pointerId)
|
2021-05-30 13:14:35 +00:00
|
|
|
state.send('POINTED_BOUNDS_HANDLE', inputs.pointerDown(e, handle))
|
2021-05-22 15:45:24 +00:00
|
|
|
},
|
|
|
|
[handle]
|
|
|
|
)
|
|
|
|
|
|
|
|
const onPointerMove = useCallback(
|
|
|
|
(e) => {
|
|
|
|
if (e.buttons !== 1) return
|
2021-05-30 13:14:35 +00:00
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
2021-05-22 15:45:24 +00:00
|
|
|
e.stopPropagation()
|
2021-05-30 13:14:35 +00:00
|
|
|
state.send('MOVED_POINTER', inputs.pointerMove(e))
|
2021-05-22 15:45:24 +00:00
|
|
|
},
|
|
|
|
[handle]
|
|
|
|
)
|
|
|
|
|
|
|
|
const onPointerUp = useCallback((e) => {
|
|
|
|
if (e.buttons !== 1) return
|
2021-05-30 13:14:35 +00:00
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
2021-05-22 15:45:24 +00:00
|
|
|
e.stopPropagation()
|
|
|
|
e.currentTarget.releasePointerCapture(e.pointerId)
|
|
|
|
e.currentTarget.replaceWith(e.currentTarget)
|
2021-05-30 13:14:35 +00:00
|
|
|
state.send('STOPPED_POINTING', inputs.pointerUp(e))
|
2021-05-22 15:45:24 +00:00
|
|
|
}, [])
|
|
|
|
|
|
|
|
return { onPointerDown, onPointerMove, onPointerUp }
|
|
|
|
}
|