-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
Pointer.ts
898 lines (775 loc) · 26.5 KB
/
Pointer.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
import { Engine, ScrollPreventionMode } from './../Engine';
import { GameEvent } from '../Events';
import { Actor } from '../Actor';
import { Vector, GlobalCoordinates } from '../Algebra';
import { Class } from '../Class';
import * as Actors from '../Util/Actors';
import * as Util from '../Util/Util';
import * as Events from '../Events';
export interface IActorsUnderPointer {
[ActorId: number]: Actor;
length: number;
}
/**
* The type of pointer for a [[PointerEvent]].
*/
export enum PointerType {
Touch,
Mouse,
Pen,
Unknown
}
/**
* The mouse button being pressed.
*/
export enum PointerButton {
Left,
Middle,
Right,
Unknown
}
export enum WheelDeltaMode {
Pixel,
Line,
Page
}
/**
* Determines the scope of handling mouse/touch events. See [[Pointers]] for more information.
*/
export enum PointerScope {
/**
* Handle events on the `canvas` element only. Events originating outside the
* `canvas` will not be handled.
*/
Canvas,
/**
* Handles events on the entire document. All events will be handled by Excalibur.
*/
Document
}
/**
* A constant used to normalize wheel events across different browsers
*
* This normalization factor is pulled from https://developer.mozilla.org/en-US/docs/Web/Events/wheel#Listening_to_this_event_across_browser
*/
const ScrollWheelNormalizationFactor = -1 / 40;
/**
* Pointer events
*
* Represents a mouse, touch, or stylus event. See [[Pointers]] for more information on
* handling pointer input.
*
* For mouse-based events, you can inspect [[PointerEvent.button]] to see what button was pressed.
*/
export class PointerEvent extends GameEvent<Actor> {
/** The world coordinates of the event. */
public get worldPos(): Vector {
return this.coordinates.worldPos.clone();
}
/** The page coordinates of the event. */
public get pagePos(): Vector {
return this.coordinates.pagePos.clone();
}
/** The screen coordinates of the event. */
public get screenPos(): Vector {
return this.coordinates.screenPos.clone();
}
/**
* @param coordinates The [[GlobalCoordinates]] of the event
* @param pointer The [[Pointer]] of the event
* @param index The index of the pointer (zero-based)
* @param pointerType The type of pointer
* @param button The button pressed (if [[PointerType.Mouse]])
* @param ev The raw DOM event being handled
* @param pos (Will be added to signature in 0.14.0 release) The position of the event (in world coordinates)
*/
constructor(
protected coordinates: GlobalCoordinates,
public pointer: Pointer,
public index: number,
public pointerType: PointerType,
public button: PointerButton,
public ev: any
) {
super();
}
public get pos(): Vector {
return this.coordinates.worldPos.clone();
}
/**
* Sets event path to actor, depending on actor provided.
* If actor is lower in hierarchy tree, previous path'll be kept.
* @param actor Actor, to lay path form.
*/
public layPath(actor: Actor): void {
if (!this.pointer.isActorUnderPointer(actor)) {
return;
}
const actorPath = actor.getAncestors();
this._path = actorPath.length > this._path.length ? actorPath : this._path;
}
}
export class PointerEventFactory<T extends PointerEvent> {
constructor(
protected _pointerEventType: new (
coordinates: GlobalCoordinates,
pointer: Pointer,
index: number,
pointerType: PointerType,
button: PointerButton,
ev: any
) => T
) {}
/**
* Create specific PointerEvent
*/
public create(
coordinates: GlobalCoordinates,
pointer: Pointer,
index: number,
pointerType: PointerType,
button: PointerButton,
ev: any
): T {
return new this._pointerEventType(coordinates, pointer, index, pointerType, button, ev);
}
}
export class PointerDragEvent extends PointerEvent {}
export class PointerUpEvent extends PointerEvent {
protected _name = 'pointerup';
protected _onActionEnd(actor: Actor) {
const pointer = this.pointer;
if (pointer.isDragEnd && actor.capturePointer.captureDragEvents) {
actor.eventDispatcher.emit('pointerdragend', this);
}
}
}
export class PointerDownEvent extends PointerEvent {
protected _name = 'pointerdown';
protected _onActionEnd(actor: Actor) {
if (this.pointer.isDragStart && actor.capturePointer.captureDragEvents) {
actor.eventDispatcher.emit('pointerdragstart', this);
}
}
}
export class PointerMoveEvent extends PointerEvent {
protected _name = 'pointermove';
public propagate(): void {
if (!this.eventPath.length) {
if (this.pointer.hasActorsUnderPointer) {
const actors = this.pointer.getActorsUnderPointer();
actors.forEach((actor) => {
if (!this.pointer.isActorUnderPointer(actor)) {
this._onActorLeave(actor);
}
});
}
return;
}
this.action();
if (this.bubbles) {
this.propagate();
}
}
/**
* Sets event path to actor, depending on actor provided.
* If actor is in a dragging state, current dragging target would be used.
* @param actor Actor, to lay path form.
*/
public layPath(actor: Actor): void {
/**
* double-check is preformed, because user could move pointer too fast and
* in that case, pointer can appear out of boundings of the dragging element
*/
if (this.pointer.isDragging) {
actor = this.pointer.dragTarget || actor;
} else if (!this.pointer.isActorUnderPointer(actor)) {
return;
}
const actorPath = actor.getAncestors();
this._path = actorPath.length > this._path.length ? actorPath : this._path;
}
protected _onActionStart(actor: Actor) {
if (!actor.capturePointer.captureMoveEvents) {
return;
}
if (this.pointer.isActorUnderPointer(actor) && !this.pointer.hasActorUnderPointerInList(actor)) {
this._onActorEnter(actor);
}
if (this.pointer.isDragging && actor.capturePointer.captureDragEvents) {
actor.eventDispatcher.emit('pointerdragmove', this);
}
}
private _onActorEnter(actor: Actor) {
const pe = createPointerEventByName('enter', this.coordinates, this.pointer, this.index, this.pointerType, this.button, this.ev);
pe.layPath(actor);
pe.propagate();
this.pointer.addActorUnderPointer(actor);
if (this.pointer.isDragging) {
this.pointer.dragTarget = actor;
}
}
private _onActorLeave(actor: Actor) {
const pe = createPointerEventByName('leave', this.coordinates, this.pointer, this.index, this.pointerType, this.button, this.ev);
pe.layPath(actor);
pe.propagate();
this.pointer.removeActorUnderPointer(actor);
}
}
export class PointerEnterEvent extends PointerEvent {
protected _name = 'pointerenter';
protected _onActionStart(actor: Actor) {
if (!actor.capturePointer.captureMoveEvents) {
return;
}
}
protected _onActionEnd(actor: Actor) {
const pointer = this.pointer;
if (pointer.isDragging && actor.capturePointer.captureDragEvents) {
actor.eventDispatcher.emit('pointerdragenter', this);
}
}
}
export class PointerLeaveEvent extends PointerEvent {
protected _name = 'pointerleave';
public layPath(actor: Actor): void {
const actorPath = actor.getAncestors();
this._path = actorPath.length > this._path.length ? actorPath : this._path;
}
protected _onActionStart(actor: Actor) {
if (!actor.capturePointer.captureMoveEvents) {
return;
}
}
protected _onActionEnd(actor: Actor) {
const pointer = this.pointer;
if (pointer.isDragging && actor.capturePointer.captureDragEvents) {
actor.eventDispatcher.emit('pointerdragleave', this);
}
}
}
export class PointerCancelEvent extends PointerEvent {
protected _name = 'pointercancel';
}
/**
* Wheel Events
*
* Represents a mouse wheel event. See [[Pointers]] for more information on
* handling point input.
*/
export class WheelEvent extends GameEvent<Actor> {
/**
* @param x The `x` coordinate of the event (in world coordinates)
* @param y The `y` coordinate of the event (in world coordinates)
* @param pageX The `x` coordinate of the event (in document coordinates)
* @param pageY The `y` coordinate of the event (in document coordinates)
* @param screenX The `x` coordinate of the event (in screen coordinates)
* @param screenY The `y` coordinate of the event (in screen coordinates)
* @param index The index of the pointer (zero-based)
* @param deltaX The type of pointer
* @param deltaY The type of pointer
* @param deltaZ The type of pointer
* @param deltaMode The type of movement [[WheelDeltaMode]]
* @param ev The raw DOM event being handled
*/
constructor(
public x: number,
public y: number,
public pageX: number,
public pageY: number,
public screenX: number,
public screenY: number,
public index: number,
public deltaX: number,
public deltaY: number,
public deltaZ: number,
public deltaMode: WheelDeltaMode,
public ev: any
) {
super();
}
}
function createPointerEventByName(
eventName: string,
coordinates: GlobalCoordinates,
pointer: Pointer,
index: number,
pointerType: PointerType,
button: PointerButton,
ev: any
) {
let factory: PointerEventFactory<PointerEvent>;
switch (eventName) {
case 'up':
factory = new PointerEventFactory(<any>PointerUpEvent);
break;
case 'down':
factory = new PointerEventFactory(<any>PointerDownEvent);
break;
case 'move':
factory = new PointerEventFactory(<any>PointerMoveEvent);
break;
case 'cancel':
factory = new PointerEventFactory(<any>PointerCancelEvent);
break;
case 'enter':
factory = new PointerEventFactory(<any>PointerEnterEvent);
break;
case 'leave':
factory = new PointerEventFactory(<any>PointerLeaveEvent);
break;
}
return factory.create(coordinates, pointer, index, pointerType, button, ev);
}
/**
* Handles pointer events (mouse, touch, stylus, etc.) and normalizes to
* [W3C Pointer Events](http://www.w3.org/TR/pointerevents/).
*
* [[include:Pointers.md]]
*/
export class Pointers extends Class {
private _engine: Engine;
private _pointerDown: PointerEvent[] = [];
private _pointerUp: PointerEvent[] = [];
private _pointerMove: PointerEvent[] = [];
private _pointerCancel: PointerEvent[] = [];
private _wheel: WheelEvent[] = [];
private _pointers: Pointer[] = [];
private _activePointers: number[] = [];
constructor(engine: Engine) {
super();
this._engine = engine;
this._pointers.push(new Pointer());
this._activePointers = [-1];
this.primary = this._pointers[0];
}
public on(eventName: Events.up, handler: (event?: PointerEvent) => void): void;
public on(eventName: Events.down, handler: (event?: PointerEvent) => void): void;
public on(eventName: Events.move, handler: (event?: PointerEvent) => void): void;
public on(eventName: Events.enter, handler: (event?: PointerEvent) => void): void;
public on(eventName: Events.leave, handler: (event?: PointerEvent) => void): void;
public on(eventName: Events.cancel, handler: (event?: PointerEvent) => void): void;
public on(eventName: Events.wheel, handler: (event?: WheelEvent) => void): void;
public on(eventName: string, handler: (event?: GameEvent<any>) => void): void;
public on(eventName: string, handler: (event?: any) => void): void {
super.on(eventName, handler);
}
/**
* Primary pointer (mouse, 1 finger, stylus, etc.)
*/
public primary: Pointer;
/**
* Initializes pointer event listeners
*/
public init(target?: GlobalEventHandlers): void {
target = target || this._engine.canvas;
// Touch Events
target.addEventListener('touchstart', this._handleTouchEvent('down', this._pointerDown));
target.addEventListener('touchend', this._handleTouchEvent('up', this._pointerUp));
target.addEventListener('touchmove', this._handleTouchEvent('move', this._pointerMove));
target.addEventListener('touchcancel', this._handleTouchEvent('cancel', this._pointerCancel));
// W3C Pointer Events
// Current: IE11, IE10
if ((<any>window).PointerEvent) {
// IE11
this._engine.canvas.style.touchAction = 'none';
target.addEventListener('pointerdown', this._handlePointerEvent('down', this._pointerDown));
target.addEventListener('pointerup', this._handlePointerEvent('up', this._pointerUp));
target.addEventListener('pointermove', this._handlePointerEvent('move', this._pointerMove));
target.addEventListener('pointercancel', this._handlePointerEvent('cancel', this._pointerCancel));
} else if ((<any>window).MSPointerEvent) {
// IE10
this._engine.canvas.style.msTouchAction = 'none';
target.addEventListener('MSPointerDown', this._handlePointerEvent('down', this._pointerDown));
target.addEventListener('MSPointerUp', this._handlePointerEvent('up', this._pointerUp));
target.addEventListener('MSPointerMove', this._handlePointerEvent('move', this._pointerMove));
target.addEventListener('MSPointerCancel', this._handlePointerEvent('cancel', this._pointerCancel));
} else {
// Mouse Events
target.addEventListener('mousedown', this._handleMouseEvent('down', this._pointerDown));
target.addEventListener('mouseup', this._handleMouseEvent('up', this._pointerUp));
target.addEventListener('mousemove', this._handleMouseEvent('move', this._pointerMove));
}
// MDN MouseWheelEvent
if ('onwheel' in document.createElement('div')) {
// Modern Browsers
target.addEventListener('wheel', this._handleWheelEvent('wheel', this._wheel));
} else if (document.onmousewheel !== undefined) {
// Webkit and IE
target.addEventListener('mousewheel', this._handleWheelEvent('wheel', this._wheel));
} else {
// Remaining browser and older Firefox
target.addEventListener('MozMousePixelScroll', this._handleWheelEvent('wheel', this._wheel));
}
}
public update(): void {
this._pointerUp.length = 0;
this._pointerDown.length = 0;
this._pointerMove.length = 0;
this._pointerCancel.length = 0;
this._wheel.length = 0;
for (let i = 0; i < this._pointers.length; i++) {
this._pointers[i].update();
}
}
/**
* Safely gets a Pointer at a specific index and initializes one if it doesn't yet exist
* @param index The pointer index to retrieve
*/
public at(index: number): Pointer {
if (index >= this._pointers.length) {
// Ensure there is a pointer to retrieve
for (var i = this._pointers.length - 1, max = index; i < max; i++) {
this._pointers.push(new Pointer());
this._activePointers.push(-1);
}
}
return this._pointers[index];
}
/**
* Get number of pointers being watched
*/
public count(): number {
return this._pointers.length;
}
/**
* Propogates events through ancestors chain if necessary
*/
public propagate() {
this._propagatePointerEvent(this._pointerDown);
this._propagatePointerEvent(this._pointerUp);
this._propagatePointerEvent(this._pointerMove);
this._propagatePointerEvent(this._pointerCancel);
this._propagateWheelEvent('pointerwheel', this._wheel);
}
/**
* Revises pointer events paths accordingly to actor provided
* @param actor Actor to be revised
*/
public revisePointerEventsPaths(actor: Actor) {
this._revisePointerEventPath(actor, this._pointerDown);
this._revisePointerEventPath(actor, this._pointerUp);
this._revisePointerEventPath(actor, this._pointerMove);
this._revisePointerEventPath(actor, this._pointerCancel);
this._validateWheelEventPath(this._wheel, actor);
}
private _propagatePointerEvent(pointers: PointerEvent[]) {
const len = pointers.length;
for (let i = 0; i < len; i++) {
pointers[i].propagate();
}
}
private _revisePointerEventPath(actor: Actor, pointers: PointerEvent[]) {
const len = pointers.length;
for (let i = 0; i < len; i++) {
const pointerEvent = pointers[i];
pointerEvent.layPath(actor);
}
}
private _validateWheelEventPath(pointers: WheelEvent[], actor: Actor): void {
for (let i = 0; i < pointers.length; i++) {
const wheelEvent = pointers[i];
const isNotUIActor = !Actors.isUIActor(actor);
if (actor.contains(wheelEvent.x, wheelEvent.y, isNotUIActor)) {
wheelEvent.layPath(actor);
}
}
}
private _propagateWheelEvent(eventName: string, pointers: WheelEvent[]) {
const len = pointers.length;
let wheelEvent: WheelEvent;
for (let i = 0; i < len; i++) {
wheelEvent = pointers[i];
switch (eventName) {
case 'pointerwheel':
this._propagateWheelPointerEvent(wheelEvent);
break;
}
}
}
private _propagateWheelPointerEvent(wheelEvent: WheelEvent) {
if (!wheelEvent.eventPath.length) {
return;
}
const actor = wheelEvent.eventPath.pop();
actor.eventDispatcher.emit('pointerwheel', wheelEvent);
if (wheelEvent.bubbles) {
this._propagateWheelPointerEvent(wheelEvent);
}
}
private _handleMouseEvent(eventName: string, eventArr: PointerEvent[]) {
return (e: MouseEvent) => {
e.preventDefault();
const pointer = this.at(0);
const coordinates = GlobalCoordinates.fromPagePosition(e.pageX, e.pageY, this._engine);
const pe = createPointerEventByName(eventName, coordinates, pointer, 0, PointerType.Mouse, e.button, e);
eventArr.push(pe);
pointer.eventDispatcher.emit(eventName, pe);
};
}
private _handleTouchEvent(eventName: string, eventArr: PointerEvent[]) {
return (e: ITouchEvent) => {
e.preventDefault();
for (var i = 0, len = e.changedTouches.length; i < len; i++) {
const index = this._pointers.length > 1 ? this._getPointerIndex(e.changedTouches[i].identifier) : 0;
if (index === -1) {
continue;
}
const pointer = this.at(index);
const coordinates = GlobalCoordinates.fromPagePosition(e.changedTouches[i].pageX, e.changedTouches[i].pageY, this._engine);
const pe = createPointerEventByName(eventName, coordinates, pointer, index, PointerType.Touch, PointerButton.Unknown, e);
eventArr.push(pe);
pointer.eventDispatcher.emit(eventName, pe);
// only with multi-pointer
if (this._pointers.length > 1) {
if (eventName === 'up') {
// remove pointer ID from pool when pointer is lifted
this._activePointers[index] = -1;
} else if (eventName === 'down') {
// set pointer ID to given index
this._activePointers[index] = e.changedTouches[i].identifier;
}
}
}
};
}
private _handlePointerEvent(eventName: string, eventArr: PointerEvent[]) {
return (e: MSPointerEvent) => {
e.preventDefault();
// get the index for this pointer ID if multi-pointer is asked for
const index = this._pointers.length > 1 ? this._getPointerIndex(e.pointerId) : 0;
if (index === -1) {
return;
}
const pointer = this.at(index);
const coordinates = GlobalCoordinates.fromPagePosition(e.pageX, e.pageY, this._engine);
const pe = createPointerEventByName(eventName, coordinates, pointer, index, this._stringToPointerType(e.pointerType), e.button, e);
eventArr.push(pe);
pointer.eventDispatcher.emit(eventName, pe);
// only with multi-pointer
if (this._pointers.length > 1) {
if (eventName === 'up') {
// remove pointer ID from pool when pointer is lifted
this._activePointers[index] = -1;
} else if (eventName === 'down') {
// set pointer ID to given index
this._activePointers[index] = e.pointerId;
}
}
};
}
private _handleWheelEvent(eventName: string, eventArr: WheelEvent[]) {
return (e: MouseWheelEvent) => {
// Should we prevent page scroll because of this event
if (
this._engine.pageScrollPreventionMode === ScrollPreventionMode.All ||
(this._engine.pageScrollPreventionMode === ScrollPreventionMode.Canvas && e.target === this._engine.canvas)
) {
e.preventDefault();
}
var x: number = e.pageX - Util.getPosition(this._engine.canvas).x;
var y: number = e.pageY - Util.getPosition(this._engine.canvas).y;
var transformedPoint = this._engine.screenToWorldCoordinates(new Vector(x, y));
// deltaX, deltaY, and deltaZ are the standard modern properties
// wheelDeltaX, wheelDeltaY, are legacy properties in webkit browsers and older IE
// e.detail is only used in opera
var deltaX = e.deltaX || e.wheelDeltaX * ScrollWheelNormalizationFactor || 0;
var deltaY =
e.deltaY || e.wheelDeltaY * ScrollWheelNormalizationFactor || e.wheelDelta * ScrollWheelNormalizationFactor || e.detail || 0;
var deltaZ = e.deltaZ || 0;
var deltaMode = WheelDeltaMode.Pixel;
if (e.deltaMode) {
if (e.deltaMode === 1) {
deltaMode = WheelDeltaMode.Line;
} else if (e.deltaMode === 2) {
deltaMode = WheelDeltaMode.Page;
}
}
var we = new WheelEvent(transformedPoint.x, transformedPoint.y, e.pageX, e.pageY, x, y, 0, deltaX, deltaY, deltaZ, deltaMode, e);
eventArr.push(we);
this.at(0).eventDispatcher.emit(eventName, we);
};
}
/**
* Gets the index of the pointer specified for the given pointer ID or finds the next empty pointer slot available.
* This is required because IE10/11 uses incrementing pointer IDs so we need to store a mapping of ID => idx
*/
private _getPointerIndex(pointerId: number) {
var idx;
if ((idx = this._activePointers.indexOf(pointerId)) > -1) {
return idx;
}
for (var i = 0; i < this._activePointers.length; i++) {
if (this._activePointers[i] === -1) {
return i;
}
}
// ignore pointer because game isn't watching
return -1;
}
private _stringToPointerType(s: string) {
switch (s) {
case 'touch':
return PointerType.Touch;
case 'mouse':
return PointerType.Mouse;
case 'pen':
return PointerType.Pen;
default:
return PointerType.Unknown;
}
}
}
/**
* Captures and dispatches PointerEvents
*/
export class Pointer extends Class {
private _isDown: boolean = false;
private _wasDown: boolean = false;
private _actorsUnderPointer: IActorsUnderPointer = { length: 0 };
/**
* Whether the Pointer is currently dragging.
*/
public get isDragging(): boolean {
return this._isDown;
}
/**
* Whether the Pointer just started dragging.
*/
public get isDragStart(): boolean {
return !this._wasDown && this._isDown;
}
/**
* Whether the Pointer just ended dragging.
*/
public get isDragEnd(): boolean {
return this._wasDown && !this._isDown;
}
/**
* Returns true if pointer has any actors under
*/
public get hasActorsUnderPointer(): boolean {
return !!this._actorsUnderPointer.length;
}
/**
* The last position on the document this pointer was at. Can be `null` if pointer was never active.
*/
public lastPagePos: Vector = null;
/**
* The last position on the screen this pointer was at. Can be `null` if pointer was never active.
*/
public lastScreenPos: Vector = null;
/**
* The last position in the game world coordinates this pointer was at. Can be `null` if pointer was never active.
*/
public lastWorldPos: Vector = null;
/**
* Returns the currently dragging target or null if it isn't exist
*/
public dragTarget: Actor = null;
constructor() {
super();
this.on('move', this._onPointerMove);
this.on('down', this._onPointerDown);
this.on('up', this._onPointerUp);
}
public update(): void {
if (this._wasDown && !this._isDown) {
this._wasDown = false;
} else if (!this._wasDown && this._isDown) {
this._wasDown = true;
}
}
/**
* Adds an Actor to actorsUnderPointer object.
* @param actor An Actor to be added;
*/
public addActorUnderPointer(actor: Actor): void {
if (!this.hasActorUnderPointerInList(actor)) {
this._actorsUnderPointer[actor.id] = actor;
this._actorsUnderPointer.length += 1;
}
}
/**
* Removes an Actor from actorsUnderPointer object.
* @param actor An Actor to be removed;
*/
public removeActorUnderPointer(actor: Actor): void {
if (this.hasActorUnderPointerInList(actor)) {
delete this._actorsUnderPointer[actor.id];
this._actorsUnderPointer.length -= 1;
}
}
/**
* Returns an Actor from actorsUnderPointer object.
* @param actor An Actor to be ;
*/
public getActorsUnderPointer(): Actor[] {
const actors: Actor[] = [];
for (const id in this._actorsUnderPointer) {
if (id === 'length') {
continue;
}
const actor = this._actorsUnderPointer[id];
actors.push(actor);
}
return actors;
}
/**
* Checks if Pointer has a specific Actor under.
* @param actor An Actor for check;
*/
public isActorUnderPointer(actor: Actor): boolean {
if (this.lastWorldPos) {
return actor.contains(this.lastWorldPos.x, this.lastWorldPos.y, !Actors.isUIActor(actor));
}
return false;
}
/**
* Checks if Pointer has a specific Actor in ActrorsUnderPointer list.
* @param actor An Actor for check;
*/
public hasActorUnderPointerInList(actor: Actor): boolean {
return this._actorsUnderPointer.hasOwnProperty(actor.id.toString());
}
private _onPointerMove(ev: PointerEvent): void {
this.lastPagePos = new Vector(ev.pagePos.x, ev.pagePos.y);
this.lastScreenPos = new Vector(ev.screenPos.x, ev.screenPos.y);
this.lastWorldPos = new Vector(ev.worldPos.x, ev.worldPos.y);
}
private _onPointerDown(ev: PointerEvent): void {
this.lastPagePos = new Vector(ev.pagePos.x, ev.pagePos.y);
this.lastScreenPos = new Vector(ev.screenPos.x, ev.screenPos.y);
this.lastWorldPos = new Vector(ev.worldPos.x, ev.worldPos.y);
this._isDown = true;
}
private _onPointerUp(): void {
this._isDown = false;
this.dragTarget = null;
}
}
interface ITouchEvent extends Event {
altKey: boolean;
changedTouches: ITouch[];
ctrlKey: boolean;
metaKey: boolean;
shiftKey: boolean;
targetTouches: ITouch[];
touches: ITouch[];
type: string;
target: Element;
}
interface ITouch {
identifier: number;
screenX: number;
screenY: number;
clientX: number;
clientY: number;
pageX: number;
pageY: number;
radiusX: number;
radiusY: number;
rotationAngle: number;
force: number;
target: Element;
}