Updated sortHandlerGO to handle any depth containers.

This commit is contained in:
Richard Davey 2018-04-12 02:11:40 +01:00
parent 9c0c037d79
commit ad4109aece
2 changed files with 38 additions and 69 deletions

View file

@ -110,6 +110,7 @@
* List is now internally using all of the new Utils.Array functions.
* Rectangle.Union will now cache all vars internally so you can use one of the input rectangles as the output rectangle without corrupting it.
* When shutting down a Matter World it will now call MatterEvents.off, clearing all events, and also `removeAllListeners` for any local events.
* Removed InputPlugin.sortInteractiveObjects because the method isn't used anywhere internally.
### Animation System Updates

View file

@ -1350,91 +1350,59 @@ var InputPlugin = new Class({
/**
* Return the child lowest down the display list (with the smallest index)
* Will iterate through all parent containers, if present.
*
* @method Phaser.Input.InputPlugin#sortHandlerGO
* @since 3.0.0
*
* @param {Phaser.GameObjects.GameObject} childA - [description]
* @param {Phaser.GameObjects.GameObject} childB - [description]
* @param {Phaser.GameObjects.GameObject} childA - The first Game Object to compare.
* @param {Phaser.GameObjects.GameObject} childB - The second Game Object to compare.
*
* @return {integer} [description]
* @return {integer} Returns either a negative or positive integer, or zero if they match.
*/
sortHandlerGO: function (childA, childB)
{
// The higher the index, the lower down the display list they are.
// So entry 0 will be the top-most item (visually)
var indexA = this.displayList.getIndex(childA);
var indexB = this.displayList.getIndex(childB);
if (indexA < indexB)
if (!childA.parentContainer && !childB.parentContainer)
{
return 1;
// Quick bail out when neither child has a container
return this.displayList.getIndex(childB) - this.displayList.getIndex(childA);
}
else if (indexA > indexB)
else if (childA.parentContainer === childB.parentContainer)
{
return -1;
// Quick bail out when both children have the same container
return childB.parentContainer.getIndex(childB) - childA.parentContainer.getIndex(childA);
}
else
{
// Container index check
var listA = childA.getIndexList();
var listB = childB.getIndexList();
var len = Math.min(listA.length, listB.length);
for (var i = 0; i < len; i++)
{
// var indexA = listA[i][0];
// var indexB = listB[i][0];
var indexA = listA[i];
var indexB = listB[i];
if (indexA === indexB)
{
// Go to the next level down
continue;
}
else
{
// Non-matching parents, so return
return indexB - indexA;
}
}
}
// Technically this shouldn't happen, but if the GO wasn't part of this display list then it'll
// have an index of -1, so in some cases it can
// Technically this shouldn't happen, but ...
return 0;
},
/**
* Return the child lowest down the display list (with the smallest index)
*
* @method Phaser.Input.InputPlugin#sortHandlerIO
* @since 3.0.0
*
* @param {Phaser.Input.InteractiveObject} childA - [description]
* @param {Phaser.Input.InteractiveObject} childB - [description]
*
* @return {integer} [description]
*/
sortHandlerIO: function (childA, childB)
{
// The higher the index, the lower down the display list they are.
// So entry 0 will be the top-most item (visually)
var indexA = this.displayList.getIndex(childA.gameObject);
var indexB = this.displayList.getIndex(childB.gameObject);
if (indexA < indexB)
{
return 1;
}
else if (indexA > indexB)
{
return -1;
}
// Technically this shouldn't happen, but if the GO wasn't part of this display list then it'll
// have an index of -1, so in some cases it can
return 0;
},
/**
* Given an array of Interactive Objects, sort the array and return it,
* so that the objects are in index order with the lowest at the bottom.
*
* @method Phaser.Input.InputPlugin#sortInteractiveObjects
* @since 3.0.0
*
* @param {Phaser.Input.InteractiveObject[]} interactiveObjects - [description]
*
* @return {Phaser.Input.InteractiveObject[]} [description]
*/
sortInteractiveObjects: function (interactiveObjects)
{
if (interactiveObjects.length < 2)
{
return interactiveObjects;
}
this.scene.sys.depthSort();
return interactiveObjects.sort(this.sortHandlerIO.bind(this));
},
/**
* [description]
*