/
InputPlugin.js
3205 lines (2692 loc) · 99.8 KB
/
InputPlugin.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 Circle = require('../geom/circle/Circle');
var CircleContains = require('../geom/circle/Contains');
var Class = require('../utils/Class');
var CONST = require('./const');
var CreateInteractiveObject = require('./CreateInteractiveObject');
var CreatePixelPerfectHandler = require('./CreatePixelPerfectHandler');
var DistanceBetween = require('../math/distance/DistanceBetween');
var Ellipse = require('../geom/ellipse/Ellipse');
var EllipseContains = require('../geom/ellipse/Contains');
var Events = require('./events');
var EventEmitter = require('eventemitter3');
var GetFastValue = require('../utils/object/GetFastValue');
var GEOM_CONST = require('../geom/const');
var InputPluginCache = require('./InputPluginCache');
var IsPlainObject = require('../utils/object/IsPlainObject');
var PluginCache = require('../plugins/PluginCache');
var Rectangle = require('../geom/rectangle/Rectangle');
var RectangleContains = require('../geom/rectangle/Contains');
var SceneEvents = require('../scene/events');
var Triangle = require('../geom/triangle/Triangle');
var TriangleContains = require('../geom/triangle/Contains');
/**
* @classdesc
* The Input Plugin belongs to a Scene and handles all input related events and operations for it.
*
* You can access it from within a Scene using `this.input`.
*
* It emits events directly. For example, you can do:
*
* ```javascript
* this.input.on('pointerdown', callback, context);
* ```
*
* To listen for a pointer down event anywhere on the game canvas.
*
* Game Objects can be enabled for input by calling their `setInteractive` method. After which they
* will directly emit input events:
*
* ```javascript
* var sprite = this.add.sprite(x, y, texture);
* sprite.setInteractive();
* sprite.on('pointerdown', callback, context);
* ```
*
* There are lots of game configuration options available relating to input.
* See the [Input Config object]{@linkcode Phaser.Types.Core.InputConfig} for more details, including how to deal with Phaser
* listening for input events outside of the canvas, how to set a default number of pointers, input
* capture settings and more.
*
* Please also see the Input examples and tutorials for further information.
*
* **Incorrect input coordinates with Angular**
*
* If you are using Phaser within Angular, and use nglf or the router, to make the component in which the Phaser game resides
* change state (i.e. appear or disappear) then you'll need to notify the Scale Manager about this, as Angular will mess with
* the DOM in a way in which Phaser can't detect directly. Call `this.scale.updateBounds()` as part of your game init in order
* to refresh the canvas DOM bounds values, which Phaser uses for input point position calculations.
*
* @class InputPlugin
* @extends Phaser.Events.EventEmitter
* @memberof Phaser.Input
* @constructor
* @since 3.0.0
*
* @param {Phaser.Scene} scene - A reference to the Scene that this Input Plugin is responsible for.
*/
var InputPlugin = new Class({
Extends: EventEmitter,
initialize:
function InputPlugin (scene)
{
EventEmitter.call(this);
/**
* A reference to the Scene that this Input Plugin is responsible for.
*
* @name Phaser.Input.InputPlugin#scene
* @type {Phaser.Scene}
* @since 3.0.0
*/
this.scene = scene;
/**
* A reference to the Scene Systems class.
*
* @name Phaser.Input.InputPlugin#systems
* @type {Phaser.Scenes.Systems}
* @since 3.0.0
*/
this.systems = scene.sys;
/**
* A reference to the Scene Systems Settings.
*
* @name Phaser.Input.InputPlugin#settings
* @type {Phaser.Types.Scenes.SettingsObject}
* @since 3.5.0
*/
this.settings = scene.sys.settings;
/**
* A reference to the Game Input Manager.
*
* @name Phaser.Input.InputPlugin#manager
* @type {Phaser.Input.InputManager}
* @since 3.0.0
*/
this.manager = scene.sys.game.input;
/**
* Internal event queue used for plugins only.
*
* @name Phaser.Input.InputPlugin#pluginEvents
* @type {Phaser.Events.EventEmitter}
* @private
* @since 3.10.0
*/
this.pluginEvents = new EventEmitter();
/**
* If `true` this Input Plugin will process DOM input events.
*
* @name Phaser.Input.InputPlugin#enabled
* @type {boolean}
* @default true
* @since 3.5.0
*/
this.enabled = true;
/**
* A reference to the Scene Display List. This property is set during the `boot` method.
*
* @name Phaser.Input.InputPlugin#displayList
* @type {Phaser.GameObjects.DisplayList}
* @since 3.0.0
*/
this.displayList;
/**
* A reference to the Scene Cameras Manager. This property is set during the `boot` method.
*
* @name Phaser.Input.InputPlugin#cameras
* @type {Phaser.Cameras.Scene2D.CameraManager}
* @since 3.0.0
*/
this.cameras;
// Inject the available input plugins into this class
InputPluginCache.install(this);
/**
* A reference to the Mouse Manager.
*
* This property is only set if Mouse support has been enabled in your Game Configuration file.
*
* If you just wish to get access to the mouse pointer, use the `mousePointer` property instead.
*
* @name Phaser.Input.InputPlugin#mouse
* @type {?Phaser.Input.Mouse.MouseManager}
* @since 3.0.0
*/
this.mouse = this.manager.mouse;
/**
* When set to `true` (the default) the Input Plugin will emulate DOM behavior by only emitting events from
* the top-most Game Objects in the Display List.
*
* If set to `false` it will emit events from all Game Objects below a Pointer, not just the top one.
*
* @name Phaser.Input.InputPlugin#topOnly
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.topOnly = true;
/**
* How often should the Pointers be checked?
*
* The value is a time, given in ms, and is the time that must have elapsed between game steps before
* the Pointers will be polled again. When a pointer is polled it runs a hit test to see which Game
* Objects are currently below it, or being interacted with it.
*
* Pointers will *always* be checked if they have been moved by the user, or press or released.
*
* This property only controls how often they will be polled if they have not been updated.
* You should set this if you want to have Game Objects constantly check against the pointers, even
* if the pointer didn't itself move.
*
* Set to 0 to poll constantly. Set to -1 to only poll on user movement.
*
* @name Phaser.Input.InputPlugin#pollRate
* @type {number}
* @default -1
* @since 3.0.0
*/
this.pollRate = -1;
/**
* Internal poll timer value.
*
* @name Phaser.Input.InputPlugin#_pollTimer
* @type {number}
* @private
* @default 0
* @since 3.0.0
*/
this._pollTimer = 0;
var _eventData = { cancelled: false };
/**
* Internal event propagation callback container.
*
* @name Phaser.Input.InputPlugin#_eventContainer
* @type {Phaser.Types.Input.EventData}
* @private
* @since 3.13.0
*/
this._eventContainer = {
stopPropagation: function ()
{
_eventData.cancelled = true;
}
};
/**
* Internal event propagation data object.
*
* @name Phaser.Input.InputPlugin#_eventData
* @type {object}
* @private
* @since 3.13.0
*/
this._eventData = _eventData;
/**
* The distance, in pixels, a pointer has to move while being held down, before it thinks it is being dragged.
*
* @name Phaser.Input.InputPlugin#dragDistanceThreshold
* @type {number}
* @default 0
* @since 3.0.0
*/
this.dragDistanceThreshold = 0;
/**
* The amount of time, in ms, a pointer has to be held down before it thinks it is dragging.
*
* The default polling rate is to poll only on move so once the time threshold is reached the
* drag event will not start until you move the mouse. If you want it to start immediately
* when the time threshold is reached, you must increase the polling rate by calling
* [setPollAlways]{@linkcode Phaser.Input.InputPlugin#setPollAlways} or
* [setPollRate]{@linkcode Phaser.Input.InputPlugin#setPollRate}.
*
* @name Phaser.Input.InputPlugin#dragTimeThreshold
* @type {number}
* @default 0
* @since 3.0.0
*/
this.dragTimeThreshold = 0;
/**
* Used to temporarily store the results of the Hit Test
*
* @name Phaser.Input.InputPlugin#_temp
* @type {array}
* @private
* @default []
* @since 3.0.0
*/
this._temp = [];
/**
* Used to temporarily store the results of the Hit Test dropZones
*
* @name Phaser.Input.InputPlugin#_tempZones
* @type {array}
* @private
* @default []
* @since 3.0.0
*/
this._tempZones = [];
/**
* A list of all Game Objects that have been set to be interactive in the Scene this Input Plugin is managing.
*
* @name Phaser.Input.InputPlugin#_list
* @type {Phaser.GameObjects.GameObject[]}
* @private
* @default []
* @since 3.0.0
*/
this._list = [];
/**
* Objects waiting to be inserted to the list on the next call to 'begin'.
*
* @name Phaser.Input.InputPlugin#_pendingInsertion
* @type {Phaser.GameObjects.GameObject[]}
* @private
* @default []
* @since 3.0.0
*/
this._pendingInsertion = [];
/**
* Objects waiting to be removed from the list on the next call to 'begin'.
*
* @name Phaser.Input.InputPlugin#_pendingRemoval
* @type {Phaser.GameObjects.GameObject[]}
* @private
* @default []
* @since 3.0.0
*/
this._pendingRemoval = [];
/**
* A list of all Game Objects that have been enabled for dragging.
*
* @name Phaser.Input.InputPlugin#_draggable
* @type {Phaser.GameObjects.GameObject[]}
* @private
* @default []
* @since 3.0.0
*/
this._draggable = [];
/**
* A list of all Interactive Objects currently considered as being 'draggable' by any pointer, indexed by pointer ID.
*
* @name Phaser.Input.InputPlugin#_drag
* @type {{0:Array,1:Array,2:Array,3:Array,4:Array,5:Array,6:Array,7:Array,8:Array,9:Array,10:Array}}
* @private
* @since 3.0.0
*/
this._drag = { 0: [], 1: [], 2: [], 3: [], 4: [], 5: [], 6: [], 7: [], 8: [], 9: [], 10: [] };
/**
* A array containing the dragStates, for this Scene, index by the Pointer ID.
*
* @name Phaser.Input.InputPlugin#_dragState
* @type {number[]}
* @private
* @since 3.16.0
*/
this._dragState = [];
/**
* A list of all Interactive Objects currently considered as being 'over' by any pointer, indexed by pointer ID.
*
* @name Phaser.Input.InputPlugin#_over
* @type {{0:Array,1:Array,2:Array,3:Array,4:Array,5:Array,6:Array,7:Array,8:Array,9:Array,10:Array}}
* @private
* @since 3.0.0
*/
this._over = { 0: [], 1: [], 2: [], 3: [], 4: [], 5: [], 6: [], 7: [], 8: [], 9: [], 10: [] };
/**
* A list of valid DOM event types.
*
* @name Phaser.Input.InputPlugin#_validTypes
* @type {string[]}
* @private
* @since 3.0.0
*/
this._validTypes = [ 'onDown', 'onUp', 'onOver', 'onOut', 'onMove', 'onDragStart', 'onDrag', 'onDragEnd', 'onDragEnter', 'onDragLeave', 'onDragOver', 'onDrop' ];
/**
* Internal property that tracks frame event state.
*
* @name Phaser.Input.InputPlugin#_updatedThisFrame
* @type {boolean}
* @private
* @since 3.18.0
*/
this._updatedThisFrame = false;
scene.sys.events.once(SceneEvents.BOOT, this.boot, this);
scene.sys.events.on(SceneEvents.START, this.start, this);
},
/**
* This method is called automatically, only once, when the Scene is first created.
* Do not invoke it directly.
*
* @method Phaser.Input.InputPlugin#boot
* @fires Phaser.Input.Events#BOOT
* @private
* @since 3.5.1
*/
boot: function ()
{
this.cameras = this.systems.cameras;
this.displayList = this.systems.displayList;
this.systems.events.once(SceneEvents.DESTROY, this.destroy, this);
// Registered input plugins listen for this
this.pluginEvents.emit(Events.BOOT);
},
/**
* This method is called automatically by the Scene when it is starting up.
* It is responsible for creating local systems, properties and listening for Scene events.
* Do not invoke it directly.
*
* @method Phaser.Input.InputPlugin#start
* @fires Phaser.Input.Events#START
* @private
* @since 3.5.0
*/
start: function ()
{
var eventEmitter = this.systems.events;
eventEmitter.on(SceneEvents.TRANSITION_START, this.transitionIn, this);
eventEmitter.on(SceneEvents.TRANSITION_OUT, this.transitionOut, this);
eventEmitter.on(SceneEvents.TRANSITION_COMPLETE, this.transitionComplete, this);
eventEmitter.on(SceneEvents.PRE_UPDATE, this.preUpdate, this);
eventEmitter.once(SceneEvents.SHUTDOWN, this.shutdown, this);
this.manager.events.on(Events.GAME_OUT, this.onGameOut, this);
this.manager.events.on(Events.GAME_OVER, this.onGameOver, this);
this.enabled = true;
// Populate the pointer drag states
this._dragState = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ];
// Registered input plugins listen for this
this.pluginEvents.emit(Events.START);
},
/**
* Game Over handler.
*
* @method Phaser.Input.InputPlugin#onGameOver
* @fires Phaser.Input.Events#GAME_OVER
* @private
* @since 3.16.2
*/
onGameOver: function (event)
{
if (this.isActive())
{
this.emit(Events.GAME_OVER, event.timeStamp, event);
}
},
/**
* Game Out handler.
*
* @method Phaser.Input.InputPlugin#onGameOut
* @fires Phaser.Input.Events#GAME_OUT
* @private
* @since 3.16.2
*/
onGameOut: function (event)
{
if (this.isActive())
{
this.emit(Events.GAME_OUT, event.timeStamp, event);
}
},
/**
* The pre-update handler is responsible for checking the pending removal and insertion lists and
* deleting old Game Objects.
*
* @method Phaser.Input.InputPlugin#preUpdate
* @private
* @fires Phaser.Input.Events#PRE_UPDATE
* @since 3.0.0
*/
preUpdate: function ()
{
// Registered input plugins listen for this
this.pluginEvents.emit(Events.PRE_UPDATE);
var removeList = this._pendingRemoval;
var insertList = this._pendingInsertion;
var toRemove = removeList.length;
var toInsert = insertList.length;
if (toRemove === 0 && toInsert === 0)
{
// Quick bail
return;
}
var current = this._list;
// Delete old gameObjects
for (var i = 0; i < toRemove; i++)
{
var gameObject = removeList[i];
var index = current.indexOf(gameObject);
if (index > -1)
{
current.splice(index, 1);
this.clear(gameObject, true);
}
}
// Clear the removal list
removeList.length = 0;
this._pendingRemoval.length = 0;
// Move pendingInsertion to list (also clears pendingInsertion at the same time)
this._list = current.concat(insertList.splice(0));
},
/**
* Checks to see if both this plugin and the Scene to which it belongs is active.
*
* @method Phaser.Input.InputPlugin#isActive
* @since 3.10.0
*
* @return {boolean} `true` if the plugin and the Scene it belongs to is active.
*/
isActive: function ()
{
return (this.enabled && this.scene.sys.isActive());
},
/**
* This is called automatically by the Input Manager.
* It emits events for plugins to listen to and also handles polling updates, if enabled.
*
* @method Phaser.Input.InputPlugin#updatePoll
* @since 3.18.0
*
* @param {number} time - The current time. Either a High Resolution Timer value if it comes from Request Animation Frame, or Date.now if using SetTimeout.
* @param {number} delta - The delta time in ms since the last frame. This is a smoothed and capped value based on the FPS rate.
*
* @return {boolean} `true` if the plugin and the Scene it belongs to is active.
*/
updatePoll: function (time, delta)
{
if (!this.isActive())
{
return false;
}
// The plugins should update every frame, regardless if there has been
// any DOM input events or not (such as the Gamepad and Keyboard)
this.pluginEvents.emit(Events.UPDATE, time, delta);
// We can leave now if we've already updated once this frame via the immediate DOM event handlers
if (this._updatedThisFrame)
{
this._updatedThisFrame = false;
return false;
}
var i;
var manager = this.manager;
var pointers = manager.pointers;
var pointersTotal = manager.pointersTotal;
for (i = 0; i < pointersTotal; i++)
{
pointers[i].updateMotion();
}
// No point going any further if there aren't any interactive objects
if (this._list.length === 0)
{
return false;
}
var rate = this.pollRate;
if (rate === -1)
{
return false;
}
else if (rate > 0)
{
this._pollTimer -= delta;
if (this._pollTimer < 0)
{
// Discard timer diff, we're ready to poll again
this._pollTimer = this.pollRate;
}
else
{
// Not enough time has elapsed since the last poll, so abort now
return false;
}
}
// We got this far? Then we should poll for movement
var captured = false;
for (i = 0; i < pointersTotal; i++)
{
var total = 0;
var pointer = pointers[i];
// Always reset this array
this._tempZones = [];
// _temp contains a hit tested and camera culled list of IO objects
this._temp = this.hitTestPointer(pointer);
this.sortGameObjects(this._temp, pointer);
this.sortDropZones(this._tempZones);
if (this.topOnly)
{
// Only the top-most one counts now, so safely ignore the rest
if (this._temp.length)
{
this._temp.splice(1);
}
if (this._tempZones.length)
{
this._tempZones.splice(1);
}
}
total += this.processOverOutEvents(pointer);
if (this.getDragState(pointer) === 2)
{
this.processDragThresholdEvent(pointer, time);
}
if (total > 0)
{
// We interacted with an event in this Scene, so block any Scenes below us from doing the same this frame
captured = true;
}
}
return captured;
},
/**
* This method is called when a DOM Event is received by the Input Manager. It handles dispatching the events
* to relevant input enabled Game Objects in this scene.
*
* @method Phaser.Input.InputPlugin#update
* @private
* @fires Phaser.Input.Events#UPDATE
* @since 3.0.0
*
* @param {number} type - The type of event to process.
* @param {Phaser.Input.Pointer[]} pointers - An array of Pointers on which the event occurred.
*
* @return {boolean} `true` if this Scene has captured the input events from all other Scenes, otherwise `false`.
*/
update: function (type, pointers)
{
if (!this.isActive())
{
return false;
}
var pointersTotal = pointers.length;
var captured = false;
for (var i = 0; i < pointersTotal; i++)
{
var total = 0;
var pointer = pointers[i];
// Always reset this array
this._tempZones = [];
// _temp contains a hit tested and camera culled list of IO objects
this._temp = this.hitTestPointer(pointer);
this.sortGameObjects(this._temp, pointer);
this.sortDropZones(this._tempZones);
if (this.topOnly)
{
// Only the top-most one counts now, so safely ignore the rest
if (this._temp.length)
{
this._temp.splice(1);
}
if (this._tempZones.length)
{
this._tempZones.splice(1);
}
}
switch (type)
{
case CONST.MOUSE_DOWN:
total += this.processDragDownEvent(pointer);
total += this.processDownEvents(pointer);
total += this.processOverOutEvents(pointer);
break;
case CONST.MOUSE_UP:
total += this.processDragUpEvent(pointer);
total += this.processUpEvents(pointer);
total += this.processOverOutEvents(pointer);
break;
case CONST.TOUCH_START:
total += this.processDragDownEvent(pointer);
total += this.processDownEvents(pointer);
total += this.processOverEvents(pointer);
break;
case CONST.TOUCH_END:
case CONST.TOUCH_CANCEL:
total += this.processDragUpEvent(pointer);
total += this.processUpEvents(pointer);
total += this.processOutEvents(pointer);
break;
case CONST.MOUSE_MOVE:
case CONST.TOUCH_MOVE:
total += this.processDragMoveEvent(pointer);
total += this.processMoveEvents(pointer);
total += this.processOverOutEvents(pointer);
break;
case CONST.MOUSE_WHEEL:
total += this.processWheelEvent(pointer);
break;
}
if (total > 0)
{
// We interacted with an event in this Scene, so block any Scenes below us from doing the same this frame
captured = true;
}
}
this._updatedThisFrame = true;
return captured;
},
/**
* Clears a Game Object so it no longer has an Interactive Object associated with it.
* The Game Object is then queued for removal from the Input Plugin on the next update.
*
* @method Phaser.Input.InputPlugin#clear
* @since 3.0.0
*
* @param {Phaser.GameObjects.GameObject} gameObject - The Game Object that will have its Interactive Object removed.
* @param {boolean} [skipQueue=false] - Skip adding this Game Object into the removal queue?
*
* @return {Phaser.GameObjects.GameObject} The Game Object that had its Interactive Object removed.
*/
clear: function (gameObject, skipQueue)
{
if (skipQueue === undefined) { skipQueue = false; }
var input = gameObject.input;
// If GameObject.input already cleared from higher class
if (!input)
{
return;
}
if (!skipQueue)
{
this.queueForRemoval(gameObject);
}
input.gameObject = undefined;
input.target = undefined;
input.hitArea = undefined;
input.hitAreaCallback = undefined;
input.callbackContext = undefined;
gameObject.input = null;
// Clear from _draggable, _drag and _over
var index = this._draggable.indexOf(gameObject);
if (index > -1)
{
this._draggable.splice(index, 1);
}
index = this._drag[0].indexOf(gameObject);
if (index > -1)
{
this._drag[0].splice(index, 1);
}
index = this._over[0].indexOf(gameObject);
if (index > -1)
{
this._over[0].splice(index, 1);
this.manager.resetCursor(input);
}
return gameObject;
},
/**
* Disables Input on a single Game Object.
*
* An input disabled Game Object still retains its Interactive Object component and can be re-enabled
* at any time, by passing it to `InputPlugin.enable`.
*
* @method Phaser.Input.InputPlugin#disable
* @since 3.0.0
*
* @param {Phaser.GameObjects.GameObject} gameObject - The Game Object to have its input system disabled.
*/
disable: function (gameObject)
{
gameObject.input.enabled = false;
},
/**
* Enable a Game Object for interaction.
*
* If the Game Object already has an Interactive Object component, it is enabled and returned.
*
* Otherwise, a new Interactive Object component is created and assigned to the Game Object's `input` property.
*
* Input works by using hit areas, these are nearly always geometric shapes, such as rectangles or circles, that act as the hit area
* for the Game Object. However, you can provide your own hit area shape and callback, should you wish to handle some more advanced
* input detection.
*
* If no arguments are provided it will try and create a rectangle hit area based on the texture frame the Game Object is using. If
* this isn't a texture-bound object, such as a Graphics or BitmapText object, this will fail, and you'll need to provide a specific
* shape for it to use.
*
* You can also provide an Input Configuration Object as the only argument to this method.
*
* @method Phaser.Input.InputPlugin#enable
* @since 3.0.0
*
* @param {Phaser.GameObjects.GameObject} gameObject - The Game Object to be enabled for input.
* @param {(Phaser.Types.Input.InputConfiguration|any)} [hitArea] - Either an input configuration object, or a geometric shape that defines the hit area for the Game Object. If not specified a Rectangle will be used.
* @param {Phaser.Types.Input.HitAreaCallback} [hitAreaCallback] - The 'contains' function to invoke to check if the pointer is within the hit area.
* @param {boolean} [dropZone=false] - Is this Game Object a drop zone or not?
*
* @return {this} This Input Plugin.
*/
enable: function (gameObject, hitArea, hitAreaCallback, dropZone)
{
if (dropZone === undefined) { dropZone = false; }
if (gameObject.input)
{
// If it is already has an InteractiveObject then just enable it and return
gameObject.input.enabled = true;
}
else
{
// Create an InteractiveObject and enable it
this.setHitArea(gameObject, hitArea, hitAreaCallback);
}
if (gameObject.input && dropZone && !gameObject.input.dropZone)
{
gameObject.input.dropZone = dropZone;
}
return this;
},
/**
* Takes the given Pointer and performs a hit test against it, to see which interactive Game Objects
* it is currently above.
*
* The hit test is performed against which-ever Camera the Pointer is over. If it is over multiple
* cameras, it starts checking the camera at the top of the camera list, and if nothing is found, iterates down the list.
*
* @method Phaser.Input.InputPlugin#hitTestPointer
* @since 3.0.0
*
* @param {Phaser.Input.Pointer} pointer - The Pointer to check against the Game Objects.
*
* @return {Phaser.GameObjects.GameObject[]} An array of all the interactive Game Objects the Pointer was above.
*/
hitTestPointer: function (pointer)
{
var cameras = this.cameras.getCamerasBelowPointer(pointer);
for (var c = 0; c < cameras.length; c++)
{
var camera = cameras[c];
// Get a list of all objects that can be seen by the camera below the pointer in the scene and store in 'over' array.
// All objects in this array are input enabled, as checked by the hitTest method, so we don't need to check later on as well.
var over = this.manager.hitTest(pointer, this._list, camera);
// Filter out the drop zones
for (var i = 0; i < over.length; i++)
{
var obj = over[i];
if (obj.input.dropZone)
{
this._tempZones.push(obj);
}
}
if (over.length > 0)
{
pointer.camera = camera;
return over;
}
}
// If we got this far then there were no Game Objects below the pointer, but it was still over
// a camera, so set that the top-most one into the pointer
pointer.camera = cameras[0];
return [];
},
/**
* An internal method that handles the Pointer down event.
*
* @method Phaser.Input.InputPlugin#processDownEvents
* @private
* @fires Phaser.Input.Events#GAMEOBJECT_POINTER_DOWN
* @fires Phaser.Input.Events#GAMEOBJECT_DOWN
* @fires Phaser.Input.Events#POINTER_DOWN
* @fires Phaser.Input.Events#POINTER_DOWN_OUTSIDE
* @since 3.0.0
*
* @param {Phaser.Input.Pointer} pointer - The Pointer being tested.
*
* @return {number} The total number of objects interacted with.
*/
processDownEvents: function (pointer)
{
var total = 0;
var currentlyOver = this._temp;
var _eventData = this._eventData;
var _eventContainer = this._eventContainer;
_eventData.cancelled = false;
var aborted = false;
// Go through all objects the pointer was over and fire their events / callbacks
for (var i = 0; i < currentlyOver.length; i++)
{
var gameObject = currentlyOver[i];
if (!gameObject.input)
{
continue;
}
total++;
gameObject.emit(Events.GAMEOBJECT_POINTER_DOWN, pointer, gameObject.input.localX, gameObject.input.localY, _eventContainer);
if (_eventData.cancelled || !gameObject.input)
{
aborted = true;
break;
}
this.emit(Events.GAMEOBJECT_DOWN, pointer, gameObject, _eventContainer);
if (_eventData.cancelled || !gameObject.input)
{
aborted = true;
break;
}