phaser/src/textures/parsers/JSONHash.js

58 lines
1.6 KiB
JavaScript
Raw Normal View History

/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* Parses a JSON Array and extracts the frame data from it.
*
* @class Phaser.TextureParser.JSONArray
* @static
* @param {Phaser.Texture} texture - The parent Texture.
* @param {object} json - The JSON data from the Texture Atlas. Must be in JSON Hash format.
* @return {Phaser.FrameData} A FrameData object containing the parsed frames.
*/
2016-09-29 15:32:24 +00:00
Phaser.TextureManager.Parsers.JSONHash = function (texture, json)
{
// Malformed?
if (!json['frames'])
{
// console.warn("Phaser.AnimationParser.JSONDataHash: Invalid Texture Atlas JSON given, missing 'frames' object");
// console.log(json);
return;
}
// By this stage frames is a fully parsed array
var frames = json['frames'];
var newFrame;
for (var key in frames)
{
var src = frames[key];
// The frame values are the exact coordinates to cut the frame out of the atlas from
newFrame = texture.add(key, 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;
}
}
return data;
};