-
Notifications
You must be signed in to change notification settings - Fork 18
/
util-gesture.ts
398 lines (354 loc) · 10 KB
/
util-gesture.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
* Copyright (C) 2019 Toshiba Corporation
* SPDX-License-Identifier: Apache-2.0
*/
import { InteractionEvent, utils } from "pixi.js";
import { DApplicationTarget } from "../d-application-like";
import { DApplications } from "../d-applications";
import { DBaseStateSet } from "../d-base-state-set";
import { UtilGestureModifier } from "./util-gesture-modifier";
import { UtilGestureModifiers } from "./util-gesture-modifiers";
import { UtilGestureData } from "./util-gesture-data";
import { UtilGestureEasing, UtilGestureEasingOptions } from "./util-gesture-easing";
import { UtilPointerEvent } from "./util-pointer-event";
export interface UtilGestureTarget extends DApplicationTarget {
state: DBaseStateSet;
}
export type UtilGestureOnMove<TARGET> = (
target: TARGET,
dx: number,
dy: number,
x: number,
y: number,
ds: number
) => void;
export type UtilGestureOnStart<TARGET> = (target: TARGET) => void;
export type UtilGestureOnEnd<TARGET> = (target: TARGET) => void;
export type UtilGestureOnStop<TARGET> = (target: TARGET) => void;
export type UtilGestureOnEasingStart<TARGET> = (target: TARGET) => void;
export type UtilGestureOnEasingEnd<TARGET> = (target: TARGET) => void;
export type UtilGestureChecker<TARGET> = (
e: InteractionEvent,
modifier: UtilGestureModifier,
target: TARGET
) => boolean;
export interface UtilGestureCheckerOptions<TARGET> {
start?: UtilGestureChecker<TARGET>;
move?: UtilGestureChecker<TARGET>;
}
export interface UtilGestureOnEasingOptions<TARGET> {
start?: UtilGestureOnEasingStart<TARGET>;
end?: UtilGestureOnEasingEnd<TARGET>;
}
export interface UtilGestureOnOptions<TARGET> {
start?: UtilGestureOnStart<TARGET>;
move?: UtilGestureOnMove<TARGET>;
end?: UtilGestureOnEnd<TARGET>;
stop?: UtilGestureOnStop<TARGET>;
easing?: UtilGestureOnEasingOptions<TARGET>;
}
export interface UtilGestureOptions<TARGET> {
/**
* True to restrict to touch gestures only.
*/
touch?: boolean;
/**
* Expected mouse modifiers
*/
modifier?: UtilGestureModifier;
/**
* Checker options
*/
checker?: UtilGestureCheckerOptions<TARGET>;
/**
* Easing options
*/
easing?: boolean | UtilGestureEasingOptions;
/** Binds the pointerdown event of the given target. */
bind?: TARGET & utils.EventEmitter;
/** Event options */
on?: UtilGestureOnOptions<TARGET>;
}
export class UtilGesture<TARGET extends UtilGestureTarget> {
protected _onStart?: UtilGestureOnStart<TARGET>;
protected _onMove?: UtilGestureOnMove<TARGET>;
protected _onEnd?: UtilGestureOnEnd<TARGET>;
protected _onStop?: UtilGestureOnStop<TARGET>;
protected _onEasingStart?: UtilGestureOnEasingStart<TARGET>;
protected _onEasingEnd?: UtilGestureOnEasingEnd<TARGET>;
protected _modifier: UtilGestureModifier;
protected _checkerStart: UtilGestureChecker<TARGET>;
protected _checkerMove: UtilGestureChecker<TARGET>;
protected _easing?: boolean | UtilGestureEasingOptions;
protected _touch: boolean;
protected _unused: Array<UtilGestureData<TARGET>>;
protected _used: Map<TARGET, UtilGestureData<TARGET>>;
constructor(options: UtilGestureOptions<TARGET>) {
const on = options.on;
if (on) {
this._onStart = on.start;
this._onMove = on.move;
this._onEnd = on.end;
this._onStop = on.stop;
const easing = on.easing;
if (easing != null) {
this._onEasingStart = easing.start;
this._onEasingEnd = easing.end;
}
}
this._modifier = options?.modifier ?? UtilGestureModifier.NONE;
const checker = options.checker;
const defaultChecker = UtilGestureModifiers.match;
this._checkerStart = checker?.start ?? defaultChecker;
this._checkerMove = checker?.move ?? defaultChecker;
this._easing = options.easing;
this._touch = !!options.touch;
this._unused = [];
this._used = new Map<TARGET, UtilGestureData<TARGET>>();
const bind = options.bind;
if (bind != null) {
this.bind(bind);
}
}
protected newData(target: TARGET): UtilGestureData<TARGET> {
const used = this._used;
const result = used.get(target);
if (result != null) {
return result;
}
const poped = this._unused.pop();
if (poped != null) {
used.set(target, poped);
return poped;
}
const created = new UtilGestureData<TARGET>();
created.easing = this.newEasing(created);
created.onMove = this.newOnMove(created);
created.onEnd = this.newOnEnd(created);
used.set(target, created);
return created;
}
protected newEasing(data: UtilGestureData<TARGET>): UtilGestureEasing | undefined {
const easing = this._easing;
if (easing == null || easing !== false) {
const onEasingMoveBound = (dx: number, dy: number, ds: number, time: number): void => {
this.onEasingMove(dx, dy, ds, time, data);
};
const onEasingEndBound = (): void => {
this.onEasingEnd(data);
};
return new UtilGestureEasing(
onEasingMoveBound,
onEasingEndBound,
this.toEasingOptions(easing)
);
}
return undefined;
}
protected toEasingOptions(
options?: true | UtilGestureEasingOptions
): UtilGestureEasingOptions | undefined {
return options == null || options === true ? undefined : options;
}
protected deleteData(data: UtilGestureData<TARGET>): void {
const target = data.target;
if (target) {
if (this._used.delete(target)) {
data.target = undefined;
data.pointers.clear();
this._unused.push(data);
}
}
}
bind(target: TARGET & utils.EventEmitter): this {
target.on(UtilPointerEvent.down, (e: InteractionEvent): void => {
this.onDown(target, e);
});
return this;
}
protected isTouch(e: InteractionEvent): boolean {
const oe = e.data.originalEvent;
if ("touches" in oe) {
return true;
} else if ("pointerId" in oe) {
return oe.pointerType !== "mouse";
} else {
return false;
}
}
onDown(target: TARGET, e: InteractionEvent): void {
if (this._touch && !this.isTouch(e)) {
return;
}
if (!this._checkerStart(e, this._modifier, target)) {
return;
}
const layer = DApplications.getLayer(target);
if (layer == null) {
return;
}
e.stopPropagation();
if (target.state.isGesturing) {
this._used.get(target)?.start(e);
} else {
// User-defined handler
const onStart = this._onStart;
if (onStart != null) {
onStart(target);
}
const data = this.newData(target);
data.target = target;
target.state.isGesturing = true;
// Interaction manager
const interactionManager = layer.renderer.plugins.interaction;
data.interactionManager = interactionManager;
// Update the center
data.start(e);
// Easing util
const easing = data.easing;
if (easing) {
easing.onStart();
}
// Event handler
data.bind(e);
}
}
protected newOnMove(data: UtilGestureData<TARGET>): (e: InteractionEvent) => void {
return (e: InteractionEvent): void => {
this.onMove(e, data);
};
}
protected newOnEnd(data: UtilGestureData<TARGET>): (e: InteractionEvent) => void {
return (e: InteractionEvent): void => {
this.onEnd(e, data);
};
}
protected onMove(e: InteractionEvent, data: UtilGestureData<TARGET>): void {
const target = data.target;
if (target == null || !target.state.isGesturing) {
return;
}
if (!data.isUpdatable(e) || !this._checkerMove(e, this._modifier, target)) {
return;
}
// Update the center
const center = data.center;
const oldCenterX = center.x;
const oldCenterY = center.y;
const oldScale = data.scale;
const oldTime = data.time;
data.update(e);
const newCenterX = center.x;
const newCenterY = center.y;
const newScale = data.scale;
const newTime = data.time;
// Calculate the position
const dx = newCenterX - oldCenterX;
const dy = newCenterY - oldCenterY;
const dt = newTime - oldTime;
const epsilon = 0.00001;
const ds = epsilon < oldScale && epsilon < newScale ? newScale / oldScale : 1;
// Easing
const easing = data.easing;
if (easing) {
easing.onMove(dx, dy, ds, dt);
}
// Call the user-defined handler
const onMove = this._onMove;
if (onMove != null) {
if (dx !== 0 || dy !== 0 || ds !== 1) {
onMove(target, dx, dy, newCenterX, newCenterY, ds);
}
}
}
protected onEnd(e: InteractionEvent, data: UtilGestureData<TARGET>): void {
const target = data.target;
if (target == null || !target.state.isGesturing) {
return;
}
const oldTime = data.time;
if (!data.end(e)) {
return;
}
// State
target.state.isGesturing = false;
// Remove event handlers
const interactionManager = data.interactionManager;
data.interactionManager = undefined;
if (interactionManager) {
const onMoveBound = data.onMove;
if (onMoveBound) {
interactionManager.off(UtilPointerEvent.move, onMoveBound);
}
const onEndBound = data.onEnd;
if (onEndBound) {
interactionManager.off(UtilPointerEvent.up, onEndBound);
interactionManager.off(UtilPointerEvent.cancel, onEndBound);
}
}
// Call the user-defined handler
const onEnd = this._onEnd;
if (onEnd != null) {
onEnd(target);
}
// Start the Easing
const onEasingStart = this._onEasingStart;
if (onEasingStart) {
onEasingStart(data.target!);
}
const easing = data.easing;
if (easing) {
easing.onEnd(data.time - oldTime);
} else {
this.deleteData(data);
const onEasingEnd = this._onEasingEnd;
if (onEasingEnd) {
onEasingEnd(data.target!);
}
}
}
protected onEasingMove(
dx: number,
dy: number,
ds: number,
time: number,
data: UtilGestureData<TARGET>
): void {
const target = data.target;
if (target == null) {
return;
}
const onMove = this._onMove;
if (onMove == null) {
return;
}
if (dx === 0 && dy === 0 && ds === 1) {
return;
}
const center = data.center;
const x = center.x + dx;
const y = center.y + dy;
center.set(x, y);
onMove(data.target!, dx, dy, x, y, ds);
}
protected onEasingEnd(data: UtilGestureData<TARGET>): void {
const target = data.target;
this.deleteData(data);
const onEasingEnd = this._onEasingEnd;
if (onEasingEnd && target != null) {
onEasingEnd(target);
}
}
stop(target: TARGET): void {
const data = this._used.get(target);
if (data == null) {
return;
}
data.easing?.stop();
this.deleteData(data);
const onStop = this._onStop;
if (onStop) {
onStop(target);
}
}
}