1 | import { CoordinatesEngine } from './CoordinatesEngine'
|
---|
2 | import { pointerValues } from '../utils/events'
|
---|
3 | import { V } from '../utils/maths'
|
---|
4 |
|
---|
5 | export class MoveEngine extends CoordinatesEngine<'move'> {
|
---|
6 | ingKey = 'moving' as const
|
---|
7 |
|
---|
8 | move(event: PointerEvent) {
|
---|
9 | if (this.config.mouseOnly && event.pointerType !== 'mouse') return
|
---|
10 | if (!this.state._active) this.moveStart(event)
|
---|
11 | else this.moveChange(event)
|
---|
12 | this.timeoutStore.add('moveEnd', this.moveEnd.bind(this))
|
---|
13 | }
|
---|
14 |
|
---|
15 | moveStart(event: PointerEvent) {
|
---|
16 | this.start(event)
|
---|
17 | this.computeValues(pointerValues(event))
|
---|
18 | this.compute(event)
|
---|
19 | this.computeInitial()
|
---|
20 | this.emit()
|
---|
21 | }
|
---|
22 |
|
---|
23 | moveChange(event: PointerEvent) {
|
---|
24 | if (!this.state._active) return
|
---|
25 | const values = pointerValues(event)
|
---|
26 | const state = this.state
|
---|
27 | state._delta = V.sub(values, state._values)
|
---|
28 | V.addTo(state._movement, state._delta)
|
---|
29 |
|
---|
30 | this.computeValues(values)
|
---|
31 |
|
---|
32 | this.compute(event)
|
---|
33 | this.emit()
|
---|
34 | }
|
---|
35 |
|
---|
36 | moveEnd(event?: PointerEvent) {
|
---|
37 | if (!this.state._active) return
|
---|
38 | this.state._active = false
|
---|
39 | this.compute(event)
|
---|
40 | this.emit()
|
---|
41 | }
|
---|
42 |
|
---|
43 | bind(bindFunction: any) {
|
---|
44 | bindFunction('pointer', 'change', this.move.bind(this))
|
---|
45 | bindFunction('pointer', 'leave', this.moveEnd.bind(this))
|
---|
46 | }
|
---|
47 | }
|
---|