2013-08-28 06:02:55 +00:00
|
|
|
/**
|
2013-09-17 15:28:59 +00:00
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2016-04-04 21:15:01 +00:00
|
|
|
* @copyright 2016 Photon Storm Ltd.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
2013-09-17 15:28:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2013-08-28 06:02:55 +00:00
|
|
|
* FrameData is a container for Frame objects, which are the internal representation of animation data in Phaser.
|
|
|
|
*
|
2013-10-03 01:38:35 +00:00
|
|
|
* @class Phaser.FrameData
|
2013-09-17 15:28:59 +00:00
|
|
|
* @constructor
|
2013-08-28 06:02:55 +00:00
|
|
|
*/
|
2013-10-03 01:38:35 +00:00
|
|
|
Phaser.FrameData = function () {
|
2013-09-10 10:09:25 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* @property {Array} _frames - Local array of frames.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-09-10 10:09:25 +00:00
|
|
|
this._frames = [];
|
2013-09-10 19:40:34 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* @property {Array} _frameNames - Local array of frame names for name to index conversions.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-09-10 10:09:25 +00:00
|
|
|
this._frameNames = [];
|
|
|
|
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
|
2013-10-03 01:38:35 +00:00
|
|
|
Phaser.FrameData.prototype = {
|
2013-08-28 06:02:55 +00:00
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
/**
|
|
|
|
* Adds a new Frame to this FrameData collection. Typically called by the Animation.Parser and not directly.
|
|
|
|
*
|
2013-10-03 01:38:35 +00:00
|
|
|
* @method Phaser.FrameData#addFrame
|
|
|
|
* @param {Phaser.Frame} frame - The frame to add to this FrameData set.
|
|
|
|
* @return {Phaser.Frame} The frame that was just added.
|
2013-09-17 15:28:59 +00:00
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
addFrame: function (frame) {
|
|
|
|
|
|
|
|
frame.index = this._frames.length;
|
|
|
|
|
|
|
|
this._frames.push(frame);
|
|
|
|
|
2013-09-10 19:40:34 +00:00
|
|
|
if (frame.name !== '')
|
|
|
|
{
|
2013-08-28 06:02:55 +00:00
|
|
|
this._frameNames[frame.name] = frame.index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* Get a Frame by its numerical index.
|
2013-09-17 15:28:59 +00:00
|
|
|
*
|
2013-10-03 01:38:35 +00:00
|
|
|
* @method Phaser.FrameData#getFrame
|
2013-11-25 03:13:04 +00:00
|
|
|
* @param {number} index - The index of the frame you want to get.
|
|
|
|
* @return {Phaser.Frame} The frame, if found.
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
getFrame: function (index) {
|
|
|
|
|
2015-01-29 04:39:23 +00:00
|
|
|
if (index >= this._frames.length)
|
2013-09-10 19:40:34 +00:00
|
|
|
{
|
2014-02-07 04:12:23 +00:00
|
|
|
index = 0;
|
2013-08-28 06:02:55 +00:00
|
|
|
}
|
|
|
|
|
2014-02-07 04:12:23 +00:00
|
|
|
return this._frames[index];
|
2013-08-28 06:02:55 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
/**
|
|
|
|
* Get a Frame by its frame name.
|
|
|
|
*
|
2013-10-03 01:38:35 +00:00
|
|
|
* @method Phaser.FrameData#getFrameByName
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {string} name - The name of the frame you want to get.
|
2013-10-03 01:38:35 +00:00
|
|
|
* @return {Phaser.Frame} The frame, if found.
|
2013-09-17 15:28:59 +00:00
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
getFrameByName: function (name) {
|
|
|
|
|
2013-09-18 05:34:56 +00:00
|
|
|
if (typeof this._frameNames[name] === 'number')
|
2013-09-10 19:40:34 +00:00
|
|
|
{
|
2013-08-28 06:02:55 +00:00
|
|
|
return this._frames[this._frameNames[name]];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
/**
|
|
|
|
* Check if there is a Frame with the given name.
|
|
|
|
*
|
2013-10-03 01:38:35 +00:00
|
|
|
* @method Phaser.FrameData#checkFrameName
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {string} name - The name of the frame you want to check.
|
|
|
|
* @return {boolean} True if the frame is found, otherwise false.
|
2013-09-17 15:28:59 +00:00
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
checkFrameName: function (name) {
|
|
|
|
|
2013-09-10 19:40:34 +00:00
|
|
|
if (this._frameNames[name] == null)
|
|
|
|
{
|
2013-08-28 06:02:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-08-28 06:02:55 +00:00
|
|
|
},
|
|
|
|
|
2014-09-02 20:03:16 +00:00
|
|
|
/**
|
|
|
|
* Makes a copy of this FrameData including copies (not references) to all of the Frames it contains.
|
|
|
|
*
|
2014-11-25 00:20:41 +00:00
|
|
|
* @method Phaser.FrameData#clone
|
2014-09-02 20:03:16 +00:00
|
|
|
* @return {Phaser.FrameData} A clone of this object, including clones of the Frame objects it contains.
|
|
|
|
*/
|
|
|
|
clone: function () {
|
|
|
|
|
|
|
|
var output = new Phaser.FrameData();
|
|
|
|
|
|
|
|
// No input array, so we loop through all frames
|
|
|
|
for (var i = 0; i < this._frames.length; i++)
|
|
|
|
{
|
|
|
|
output._frames.push(this._frames[i].clone());
|
|
|
|
}
|
|
|
|
|
2014-11-08 17:08:29 +00:00
|
|
|
for (var p in this._frameNames)
|
2014-09-02 20:03:16 +00:00
|
|
|
{
|
2014-11-08 17:08:29 +00:00
|
|
|
if (this._frameNames.hasOwnProperty(p))
|
|
|
|
{
|
|
|
|
output._frameNames.push(this._frameNames[p]);
|
|
|
|
}
|
2014-09-02 20:03:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* Returns a range of frames based on the given start and end frame indexes and returns them in an Array.
|
2013-09-17 15:28:59 +00:00
|
|
|
*
|
2013-10-03 01:38:35 +00:00
|
|
|
* @method Phaser.FrameData#getFrameRange
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {number} start - The starting frame index.
|
2013-11-25 03:13:04 +00:00
|
|
|
* @param {number} end - The ending frame index.
|
|
|
|
* @param {Array} [output] - If given the results will be appended to the end of this array otherwise a new array will be created.
|
|
|
|
* @return {Array} An array of Frames between the start and end index values, or an empty array if none were found.
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
getFrameRange: function (start, end, output) {
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2015-07-22 09:37:15 +00:00
|
|
|
if (output === undefined) { output = []; }
|
2013-08-28 06:02:55 +00:00
|
|
|
|
2013-09-11 12:21:07 +00:00
|
|
|
for (var i = start; i <= end; i++)
|
|
|
|
{
|
2013-08-28 06:02:55 +00:00
|
|
|
output.push(this._frames[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* Returns all of the Frames in this FrameData set where the frame index is found in the input array.
|
2013-09-17 15:28:59 +00:00
|
|
|
* The frames are returned in the output array, or if none is provided in a new Array object.
|
|
|
|
*
|
2013-10-03 01:38:35 +00:00
|
|
|
* @method Phaser.FrameData#getFrames
|
2015-05-18 11:54:16 +00:00
|
|
|
* @param {Array} [frames] - An Array containing the indexes of the frames to retrieve. If the array is empty or undefined then all frames in the FrameData are returned.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {boolean} [useNumericIndex=true] - Are the given frames using numeric indexes (default) or strings? (false)
|
|
|
|
* @param {Array} [output] - If given the results will be appended to the end of this array otherwise a new array will be created.
|
2013-09-17 15:28:59 +00:00
|
|
|
* @return {Array} An array of all Frames in this FrameData set matching the given names or IDs.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2013-09-17 15:28:59 +00:00
|
|
|
getFrames: function (frames, useNumericIndex, output) {
|
2013-08-28 06:02:55 +00:00
|
|
|
|
2015-07-22 09:37:15 +00:00
|
|
|
if (useNumericIndex === undefined) { useNumericIndex = true; }
|
|
|
|
if (output === undefined) { output = []; }
|
2013-08-28 06:02:55 +00:00
|
|
|
|
2015-07-22 09:37:15 +00:00
|
|
|
if (frames === undefined || frames.length === 0)
|
2013-09-11 12:21:07 +00:00
|
|
|
{
|
2013-09-17 15:28:59 +00:00
|
|
|
// No input array, so we loop through all frames
|
|
|
|
for (var i = 0; i < this._frames.length; i++)
|
|
|
|
{
|
|
|
|
// We only need the indexes
|
|
|
|
output.push(this._frames[i]);
|
|
|
|
}
|
2013-08-28 06:02:55 +00:00
|
|
|
}
|
2013-09-17 15:28:59 +00:00
|
|
|
else
|
2013-09-11 12:21:07 +00:00
|
|
|
{
|
2013-09-17 15:28:59 +00:00
|
|
|
// Input array given, loop through that instead
|
2015-04-27 13:01:25 +00:00
|
|
|
for (var i = 0; i < frames.length; i++)
|
2013-09-11 12:21:07 +00:00
|
|
|
{
|
2013-09-17 15:28:59 +00:00
|
|
|
// Does the input array contain names or indexes?
|
|
|
|
if (useNumericIndex)
|
|
|
|
{
|
|
|
|
// The actual frame
|
2013-09-20 07:48:38 +00:00
|
|
|
output.push(this.getFrame(frames[i]));
|
2013-09-17 15:28:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The actual frame
|
2013-09-20 07:48:38 +00:00
|
|
|
output.push(this.getFrameByName(frames[i]));
|
2013-09-17 15:28:59 +00:00
|
|
|
}
|
2013-08-28 06:02:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
/**
|
|
|
|
* Returns all of the Frame indexes in this FrameData set.
|
|
|
|
* The frames indexes are returned in the output array, or if none is provided in a new Array object.
|
|
|
|
*
|
2013-10-03 01:38:35 +00:00
|
|
|
* @method Phaser.FrameData#getFrameIndexes
|
2015-05-18 11:54:16 +00:00
|
|
|
* @param {Array} [frames] - An Array containing the indexes of the frames to retrieve. If undefined or the array is empty then all frames in the FrameData are returned.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {boolean} [useNumericIndex=true] - Are the given frames using numeric indexes (default) or strings? (false)
|
|
|
|
* @param {Array} [output] - If given the results will be appended to the end of this array otherwise a new array will be created.
|
2013-09-17 15:28:59 +00:00
|
|
|
* @return {Array} An array of all Frame indexes matching the given names or IDs.
|
|
|
|
*/
|
2013-09-18 13:02:31 +00:00
|
|
|
getFrameIndexes: function (frames, useNumericIndex, output) {
|
2013-09-17 15:28:59 +00:00
|
|
|
|
2015-07-22 09:37:15 +00:00
|
|
|
if (useNumericIndex === undefined) { useNumericIndex = true; }
|
|
|
|
if (output === undefined) { output = []; }
|
2013-08-28 06:02:55 +00:00
|
|
|
|
2015-07-22 09:37:15 +00:00
|
|
|
if (frames === undefined || frames.length === 0)
|
2013-09-11 12:21:07 +00:00
|
|
|
{
|
2013-09-18 13:02:31 +00:00
|
|
|
// No frames array, so we loop through all frames
|
2015-04-27 13:01:25 +00:00
|
|
|
for (var i = 0; i < this._frames.length; i++)
|
2013-09-17 15:28:59 +00:00
|
|
|
{
|
|
|
|
output.push(this._frames[i].index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Input array given, loop through that instead
|
2015-05-18 11:54:16 +00:00
|
|
|
for (var i = 0; i < frames.length; i++)
|
2013-09-17 15:28:59 +00:00
|
|
|
{
|
2013-09-18 13:02:31 +00:00
|
|
|
// Does the frames array contain names or indexes?
|
2016-01-14 05:41:26 +00:00
|
|
|
if (useNumericIndex && this._frames[frames[i]])
|
2013-09-17 15:28:59 +00:00
|
|
|
{
|
2015-05-18 11:54:16 +00:00
|
|
|
output.push(this._frames[frames[i]].index);
|
2013-09-17 15:28:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-08 20:09:46 +00:00
|
|
|
if (this.getFrameByName(frames[i]))
|
|
|
|
{
|
|
|
|
output.push(this.getFrameByName(frames[i]).index);
|
|
|
|
}
|
2013-09-17 15:28:59 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-28 06:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
2013-09-17 15:28:59 +00:00
|
|
|
|
2016-04-05 23:43:09 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroys this FrameData collection by nulling the _frames and _frameNames arrays.
|
|
|
|
*
|
|
|
|
* @method Phaser.FrameData#destroy
|
|
|
|
*/
|
|
|
|
destroy: function () {
|
|
|
|
|
|
|
|
this._frames = null;
|
|
|
|
this._frameNames = null;
|
|
|
|
|
2013-08-28 06:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
Phaser.FrameData.prototype.constructor = Phaser.FrameData;
|
|
|
|
|
2013-10-02 00:16:40 +00:00
|
|
|
/**
|
2013-10-03 01:38:35 +00:00
|
|
|
* @name Phaser.FrameData#total
|
2013-10-02 00:16:40 +00:00
|
|
|
* @property {number} total - The total number of frames in this FrameData set.
|
2013-10-02 11:11:22 +00:00
|
|
|
* @readonly
|
2013-10-02 00:16:40 +00:00
|
|
|
*/
|
2013-10-03 01:38:35 +00:00
|
|
|
Object.defineProperty(Phaser.FrameData.prototype, "total", {
|
2013-09-11 12:21:07 +00:00
|
|
|
|
2013-08-28 06:02:55 +00:00
|
|
|
get: function () {
|
|
|
|
return this._frames.length;
|
2013-09-11 12:21:07 +00:00
|
|
|
}
|
|
|
|
|
2013-08-28 06:02:55 +00:00
|
|
|
});
|