forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputManager.js
More file actions
652 lines (502 loc) · 16.3 KB
/
Copy pathInputManager.js
File metadata and controls
652 lines (502 loc) · 16.3 KB
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
var Circle = require('../geom/circle/Circle');
var CircleContains = require('../geom/circle/Contains');
var Class = require('../utils/Class');
var Ellipse = require('../geom/ellipse/Ellipse');
var EllipseContains = require('../geom/ellipse/Contains');
var InputEvent = require('../input/events');
var InteractiveObject = require('../input/InteractiveObject');
var NOOP = require('../utils/NOOP');
var Rectangle = require('../geom/rectangle/Rectangle');
var RectangleContains = require('../geom/rectangle/Contains');
var Triangle = require('../geom/triangle/Triangle');
var TriangleContains = require('../geom/triangle/Contains');
var InputManager = new Class({
initialize:
function InputManager (scene, game)
{
// The Scene that owns this plugin
this.scene = scene;
this.cameras;
// GlobalInputManager
this.manager = game.input;
// Should use Scene event dispatcher?
this.events = this.manager.events;
this.keyboard = this.manager.keyboard;
this.mouse = this.manager.mouse;
// How often should the pointer input be checked?
// Time given in ms
// Pointer will *always* be checked if it has been moved by the user.
// This controls how often it will be polled if it hasn't been moved.
// Set to 0 to poll constantly. Set to -1 to only poll on user movement.
this.pollRate = -1;
this._pollTimer = 0;
this._size = 0;
// All list of all Game Objects that have been input enabled
this._list = [];
// Only those which are currently below a pointer (any pointer)
this._over = [];
// Only Game Objects which have been flagged as draggable are added to this array
this._draggable = [];
// Objects waiting to be inserted or removed from the active list
this._pendingInsertion = [];
this._pendingRemoval = [];
this._validTypes = [ 'onDown', 'onUp', 'onOver', 'onOut' ];
},
boot: function ()
{
this.cameras = this.scene.sys.cameras.cameras;
},
begin: function ()
{
var toRemove = this._pendingRemoval.length;
var toInsert = this._pendingInsertion.length;
if (toRemove === 0 && toInsert === 0)
{
// Quick bail
return;
}
var i;
var gameObject;
// Delete old gameObjects
for (i = 0; i < toRemove; i++)
{
gameObject = this._pendingRemoval[i];
var index = this._list.indexOf(gameObject);
if (index > -1)
{
this._list.splice(index, 1);
gameObject.input = null;
}
}
// Move pending to active (can swap for concat splice if we don't need anything extra here)
for (i = 0; i < toInsert; i++)
{
gameObject = this._pendingInsertion[i];
// Swap for Input Enabled Object
this._list.push(gameObject);
}
this._size = this._list.length;
// Clear the lists
this._pendingRemoval.length = 0;
this._pendingInsertion.length = 0;
},
setPollAlways: function ()
{
this.pollRate = 0;
this._pollTimer = 0;
return this;
},
setPollOnMove: function ()
{
this.pollRate = -1;
this._pollTimer = 0;
return this;
},
setPoll: function (value)
{
this.pollRate = value;
this._pollTimer = 0;
return this;
},
update: function (time, delta)
{
if (this._size === 0)
{
return;
}
var pointer = this.manager.activePointer;
var runUpdate = (pointer.dirty || this.pollRate === 0);
if (this.pollRate > -1)
{
this._pollTimer -= delta;
if (this._pollTimer < 0)
{
runUpdate = true;
// Discard timer diff
this._pollTimer = this.pollRate;
}
}
if (runUpdate)
{
this.hitTestPointer(pointer);
this.processUpDownEvents(pointer);
if (pointer.justMoved)
{
this.processMovementEvents(pointer);
}
}
},
hitTestPointer: function (pointer)
{
var i;
var tested = [];
var justOut = [];
var justOver = [];
var stillOver = [];
var currentlyOver = this._over;
var gameObject;
// Returns an array of objects the pointer is over
for (i = 0; i < this.cameras.length; i++)
{
var camera = this.cameras[i];
if (camera.inputEnabled)
{
tested = tested.concat(this.manager.hitTest(this._list, pointer.x, pointer.y, camera));
}
}
for (i = 0; i < tested.length; i++)
{
gameObject = tested[i];
if (currentlyOver.indexOf(gameObject) !== -1)
{
stillOver.push(gameObject);
}
else
{
justOver.push(gameObject);
}
}
for (i = 0; i < currentlyOver.length; i++)
{
gameObject = currentlyOver[i];
if (tested.indexOf(gameObject) === -1)
{
justOut.push(gameObject);
}
}
// Now we can process what has happened
for (i = 0; i < justOut.length; i++)
{
gameObject = justOut[i];
if (gameObject.input.enabled)
{
this.gameObjectOnOut(pointer, gameObject);
}
}
for (i = 0; i < justOver.length; i++)
{
gameObject = justOver[i];
if (gameObject.input.enabled)
{
this.gameObjectOnOver(pointer, gameObject);
}
}
// Store everything that is currently over
this._over = stillOver.concat(justOver);
},
// Has it been pressed down or released in this update?
processUpDownEvents: function (pointer)
{
var gameObject;
for (var i = 0; i < this._over.length; i++)
{
gameObject = this._over[i];
if (gameObject.input.enabled)
{
if (pointer.justDown)
{
this.gameObjectOnDown(pointer, gameObject);
}
else if (pointer.justUp)
{
this.gameObjectOnUp(pointer, gameObject);
}
}
}
},
// Has it been moved in this update?
processMovementEvents: function (pointer)
{
// Check the list of Draggable Items
for (var i = 0; i < this._draggable.length; i++)
{
var gameObject = this._draggable[i];
var input = gameObject.input;
if (!input.enabled)
{
continue;
}
if (pointer.justUp && input.isDragged)
{
// Drag End
this.gameObjectOnDragEnd(pointer, gameObject);
}
else if (input.isDragged)
{
// Drag
this.gameObjectOnDrag(pointer, gameObject);
}
}
},
gameObjectOnDragStart: function (pointer, gameObject)
{
var input = gameObject.input;
input.isDragged = true;
input.dragX = input.localX - gameObject.displayOriginX;
input.dragY = input.localY - gameObject.displayOriginY;
this.events.dispatch(new InputEvent.DRAG_START(pointer, gameObject));
gameObject.input.onDragStart(gameObject, pointer);
},
gameObjectOnDrag: function (pointer, gameObject)
{
this.events.dispatch(new InputEvent.DRAG(pointer, gameObject));
gameObject.input.onDrag(gameObject, pointer);
},
gameObjectOnDragEnd: function (pointer, gameObject)
{
var input = gameObject.input;
input.isDragged = false;
this.events.dispatch(new InputEvent.DRAG_END(pointer, gameObject));
gameObject.input.onDragEnd(gameObject, pointer);
},
gameObjectOnDown: function (pointer, gameObject)
{
var input = gameObject.input;
input.isDown = true;
this.events.dispatch(new InputEvent.DOWN(pointer, gameObject));
input.onDown(gameObject, pointer, input.localX, input.localY);
if (input.draggable && !input.isDragged)
{
// Drag Start
this.gameObjectOnDragStart(pointer, gameObject);
}
},
gameObjectOnUp: function (pointer, gameObject)
{
gameObject.input.isDown = false;
this.events.dispatch(new InputEvent.UP(pointer, gameObject));
gameObject.input.onUp(gameObject, pointer, gameObject.input.localX, gameObject.input.localY);
},
gameObjectOnOut: function (pointer, gameObject)
{
var input = gameObject.input;
input.isOver = false;
// Don't dispatch if we're dragging the gameObject, as the pointer often gets away from it
if (!input.isDragged)
{
this.events.dispatch(new InputEvent.OUT(pointer, gameObject));
input.onOut(gameObject, pointer);
}
},
gameObjectOnOver: function (pointer, gameObject)
{
var input = gameObject.input;
input.isOver = true;
// Don't dispatch if we're dragging the gameObject, as the pointer often gets away from it
if (!input.isDragged)
{
this.events.dispatch(new InputEvent.OVER(pointer, gameObject));
input.onOver(gameObject, pointer, input.localX, input.localY);
}
},
enable: function (gameObject, shape, callback)
{
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, shape, callback);
}
return this;
},
disable: function (gameObject)
{
gameObject.input.enabled = false;
},
// Queues a Game Object for insertion into this Input Manager on the next update.
queueForInsertion: function (child)
{
if (this._pendingInsertion.indexOf(child) === -1 && this._list.indexOf(child) === -1)
{
this._pendingInsertion.push(child);
}
return this;
},
// Queues a Game Object for removal from this Input Manager on the next update.
queueForRemoval: function (child)
{
this._pendingRemoval.push(child);
return this;
},
// Set Hit Areas
setHitArea: function (gameObjects, shape, callback)
{
if (shape === undefined)
{
return this.setHitAreaFromTexture(gameObjects);
}
if (!Array.isArray(gameObjects))
{
gameObjects = [ gameObjects ];
}
for (var i = 0; i < gameObjects.length; i++)
{
var gameObject = gameObjects[i];
gameObject.input = InteractiveObject(gameObject, shape, callback);
this.queueForInsertion(gameObject);
}
return this;
},
setHitAreaFromTexture: function (gameObjects, callback)
{
if (callback === undefined) { callback = RectangleContains; }
if (!Array.isArray(gameObjects))
{
gameObjects = [ gameObjects ];
}
for (var i = 0; i < gameObjects.length; i++)
{
var gameObject = gameObjects[i];
if (gameObject.frame)
{
gameObject.input = InteractiveObject(gameObject, new Rectangle(0, 0, gameObject.frame.width, gameObject.frame.height), callback);
this.queueForInsertion(gameObject);
}
}
return this;
},
setHitAreaRectangle: function (gameObjects, x, y, width, height, callback)
{
if (callback === undefined) { callback = RectangleContains; }
var shape = new Rectangle(x, y, width, height);
return this.setHitArea(gameObjects, shape, callback);
},
setHitAreaCircle: function (gameObjects, x, y, radius, callback)
{
if (callback === undefined) { callback = CircleContains; }
var shape = new Circle(x, y, radius);
return this.setHitArea(gameObjects, shape, callback);
},
setHitAreaEllipse: function (gameObjects, x, y, width, height, callback)
{
if (callback === undefined) { callback = EllipseContains; }
var shape = new Ellipse(x, y, width, height);
return this.setHitArea(gameObjects, shape, callback);
},
setHitAreaTriangle: function (gameObjects, x1, y1, x2, y2, x3, y3, callback)
{
if (callback === undefined) { callback = TriangleContains; }
var shape = new Triangle(x1, y1, x2, y2, x3, y3);
return this.setHitArea(gameObjects, shape, callback);
},
// type = onDown, onUp, onOver, onOut
setOnDownCallback: function (gameObjects, callback, context)
{
return this.setCallback(gameObjects, 'onDown', callback, context);
},
setOnUpCallback: function (gameObjects, callback, context)
{
return this.setCallback(gameObjects, 'onUp', callback, context);
},
setOnOverCallback: function (gameObjects, callback, context)
{
return this.setCallback(gameObjects, 'onOver', callback, context);
},
setOnOutCallback: function (gameObjects, callback, context)
{
return this.setCallback(gameObjects, 'onOut', callback, context);
},
setCallbacks: function (gameObjects, onDown, onUp, onOver, onOut, context)
{
if (onDown)
{
this.setOnDownCallback(gameObjects, onDown, context);
}
if (onUp)
{
this.setOnDownCallback(gameObjects, onUp, context);
}
if (onOver)
{
this.setOnDownCallback(gameObjects, onOver, context);
}
if (onOut)
{
this.setOnDownCallback(gameObjects, onOut, context);
}
return this;
},
setCallback: function (gameObjects, type, callback, context)
{
if (this._validTypes.indexOf(type) === -1)
{
return this;
}
if (!Array.isArray(gameObjects))
{
gameObjects = [ gameObjects ];
}
for (var i = 0; i < gameObjects.length; i++)
{
var gameObject = gameObjects[i];
if (gameObject.input)
{
gameObject.input[type] = callback;
if (context)
{
gameObject.input.callbackContext = context;
}
}
}
return this;
},
// Drag Events
// https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API
setDraggable: function (gameObjects, value)
{
if (value === undefined) { value = true; }
if (!Array.isArray(gameObjects))
{
gameObjects = [ gameObjects ];
}
for (var i = 0; i < gameObjects.length; i++)
{
var gameObject = gameObjects[i];
var index = this._draggable.indexOf(gameObject);
if (value)
{
// Make draggable
gameObject.input.draggable = true;
if (index === -1)
{
this._draggable.push(gameObject);
}
}
else
{
// Disable drag
gameObject.input.draggable = false;
if (index === -1)
{
this._draggable.splice(index, 1);
}
}
}
return true;
},
// Scene that owns this is shutting down
shutdown: function ()
{
this._list = [];
this._over = [];
this._draggable = [];
this._pendingRemoval = [];
this._pendingInsertion = [];
},
// Game level nuke
destroy: function ()
{
this.shutdown();
this.scene = undefined;
this.cameras = undefined;
this.manager = undefined;
this.events = undefined;
this.keyboard = undefined;
this.mouse = undefined;
}
});
module.exports = InputManager;