phaser/Phaser/loader/AnimationLoader.ts

153 lines
4.7 KiB
TypeScript
Raw Normal View History

/// <reference path="../_definitions.ts" />
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
/**
2013-04-18 15:49:08 +00:00
* Phaser - AnimationLoader
*
* Responsible for parsing sprite sheet and JSON data into the internal FrameData format that Phaser uses for animations.
2013-04-18 13:16:18 +00:00
*/
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
module Phaser {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
export class AnimationLoader {
2013-04-12 16:19:56 +00:00
2013-05-03 11:32:39 +00:00
/**
* Parse a sprite sheet from asset data.
* @param key {string} Asset key for the sprite sheet data.
* @param frameWidth {number} Width of animation frame.
* @param frameHeight {number} Height of animation frame.
* @param frameMax {number} Number of animation frames.
* @return {FrameData} Generated FrameData object.
2013-05-03 11:32:39 +00:00
*/
2013-04-18 13:16:18 +00:00
public static parseSpriteSheet(game: Game, key: string, frameWidth: number, frameHeight: number, frameMax: number): FrameData {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
// How big is our image?
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
var img = game.cache.getImage(key);
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if (img == null)
{
return null;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
var width = img.width;
var height = img.height;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
var row = Math.round(width / frameWidth);
var column = Math.round(height / frameHeight);
var total = row * column;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if (frameMax !== -1)
{
total = frameMax;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
// Zero or smaller than frame sizes?
if (width == 0 || height == 0 || width < frameWidth || height < frameHeight || total === 0)
{
throw new Error("AnimationLoader.parseSpriteSheet: width/height zero or width/height < given frameWidth/frameHeight");
2013-04-18 13:16:18 +00:00
return null;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
// Let's create some frames then
var data: FrameData = new FrameData();
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
var x = 0;
var y = 0;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
for (var i = 0; i < total; i++)
2013-04-12 16:19:56 +00:00
{
2013-04-18 13:16:18 +00:00
data.addFrame(new Frame(x, y, frameWidth, frameHeight, ''));
x += frameWidth;
if (x === width)
{
x = 0;
y += frameHeight;
}
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
return data;
2013-04-12 16:19:56 +00:00
}
2013-05-03 11:32:39 +00:00
/**
* Parse frame datas from json.
* @param json {object} Json data you want to parse.
* @return {FrameData} Generated FrameData object.
2013-05-03 11:32:39 +00:00
*/
2013-04-18 13:16:18 +00:00
public static parseJSONData(game: Game, json): FrameData {
2013-04-12 16:19:56 +00:00
// Malformed?
if (!json['frames'])
{
console.log(json);
throw new Error("Phaser.AnimationLoader.parseJSONData: Invalid Texture Atlas JSON given, missing 'frames' array");
}
2013-04-18 13:16:18 +00:00
// Let's create some frames then
var data: FrameData = new FrameData();
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
// By this stage frames is a fully parsed array
var frames = json['frames'];
2013-04-18 13:16:18 +00:00
var newFrame: Frame;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
for (var i = 0; i < frames.length; i++)
{
newFrame = data.addFrame(new Frame(
frames[i].frame.x,
frames[i].frame.y,
frames[i].frame.w,
frames[i].frame.h,
frames[i].filename));
newFrame.setTrim(
frames[i].trimmed,
frames[i].sourceSize.w,
frames[i].sourceSize.h,
frames[i].spriteSourceSize.x,
frames[i].spriteSourceSize.y,
frames[i].spriteSourceSize.w,
frames[i].spriteSourceSize.h);
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
return data;
2013-04-12 16:19:56 +00:00
}
public static parseXMLData(game: Game, xml, format: number): FrameData {
// Malformed?
if (!xml.getElementsByTagName('TextureAtlas'))
{
throw new Error("Phaser.AnimationLoader.parseXMLData: Invalid Texture Atlas XML given, missing <TextureAtlas> tag");
}
// Let's create some frames then
var data: FrameData = new FrameData();
var frames = xml.getElementsByTagName('SubTexture');
var newFrame: Frame;
for (var i = 0; i < frames.length; i++)
{
var frame = frames[i].attributes;
newFrame = data.addFrame(new Frame(frame.x.nodeValue, frame.y.nodeValue, frame.width.nodeValue, frame.height.nodeValue, frame.name.nodeValue));
// Trimmed?
if (frame.frameX.nodeValue != '-0' || frame.frameY.nodeValue != '-0')
{
newFrame.setTrim(true, frame.width.nodeValue, frame.height.nodeValue, Math.abs(frame.frameX.nodeValue), Math.abs(frame.frameY.nodeValue), frame.frameWidth.nodeValue, frame.frameHeight.nodeValue);
}
}
return data;
}
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
}