Skip to content

Commit 57e3276

Browse files
committed
Started work on multi-touch support.
1 parent 20d702c commit 57e3276

2 files changed

Lines changed: 391 additions & 68 deletions

File tree

src/input/InputManager.js

Lines changed: 155 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -90,49 +90,87 @@ var InputManager = new Class({
9090
this.queue = [];
9191

9292
/**
93-
* [description]
93+
* A reference to the Keyboard Manager class, if enabled via the `input.keyboard` Game Config property.
9494
*
9595
* @name Phaser.Input.InputManager#keyboard
96-
* @type {Phaser.Input.Keyboard.KeyboardManager}
96+
* @type {?Phaser.Input.Keyboard.KeyboardManager}
9797
* @since 3.0.0
9898
*/
99-
this.keyboard = new Keyboard(this);
99+
this.keyboard = (config.inputKeyboard) ? new Keyboard(this) : null;
100100

101101
/**
102-
* [description]
102+
* A reference to the Mouse Manager class, if enabled via the `input.mouse` Game Config property.
103103
*
104104
* @name Phaser.Input.InputManager#mouse
105-
* @type {Phaser.Input.Mouse.MouseManager}
105+
* @type {?Phaser.Input.Mouse.MouseManager}
106106
* @since 3.0.0
107107
*/
108-
this.mouse = new Mouse(this);
108+
this.mouse = (config.inputMouse) ? new Mouse(this) : null;
109109

110110
/**
111-
* [description]
111+
* A reference to the Touch Manager class, if enabled via the `input.touch` Game Config property.
112112
*
113113
* @name Phaser.Input.InputManager#touch
114114
* @type {Phaser.Input.Touch.TouchManager}
115115
* @since 3.0.0
116116
*/
117-
this.touch = new Touch(this);
117+
this.touch = (config.inputTouch) ? new Touch(this) : null;
118118

119119
/**
120-
* [description]
120+
* A reference to the Gamepad Manager class, if enabled via the `input.gamepad` Game Config property.
121121
*
122122
* @name Phaser.Input.InputManager#gamepad
123123
* @type {Phaser.Input.Gamepad.GamepadManager}
124124
* @since 3.0.0
125125
*/
126-
this.gamepad = new Gamepad(this);
126+
this.gamepad = (config.inputGamepad) ? new Gamepad(this) : null;
127127

128128
/**
129-
* [description]
129+
* An array of Pointers that have been added to the game.
130+
* If you need more than 2 then use the `addPointer` method to create them.
131+
*
132+
* @name Phaser.Input.InputManager#pointers
133+
* @type {Phaser.Input.Pointer[]}
134+
* @since 3.10.0
135+
*/
136+
this.pointers = [
137+
new Pointer(this, 0),
138+
new Pointer(this, 1)
139+
];
140+
141+
/**
142+
* The mouse has its own unique Pointer object, which you can reference directly if making a _desktop specific game_.
143+
* If you are supporting both desktop and touch devices then do not use this property, instead use `activePointer`
144+
* which will always map to the most recently interacted pointer.
145+
*
146+
* @name Phaser.Input.InputManager#mousePointer
147+
* @type {Phaser.Input.Pointer}
148+
* @since 3.10.0
149+
*/
150+
this.mousePointer = this.pointers[0];
151+
152+
/**
153+
* The most recently active Pointer object.
154+
*
155+
* If you've only 1 Pointer in your game then this will accurately be either the first finger touched, or the mouse.
156+
*
157+
* If your game doesn't need to support multi-touch then you can safely use this property in all of your game
158+
* code and it will adapt to be either the mouse or the touch, based on device.
130159
*
131160
* @name Phaser.Input.InputManager#activePointer
132161
* @type {Phaser.Input.Pointer}
133162
* @since 3.0.0
134163
*/
135-
this.activePointer = new Pointer(this, 0);
164+
this.activePointer = this.pointers[0];
165+
166+
/**
167+
* Reset every frame. Set to `true` if any of the Pointers are dirty this frame.
168+
*
169+
* @name Phaser.Input.InputManager#dirty
170+
* @type {boolean}
171+
* @since 3.10.0
172+
*/
173+
this.dirty = false;
136174

137175
/**
138176
* [description]
@@ -222,10 +260,7 @@ var InputManager = new Class({
222260

223261
this.updateBounds();
224262

225-
this.keyboard.boot();
226-
this.mouse.boot();
227-
this.touch.boot();
228-
this.gamepad.boot();
263+
this.events.emit('boot');
229264

230265
this.game.events.on('prestep', this.update, this);
231266
this.game.events.once('destroy', this.destroy, this);
@@ -282,17 +317,20 @@ var InputManager = new Class({
282317
*/
283318
update: function (time)
284319
{
285-
this.keyboard.update();
286-
this.gamepad.update();
320+
this.events.emit('update');
287321

288322
this.ignoreEvents = false;
289323

324+
this.dirty = false;
325+
290326
var len = this.queue.length;
291327

292-
// Currently just 1 pointer supported
293-
var pointer = this.activePointer;
328+
var pointers = this.pointers;
294329

295-
pointer.reset();
330+
for (var i = 0; i < pointers.length; i++)
331+
{
332+
pointers[i].reset();
333+
}
296334

297335
if (!this.enabled || len === 0)
298336
{
@@ -313,37 +351,48 @@ var InputManager = new Class({
313351
{
314352
var event = queue[i];
315353

316-
// TODO: Move to CONSTs so we can do integer comparisons instead of strings.
317354
switch (event.type)
318355
{
319356
case 'mousemove':
320357

321-
pointer.move(event, time);
358+
this.mousePointer.move(event, time);
322359
break;
323360

324361
case 'mousedown':
325362

326-
pointer.down(event, time);
363+
this.mousePointer.down(event, time);
327364
break;
328365

329366
case 'mouseup':
330367

331-
pointer.up(event, time);
368+
this.mousePointer.up(event, time);
332369
break;
333370

334371
case 'touchmove':
335372

336-
pointer.touchmove(event, time);
373+
for (i = 0; i < event.changedTouches.length; i++)
374+
{
375+
this.updatePointer(event.changedTouches[i], event, time);
376+
}
377+
337378
break;
338379

339380
case 'touchstart':
340381

341-
pointer.touchstart(event, time);
382+
for (i = 0; i < event.changedTouches.length; i++)
383+
{
384+
this.startPointer(event.changedTouches[i], event, time);
385+
}
386+
342387
break;
343388

344389
case 'touchend':
345390

346-
pointer.touchend(event, time);
391+
for (i = 0; i < event.changedTouches.length; i++)
392+
{
393+
this.stopPointer(event.changedTouches[i], event, time);
394+
}
395+
347396
break;
348397

349398
case 'pointerlockchange':
@@ -354,6 +403,84 @@ var InputManager = new Class({
354403
}
355404
},
356405

406+
// event.targetTouches = list of all touches on the TARGET ELEMENT (i.e. game dom element)
407+
// event.touches = list of all touches on the ENTIRE DOCUMENT, not just the target element
408+
// event.changedTouches = the touches that CHANGED in this event, not the total number of them
409+
startPointer: function (changedTouch, event, time)
410+
{
411+
var pointers = this.pointers;
412+
413+
for (var i = 1; i < pointers.length; i++)
414+
{
415+
var pointer = pointers[i];
416+
417+
if (!pointer.active)
418+
{
419+
pointer.touchstart(changedTouch, time);
420+
this.activePointer = pointer;
421+
this.dirty = true;
422+
break;
423+
}
424+
}
425+
},
426+
427+
updatePointer: function (changedTouch, event, time)
428+
{
429+
var pointers = this.pointers;
430+
431+
for (var i = 1; i < pointers.length; i++)
432+
{
433+
var pointer = pointers[i];
434+
435+
if (pointer.active && pointer.identifier === changedTouch.identifier)
436+
{
437+
pointer.touchmove(changedTouch, time);
438+
this.activePointer = pointer;
439+
this.dirty = true;
440+
break;
441+
}
442+
}
443+
},
444+
445+
// For touch end its a list of the touch points that have been removed from the surface
446+
// https://developer.mozilla.org/en-US/docs/DOM/TouchList
447+
// event.changedTouches = the touches that CHANGED in this event, not the total number of them
448+
stopPointer: function (changedTouch, event, time)
449+
{
450+
var pointers = this.pointers;
451+
452+
for (var i = 1; i < pointers.length; i++)
453+
{
454+
var pointer = pointers[i];
455+
456+
if (pointer.active && pointer.identifier === changedTouch.identifier)
457+
{
458+
pointer.touchend(changedTouch, time);
459+
this.dirty = true;
460+
break;
461+
}
462+
}
463+
},
464+
465+
/**
466+
* Add a new Pointer object to the Input Manager.
467+
* By default Input creates 3 pointer objects: `mousePointer` (not include in part of general pointer pool), `pointer1` and `pointer2`.
468+
* This method adds an additional pointer, up to a maximum of Phaser.Input.MAX_POINTERS (default of 10).
469+
*
470+
* @method Phaser.Input#addPointer
471+
* @return {Phaser.Pointer|null} The new Pointer object that was created; null if a new pointer could not be added.
472+
*/
473+
addPointer: function ()
474+
{
475+
var id = this.pointers.length;
476+
477+
var pointer = new Pointer(this, id);
478+
479+
this.pointers.push(pointer);
480+
481+
return pointer;
482+
},
483+
357484
/**
358485
* Will always return an array.
359486
* Array contains matching Interactive Objects.

0 commit comments

Comments
 (0)