/
InputManager.js
1086 lines (923 loc) · 32.9 KB
/
InputManager.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 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../utils/Class');
var CONST = require('./const');
var EventEmitter = require('eventemitter3');
var Events = require('./events');
var GameEvents = require('../core/events');
var Keyboard = require('./keyboard/KeyboardManager');
var Mouse = require('./mouse/MouseManager');
var Pointer = require('./Pointer');
var Touch = require('./touch/TouchManager');
var TransformMatrix = require('../gameobjects/components/TransformMatrix');
var TransformXY = require('../math/TransformXY');
/**
* @classdesc
* The Input Manager is responsible for handling the pointer related systems in a single Phaser Game instance.
*
* Based on the Game Config it will create handlers for mouse and touch support.
*
* Keyboard and Gamepad are plugins, handled directly by the InputPlugin class.
*
* It then manages the events, pointer creation and general hit test related operations.
*
* You rarely need to interact with the Input Manager directly, and as such, all of its properties and methods
* should be considered private. Instead, you should use the Input Plugin, which is a Scene level system, responsible
* for dealing with all input events for a Scene.
*
* @class InputManager
* @memberof Phaser.Input
* @constructor
* @since 3.0.0
*
* @param {Phaser.Game} game - The Game instance that owns the Input Manager.
* @param {object} config - The Input Configuration object, as set in the Game Config.
*/
var InputManager = new Class({
initialize:
function InputManager (game, config)
{
/**
* The Game instance that owns the Input Manager.
* A Game only maintains on instance of the Input Manager at any time.
*
* @name Phaser.Input.InputManager#game
* @type {Phaser.Game}
* @readonly
* @since 3.0.0
*/
this.game = game;
/**
* A reference to the global Game Scale Manager.
* Used for all bounds checks and pointer scaling.
*
* @name Phaser.Input.InputManager#scaleManager
* @type {Phaser.Scale.ScaleManager}
* @since 3.16.0
*/
this.scaleManager;
/**
* The Canvas that is used for all DOM event input listeners.
*
* @name Phaser.Input.InputManager#canvas
* @type {HTMLCanvasElement}
* @since 3.0.0
*/
this.canvas;
/**
* The Game Configuration object, as set during the game boot.
*
* @name Phaser.Input.InputManager#config
* @type {Phaser.Core.Config}
* @since 3.0.0
*/
this.config = config;
/**
* If set, the Input Manager will run its update loop every frame.
*
* @name Phaser.Input.InputManager#enabled
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.enabled = true;
/**
* The Event Emitter instance that the Input Manager uses to emit events from.
*
* @name Phaser.Input.InputManager#events
* @type {Phaser.Events.EventEmitter}
* @since 3.0.0
*/
this.events = new EventEmitter();
/**
* Are any mouse or touch pointers currently over the game canvas?
* This is updated automatically by the canvas over and out handlers.
*
* @name Phaser.Input.InputManager#isOver
* @type {boolean}
* @readonly
* @since 3.16.0
*/
this.isOver = true;
/**
* The default CSS cursor to be used when interacting with your game.
*
* See the `setDefaultCursor` method for more details.
*
* @name Phaser.Input.InputManager#defaultCursor
* @type {string}
* @since 3.10.0
*/
this.defaultCursor = '';
/**
* A reference to the Keyboard Manager class, if enabled via the `input.keyboard` Game Config property.
*
* @name Phaser.Input.InputManager#keyboard
* @type {?Phaser.Input.Keyboard.KeyboardManager}
* @since 3.16.0
*/
this.keyboard = (config.inputKeyboard) ? new Keyboard(this) : null;
/**
* A reference to the Mouse Manager class, if enabled via the `input.mouse` Game Config property.
*
* @name Phaser.Input.InputManager#mouse
* @type {?Phaser.Input.Mouse.MouseManager}
* @since 3.0.0
*/
this.mouse = (config.inputMouse) ? new Mouse(this) : null;
/**
* A reference to the Touch Manager class, if enabled via the `input.touch` Game Config property.
*
* @name Phaser.Input.InputManager#touch
* @type {Phaser.Input.Touch.TouchManager}
* @since 3.0.0
*/
this.touch = (config.inputTouch) ? new Touch(this) : null;
/**
* An array of Pointers that have been added to the game.
* The first entry is reserved for the Mouse Pointer, the rest are Touch Pointers.
*
* By default there is 1 touch pointer enabled. If you need more use the `addPointer` method to start them,
* or set the `input.activePointers` property in the Game Config.
*
* @name Phaser.Input.InputManager#pointers
* @type {Phaser.Input.Pointer[]}
* @since 3.10.0
*/
this.pointers = [];
/**
* The number of touch objects activated and being processed each update.
*
* You can change this by either calling `addPointer` at run-time, or by
* setting the `input.activePointers` property in the Game Config.
*
* @name Phaser.Input.InputManager#pointersTotal
* @type {number}
* @readonly
* @since 3.10.0
*/
this.pointersTotal = config.inputActivePointers;
if (config.inputTouch && this.pointersTotal === 1)
{
this.pointersTotal = 2;
}
for (var i = 0; i <= this.pointersTotal; i++)
{
var pointer = new Pointer(this, i);
pointer.smoothFactor = config.inputSmoothFactor;
this.pointers.push(pointer);
}
/**
* The mouse has its own unique Pointer object, which you can reference directly if making a _desktop specific game_.
* If you are supporting both desktop and touch devices then do not use this property, instead use `activePointer`
* which will always map to the most recently interacted pointer.
*
* @name Phaser.Input.InputManager#mousePointer
* @type {?Phaser.Input.Pointer}
* @since 3.10.0
*/
this.mousePointer = (config.inputMouse) ? this.pointers[0] : null;
/**
* The most recently active Pointer object.
*
* If you've only 1 Pointer in your game then this will accurately be either the first finger touched, or the mouse.
*
* If your game doesn't need to support multi-touch then you can safely use this property in all of your game
* code and it will adapt to be either the mouse or the touch, based on device.
*
* @name Phaser.Input.InputManager#activePointer
* @type {Phaser.Input.Pointer}
* @since 3.0.0
*/
this.activePointer = this.pointers[0];
/**
* If the top-most Scene in the Scene List receives an input it will stop input from
* propagating any lower down the scene list, i.e. if you have a UI Scene at the top
* and click something on it, that click will not then be passed down to any other
* Scene below. Disable this to have input events passed through all Scenes, all the time.
*
* @name Phaser.Input.InputManager#globalTopOnly
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.globalTopOnly = true;
/**
* The time this Input Manager was last updated.
* This value is populated by the Game Step each frame.
*
* @name Phaser.Input.InputManager#time
* @type {number}
* @readonly
* @since 3.16.2
*/
this.time = 0;
/**
* A re-cycled point-like object to store hit test values in.
*
* @name Phaser.Input.InputManager#_tempPoint
* @type {{x:number, y:number}}
* @private
* @since 3.0.0
*/
this._tempPoint = { x: 0, y: 0 };
/**
* A re-cycled array to store hit results in.
*
* @name Phaser.Input.InputManager#_tempHitTest
* @type {array}
* @private
* @default []
* @since 3.0.0
*/
this._tempHitTest = [];
/**
* A re-cycled matrix used in hit test calculations.
*
* @name Phaser.Input.InputManager#_tempMatrix
* @type {Phaser.GameObjects.Components.TransformMatrix}
* @private
* @since 3.4.0
*/
this._tempMatrix = new TransformMatrix();
/**
* A re-cycled matrix used in hit test calculations.
*
* @name Phaser.Input.InputManager#_tempMatrix2
* @type {Phaser.GameObjects.Components.TransformMatrix}
* @private
* @since 3.12.0
*/
this._tempMatrix2 = new TransformMatrix();
/**
* An internal private var that records Scenes aborting event processing.
*
* @name Phaser.Input.InputManager#_tempSkip
* @type {boolean}
* @private
* @since 3.18.0
*/
this._tempSkip = false;
/**
* An internal private array that avoids needing to create a new array on every DOM mouse event.
*
* @name Phaser.Input.InputManager#mousePointerContainer
* @type {Phaser.Input.Pointer[]}
* @private
* @since 3.18.0
*/
this.mousePointerContainer = [ this.mousePointer ];
game.events.once(GameEvents.BOOT, this.boot, this);
},
/**
* The Boot handler is called by Phaser.Game when it first starts up.
* The renderer is available by now.
*
* @method Phaser.Input.InputManager#boot
* @protected
* @fires Phaser.Input.Events#MANAGER_BOOT
* @since 3.0.0
*/
boot: function ()
{
this.canvas = this.game.canvas;
this.scaleManager = this.game.scale;
this.events.emit(Events.MANAGER_BOOT);
this.game.events.on(GameEvents.PRE_RENDER, this.preRender, this);
this.game.events.once(GameEvents.DESTROY, this.destroy, this);
},
/**
* Internal canvas state change, called automatically by the Mouse Manager.
*
* @method Phaser.Input.InputManager#setCanvasOver
* @fires Phaser.Input.Events#GAME_OVER
* @private
* @since 3.16.0
*
* @param {(MouseEvent|TouchEvent)} event - The DOM Event.
*/
setCanvasOver: function (event)
{
this.isOver = true;
this.events.emit(Events.GAME_OVER, event);
},
/**
* Internal canvas state change, called automatically by the Mouse Manager.
*
* @method Phaser.Input.InputManager#setCanvasOut
* @fires Phaser.Input.Events#GAME_OUT
* @private
* @since 3.16.0
*
* @param {(MouseEvent|TouchEvent)} event - The DOM Event.
*/
setCanvasOut: function (event)
{
this.isOver = false;
this.events.emit(Events.GAME_OUT, event);
},
/**
* Internal update, called automatically by the Game Step right at the start.
*
* @method Phaser.Input.InputManager#preRender
* @private
* @since 3.18.0
*/
preRender: function ()
{
var time = this.game.loop.now;
var delta = this.game.loop.delta;
var scenes = this.game.scene.getScenes(true, true);
this.time = time;
this.events.emit(Events.MANAGER_UPDATE);
for (var i = 0; i < scenes.length; i++)
{
var scene = scenes[i];
if (scene.sys.input && scene.sys.input.updatePoll(time, delta) && this.globalTopOnly)
{
// If the Scene returns true, it means it captured some input that no other Scene should get, so we bail out
return;
}
}
},
/**
* Tells the Input system to set a custom cursor.
*
* This cursor will be the default cursor used when interacting with the game canvas.
*
* If an Interactive Object also sets a custom cursor, this is the cursor that is reset after its use.
*
* Any valid CSS cursor value is allowed, including paths to image files, i.e.:
*
* ```javascript
* this.input.setDefaultCursor('url(assets/cursors/sword.cur), pointer');
* ```
*
* Please read about the differences between browsers when it comes to the file formats and sizes they support:
*
* https://developer.mozilla.org/en-US/docs/Web/CSS/cursor
* https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_User_Interface/Using_URL_values_for_the_cursor_property
*
* It's up to you to pick a suitable cursor format that works across the range of browsers you need to support.
*
* @method Phaser.Input.InputManager#setDefaultCursor
* @since 3.10.0
*
* @param {string} cursor - The CSS to be used when setting the default cursor.
*/
setDefaultCursor: function (cursor)
{
this.defaultCursor = cursor;
if (this.canvas.style.cursor !== cursor)
{
this.canvas.style.cursor = cursor;
}
},
/**
* Called by the InputPlugin when processing over and out events.
*
* Tells the Input Manager to set a custom cursor during its postUpdate step.
*
* https://developer.mozilla.org/en-US/docs/Web/CSS/cursor
*
* @method Phaser.Input.InputManager#setCursor
* @private
* @since 3.10.0
*
* @param {Phaser.Types.Input.InteractiveObject} interactiveObject - The Interactive Object that called this method.
*/
setCursor: function (interactiveObject)
{
if (interactiveObject.cursor)
{
this.canvas.style.cursor = interactiveObject.cursor;
}
},
/**
* Called by the InputPlugin when processing over and out events.
*
* Tells the Input Manager to clear the hand cursor, if set, during its postUpdate step.
*
* @method Phaser.Input.InputManager#resetCursor
* @private
* @since 3.10.0
*
* @param {Phaser.Types.Input.InteractiveObject} interactiveObject - The Interactive Object that called this method.
*/
resetCursor: function (interactiveObject)
{
if (interactiveObject.cursor && this.canvas)
{
this.canvas.style.cursor = this.defaultCursor;
}
},
/**
* Adds new Pointer objects to the Input Manager.
*
* By default Phaser creates 2 pointer objects: `mousePointer` and `pointer1`.
*
* You can create more either by calling this method, or by setting the `input.activePointers` property
* in the Game Config, up to a maximum of 10 pointers.
*
* The first 10 pointers are available via the `InputPlugin.pointerX` properties, once they have been added
* via this method.
*
* @method Phaser.Input.InputManager#addPointer
* @since 3.10.0
*
* @param {number} [quantity=1] The number of new Pointers to create. A maximum of 10 is allowed in total.
*
* @return {Phaser.Input.Pointer[]} An array containing all of the new Pointer objects that were created.
*/
addPointer: function (quantity)
{
if (quantity === undefined) { quantity = 1; }
var output = [];
if (this.pointersTotal + quantity > 10)
{
quantity = 10 - this.pointersTotal;
}
for (var i = 0; i < quantity; i++)
{
var id = this.pointers.length;
var pointer = new Pointer(this, id);
pointer.smoothFactor = this.config.inputSmoothFactor;
this.pointers.push(pointer);
this.pointersTotal++;
output.push(pointer);
}
return output;
},
/**
* Internal method that gets a list of all the active Input Plugins in the game
* and updates each of them in turn, in reverse order (top to bottom), to allow
* for DOM top-level event handling simulation.
*
* @method Phaser.Input.InputManager#updateInputPlugins
* @since 3.16.0
*
* @param {number} type - The type of event to process.
* @param {Phaser.Input.Pointer[]} pointers - An array of Pointers on which the event occurred.
*/
updateInputPlugins: function (type, pointers)
{
var scenes = this.game.scene.getScenes(true, true);
this._tempSkip = false;
for (var i = 0; i < scenes.length; i++)
{
var scene = scenes[i];
if (scene.sys.input)
{
var capture = scene.sys.input.update(type, pointers);
if ((capture && this.globalTopOnly) || this._tempSkip)
{
// If the Scene returns true, or called stopPropagation, it means it captured some input that no other Scene should get, so we bail out
return;
}
}
}
},
// event.targetTouches = list of all touches on the TARGET ELEMENT (i.e. game dom element)
// event.touches = list of all touches on the ENTIRE DOCUMENT, not just the target element
// event.changedTouches = the touches that CHANGED in this event, not the total number of them
/**
* Processes a touch start event, as passed in by the TouchManager.
*
* @method Phaser.Input.InputManager#onTouchStart
* @private
* @since 3.18.0
*
* @param {TouchEvent} event - The native DOM Touch event.
*/
onTouchStart: function (event)
{
var pointers = this.pointers;
var changed = [];
for (var c = 0; c < event.changedTouches.length; c++)
{
var changedTouch = event.changedTouches[c];
for (var i = 1; i < this.pointersTotal; i++)
{
var pointer = pointers[i];
if (!pointer.active)
{
pointer.touchstart(changedTouch, event);
this.activePointer = pointer;
changed.push(pointer);
break;
}
}
}
this.updateInputPlugins(CONST.TOUCH_START, changed);
},
/**
* Processes a touch move event, as passed in by the TouchManager.
*
* @method Phaser.Input.InputManager#onTouchMove
* @private
* @since 3.18.0
*
* @param {TouchEvent} event - The native DOM Touch event.
*/
onTouchMove: function (event)
{
var pointers = this.pointers;
var changed = [];
for (var c = 0; c < event.changedTouches.length; c++)
{
var changedTouch = event.changedTouches[c];
for (var i = 1; i < this.pointersTotal; i++)
{
var pointer = pointers[i];
if (pointer.active && pointer.identifier === changedTouch.identifier)
{
pointer.touchmove(changedTouch, event);
this.activePointer = pointer;
changed.push(pointer);
break;
}
}
}
this.updateInputPlugins(CONST.TOUCH_MOVE, changed);
},
// For touch end its a list of the touch points that have been removed from the surface
// https://developer.mozilla.org/en-US/docs/DOM/TouchList
// event.changedTouches = the touches that CHANGED in this event, not the total number of them
/**
* Processes a touch end event, as passed in by the TouchManager.
*
* @method Phaser.Input.InputManager#onTouchEnd
* @private
* @since 3.18.0
*
* @param {TouchEvent} event - The native DOM Touch event.
*/
onTouchEnd: function (event)
{
var pointers = this.pointers;
var changed = [];
for (var c = 0; c < event.changedTouches.length; c++)
{
var changedTouch = event.changedTouches[c];
for (var i = 1; i < this.pointersTotal; i++)
{
var pointer = pointers[i];
if (pointer.active && pointer.identifier === changedTouch.identifier)
{
pointer.touchend(changedTouch, event);
changed.push(pointer);
break;
}
}
}
this.updateInputPlugins(CONST.TOUCH_END, changed);
},
/**
* Processes a touch cancel event, as passed in by the TouchManager.
*
* @method Phaser.Input.InputManager#onTouchCancel
* @private
* @since 3.18.0
*
* @param {TouchEvent} event - The native DOM Touch event.
*/
onTouchCancel: function (event)
{
var pointers = this.pointers;
var changed = [];
for (var c = 0; c < event.changedTouches.length; c++)
{
var changedTouch = event.changedTouches[c];
for (var i = 1; i < this.pointersTotal; i++)
{
var pointer = pointers[i];
if (pointer.active && pointer.identifier === changedTouch.identifier)
{
pointer.touchcancel(changedTouch, event);
changed.push(pointer);
break;
}
}
}
this.updateInputPlugins(CONST.TOUCH_CANCEL, changed);
},
/**
* Processes a mouse down event, as passed in by the MouseManager.
*
* @method Phaser.Input.InputManager#onMouseDown
* @private
* @since 3.18.0
*
* @param {MouseEvent} event - The native DOM Mouse event.
*/
onMouseDown: function (event)
{
var mousePointer = this.mousePointer;
mousePointer.down(event);
mousePointer.updateMotion();
this.activePointer = mousePointer;
this.updateInputPlugins(CONST.MOUSE_DOWN, this.mousePointerContainer);
},
/**
* Processes a mouse move event, as passed in by the MouseManager.
*
* @method Phaser.Input.InputManager#onMouseMove
* @private
* @since 3.18.0
*
* @param {MouseEvent} event - The native DOM Mouse event.
*/
onMouseMove: function (event)
{
var mousePointer = this.mousePointer;
mousePointer.move(event);
mousePointer.updateMotion();
this.activePointer = mousePointer;
this.updateInputPlugins(CONST.MOUSE_MOVE, this.mousePointerContainer);
},
/**
* Processes a mouse up event, as passed in by the MouseManager.
*
* @method Phaser.Input.InputManager#onMouseUp
* @private
* @since 3.18.0
*
* @param {MouseEvent} event - The native DOM Mouse event.
*/
onMouseUp: function (event)
{
var mousePointer = this.mousePointer;
mousePointer.up(event);
mousePointer.updateMotion();
this.activePointer = mousePointer;
this.updateInputPlugins(CONST.MOUSE_UP, this.mousePointerContainer);
},
/**
* Processes a mouse wheel event, as passed in by the MouseManager.
*
* @method Phaser.Input.InputManager#onMouseWheel
* @private
* @since 3.18.0
*
* @param {WheelEvent} event - The native DOM Wheel event.
*/
onMouseWheel: function (event)
{
var mousePointer = this.mousePointer;
mousePointer.wheel(event);
this.activePointer = mousePointer;
this.updateInputPlugins(CONST.MOUSE_WHEEL, this.mousePointerContainer);
},
/**
* Processes a pointer lock change event, as passed in by the MouseManager.
*
* @method Phaser.Input.InputManager#onPointerLockChange
* @fires Phaser.Input.Events#POINTERLOCK_CHANGE
* @private
* @since 3.19.0
*
* @param {MouseEvent} event - The native DOM Mouse event.
*/
onPointerLockChange: function (event)
{
var isLocked = this.mouse.locked;
this.mousePointer.locked = isLocked;
this.events.emit(Events.POINTERLOCK_CHANGE, event, isLocked);
},
/**
* Checks if the given Game Object should be considered as a candidate for input or not.
*
* Checks if the Game Object has an input component that is enabled, that it will render,
* and finally, if it has a parent, that the parent parent, or any ancestor, is visible or not.
*
* @method Phaser.Input.InputManager#inputCandidate
* @private
* @since 3.10.0
*
* @param {Phaser.GameObjects.GameObject} gameObject - The Game Object to test.
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera which is being tested against.
*
* @return {boolean} `true` if the Game Object should be considered for input, otherwise `false`.
*/
inputCandidate: function (gameObject, camera)
{
var input = gameObject.input;
if (!input || !input.enabled || (!input.alwaysEnabled && !gameObject.willRender(camera)))
{
return false;
}
var visible = true;
var parent = gameObject.parentContainer;
if (parent)
{
do
{
if (!parent.willRender(camera))
{
visible = false;
break;
}
parent = parent.parentContainer;
} while (parent);
}
return visible;
},
/**
* Performs a hit test using the given Pointer and camera, against an array of interactive Game Objects.
*
* The Game Objects are culled against the camera, and then the coordinates are translated into the local camera space
* and used to determine if they fall within the remaining Game Objects hit areas or not.
*
* If nothing is matched an empty array is returned.
*
* This method is called automatically by InputPlugin.hitTestPointer and doesn't usually need to be invoked directly.
*
* @method Phaser.Input.InputManager#hitTest
* @since 3.0.0
*
* @param {Phaser.Input.Pointer} pointer - The Pointer to test against.
* @param {array} gameObjects - An array of interactive Game Objects to check.
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera which is being tested against.
* @param {array} [output] - An array to store the results in. If not given, a new empty array is created.
*
* @return {array} An array of the Game Objects that were hit during this hit test.
*/
hitTest: function (pointer, gameObjects, camera, output)
{
if (output === undefined) { output = this._tempHitTest; }
var tempPoint = this._tempPoint;
var csx = camera.scrollX;
var csy = camera.scrollY;
output.length = 0;
var x = pointer.x;
var y = pointer.y;
// Stores the world point inside of tempPoint
camera.getWorldPoint(x, y, tempPoint);
pointer.worldX = tempPoint.x;
pointer.worldY = tempPoint.y;
var point = { x: 0, y: 0 };
var matrix = this._tempMatrix;
var parentMatrix = this._tempMatrix2;
for (var i = 0; i < gameObjects.length; i++)
{
var gameObject = gameObjects[i];
// Checks if the Game Object can receive input (isn't being ignored by the camera, invisible, etc)
// and also checks all of its parents, if any
if (!this.inputCandidate(gameObject, camera))
{
continue;
}
var px = tempPoint.x + (csx * gameObject.scrollFactorX) - csx;
var py = tempPoint.y + (csy * gameObject.scrollFactorY) - csy;
if (gameObject.parentContainer)
{
gameObject.getWorldTransformMatrix(matrix, parentMatrix);
matrix.applyInverse(px, py, point);
}
else
{
TransformXY(px, py, gameObject.x, gameObject.y, gameObject.rotation, gameObject.scaleX, gameObject.scaleY, point);
}
if (this.pointWithinHitArea(gameObject, point.x, point.y))
{
output.push(gameObject);
}
}
return output;
},
/**
* Checks if the given x and y coordinate are within the hit area of the Game Object.
*
* This method assumes that the coordinate values have already been translated into the space of the Game Object.
*
* If the coordinates are within the hit area they are set into the Game Objects Input `localX` and `localY` properties.
*
* @method Phaser.Input.InputManager#pointWithinHitArea
* @since 3.0.0
*
* @param {Phaser.GameObjects.GameObject} gameObject - The interactive Game Object to check against.
* @param {number} x - The translated x coordinate for the hit test.
* @param {number} y - The translated y coordinate for the hit test.
*
* @return {boolean} `true` if the coordinates were inside the Game Objects hit area, otherwise `false`.
*/
pointWithinHitArea: function (gameObject, x, y)
{
// Normalize the origin
x += gameObject.displayOriginX;
y += gameObject.displayOriginY;
var input = gameObject.input;
if (input && input.hitAreaCallback(input.hitArea, x, y, gameObject))
{
input.localX = x;
input.localY = y;
return true;
}
else
{
return false;
}
},
/**
* Checks if the given x and y coordinate are within the hit area of the Interactive Object.
*
* This method assumes that the coordinate values have already been translated into the space of the Interactive Object.
*
* If the coordinates are within the hit area they are set into the Interactive Objects Input `localX` and `localY` properties.
*
* @method Phaser.Input.InputManager#pointWithinInteractiveObject
* @since 3.0.0
*
* @param {Phaser.Types.Input.InteractiveObject} object - The Interactive Object to check against.
* @param {number} x - The translated x coordinate for the hit test.
* @param {number} y - The translated y coordinate for the hit test.
*
* @return {boolean} `true` if the coordinates were inside the Game Objects hit area, otherwise `false`.
*/
pointWithinInteractiveObject: function (object, x, y)
{
if (!object.hitArea)
{
return false;
}
// Normalize the origin
x += object.gameObject.displayOriginX;
y += object.gameObject.displayOriginY;
object.localX = x;
object.localY = y;
return object.hitAreaCallback(object.hitArea, x, y, object);