Skip to content

Commit 45c373f

Browse files
committed
Multi-touch support working properly up to 10 pointers and mouse re-enabled.
1 parent 7371a52 commit 45c373f

5 files changed

Lines changed: 270 additions & 219 deletions

File tree

src/input/InputManager.js

Lines changed: 76 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -127,27 +127,40 @@ var InputManager = new Class({
127127

128128
/**
129129
* 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.
130+
* The first entry is reserved for the Mouse Pointer, the rest are Touch Pointers.
131+
*
132+
* By default there are 2 pointers enabled. If you need more use the `addPointer` method to create them.
131133
*
132134
* @name Phaser.Input.InputManager#pointers
133135
* @type {Phaser.Input.Pointer[]}
134136
* @since 3.10.0
135137
*/
136138
this.pointers = [
137139
new Pointer(this, 0),
138-
new Pointer(this, 1)
140+
new Pointer(this, 1),
141+
new Pointer(this, 2),
142+
new Pointer(this, 3),
143+
new Pointer(this, 4),
144+
new Pointer(this, 5),
145+
new Pointer(this, 6),
146+
new Pointer(this, 7),
147+
new Pointer(this, 8),
148+
new Pointer(this, 9),
149+
new Pointer(this, 10)
139150
];
140151

152+
this.pointersTotal = 2;
153+
141154
/**
142155
* The mouse has its own unique Pointer object, which you can reference directly if making a _desktop specific game_.
143156
* If you are supporting both desktop and touch devices then do not use this property, instead use `activePointer`
144157
* which will always map to the most recently interacted pointer.
145158
*
146159
* @name Phaser.Input.InputManager#mousePointer
147-
* @type {Phaser.Input.Pointer}
160+
* @type {?Phaser.Input.Pointer}
148161
* @since 3.10.0
149162
*/
150-
this.mousePointer = this.pointers[0];
163+
this.mousePointer = (config.inputMouse) ? this.pointers[0] : null;
151164

152165
/**
153166
* The most recently active Pointer object.
@@ -327,7 +340,7 @@ var InputManager = new Class({
327340

328341
var pointers = this.pointers;
329342

330-
for (var i = 0; i < pointers.length; i++)
343+
for (var i = 0; i < this.pointersTotal; i++)
331344
{
332345
pointers[i].reset();
333346
}
@@ -337,6 +350,8 @@ var InputManager = new Class({
337350
return;
338351
}
339352

353+
this.dirty = true;
354+
340355
this.updateBounds();
341356

342357
this.scale.x = this.game.config.width / this.bounds.width;
@@ -347,117 +362,86 @@ var InputManager = new Class({
347362
var queue = this.queue.splice(0, len);
348363

349364
// Process the event queue, dispatching all of the events that have stored up
350-
for (var i = 0; i < len; i++)
365+
for (var i = 0; i < len; i += 3)
351366
{
352-
var event = queue[i];
353-
354-
switch (event.type)
355-
{
356-
case 'mousemove':
357-
358-
this.mousePointer.move(event, time);
359-
break;
360-
361-
case 'mousedown':
362-
363-
this.mousePointer.down(event, time);
364-
break;
365-
366-
case 'mouseup':
367-
368-
this.mousePointer.up(event, time);
369-
break;
370-
371-
case 'touchmove':
372-
373-
for (i = 0; i < event.changedTouches.length; i++)
374-
{
375-
this.updatePointer(event.changedTouches[i], event, time);
376-
}
377-
378-
break;
379-
380-
case 'touchstart':
381-
382-
for (i = 0; i < event.changedTouches.length; i++)
383-
{
384-
this.startPointer(event.changedTouches[i], event, time);
385-
}
386-
387-
break;
388-
389-
case 'touchend':
390-
391-
for (i = 0; i < event.changedTouches.length; i++)
392-
{
393-
this.stopPointer(event.changedTouches[i], event, time);
394-
}
395-
396-
break;
367+
var callback = queue[i];
368+
var context = queue[i + 1];
369+
var event = queue[i + 2];
397370

398-
case 'pointerlockchange':
371+
callback.call(context, event, time);
399372

400-
this.events.emit('pointerlockchange', event, this.mouse.locked);
401-
break;
402-
}
373+
// case 'pointerlockchange':
374+
// this.events.emit('pointerlockchange', event, this.mouse.locked);
403375
}
404376
},
405377

406378
// event.targetTouches = list of all touches on the TARGET ELEMENT (i.e. game dom element)
407379
// event.touches = list of all touches on the ENTIRE DOCUMENT, not just the target element
408380
// event.changedTouches = the touches that CHANGED in this event, not the total number of them
409-
startPointer: function (changedTouch, event, time)
381+
startPointer: function (event, time)
410382
{
411383
var pointers = this.pointers;
412384

413-
for (var i = 1; i < pointers.length; i++)
385+
for (var c = 0; c < event.changedTouches.length; c++)
414386
{
415-
var pointer = pointers[i];
387+
var changedTouch = event.changedTouches[c];
416388

417-
if (!pointer.active)
389+
for (var i = 1; i < this.pointersTotal; i++)
418390
{
419-
pointer.touchstart(changedTouch, time);
420-
this.activePointer = pointer;
421-
this.dirty = true;
422-
break;
391+
var pointer = pointers[i];
392+
393+
if (!pointer.active)
394+
{
395+
pointer.touchstart(changedTouch, time);
396+
this.activePointer = pointer;
397+
break;
398+
}
423399
}
424400
}
425401
},
426402

427-
updatePointer: function (changedTouch, event, time)
403+
updatePointer: function (event, time)
428404
{
429405
var pointers = this.pointers;
430406

431-
for (var i = 1; i < pointers.length; i++)
407+
for (var c = 0; c < event.changedTouches.length; c++)
432408
{
433-
var pointer = pointers[i];
409+
var changedTouch = event.changedTouches[c];
434410

435-
if (pointer.active && pointer.identifier === changedTouch.identifier)
411+
for (var i = 1; i < this.pointersTotal; i++)
436412
{
437-
pointer.touchmove(changedTouch, time);
438-
this.activePointer = pointer;
439-
this.dirty = true;
440-
break;
413+
var pointer = pointers[i];
414+
415+
if (pointer.active && pointer.identifier === changedTouch.identifier)
416+
{
417+
pointer.touchmove(changedTouch, time);
418+
this.activePointer = pointer;
419+
break;
420+
}
441421
}
442422
}
443423
},
444424

445425
// For touch end its a list of the touch points that have been removed from the surface
446426
// https://developer.mozilla.org/en-US/docs/DOM/TouchList
447427
// event.changedTouches = the touches that CHANGED in this event, not the total number of them
448-
stopPointer: function (changedTouch, event, time)
428+
stopPointer: function (event, time)
449429
{
450430
var pointers = this.pointers;
451431

452-
for (var i = 1; i < pointers.length; i++)
432+
for (var c = 0; c < event.changedTouches.length; c++)
453433
{
454-
var pointer = pointers[i];
434+
var changedTouch = event.changedTouches[c];
455435

456-
if (pointer.active && pointer.identifier === changedTouch.identifier)
436+
for (var i = 1; i < this.pointersTotal; i++)
457437
{
458-
pointer.touchend(changedTouch, time);
459-
this.dirty = true;
460-
break;
438+
var pointer = pointers[i];
439+
440+
if (pointer.active && pointer.identifier === changedTouch.identifier)
441+
{
442+
pointer.touchend(changedTouch, time);
443+
break;
444+
}
461445
}
462446
}
463447
},
@@ -474,11 +458,22 @@ var InputManager = new Class({
474458
{
475459
var id = this.pointers.length;
476460

477-
var pointer = new Pointer(this, id);
461+
if (this.pointersTotal < id)
462+
{
463+
this.pointersTotal++;
464+
465+
return this.pointers[this.pointersTotal];
466+
}
467+
else
468+
{
469+
var pointer = new Pointer(this, id);
478470

479-
this.pointers.push(pointer);
471+
this.pointers.push(pointer);
480472

481-
return pointer;
473+
this.pointersTotal++;
474+
475+
return pointer;
476+
}
482477
},
483478

484479
/**

src/input/InputPlugin.js

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,7 +1521,7 @@ var InputPlugin = new Class({
15211521

15221522
var pointers = this.manager.pointers;
15231523

1524-
for (var i = 0; i < pointers.length; i++)
1524+
for (var i = 0; i < this.manager.pointersTotal; i++)
15251525
{
15261526
var pointer = pointers[i];
15271527

@@ -1550,6 +1550,7 @@ var InputPlugin = new Class({
15501550

15511551
var total = this.processDragEvents(pointer, time);
15521552

1553+
// TODO: Enable for touch
15531554
if (!pointer.wasTouch)
15541555
{
15551556
total += this.processOverOutEvents(pointer);
@@ -1687,54 +1688,73 @@ var InputPlugin = new Class({
16871688
},
16881689

16891690
/**
1690-
* The current active input Pointer.
1691+
* The x coordinates of the ActivePointer based on the first camera in the camera list.
1692+
* This is only safe to use if your game has just 1 non-transformed camera and doesn't use multi-touch.
16911693
*
1692-
* @name Phaser.Input.InputPlugin#activePointer
1693-
* @type {Phaser.Input.Pointer}
1694+
* @name Phaser.Input.InputPlugin#x
1695+
* @type {number}
16941696
* @readOnly
16951697
* @since 3.0.0
16961698
*/
1697-
activePointer: {
1699+
x: {
16981700

16991701
get: function ()
17001702
{
1701-
return this.manager.activePointer;
1703+
return this.manager.activePointer.x;
17021704
}
17031705

17041706
},
17051707

17061708
/**
1707-
* The x coordinates of the ActivePointer based on the first camera in the camera list.
1709+
* The y coordinates of the ActivePointer based on the first camera in the camera list.
17081710
* This is only safe to use if your game has just 1 non-transformed camera and doesn't use multi-touch.
17091711
*
1710-
* @name Phaser.Input.InputPlugin#x
1712+
* @name Phaser.Input.InputPlugin#y
17111713
* @type {number}
17121714
* @readOnly
17131715
* @since 3.0.0
17141716
*/
1715-
x: {
1717+
y: {
17161718

17171719
get: function ()
17181720
{
1719-
return this.manager.activePointer.x;
1721+
return this.manager.activePointer.y;
17201722
}
17211723

17221724
},
17231725

17241726
/**
1725-
* The y coordinates of the ActivePointer based on the first camera in the camera list.
1726-
* This is only safe to use if your game has just 1 non-transformed camera and doesn't use multi-touch.
1727+
* The mouse has its own unique Pointer object, which you can reference directly if making a _desktop specific game_.
1728+
* If you are supporting both desktop and touch devices then do not use this property, instead use `activePointer`
1729+
* which will always map to the most recently interacted pointer.
17271730
*
1728-
* @name Phaser.Input.InputPlugin#y
1729-
* @type {number}
1731+
* @name Phaser.Input.InputPlugin#mousePointer
1732+
* @type {Phaser.Input.Pointer}
1733+
* @readOnly
1734+
* @since 3.10.0
1735+
*/
1736+
mousePointer: {
1737+
1738+
get: function ()
1739+
{
1740+
return this.manager.mousePointer;
1741+
}
1742+
1743+
},
1744+
1745+
/**
1746+
* The current active input Pointer.
1747+
*
1748+
* @name Phaser.Input.InputPlugin#activePointer
1749+
* @type {Phaser.Input.Pointer}
17301750
* @readOnly
17311751
* @since 3.0.0
17321752
*/
1733-
y: {
1753+
activePointer: {
17341754

17351755
get: function ()
17361756
{
1737-
return this.manager.activePointer.y;
1757+
return this.manager.activePointer;
17381758
}
17391759

17401760
},

0 commit comments

Comments
 (0)