2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2020-01-15 12:07:09 +00:00
|
|
|
* @copyright 2020 Photon Storm Ltd.
|
2019-05-10 15:15:04 +00:00
|
|
|
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
2018-02-12 16:01:20 +00:00
|
|
|
*/
|
|
|
|
|
2018-01-19 18:23:25 +00:00
|
|
|
var Clone = require('../../utils/object/Clone');
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
|
|
|
* Parses a Texture Atlas JSON Array and adds the Frames to the Texture.
|
|
|
|
* JSON format expected to match that defined by Texture Packer, with the frames property containing an array of Frames.
|
|
|
|
*
|
|
|
|
* @function Phaser.Textures.Parsers.JSONArray
|
2018-10-10 09:49:13 +00:00
|
|
|
* @memberof Phaser.Textures.Parsers
|
2018-04-16 15:37:07 +00:00
|
|
|
* @private
|
2018-02-08 04:01:44 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Textures.Texture} texture - The Texture to add the Frames to.
|
|
|
|
* @param {integer} sourceIndex - The index of the TextureSource.
|
|
|
|
* @param {object} json - The JSON data.
|
|
|
|
*
|
|
|
|
* @return {Phaser.Textures.Texture} The Texture modified by this parser.
|
|
|
|
*/
|
2017-04-18 14:31:30 +00:00
|
|
|
var JSONArray = function (texture, sourceIndex, json)
|
2016-12-06 16:18:28 +00:00
|
|
|
{
|
|
|
|
// Malformed?
|
2018-02-09 15:22:55 +00:00
|
|
|
if (!json['frames'] && !json['textures'])
|
2016-12-06 16:18:28 +00:00
|
|
|
{
|
2018-05-04 01:37:41 +00:00
|
|
|
console.warn('Invalid Texture Atlas JSON Array');
|
2016-12-06 16:18:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-07 10:58:35 +00:00
|
|
|
// Add in a __BASE entry (for the entire atlas)
|
|
|
|
var source = texture.source[sourceIndex];
|
2018-01-19 18:23:25 +00:00
|
|
|
|
2016-12-07 10:58:35 +00:00
|
|
|
texture.add('__BASE', sourceIndex, 0, 0, source.width, source.height);
|
|
|
|
|
2016-12-06 16:18:28 +00:00
|
|
|
// By this stage frames is a fully parsed array
|
2018-02-27 13:48:45 +00:00
|
|
|
var frames = (Array.isArray(json.textures)) ? json.textures[sourceIndex].frames : json.frames;
|
2018-02-09 15:22:55 +00:00
|
|
|
|
2016-12-06 16:18:28 +00:00
|
|
|
var newFrame;
|
|
|
|
|
|
|
|
for (var i = 0; i < frames.length; i++)
|
|
|
|
{
|
|
|
|
var src = frames[i];
|
|
|
|
|
|
|
|
// The frame values are the exact coordinates to cut the frame out of the atlas from
|
|
|
|
newFrame = texture.add(src.filename, sourceIndex, src.frame.x, src.frame.y, src.frame.w, src.frame.h);
|
|
|
|
|
|
|
|
// These are the original (non-trimmed) sprite values
|
|
|
|
if (src.trimmed)
|
|
|
|
{
|
|
|
|
newFrame.setTrim(
|
|
|
|
src.sourceSize.w,
|
|
|
|
src.sourceSize.h,
|
|
|
|
src.spriteSourceSize.x,
|
|
|
|
src.spriteSourceSize.y,
|
|
|
|
src.spriteSourceSize.w,
|
|
|
|
src.spriteSourceSize.h
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src.rotated)
|
|
|
|
{
|
|
|
|
newFrame.rotated = true;
|
2017-01-23 18:30:25 +00:00
|
|
|
newFrame.updateUVsInverted();
|
2016-12-06 16:18:28 +00:00
|
|
|
}
|
2018-01-19 18:23:25 +00:00
|
|
|
|
2020-04-27 05:12:06 +00:00
|
|
|
var pivot = src.anchor || src.pivot;
|
2020-04-27 08:59:40 +00:00
|
|
|
|
2020-04-27 05:12:06 +00:00
|
|
|
if (pivot)
|
2018-02-09 15:22:55 +00:00
|
|
|
{
|
|
|
|
newFrame.customPivot = true;
|
2020-04-27 05:12:06 +00:00
|
|
|
newFrame.pivotX = pivot.x;
|
|
|
|
newFrame.pivotY = pivot.y;
|
2018-02-09 15:22:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 18:23:25 +00:00
|
|
|
// Copy over any extra data
|
|
|
|
newFrame.customData = Clone(src);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy over any additional data that was in the JSON to Texture.customData
|
|
|
|
for (var dataKey in json)
|
|
|
|
{
|
|
|
|
if (dataKey === 'frames')
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(json[dataKey]))
|
|
|
|
{
|
|
|
|
texture.customData[dataKey] = json[dataKey].slice(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
texture.customData[dataKey] = json[dataKey];
|
|
|
|
}
|
2016-12-06 16:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
};
|
|
|
|
|
2017-04-18 14:31:30 +00:00
|
|
|
module.exports = JSONArray;
|