Skip to content

Commit abe344b

Browse files
committed
More work on the pointer handlers, nearly there!
1 parent 90b1946 commit abe344b

2 files changed

Lines changed: 54 additions & 25 deletions

File tree

src/input/InputHandler.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ Phaser.InputHandler = function (sprite) {
4949
*/
5050
this.consumePointerEvent = false;
5151

52+
this._tempPoint = new Phaser.Point;
53+
5254
};
5355

5456
Phaser.InputHandler.prototype = {
@@ -102,7 +104,7 @@ Phaser.InputHandler.prototype = {
102104
this.enabled = true;
103105

104106
// Create the signals the Input component will emit
105-
if (this.sprite.events.onInputOver == null)
107+
if (this.sprites.events && this.sprite.events.onInputOver == null)
106108
{
107109
this.sprite.events.onInputOver = new Phaser.Signal;
108110
this.sprite.events.onInputOut = new Phaser.Signal;
@@ -318,12 +320,29 @@ Phaser.InputHandler.prototype = {
318320
*/
319321
checkPointerOver: function (pointer) {
320322

321-
if (this.enabled == false || this.sprite.visible == false)
323+
if (this.enabled && this.sprite.visible)
322324
{
323-
return false;
325+
this.sprite.getLocalPosition(this._tempPoint, pointer.x, pointer.y);
326+
327+
// Check against bounds
328+
var width = this.sprite.texture.frame.width,
329+
height = this.sprite.texture.frame.height,
330+
x1 = -width * this.sprite.anchor.x,
331+
y1;
332+
333+
if(x > x1 && x < x1 + width)
334+
{
335+
y1 = -height * this.sprite.anchor.y;
336+
337+
if(y > y1 && y < y1 + height)
338+
{
339+
return true;
340+
}
341+
}
324342
}
325343
else
326344
{
345+
return false;
327346
}
328347

329348
},

src/input/Pointer.js

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ Phaser.Pointer.prototype = {
362362

363363
// Work out which object is on the top
364364
this._highestRenderOrderID = -1;
365-
this._highestRenderObject = -1;
365+
this._highestRenderObject = null;
366366
this._highestInputPriorityID = -1;
367367

368368
// Just run through the linked list
@@ -372,9 +372,12 @@ Phaser.Pointer.prototype = {
372372

373373
do
374374
{
375-
if (currentNode['update'])
375+
// If the object has a higher InputManager.PriorityID OR if the priority ID is the same as the current highest AND it has a higher renderOrderID, then set it to the top
376+
if (currentNode.priorityID > this._highestInputPriorityID || (currentNode.priorityID == this._highestInputPriorityID && currentNode.sprite.renderOrderID > this._highestRenderOrderID) && currentNode.checkPointerOver(this))
376377
{
377-
currentNode.update();
378+
this._highestRenderOrderID = currentNode.sprite.renderOrderID;
379+
this._highestInputPriorityID = currentNode.priorityID;
380+
this._highestRenderObject = currentNode;
378381
}
379382

380383
currentNode = currentNode.next;
@@ -383,6 +386,7 @@ Phaser.Pointer.prototype = {
383386
}
384387

385388

389+
/*
386390
for (var i = 0; i < this.game.input.totalTrackedObjects; i++)
387391
{
388392
if (this.game.input.inputObjects[i] && this.game.input.inputObjects[i].input && this.game.input.inputObjects[i].input.checkPointerOver(this))
@@ -396,13 +400,14 @@ Phaser.Pointer.prototype = {
396400
}
397401
}
398402
}
403+
*/
399404

400-
if (this._highestRenderObject == -1)
405+
if (this._highestRenderObject == null)
401406
{
402-
// The pointer isn't over anything, check if we've got a lingering previous target
403-
if (this.targetObject !== null)
407+
// The pointer isn't currently over anything, check if we've got a lingering previous target
408+
if (this.targetObject)
404409
{
405-
this.targetObject.input._pointerOutHandler(this);
410+
this.targetObject._pointerOutHandler(this);
406411
this.targetObject = null;
407412
}
408413
}
@@ -411,28 +416,28 @@ Phaser.Pointer.prototype = {
411416
if (this.targetObject == null)
412417
{
413418
// And now set the new one
414-
this.targetObject = this.game.input.inputObjects[this._highestRenderObject];
415-
this.targetObject.input._pointerOverHandler(this);
419+
this.targetObject = this._highestRenderObject;
420+
this._highestRenderObject._pointerOverHandler(this);
416421
}
417422
else
418423
{
419424
// We've got a target from the last update
420-
if (this.targetObject == this.game.input.inputObjects[this._highestRenderObject])
425+
if (this.targetObject == this._highestRenderObject)
421426
{
422427
// Same target as before, so update it
423-
if (this.targetObject.input.update(this) == false)
428+
if (this._highestRenderObject.update(this) == false)
424429
{
425430
this.targetObject = null;
426431
}
427432
}
428433
else
429434
{
430435
// The target has changed, so tell the old one we've left it
431-
this.targetObject.input._pointerOutHandler(this);
436+
this.targetObject._pointerOutHandler(this);
432437

433438
// And now set the new one
434-
this.targetObject = this.game.input.inputObjects[this._highestRenderObject];
435-
this.targetObject.input._pointerOverHandler(this);
439+
this.targetObject = this._highestRenderObject;
440+
this.targetObject._pointerOverHandler(this);
436441
}
437442
}
438443
}
@@ -506,17 +511,22 @@ Phaser.Pointer.prototype = {
506511
this.game.input.currentPointers--;
507512
}
508513

509-
for (var i = 0; i < this.game.input.totalTrackedObjects; i++)
514+
if (this.game.input.interactiveItems.next)
510515
{
511-
if (this.game.input.inputObjects[i] && this.game.input.inputObjects[i].input && this.game.input.inputObjects[i].input.enabled)
516+
var currentNode = this.game.input.interactiveItems.next;
517+
518+
do
512519
{
513-
this.game.input.inputObjects[i].input._releasedHandler(this);
520+
currentNode._releasedHandler(this);
521+
522+
currentNode = currentNode.next;
514523
}
524+
while (currentNode != this.game.input.interactiveItems.next)
515525
}
516526

517527
if (this.targetObject)
518528
{
519-
this.targetObject.input._releasedHandler(this);
529+
this.targetObject._releasedHandler(this);
520530
}
521531

522532
this.targetObject = null;
@@ -532,7 +542,7 @@ Phaser.Pointer.prototype = {
532542
*/
533543
justPressed: function (duration) {
534544

535-
if (typeof duration === "undefined") { duration = this.game.input.justPressedRate; }
545+
duration = duration || this.game.input.justPressedRate;
536546

537547
return (this.isDown === true && (this.timeDown + duration) > this.game.time.now);
538548

@@ -546,7 +556,7 @@ Phaser.Pointer.prototype = {
546556
*/
547557
justReleased: function (duration) {
548558

549-
if (typeof duration === "undefined") { duration = this.game.input.justReleasedRate; }
559+
duration = duration || this.game.input.justReleasedRate;
550560

551561
return (this.isUp === true && (this.timeUp + duration) > this.game.time.now);
552562

@@ -571,9 +581,9 @@ Phaser.Pointer.prototype = {
571581
this._history.length = 0;
572582
this._stateReset = true;
573583

574-
if (this.targetObject && this.targetObject.input)
584+
if (this.targetObject)
575585
{
576-
this.targetObject.input._releasedHandler(this);
586+
this.targetObject._releasedHandler(this);
577587
}
578588

579589
this.targetObject = null;

0 commit comments

Comments
 (0)