Newer
Older
pixi.js / src / loaders / spritesheetParser.js
@mathew groves mathew groves on 18 Feb 2015 2 KB some code linting..
var Resource = require('resource-loader').Resource,
    path = require('path'),
    core = require('../core');

module.exports = function ()
{
    return function (resource, next)
    {
        // if this is a spritesheet object
        if (resource.data && resource.data.frames)
        {
            var loadOptions = {
                crossOrigin: resource.crossOrigin,
                loadType: Resource.LOAD_TYPE.IMAGE
            };

            var route = path.dirname(resource.url.replace(this.baseUrl, ''));

            var resolution = core.utils.getResolutionOfUrl( resource.url );

            // load the image for this sheet
            this.add(resource.name + '_image', this.baseUrl + route + '/' + resource.data.meta.image, loadOptions, function (res)
            {
                resource.textures = {};

                var frames = resource.data.frames;

                for (var i in frames)
                {
                    var rect = frames[i].frame;

                    if (rect)
                    {
                        var size = null;
                        var trim = null;

                        if (frames[i].rotated) {
                            size = new core.math.Rectangle(rect.x, rect.y, rect.h, rect.w);
                        }
                        else {
                            size = new core.math.Rectangle(rect.x, rect.y, rect.w, rect.h);
                        }

                        //  Check to see if the sprite is trimmed
                        if (frames[i].trimmed)
                        {
                            trim = new core.math.Rectangle(
                                frames[i].spriteSourceSize.x / resolution,
                                frames[i].spriteSourceSize.y / resolution,
                                frames[i].sourceSize.w / resolution,
                                frames[i].sourceSize.h / resolution
                             );
                        }

                        // flip the width and height!
                        if (frames[i].rotated)
                        {
                            var temp = size.width;
                            size.width = size.height;
                            size.height = temp;
                        }

                        size.x /= resolution;
                        size.y /= resolution;
                        size.width /= resolution;
                        size.height /= resolution;

                        resource.textures[i] = new core.Texture(res.texture.baseTexture, size, size.clone(), trim, frames[i].rotated);

                        // lets also add the frame to pixi's global cache for fromFrame and fromImage fucntions
                        core.utils.TextureCache[i] = resource.textures[i];
                    }
                }

                next();
            });
        }
        else {
            next();
        }
    };
};