Skip to content

Commit bf48c53

Browse files
committed
Added new over and out handlers for touch events
1 parent 9b93ad9 commit bf48c53

1 file changed

Lines changed: 185 additions & 17 deletions

File tree

src/input/InputPlugin.js

Lines changed: 185 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ var InputPlugin = new Class({
321321
* A list of all Interactive Objects currently considered as being 'draggable' by any pointer, indexed by pointer ID.
322322
*
323323
* @name Phaser.Input.InputPlugin#_drag
324-
* @type {{0:Array,2:Array,3:Array,4:Array,5:Array,6:Array,7:Array,8:Array,9:Array}}
324+
* @type {{0:Array,1:Array,2:Array,3:Array,4:Array,5:Array,6:Array,7:Array,8:Array,9:Array,10:Array}}
325325
* @private
326326
* @since 3.0.0
327327
*/
@@ -341,7 +341,7 @@ var InputPlugin = new Class({
341341
* A list of all Interactive Objects currently considered as being 'over' by any pointer, indexed by pointer ID.
342342
*
343343
* @name Phaser.Input.InputPlugin#_over
344-
* @type {{0:Array,2:Array,3:Array,4:Array,5:Array,6:Array,7:Array,8:Array,9:Array}}
344+
* @type {{0:Array,1:Array,2:Array,3:Array,4:Array,5:Array,6:Array,7:Array,8:Array,9:Array,10:Array}}
345345
* @private
346346
* @since 3.0.0
347347
*/
@@ -676,27 +676,38 @@ var InputPlugin = new Class({
676676
switch (type)
677677
{
678678
case CONST.MOUSE_DOWN:
679-
case CONST.TOUCH_START:
680679
total += this.processDragDownEvent(pointer);
681680
total += this.processDownEvents(pointer);
681+
total += this.processOverOutEvents(pointer);
682682
break;
683683

684684
case CONST.MOUSE_UP:
685+
total += this.processDragUpEvent(pointer);
686+
total += this.processUpEvents(pointer);
687+
total += this.processOverOutEvents(pointer);
688+
break;
689+
690+
case CONST.TOUCH_START:
691+
total += this.processDragDownEvent(pointer);
692+
total += this.processDownEvents(pointer);
693+
total += this.processOverEvents(pointer);
694+
break;
695+
685696
case CONST.TOUCH_END:
686697
case CONST.TOUCH_CANCEL:
687698
total += this.processDragUpEvent(pointer);
688699
total += this.processUpEvents(pointer);
700+
total += this.processOutEvents(pointer);
689701
break;
690702

691703
case CONST.MOUSE_MOVE:
692704
case CONST.TOUCH_MOVE:
693705
total += this.processDragMoveEvent(pointer);
694706
total += this.processMoveEvents(pointer);
707+
total += this.processOverOutEvents(pointer);
695708
break;
696709
}
697710

698-
total += this.processOverOutEvents(pointer);
699-
700711
if (total > 0)
701712
{
702713
// We interacted with an event in this Scene, so block any Scenes below us from doing the same this frame
@@ -1417,6 +1428,169 @@ var InputPlugin = new Class({
14171428
return total;
14181429
},
14191430

1431+
/**
1432+
* An internal method that handles the Pointer over events.
1433+
* This is called when a touch input hits the canvas, having previously been off of it.
1434+
*
1435+
* @method Phaser.Input.InputPlugin#processOverEvents
1436+
* @private
1437+
* @fires Phaser.Input.Events#GAMEOBJECT_POINTER_OVER
1438+
* @fires Phaser.Input.Events#GAMEOBJECT_OVER
1439+
* @fires Phaser.Input.Events#POINTER_OVER
1440+
* @since 3.18.0
1441+
*
1442+
* @param {Phaser.Input.Pointer} pointer - The pointer to check for events against.
1443+
*
1444+
* @return {integer} The total number of objects interacted with.
1445+
*/
1446+
processOverEvents: function (pointer)
1447+
{
1448+
var currentlyOver = this._temp;
1449+
1450+
var totalInteracted = 0;
1451+
1452+
var total = currentlyOver.length;
1453+
1454+
var justOver = [];
1455+
1456+
if (total > 0)
1457+
{
1458+
var manager = this.manager;
1459+
1460+
var _eventData = this._eventData;
1461+
var _eventContainer = this._eventContainer;
1462+
1463+
_eventData.cancelled = false;
1464+
1465+
var aborted = false;
1466+
1467+
for (var i = 0; i < total; i++)
1468+
{
1469+
var gameObject = currentlyOver[i];
1470+
1471+
if (!gameObject.input)
1472+
{
1473+
continue;
1474+
}
1475+
1476+
justOver.push(gameObject);
1477+
1478+
manager.setCursor(gameObject.input);
1479+
1480+
gameObject.emit(Events.GAMEOBJECT_POINTER_OVER, pointer, gameObject.input.localX, gameObject.input.localY, _eventContainer);
1481+
1482+
totalInteracted++;
1483+
1484+
if (_eventData.cancelled || !gameObject.input)
1485+
{
1486+
aborted = true;
1487+
break;
1488+
}
1489+
1490+
this.emit(Events.GAMEOBJECT_OVER, pointer, gameObject, _eventContainer);
1491+
1492+
if (_eventData.cancelled || !gameObject.input)
1493+
{
1494+
aborted = true;
1495+
break;
1496+
}
1497+
}
1498+
1499+
if (!aborted)
1500+
{
1501+
this.emit(Events.POINTER_OVER, pointer, justOver);
1502+
}
1503+
}
1504+
1505+
// Then sort it into display list order
1506+
this._over[pointer.id] = justOver;
1507+
1508+
return totalInteracted;
1509+
},
1510+
1511+
/**
1512+
* An internal method that handles the Pointer out events.
1513+
* This is called when a touch input leaves the canvas, as it can never be 'over' in this case.
1514+
*
1515+
* @method Phaser.Input.InputPlugin#processOutEvents
1516+
* @private
1517+
* @fires Phaser.Input.Events#GAMEOBJECT_POINTER_OUT
1518+
* @fires Phaser.Input.Events#GAMEOBJECT_OUT
1519+
* @fires Phaser.Input.Events#POINTER_OUT
1520+
* @since 3.18.0
1521+
*
1522+
* @param {Phaser.Input.Pointer} pointer - The pointer to check for events against.
1523+
*
1524+
* @return {integer} The total number of objects interacted with.
1525+
*/
1526+
processOutEvents: function (pointer)
1527+
{
1528+
var previouslyOver = this._over[pointer.id];
1529+
1530+
var totalInteracted = 0;
1531+
1532+
var total = previouslyOver.length;
1533+
1534+
if (total > 0)
1535+
{
1536+
var manager = this.manager;
1537+
1538+
var _eventData = this._eventData;
1539+
var _eventContainer = this._eventContainer;
1540+
1541+
_eventData.cancelled = false;
1542+
1543+
var aborted = false;
1544+
1545+
this.sortGameObjects(previouslyOver);
1546+
1547+
for (var i = 0; i < total; i++)
1548+
{
1549+
var gameObject = previouslyOver[i];
1550+
1551+
// Call onOut for everything in the previouslyOver array
1552+
for (i = 0; i < total; i++)
1553+
{
1554+
gameObject = previouslyOver[i];
1555+
1556+
if (!gameObject.input)
1557+
{
1558+
continue;
1559+
}
1560+
1561+
manager.resetCursor(gameObject.input);
1562+
1563+
gameObject.emit(Events.GAMEOBJECT_POINTER_OUT, pointer, _eventContainer);
1564+
1565+
totalInteracted++;
1566+
1567+
if (_eventData.cancelled || !gameObject.input)
1568+
{
1569+
aborted = true;
1570+
break;
1571+
}
1572+
1573+
this.emit(Events.GAMEOBJECT_OUT, pointer, gameObject, _eventContainer);
1574+
1575+
if (_eventData.cancelled || !gameObject.input)
1576+
{
1577+
aborted = true;
1578+
break;
1579+
}
1580+
}
1581+
1582+
if (!aborted)
1583+
{
1584+
this.emit(Events.POINTER_OUT, pointer, previouslyOver);
1585+
}
1586+
}
1587+
1588+
this._over[pointer.id] = [];
1589+
}
1590+
1591+
return totalInteracted;
1592+
},
1593+
14201594
/**
14211595
* An internal method that handles the Pointer over and out events.
14221596
*
@@ -1509,10 +1683,11 @@ var InputPlugin = new Class({
15091683
continue;
15101684
}
15111685

1512-
gameObject.emit(Events.GAMEOBJECT_POINTER_OUT, pointer, _eventContainer);
1513-
1686+
// Reset cursor before we emit the event, in case they want to change it during the event
15141687
manager.resetCursor(gameObject.input);
15151688

1689+
gameObject.emit(Events.GAMEOBJECT_POINTER_OUT, pointer, _eventContainer);
1690+
15161691
totalInteracted++;
15171692

15181693
if (_eventData.cancelled || !gameObject.input)
@@ -1557,10 +1732,11 @@ var InputPlugin = new Class({
15571732
continue;
15581733
}
15591734

1560-
gameObject.emit(Events.GAMEOBJECT_POINTER_OVER, pointer, gameObject.input.localX, gameObject.input.localY, _eventContainer);
1561-
1735+
// Set cursor before we emit the event, in case they want to change it during the event
15621736
manager.setCursor(gameObject.input);
15631737

1738+
gameObject.emit(Events.GAMEOBJECT_POINTER_OVER, pointer, gameObject.input.localX, gameObject.input.localY, _eventContainer);
1739+
15641740
totalInteracted++;
15651741

15661742
if (_eventData.cancelled || !gameObject.input)
@@ -1631,14 +1807,6 @@ var InputPlugin = new Class({
16311807

16321808
gameObject.emit(Events.GAMEOBJECT_POINTER_UP, pointer, gameObject.input.localX, gameObject.input.localY, _eventContainer);
16331809

1634-
// Clear over and emit 'pointerout' on touch.
1635-
if (pointer.wasTouch && gameObject.input)
1636-
{
1637-
this._over[pointer.id] = [];
1638-
1639-
gameObject.emit(Events.GAMEOBJECT_POINTER_OUT, pointer, gameObject.input.localX, gameObject.input.localY, _eventContainer);
1640-
}
1641-
16421810
if (_eventData.cancelled || !gameObject.input)
16431811
{
16441812
aborted = true;

0 commit comments

Comments
 (0)