/
Pointer.js
1271 lines (1082 loc) · 41.2 KB
/
Pointer.js
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
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* A Pointer object is used by the Mouse, Touch and MSPoint managers and represents a single finger on the touch screen.
*
* @class Phaser.Pointer
* @constructor
* @param {Phaser.Game} game - A reference to the currently running game.
* @param {number} id - The ID of the Pointer object within the game. Each game can have up to 10 active pointers.
* @param {Phaser.PointerMode} pointerMode=(CURSOR|CONTACT) - The operational mode of this pointer, eg. CURSOR or CONTACT.
*/
Phaser.Pointer = function (game, id, pointerMode)
{
/**
* @property {Phaser.Game} game - A reference to the currently running game.
*/
this.game = game;
/**
* @property {number} id - The ID of the Pointer object within the game. Each game can have up to 10 active pointers.
*/
this.id = id;
/**
* @property {number} type - The const type of this object.
* @readonly
*/
this.type = Phaser.POINTER;
/**
* @property {boolean} exists - A Pointer object that exists is allowed to be checked for physics collisions and overlaps.
* @default
*/
this.exists = true;
/**
* @property {number} identifier - The identifier property of the Pointer as set by the DOM event when this Pointer is started.
* @default
*/
this.identifier = 0;
/**
* @property {number} pointerId - The pointerId property of the Pointer as set by the DOM event when this Pointer is started. The browser can and will recycle this value.
* @default
*/
this.pointerId = null;
/**
* @property {Phaser.PointerMode} pointerMode - The operational mode of this pointer.
*/
this.pointerMode = pointerMode || (Phaser.PointerMode.CURSOR | Phaser.PointerMode.CONTACT);
/**
* @property {any} target - The target property of the Pointer as set by the DOM event when this Pointer is started.
* @default
*/
this.target = null;
/**
* The button property of the most recent DOM event when this Pointer is started.
* You should not rely on this value for accurate button detection, instead use the Pointer properties
* `leftButton`, `rightButton`, `middleButton` and so on.
* @property {any} button
* @default
*/
this.button = null;
/**
* If this Pointer is a Mouse or Pen / Stylus then you can access its left button directly through this property.
*
* The DeviceButton has its own properties such as `isDown`, `duration` and methods like `justReleased` for more fine-grained
* button control.
*
* @property {Phaser.DeviceButton} leftButton
* @default
*/
this.leftButton = new Phaser.DeviceButton(this, Phaser.Pointer.LEFT_BUTTON);
/**
* If this Pointer is a Mouse or Pen / Stylus then you can access its middle button directly through this property.
*
* The DeviceButton has its own properties such as `isDown`, `duration` and methods like `justReleased` for more fine-grained
* button control.
*
* Please see the DeviceButton docs for details on browser button limitations.
*
* @property {Phaser.DeviceButton} middleButton
* @default
*/
this.middleButton = new Phaser.DeviceButton(this, Phaser.Pointer.MIDDLE_BUTTON);
/**
* If this Pointer is a Mouse or Pen / Stylus then you can access its right button directly through this property.
*
* The DeviceButton has its own properties such as `isDown`, `duration` and methods like `justReleased` for more fine-grained
* button control.
*
* Please see the DeviceButton docs for details on browser button limitations.
*
* @property {Phaser.DeviceButton} rightButton
* @default
*/
this.rightButton = new Phaser.DeviceButton(this, Phaser.Pointer.RIGHT_BUTTON);
/**
* If this Pointer is a Mouse or Pen / Stylus then you can access its X1 (back) button directly through this property.
*
* The DeviceButton has its own properties such as `isDown`, `duration` and methods like `justReleased` for more fine-grained
* button control.
*
* Please see the DeviceButton docs for details on browser button limitations.
*
* @property {Phaser.DeviceButton} backButton
* @default
*/
this.backButton = new Phaser.DeviceButton(this, Phaser.Pointer.BACK_BUTTON);
/**
* If this Pointer is a Mouse or Pen / Stylus then you can access its X2 (forward) button directly through this property.
*
* The DeviceButton has its own properties such as `isDown`, `duration` and methods like `justReleased` for more fine-grained
* button control.
*
* Please see the DeviceButton docs for details on browser button limitations.
*
* @property {Phaser.DeviceButton} forwardButton
* @default
*/
this.forwardButton = new Phaser.DeviceButton(this, Phaser.Pointer.FORWARD_BUTTON);
/**
* If this Pointer is a Pen / Stylus then you can access its eraser button directly through this property.
*
* The DeviceButton has its own properties such as `isDown`, `duration` and methods like `justReleased` for more fine-grained
* button control.
*
* Please see the DeviceButton docs for details on browser button limitations.
*
* @property {Phaser.DeviceButton} eraserButton
* @default
*/
this.eraserButton = new Phaser.DeviceButton(this, Phaser.Pointer.ERASER_BUTTON);
/**
* @property {boolean} _holdSent - Local private variable to store the status of dispatching a hold event.
* @private
* @default
*/
this._holdSent = false;
/**
* @property {array} _history - Local private variable storing the short-term history of pointer movements.
* @private
*/
this._history = [];
/**
* @property {number} _nextDrop - Local private variable storing the time at which the next history drop should occur.
* @private
*/
this._nextDrop = 0;
/**
* @property {boolean} _stateReset - Monitor events outside of a state reset loop.
* @private
*/
this._stateReset = false;
/**
* @property {boolean} withinGame - true if the Pointer is over the game canvas, otherwise false.
*/
this.withinGame = false;
/**
* @property {number} clientX - The horizontal coordinate of the Pointer within the application's client area at which the event occurred (as opposed to the coordinates within the page).
*/
this.clientX = -1;
/**
* @property {number} clientY - The vertical coordinate of the Pointer within the application's client area at which the event occurred (as opposed to the coordinates within the page).
*/
this.clientY = -1;
/**
* @property {number} pageX - The horizontal coordinate of the Pointer relative to whole document.
*/
this.pageX = -1;
/**
* @property {number} pageY - The vertical coordinate of the Pointer relative to whole document.
*/
this.pageY = -1;
/**
* @property {number} screenX - The horizontal coordinate of the Pointer relative to the screen.
*/
this.screenX = -1;
/**
* @property {number} screenY - The vertical coordinate of the Pointer relative to the screen.
*/
this.screenY = -1;
/**
* @property {number} rawMovementX - The horizontal raw relative movement of the Pointer in pixels at the last event, if this is a Mouse Pointer in a locked state.
* @default
* @see https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/movementX
*/
this.rawMovementX = 0;
/**
* @property {number} rawMovementY - The vertical raw relative movement of the Pointer in pixels at the last event, if this is a Mouse Pointer in a locked state.
* @default
* @see https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/movementY
*/
this.rawMovementY = 0;
/**
* @property {number} movementX - The cumulative horizontal relative movement of the Pointer in pixels since resetMovement() was called, if this is a Mouse Pointer in a locked state.
* @default
*/
this.movementX = 0;
/**
* @property {number} movementY - The cumulative vertical relative movement of the Pointer in pixels since resetMovement() was called, if this is a Mouse Pointer in a locked state..
* @default
*/
this.movementY = 0;
/**
* @property {number} x - The horizontal coordinate of the Pointer. This value is automatically scaled based on the game scale.
* @default
*/
this.x = -1;
/**
* @property {number} y - The vertical coordinate of the Pointer. This value is automatically scaled based on the game scale.
* @default
*/
this.y = -1;
/**
* @property {boolean} isMouse - If the Pointer is a mouse or pen / stylus this is true, otherwise false.
*/
this.isMouse = (id === 0);
/**
* If the Pointer is touching the touchscreen, or *any* mouse or pen button is held down, isDown is set to true.
* If you need to check a specific mouse or pen button then use the button properties, i.e. Pointer.rightButton.isDown.
* @property {boolean} isDown
* @default
*/
this.isDown = false;
/**
* If the Pointer is not touching the touchscreen, or *all* mouse or pen buttons are up, isUp is set to true.
* If you need to check a specific mouse or pen button then use the button properties, i.e. Pointer.rightButton.isUp.
* @property {boolean} isUp
* @default
*/
this.isUp = true;
/**
* @property {number} timeDown - A timestamp representing when the Pointer first touched the touchscreen.
* @default
*/
this.timeDown = 0;
/**
* @property {number} timeUp - A timestamp representing when the Pointer left the touchscreen.
* @default
*/
this.timeUp = 0;
/**
* @property {number} previousTapTime - A timestamp representing when the Pointer was last tapped or clicked.
* @default
*/
this.previousTapTime = 0;
/**
* @property {number} totalTouches - The total number of times this Pointer has been touched to the touchscreen.
* @default
*/
this.totalTouches = 0;
/**
* @property {number} msSinceLastClick - The number of milliseconds since the last click or touch event.
* @default
*/
this.msSinceLastClick = Number.MAX_VALUE;
/**
* @property {any} targetObject - The Game Object this Pointer is currently over / touching / dragging.
* @default
*/
this.targetObject = null;
/**
* This array is erased and re-populated every time this Pointer is updated. It contains references to all
* of the Game Objects that were considered as being valid for processing by this Pointer, this frame. To be
* valid they must have suitable a `priorityID`, be Input enabled, visible and actually have the Pointer over
* them. You can check the contents of this array in events such as `onInputDown`, but beware it is reset
* every frame.
* @property {array} interactiveCandidates
* @default
*/
this.interactiveCandidates = [];
/**
* @property {boolean} active - An active pointer is one that is currently pressed down on the display. A Mouse is always active.
* @default
*/
this.active = false;
/**
* @property {boolean} dirty - A dirty pointer needs to re-poll any interactive objects it may have been over, regardless if it has moved or not.
* @default
*/
this.dirty = false;
/**
* @property {Phaser.Point} position - A Phaser.Point object containing the current x/y values of the pointer on the display.
*/
this.position = new Phaser.Point();
/**
* @property {Phaser.Point} positionDown - A Phaser.Point object containing the x/y values of the pointer when it was last in a down state on the display.
*/
this.positionDown = new Phaser.Point();
/**
* @property {Phaser.Point} positionUp - A Phaser.Point object containing the x/y values of the pointer when it was last released.
*/
this.positionUp = new Phaser.Point();
/**
* A Phaser.Circle that is centered on the x/y coordinates of this pointer, useful for hit detection.
* The Circle size is 44px (Apples recommended "finger tip" size).
* @property {Phaser.Circle} circle
*/
this.circle = new Phaser.Circle(0, 0, 44);
/**
* Click trampolines associated with this pointer. See `addClickTrampoline`.
* @property {object[]|null} _clickTrampolines
* @private
*/
this._clickTrampolines = null;
/**
* When the Pointer has click trampolines the last target object is stored here
* so it can be used to check for validity of the trampoline in a post-Up/'stop'.
* @property {object} _trampolineTargetObject
* @private
*/
this._trampolineTargetObject = null;
};
/**
* No buttons at all.
* @constant
* @type {number}
*/
Phaser.Pointer.NO_BUTTON = 0;
/**
* The Left Mouse button, or in PointerEvent devices a Touch contact or Pen contact.
* @constant
* @type {number}
*/
Phaser.Pointer.LEFT_BUTTON = 1;
/**
* The Right Mouse button, or in PointerEvent devices a Pen contact with a barrel button.
* @constant
* @type {number}
*/
Phaser.Pointer.RIGHT_BUTTON = 2;
/**
* The Middle Mouse button.
* @constant
* @type {number}
*/
Phaser.Pointer.MIDDLE_BUTTON = 4;
/**
* The X1 button. This is typically the mouse Back button, but is often reconfigured.
* On Linux (GTK) this is unsupported. On Windows if advanced pointer software (such as IntelliPoint) is installed this doesn't register.
* @constant
* @type {number}
*/
Phaser.Pointer.BACK_BUTTON = 8;
/**
* The X2 button. This is typically the mouse Forward button, but is often reconfigured.
* On Linux (GTK) this is unsupported. On Windows if advanced pointer software (such as IntelliPoint) is installed this doesn't register.
* @constant
* @type {number}
*/
Phaser.Pointer.FORWARD_BUTTON = 16;
/**
* The Eraser pen button on PointerEvent supported devices only.
* @constant
* @type {number}
*/
Phaser.Pointer.ERASER_BUTTON = 32;
Phaser.Pointer.prototype = {
/**
* Resets the states of all the button booleans.
*
* @method Phaser.Pointer#resetButtons
* @protected
*/
resetButtons: function ()
{
this.isDown = false;
this.isUp = true;
if (this.isMouse)
{
this.leftButton.reset();
this.middleButton.reset();
this.rightButton.reset();
this.backButton.reset();
this.forwardButton.reset();
this.eraserButton.reset();
}
},
/**
* Called by processButtonsUpDown.
*
* @method Phaser.Pointer#processButtonsDown
* @private
* @param {integer} button - {@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button MouseEvent#button} value.
* @param {MouseEvent} event - The DOM event.
*/
processButtonsDown: function (button, event)
{
switch (button)
{
case (Phaser.Mouse.LEFT_BUTTON):
this.leftButton.start(event);
break;
case (Phaser.Mouse.RIGHT_BUTTON):
this.rightButton.start(event);
break;
case (Phaser.Mouse.MIDDLE_BUTTON):
this.middleButton.start(event);
break;
case (Phaser.Mouse.BACK_BUTTON):
this.backButton.start(event);
break;
case (Phaser.Mouse.FORWARD_BUTTON):
this.forwardButton.start(event);
break;
}
},
/**
* Called by processButtonsUpDown.
*
* @method Phaser.Pointer#processButtonsUp
* @private
* @param {integer} button - {@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button MouseEvent#button} value.
* @param {MouseEvent} event - The DOM event.
*/
processButtonsUp: function (button, event)
{
switch (button)
{
case (Phaser.Mouse.LEFT_BUTTON):
this.leftButton.stop(event);
break;
case (Phaser.Mouse.RIGHT_BUTTON):
this.rightButton.stop(event);
break;
case (Phaser.Mouse.MIDDLE_BUTTON):
this.middleButton.stop(event);
break;
case (Phaser.Mouse.BACK_BUTTON):
this.backButton.stop(event);
break;
case (Phaser.Mouse.FORWARD_BUTTON):
this.forwardButton.stop(event);
break;
}
},
/**
* Called by updateButtons.
*
* @method Phaser.Pointer#processButtonsUpDown
* @private
* @param {integer} buttons - {@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons MouseEvent#buttons} value.
* @param {MouseEvent} event - The DOM event.
*/
processButtonsUpDown: function (buttons, event)
{
var type = event.type.toLowerCase().substr(-4);
var down = (type === 'down');
var move = (type === 'move');
if (buttons !== undefined)
{
// On OS X (and other devices with trackpads) you have to press CTRL + the pad to initiate a right-click event.
if (down && buttons === 1 && event.ctrlKey)
{
buttons = 2;
}
// Note: These are bitwise checks, not booleans
this.leftButton.startStop(Phaser.Pointer.LEFT_BUTTON & buttons, event);
this.rightButton.startStop(Phaser.Pointer.RIGHT_BUTTON & buttons, event);
this.middleButton.startStop(Phaser.Pointer.MIDDLE_BUTTON & buttons, event);
this.backButton.startStop(Phaser.Pointer.BACK_BUTTON & buttons, event);
this.forwardButton.startStop(Phaser.Pointer.FORWARD_BUTTON & buttons, event);
this.eraserButton.startStop(Phaser.Pointer.ERASER_BUTTON & buttons, event);
}
else
{
/*
* No buttons property (like Safari on OSX when using a trackpad)
* Attempt to use event.button property or fallback to default
*/
if (event.button !== undefined)
{
// On OS X (and other devices with trackpads) you have to press CTRL + the pad to initiate a right-click event.
if (down && event.ctrlKey && event.button === 0)
{
this.rightButton.start(event);
}
else
if (down)
{
this.processButtonsDown(event.button, event);
}
else if (!move)
{
this.processButtonsUp(event.button, event);
}
}
else
if (down)
{
// On OS X (and other devices with trackpads) you have to press CTRL + the pad to initiate a right-click event.
if (event.ctrlKey)
{
this.rightButton.start(event);
}
else
{
this.leftButton.start(event);
}
}
else
{
this.leftButton.stop(event);
this.rightButton.stop(event);
}
}
},
/**
* Called when the event.buttons property changes from zero.
* Contains a button bitmask.
*
* @method Phaser.Pointer#updateButtons
* @protected
* @param {MouseEvent} event - The DOM event.
*/
updateButtons: function (event)
{
this.button = event.button;
this.processButtonsUpDown(event.buttons, event);
this.isUp = true;
this.isDown = false;
if (this.leftButton.isDown || this.rightButton.isDown || this.middleButton.isDown || this.backButton.isDown || this.forwardButton.isDown || this.eraserButton.isDown)
{
this.isUp = false;
this.isDown = true;
}
},
/**
* Called when the Pointer is pressed onto the touchscreen.
* @method Phaser.Pointer#start
* @param {any} event - The DOM event from the browser.
*/
start: function (event)
{
var input = this.game.input;
if (event.pointerId)
{
this.pointerId = event.pointerId;
}
this.identifier = event.identifier;
this.target = event.target;
if (this.isMouse)
{
this.updateButtons(event);
}
else
{
this.isDown = true;
this.isUp = false;
}
this.active = true;
this.withinGame = true;
this.dirty = false;
this._history = [];
this._clickTrampolines = null;
this._trampolineTargetObject = null;
// Work out how long it has been since the last click
this.msSinceLastClick = this.game.time.time - this.timeDown;
this.timeDown = this.game.time.time;
this._holdSent = false;
// This sets the x/y and other local values
this.move(event, true);
// x and y are the old values here?
this.positionDown.setTo(this.x, this.y);
if (input.multiInputOverride === Phaser.Input.MOUSE_OVERRIDES_TOUCH ||
input.multiInputOverride === Phaser.Input.MOUSE_TOUCH_COMBINE ||
(input.multiInputOverride === Phaser.Input.TOUCH_OVERRIDES_MOUSE && input.totalActivePointers === 0))
{
input.x = this.x;
input.y = this.y;
input.position.setTo(this.x, this.y);
input.onDown.dispatch(this, event);
input.resetSpeed(this.x, this.y);
}
this._stateReset = false;
this.totalTouches++;
if (this.targetObject !== null)
{
this.targetObject._touchedHandler(this);
}
return this;
},
/**
* Called by the Input Manager.
* @method Phaser.Pointer#update
*/
update: function ()
{
var input = this.game.input;
if (this.active)
{
// Force a check?
if (this.dirty)
{
if (input.interactiveItems.total > 0)
{
this.processInteractiveObjects(false);
}
this.dirty = false;
}
if (this._holdSent === false && this.duration >= input.holdRate)
{
if (input.multiInputOverride === Phaser.Input.MOUSE_OVERRIDES_TOUCH ||
input.multiInputOverride === Phaser.Input.MOUSE_TOUCH_COMBINE ||
(input.multiInputOverride === Phaser.Input.TOUCH_OVERRIDES_MOUSE && input.totalActivePointers === 0))
{
input.onHold.dispatch(this);
}
this._holdSent = true;
}
// Update the droppings history
if (input.recordPointerHistory && this.game.time.time >= this._nextDrop)
{
this._nextDrop = this.game.time.time + input.recordRate;
this._history.push({
x: this.position.x,
y: this.position.y
});
if (this._history.length > input.recordLimit)
{
this._history.shift();
}
}
}
},
/**
* Called when the Pointer is moved.
*
* @method Phaser.Pointer#move
* @param {MouseEvent|PointerEvent|TouchEvent} event - The event passed up from the input handler.
* @param {boolean} [fromClick=false] - Was this called from the click event?
*/
move: function (event, fromClick)
{
var input = this.game.input;
if (input.pollLocked)
{
return;
}
if (fromClick === undefined) { fromClick = false; }
if (event.button !== undefined)
{
this.button = event.button;
}
if (this.isMouse)
{
this.updateButtons(event);
}
this.clientX = event.clientX;
this.clientY = event.clientY;
this.pageX = event.pageX;
this.pageY = event.pageY;
this.screenX = event.screenX;
this.screenY = event.screenY;
if (this.isMouse && input.pointerLock.locked && !fromClick)
{
this.rawMovementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
this.rawMovementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
this.movementX += this.rawMovementX;
this.movementY += this.rawMovementY;
}
this.x = (this.pageX - this.game.scale.offset.x) * input.scale.x;
this.y = (this.pageY - this.game.scale.offset.y) * input.scale.y;
this.position.setTo(this.x, this.y);
this.circle.x = this.x;
this.circle.y = this.y;
if (input.multiInputOverride === Phaser.Input.MOUSE_OVERRIDES_TOUCH ||
input.multiInputOverride === Phaser.Input.MOUSE_TOUCH_COMBINE ||
(input.multiInputOverride === Phaser.Input.TOUCH_OVERRIDES_MOUSE && input.totalActivePointers === 0))
{
input.activePointer = this;
input.x = this.x;
input.y = this.y;
input.position.setTo(input.x, input.y);
input.circle.x = input.x;
input.circle.y = input.y;
}
this.withinGame = this.game.scale.bounds.contains(this.pageX, this.pageY);
var i = input.moveCallbacks.length;
while (i--)
{
input.moveCallbacks[i].callback.call(input.moveCallbacks[i].context, this, this.x, this.y, fromClick, event);
}
// Easy out if we're dragging something and it still exists
if (this.targetObject !== null && (!this.game.paused || this.targetObject.noPause) && this.targetObject.isDragged === true)
{
if (this.targetObject.update(this) === false)
{
this.targetObject = null;
}
}
else if (input.interactiveItems.total > 0)
{
this.processInteractiveObjects(fromClick);
}
return this;
},
/**
* Process all interactive objects to find out which ones were updated in the recent Pointer move.
*
* @method Phaser.Pointer#processInteractiveObjects
* @protected
* @param {boolean} [fromClick=false] - Was this called from the click event?
* @return {boolean} True if this method processes an object (i.e. a Sprite becomes the Pointers currentTarget), otherwise false.
*/
processInteractiveObjects: function (fromClick)
{
// Work out which object is on the top
var highestRenderOrderID = 0;
var highestInputPriorityID = -1;
var candidateTarget = null;
/*
* First pass gets all objects that the pointer is over that DON'T use pixelPerfect checks and get the highest ID
* We know they'll be valid for input detection but not which is the top just yet
*/
var currentNode = this.game.input.interactiveItems.first;
this.interactiveCandidates = [];
while (currentNode)
{
// Reset checked status
currentNode.checked = false;
if (currentNode.validForInput(highestInputPriorityID, highestRenderOrderID, false) && (!this.game.paused || currentNode.sprite.noPause))
{
// Flag it as checked so we don't re-scan it on the next phase
currentNode.checked = true;
if ((fromClick && currentNode.checkPointerDown(this, true)) ||
(!fromClick && currentNode.checkPointerOver(this, true)))
{
highestRenderOrderID = currentNode.sprite.renderOrderID;
highestInputPriorityID = currentNode.priorityID;
candidateTarget = currentNode;
this.interactiveCandidates.push(currentNode);
}
}
currentNode = this.game.input.interactiveItems.next;
}
/*
* Then in the second sweep we process ONLY the pixel perfect ones that are checked and who have a higher ID
* because if their ID is lower anyway then we can just automatically discount them
* (A node that was previously checked did not request a pixel-perfect check.)
*/
currentNode = this.game.input.interactiveItems.first;
while (currentNode)
{
if (!currentNode.checked &&
currentNode.validForInput(highestInputPriorityID, highestRenderOrderID, true))
{
if ((fromClick && currentNode.checkPointerDown(this, false)) ||
(!fromClick && currentNode.checkPointerOver(this, false)))
{
highestRenderOrderID = currentNode.sprite.renderOrderID;
highestInputPriorityID = currentNode.priorityID;
candidateTarget = currentNode;
this.interactiveCandidates.push(currentNode);
}
}
currentNode = this.game.input.interactiveItems.next;
}
if (this.game.input.customCandidateHandler)
{
candidateTarget = this.game.input.customCandidateHandler.call(this.game.input.customCandidateHandlerContext, this, this.interactiveCandidates, candidateTarget);
}
this.swapTarget(candidateTarget, false);
return (this.targetObject !== null);
},
/**
* This will change the `Pointer.targetObject` object to be the one provided.
*
* This allows you to have fine-grained control over which object the Pointer is targeting.
*
* Note that even if you set a new Target here, it is still able to be replaced by any other valid
* target during the next Pointer update.
*
* @method Phaser.Pointer#swapTarget
* @param {Phaser.InputHandler} newTarget - The new target for this Pointer. Note this is an `InputHandler`, so don't pass a Sprite, instead pass `sprite.input` to it.
* @param {boolean} [silent=false] - If true the new target AND the old one will NOT dispatch their `onInputOver` or `onInputOut` events.
*/
swapTarget: function (newTarget, silent)
{
if (silent === undefined) { silent = false; }
// Now we know the top-most item (if any) we can process it
if (newTarget === null)
{
// The pointer isn't currently over anything, check if we've got a lingering previous target
if (this.targetObject)
{
this.targetObject._pointerOutHandler(this, silent);
this.targetObject = null;
}
}
else
if (this.targetObject === null)
{
// And now set the new one
this.targetObject = newTarget;
newTarget._pointerOverHandler(this, silent);
}
else
{
// We've got a target from the last update
if (this.targetObject === newTarget)
{
// Same target as before, so update it
if (newTarget.update(this) === false)
{
this.targetObject = null;
}
}
else
{
// The target has changed, so tell the old one we've left it
this.targetObject._pointerOutHandler(this, silent);
// And now set the new one
this.targetObject = newTarget;
this.targetObject._pointerOverHandler(this, silent);
}
}
},
/**
* Called when the Pointer leaves the target area.
*
* @method Phaser.Pointer#leave
* @param {MouseEvent|PointerEvent|TouchEvent} event - The event passed up from the input handler.
*/
leave: function (event)
{
this.withinGame = false;
this.move(event, false);
},
/**
* Called when the Pointer leaves the touchscreen.
*
* @method Phaser.Pointer#stop
* @param {MouseEvent|PointerEvent|TouchEvent} event - The event passed up from the input handler.
*/
stop: function (event)
{
var input = this.game.input;
if (this._stateReset && this.withinGame)
{
event.preventDefault();
return;
}
this.timeUp = this.game.time.time;
if (input.multiInputOverride === Phaser.Input.MOUSE_OVERRIDES_TOUCH ||
input.multiInputOverride === Phaser.Input.MOUSE_TOUCH_COMBINE ||
(input.multiInputOverride === Phaser.Input.TOUCH_OVERRIDES_MOUSE && input.totalActivePointers === 0))
{
input.onUp.dispatch(this, event);
// Was it a tap?
if (this.duration >= 0 && this.duration <= input.tapRate)
{
// Was it a double-tap?
var doubleTap = (this.timeUp - this.previousTapTime < input.doubleTapRate);
input.onTap.dispatch(this, doubleTap, event);
this.previousTapTime = this.timeUp;
}
}