2014-04-25 14:11:54 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2014 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-11-13 09:10:05 +00:00
|
|
|
* ArraySet is a Set data structure (items must be unique within the set) that also maintains order.
|
|
|
|
* This allows specific items to be easily added or removed from the Set.
|
2014-04-25 14:11:54 +00:00
|
|
|
*
|
2014-11-13 09:10:05 +00:00
|
|
|
* Item equality (and uniqueness) is determined by the behavior of `Array.indexOf`.
|
|
|
|
*
|
|
|
|
* This used primarily by the Input subsystem.
|
|
|
|
*
|
|
|
|
* @class Phaser.ArraySet
|
2014-04-25 14:11:54 +00:00
|
|
|
* @constructor
|
2014-11-18 02:04:32 +00:00
|
|
|
* @param {any[]} [list=(new array)] - The backing array: if specified the items in the list _must_ be unique, per `Array.indexOf`, and the ownership of the array _should_ be relinquished to the ArraySet.
|
2014-04-25 14:11:54 +00:00
|
|
|
*/
|
2014-11-13 09:10:05 +00:00
|
|
|
Phaser.ArraySet = function (list) {
|
2014-04-25 14:11:54 +00:00
|
|
|
|
|
|
|
/**
|
2014-11-13 09:10:05 +00:00
|
|
|
* Current cursor position as established by `first` and `next`.
|
|
|
|
* @property {integer} position
|
2014-04-25 14:11:54 +00:00
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.position = 0;
|
|
|
|
|
|
|
|
/**
|
2014-11-18 02:04:32 +00:00
|
|
|
* The backing array.
|
|
|
|
* @property {any[]} list
|
2014-04-25 14:11:54 +00:00
|
|
|
*/
|
2014-11-13 09:10:05 +00:00
|
|
|
this.list = list || [];
|
2014-04-25 14:11:54 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-11-13 09:10:05 +00:00
|
|
|
Phaser.ArraySet.prototype = {
|
2014-04-25 14:11:54 +00:00
|
|
|
|
|
|
|
/**
|
2014-11-13 09:10:05 +00:00
|
|
|
* Adds a new element to the end of the list.
|
|
|
|
* If the item already exists in the list it is not moved.
|
2014-04-25 14:11:54 +00:00
|
|
|
*
|
2014-11-13 09:10:05 +00:00
|
|
|
* @method Phaser.ArraySet#add
|
|
|
|
* @param {*} item - The element to add to this list.
|
|
|
|
* @return {*} The item that was added.
|
2014-04-25 14:11:54 +00:00
|
|
|
*/
|
2014-11-13 09:10:05 +00:00
|
|
|
add: function (item) {
|
2014-04-25 14:11:54 +00:00
|
|
|
|
2014-11-13 09:10:05 +00:00
|
|
|
if (!this.exists(item))
|
2014-04-25 14:11:54 +00:00
|
|
|
{
|
2014-11-13 09:10:05 +00:00
|
|
|
this.list.push(item);
|
2014-04-25 14:11:54 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 09:10:05 +00:00
|
|
|
return item;
|
2014-04-25 14:11:54 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-04-25 14:24:55 +00:00
|
|
|
/**
|
2014-11-13 09:10:05 +00:00
|
|
|
* Gets the index of the item in the list, or -1 if it isn't in the list.
|
2014-04-25 14:24:55 +00:00
|
|
|
*
|
2014-11-13 09:10:05 +00:00
|
|
|
* @method Phaser.ArraySet#getIndex
|
|
|
|
* @param {*} item - The element to get the list index for.
|
|
|
|
* @return {number} The index of the item or -1 if not found.
|
2014-04-25 14:24:55 +00:00
|
|
|
*/
|
2014-11-13 09:10:05 +00:00
|
|
|
getIndex: function (item) {
|
2014-04-25 14:11:54 +00:00
|
|
|
|
2014-11-13 09:10:05 +00:00
|
|
|
return this.list.indexOf(item);
|
2014-04-25 14:11:54 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-04-25 14:24:55 +00:00
|
|
|
/**
|
2014-11-13 09:10:05 +00:00
|
|
|
* Checks for the item within this list.
|
2014-04-25 14:24:55 +00:00
|
|
|
*
|
2014-11-13 09:10:05 +00:00
|
|
|
* @method Phaser.ArraySet#exists
|
|
|
|
* @param {*} item - The element to get the list index for.
|
|
|
|
* @return {boolean} True if the item is found in the list, otherwise false.
|
2014-04-25 14:24:55 +00:00
|
|
|
*/
|
2014-11-13 09:10:05 +00:00
|
|
|
exists: function (item) {
|
2014-04-25 14:11:54 +00:00
|
|
|
|
2014-11-13 09:10:05 +00:00
|
|
|
return (this.list.indexOf(item) > -1);
|
2014-04-25 14:11:54 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-11-13 09:10:05 +00:00
|
|
|
* Removes all the items.
|
2014-04-25 14:11:54 +00:00
|
|
|
*
|
2014-11-13 09:10:05 +00:00
|
|
|
* @method Phaser.ArraySet#reset
|
2014-04-25 14:11:54 +00:00
|
|
|
*/
|
|
|
|
reset: function () {
|
|
|
|
|
|
|
|
this.list.length = 0;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-25 14:24:55 +00:00
|
|
|
* Removes the given element from this list if it exists.
|
2014-04-25 14:11:54 +00:00
|
|
|
*
|
2014-11-13 09:10:05 +00:00
|
|
|
* @method Phaser.ArraySet#remove
|
|
|
|
* @param {*} item - The item to be removed from the list.
|
|
|
|
* @return {*} item - The item that was removed.
|
2014-04-25 14:11:54 +00:00
|
|
|
*/
|
2014-11-13 09:10:05 +00:00
|
|
|
remove: function (item) {
|
2014-04-25 14:11:54 +00:00
|
|
|
|
2014-11-13 09:10:05 +00:00
|
|
|
var idx = this.list.indexOf(item);
|
2014-04-25 14:11:54 +00:00
|
|
|
|
|
|
|
if (idx > -1)
|
|
|
|
{
|
|
|
|
this.list.splice(idx, 1);
|
2014-11-13 09:10:05 +00:00
|
|
|
return item;
|
2014-04-25 14:11:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-07-15 10:20:43 +00:00
|
|
|
/**
|
|
|
|
* Sets the property `key` to the given value on all members of this list.
|
|
|
|
*
|
2014-11-13 09:10:05 +00:00
|
|
|
* @method Phaser.ArraySet#setAll
|
2014-11-18 02:04:32 +00:00
|
|
|
* @param {*} key - The propety of the item to set.
|
2014-07-15 10:20:43 +00:00
|
|
|
* @param {*} value - The value to set the property to.
|
|
|
|
*/
|
|
|
|
setAll: function (key, value) {
|
|
|
|
|
|
|
|
var i = this.list.length;
|
|
|
|
|
|
|
|
while (i--)
|
|
|
|
{
|
2014-11-13 09:10:05 +00:00
|
|
|
if (this.list[i])
|
2014-07-15 10:20:43 +00:00
|
|
|
{
|
|
|
|
this.list[i][key] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-04-25 14:11:54 +00:00
|
|
|
/**
|
|
|
|
* Calls a function on all members of this list, using the member as the context for the callback.
|
2014-11-18 02:04:32 +00:00
|
|
|
*
|
|
|
|
* If the `key` property is present it must be a function.
|
|
|
|
* The function is invoked using the item as the context.
|
2014-04-25 14:11:54 +00:00
|
|
|
*
|
2014-11-13 09:10:05 +00:00
|
|
|
* @method Phaser.ArraySet#callAll
|
2014-11-18 02:04:32 +00:00
|
|
|
* @param {string} key - The name of the property with the function to call.
|
2014-04-25 14:11:54 +00:00
|
|
|
* @param {...*} parameter - Additional parameters that will be passed to the callback.
|
|
|
|
*/
|
2014-11-18 02:04:32 +00:00
|
|
|
callAll: function (key) {
|
2014-04-25 14:11:54 +00:00
|
|
|
|
|
|
|
var args = Array.prototype.splice.call(arguments, 1);
|
|
|
|
|
|
|
|
var i = this.list.length;
|
|
|
|
|
|
|
|
while (i--)
|
|
|
|
{
|
2014-11-18 02:04:32 +00:00
|
|
|
if (this.list[i] && this.list[i][key])
|
2014-04-25 18:38:10 +00:00
|
|
|
{
|
|
|
|
this.list[i][callback].apply(this.list[i], args);
|
|
|
|
}
|
2014-04-25 14:11:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-11-13 09:10:05 +00:00
|
|
|
* Number of items in the ArraySet. Same as `list.length`.
|
|
|
|
*
|
|
|
|
* @name Phaser.ArraySet#total
|
|
|
|
* @property {integer} total
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.ArraySet.prototype, "total", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
return this.list.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the first item and resets the cursor to the start.
|
2014-04-25 14:11:54 +00:00
|
|
|
*
|
2014-11-13 09:10:05 +00:00
|
|
|
* @name Phaser.ArraySet#first
|
|
|
|
* @property {*} first
|
2014-04-25 14:11:54 +00:00
|
|
|
*/
|
2014-11-13 09:10:05 +00:00
|
|
|
Object.defineProperty(Phaser.ArraySet.prototype, "first", {
|
2014-04-25 14:11:54 +00:00
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
|
|
|
this.position = 0;
|
|
|
|
|
2014-11-13 09:10:05 +00:00
|
|
|
if (this.list.length > 0)
|
2014-04-25 14:11:54 +00:00
|
|
|
{
|
|
|
|
return this.list[0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2014-11-13 09:10:05 +00:00
|
|
|
* Returns the the next item (based on the cursor) and advances the cursor.
|
2014-04-25 14:11:54 +00:00
|
|
|
*
|
2014-11-13 09:10:05 +00:00
|
|
|
* @name Phaser.ArraySet#next
|
|
|
|
* @property {*} next
|
2014-04-25 14:11:54 +00:00
|
|
|
*/
|
2014-11-13 09:10:05 +00:00
|
|
|
Object.defineProperty(Phaser.ArraySet.prototype, "next", {
|
2014-04-25 14:11:54 +00:00
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
2014-11-13 09:10:05 +00:00
|
|
|
if (this.position < this.list.length)
|
2014-04-25 14:11:54 +00:00
|
|
|
{
|
|
|
|
this.position++;
|
|
|
|
|
|
|
|
return this.list[this.position];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-11-13 09:10:05 +00:00
|
|
|
Phaser.ArraySet.prototype.constructor = Phaser.ArraySet;
|
|
|
|
|
|
|
|
/**
|
2014-11-18 02:04:32 +00:00
|
|
|
* Phaser.ArrayList is a deprecated alias for Phaser.ArraySet.
|
2014-11-13 09:10:05 +00:00
|
|
|
*
|
|
|
|
* @class Phaser.ArrayList
|
|
|
|
* @constructor
|
|
|
|
* @deprecated 2.2.0 - Use {@link Phaser.ArraySet} instead.
|
|
|
|
*/
|
|
|
|
Phaser.ArrayList = Phaser.ArraySet;
|