2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2018-01-16 19:49:13 +00:00
|
|
|
var Class = require('../utils/Class');
|
2018-02-12 16:59:57 +00:00
|
|
|
var List = require('../structs/List');
|
|
|
|
var PluginManager = require('../plugins/PluginManager');
|
2018-01-16 19:49:13 +00:00
|
|
|
var StableSort = require('../utils/array/StableSort');
|
2016-12-07 01:40:56 +00:00
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @class DisplayList
|
|
|
|
* @extends Phaser.Structs.List
|
|
|
|
* @memberOf Phaser.GameObjects
|
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Scene} scene - [description]
|
|
|
|
*/
|
2017-07-04 00:59:31 +00:00
|
|
|
var DisplayList = new Class({
|
2016-12-07 01:40:56 +00:00
|
|
|
|
2018-02-12 16:59:57 +00:00
|
|
|
Extends: List,
|
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
initialize:
|
2016-12-07 01:40:56 +00:00
|
|
|
|
2018-02-12 16:59:57 +00:00
|
|
|
function DisplayList (scene)
|
2017-06-30 14:47:51 +00:00
|
|
|
{
|
2018-02-12 16:59:57 +00:00
|
|
|
List.call(this, scene);
|
2016-12-07 01:40:56 +00:00
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.DisplayList#sortChildrenFlag
|
|
|
|
* @type {boolean}
|
|
|
|
* @default false
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-11 13:59:06 +00:00
|
|
|
this.sortChildrenFlag = false;
|
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.DisplayList#scene
|
|
|
|
* @type {Phaser.Scene}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-02-12 16:59:57 +00:00
|
|
|
this.scene = scene;
|
2016-12-07 01:40:56 +00:00
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.DisplayList#systems
|
|
|
|
* @type {Phaser.Scenes.Systems}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-02-12 16:59:57 +00:00
|
|
|
this.systems = scene.sys;
|
2018-01-11 13:59:06 +00:00
|
|
|
|
2018-02-12 16:59:57 +00:00
|
|
|
if (!scene.sys.settings.isBooted)
|
|
|
|
{
|
|
|
|
scene.sys.events.once('boot', this.boot, this);
|
2018-01-11 13:59:06 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.DisplayList#boot
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-02-12 16:59:57 +00:00
|
|
|
boot: function ()
|
2018-01-11 13:59:06 +00:00
|
|
|
{
|
2018-02-12 16:59:57 +00:00
|
|
|
var eventEmitter = this.systems.events;
|
|
|
|
|
|
|
|
eventEmitter.on('shutdown', this.shutdown, this);
|
|
|
|
eventEmitter.on('destroy', this.destroy, this);
|
2018-01-11 13:59:06 +00:00
|
|
|
},
|
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
|
|
|
* Force a sort of the display list on the next call to depthSort.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.DisplayList#queueDepthSort
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-11 13:59:06 +00:00
|
|
|
queueDepthSort: function ()
|
|
|
|
{
|
|
|
|
this.sortChildrenFlag = true;
|
|
|
|
},
|
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
|
|
|
* Immediately sorts the display list if the flag is set.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.DisplayList#depthSort
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-11 13:59:06 +00:00
|
|
|
depthSort: function ()
|
|
|
|
{
|
|
|
|
if (this.sortChildrenFlag)
|
|
|
|
{
|
2018-02-12 16:59:57 +00:00
|
|
|
StableSort.inplace(this.list, this.sortByDepth);
|
2018-01-11 13:59:06 +00:00
|
|
|
|
|
|
|
this.sortChildrenFlag = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.DisplayList#sortByDepth
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.GameObjects.GameObject} childA - [description]
|
|
|
|
* @param {Phaser.GameObjects.GameObject} childB - [description]
|
|
|
|
*
|
|
|
|
* @return {integer} [description]
|
|
|
|
*/
|
2018-02-12 16:59:57 +00:00
|
|
|
sortByDepth: function (childA, childB)
|
2016-12-07 01:40:56 +00:00
|
|
|
{
|
2018-02-12 16:59:57 +00:00
|
|
|
return childA._depth - childB._depth;
|
2016-12-07 01:40:56 +00:00
|
|
|
},
|
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
|
|
|
* Given an array of Game Objects, sort the array and return it,
|
|
|
|
* so that the objects are in index order with the lowest at the bottom.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.DisplayList#sortGameObjects
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.GameObjects.GameObject[]} gameObjects - [description]
|
|
|
|
*
|
|
|
|
* @return {array} [description]
|
|
|
|
*/
|
2017-07-20 11:50:38 +00:00
|
|
|
sortGameObjects: function (gameObjects)
|
|
|
|
{
|
|
|
|
if (gameObjects === undefined) { gameObjects = this.list; }
|
|
|
|
|
|
|
|
this.scene.sys.depthSort();
|
|
|
|
|
|
|
|
return gameObjects.sort(this.sortIndexHandler.bind(this));
|
|
|
|
},
|
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
|
|
|
* Note that the given array is sorted in place, even though it isn't returned directly it will still be updated.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.DisplayList#getTopGameObject
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.GameObjects.GameObject[]} gameObjects - [description]
|
|
|
|
*
|
|
|
|
* @return {Phaser.GameObjects.GameObject} The top-most Game Object on the Display List.
|
|
|
|
*/
|
2017-07-20 11:50:38 +00:00
|
|
|
getTopGameObject: function (gameObjects)
|
|
|
|
{
|
|
|
|
this.sortGameObjects(gameObjects);
|
|
|
|
|
|
|
|
return gameObjects[gameObjects.length - 1];
|
2016-12-07 01:40:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-02-12 16:59:57 +00:00
|
|
|
PluginManager.register('DisplayList', DisplayList, 'displayList');
|
|
|
|
|
2017-07-04 00:59:31 +00:00
|
|
|
module.exports = DisplayList;
|