diff --git a/src/loaders/bitmapFontParser.js b/src/loaders/bitmapFontParser.js index fa6a4b8..9e2206d 100644 --- a/src/loaders/bitmapFontParser.js +++ b/src/loaders/bitmapFontParser.js @@ -7,25 +7,16 @@ { return function (resource, next) { - if (!resource.data || navigator.isCocoonJS) + // skip if no data + if (!resource.data) { - if (window.DOMParser) - { - var domparser = new DOMParser(); - resource.data = domparser.parseFromString(this.xhr.responseText, 'text/xml'); - } - else - { - var div = document.createElement('div'); - div.innerHTML = this.xhr.responseText; - resource.data = div; - } + return next(); } - var name = resource.data.nodeName; + var name = resource.data.nodeName && resource.data.nodeName.toLowerCase(); // skip if not xml data - if (!resource.data || !name || (name.toLowerCase() !== '#document' && name.toLowerCase() !== 'div')) + if (!name || (name !== '#document' && name !== 'div')) { return next(); } diff --git a/src/loaders/bitmapFontParser.js b/src/loaders/bitmapFontParser.js index fa6a4b8..9e2206d 100644 --- a/src/loaders/bitmapFontParser.js +++ b/src/loaders/bitmapFontParser.js @@ -7,25 +7,16 @@ { return function (resource, next) { - if (!resource.data || navigator.isCocoonJS) + // skip if no data + if (!resource.data) { - if (window.DOMParser) - { - var domparser = new DOMParser(); - resource.data = domparser.parseFromString(this.xhr.responseText, 'text/xml'); - } - else - { - var div = document.createElement('div'); - div.innerHTML = this.xhr.responseText; - resource.data = div; - } + return next(); } - var name = resource.data.nodeName; + var name = resource.data.nodeName && resource.data.nodeName.toLowerCase(); // skip if not xml data - if (!resource.data || !name || (name.toLowerCase() !== '#document' && name.toLowerCase() !== 'div')) + if (!name || (name !== '#document' && name !== 'div')) { return next(); } diff --git a/src/loaders/loader.js b/src/loaders/loader.js index d022f1e..3d96fbf 100644 --- a/src/loaders/loader.js +++ b/src/loaders/loader.js @@ -21,29 +21,28 @@ * @class * @extends ResourceLoader * @memberof PIXI.loaders + * @param [baseUrl=''] {string} The base url for all resources loaded by this loader. + * @param [concurrency=10] {number} The number of resources to load concurrently. */ -var Loader = function() +function Loader(baseUrl, concurrency) { - ResourceLoader.call(this); - - // parse any json strings into objects - this.use(ResourceLoader.middleware.parsing.json()) + ResourceLoader.call(this, baseUrl, concurrency); // parse any blob into more usable objects (e.g. Image) - .use(ResourceLoader.middleware.parsing.blob()) + this.use(ResourceLoader.middleware.parsing.blob()); // parse any Image objects into textures - .use(textureParser()) + this.use(textureParser()); // parse any spritesheet data into multiple textures - .use(spritesheetParser()) + this.use(spritesheetParser()); // parse any spine data into a spine object - .use(spineAtlasParser()) + this.use(spineAtlasParser()); // parse any spritesheet data into multiple textures - .use(bitmapFontParser()); -}; + this.use(bitmapFontParser()); +} Loader.prototype = Object.create(ResourceLoader.prototype); Loader.prototype.constructor = Loader; diff --git a/src/loaders/bitmapFontParser.js b/src/loaders/bitmapFontParser.js index fa6a4b8..9e2206d 100644 --- a/src/loaders/bitmapFontParser.js +++ b/src/loaders/bitmapFontParser.js @@ -7,25 +7,16 @@ { return function (resource, next) { - if (!resource.data || navigator.isCocoonJS) + // skip if no data + if (!resource.data) { - if (window.DOMParser) - { - var domparser = new DOMParser(); - resource.data = domparser.parseFromString(this.xhr.responseText, 'text/xml'); - } - else - { - var div = document.createElement('div'); - div.innerHTML = this.xhr.responseText; - resource.data = div; - } + return next(); } - var name = resource.data.nodeName; + var name = resource.data.nodeName && resource.data.nodeName.toLowerCase(); // skip if not xml data - if (!resource.data || !name || (name.toLowerCase() !== '#document' && name.toLowerCase() !== 'div')) + if (!name || (name !== '#document' && name !== 'div')) { return next(); } diff --git a/src/loaders/loader.js b/src/loaders/loader.js index d022f1e..3d96fbf 100644 --- a/src/loaders/loader.js +++ b/src/loaders/loader.js @@ -21,29 +21,28 @@ * @class * @extends ResourceLoader * @memberof PIXI.loaders + * @param [baseUrl=''] {string} The base url for all resources loaded by this loader. + * @param [concurrency=10] {number} The number of resources to load concurrently. */ -var Loader = function() +function Loader(baseUrl, concurrency) { - ResourceLoader.call(this); - - // parse any json strings into objects - this.use(ResourceLoader.middleware.parsing.json()) + ResourceLoader.call(this, baseUrl, concurrency); // parse any blob into more usable objects (e.g. Image) - .use(ResourceLoader.middleware.parsing.blob()) + this.use(ResourceLoader.middleware.parsing.blob()); // parse any Image objects into textures - .use(textureParser()) + this.use(textureParser()); // parse any spritesheet data into multiple textures - .use(spritesheetParser()) + this.use(spritesheetParser()); // parse any spine data into a spine object - .use(spineAtlasParser()) + this.use(spineAtlasParser()); // parse any spritesheet data into multiple textures - .use(bitmapFontParser()); -}; + this.use(bitmapFontParser()); +} Loader.prototype = Object.create(ResourceLoader.prototype); Loader.prototype.constructor = Loader; diff --git a/src/loaders/spineAtlasParser.js b/src/loaders/spineAtlasParser.js index 2a42091..c6d2d3f 100644 --- a/src/loaders/spineAtlasParser.js +++ b/src/loaders/spineAtlasParser.js @@ -6,51 +6,50 @@ { return function (resource, next) { - // if this is a spritesheet object - if (resource.data && resource.data.bones) + // skip if no data + if (!resource.data || !resource.data.bones) { - /** - * use a bit of hackery to load the atlas file, here we assume that the .json, .atlas and .png files - * that correspond to the spine file are in the same base URL and that the .json and .atlas files - * have the same name - */ - var atlasPath = resource.url.substr(0, resource.url.lastIndexOf('.')) + '.atlas'; - var atlasOptions = { - crossOrigin: resource.crossOrigin, - xhrType: Resource.XHR_RESPONSE_TYPE.TEXT - }; - var baseUrl = resource.url.substr(0, resource.url.lastIndexOf('/') + 1); + return next(); + } + + /** + * use a bit of hackery to load the atlas file, here we assume that the .json, .atlas and .png files + * that correspond to the spine file are in the same base URL and that the .json and .atlas files + * have the same name + */ + var atlasPath = resource.url.substr(0, resource.url.lastIndexOf('.')) + '.atlas'; + var atlasOptions = { + crossOrigin: resource.crossOrigin, + xhrType: Resource.XHR_RESPONSE_TYPE.TEXT + }; + var baseUrl = resource.url.substr(0, resource.url.lastIndexOf('/') + 1); - this.add(resource.name + '_atlas', atlasPath, atlasOptions, function (res) + this.add(resource.name + '_atlas', atlasPath, atlasOptions, function (res) + { + // create a spine atlas using the loaded text + var spineAtlas = new spine.SpineRuntime.Atlas(this.xhr.responseText, baseUrl, res.crossOrigin); + + // spine animation + var spineJsonParser = new spine.SpineRuntime.SkeletonJsonParser(new spine.SpineRuntime.AtlasAttachmentParser(spineAtlas)); + var skeletonData = spineJsonParser.readSkeletonData(resource.data); + + resource.spineData = skeletonData; + resource.spineAtlas = spineAtlas; + + // Go through each spineAtlas.pages and wait for page.rendererObject (a baseTexture) to + // load. Once all loaded, then call the next function. + async.each(spineAtlas.pages, function (page, done) { - // create a spine atlas using the loaded text - var spineAtlas = new spine.SpineRuntime.Atlas(this.xhr.responseText, baseUrl, res.crossOrigin); - - // spine animation - var spineJsonParser = new spine.SpineRuntime.SkeletonJsonParser(new spine.SpineRuntime.AtlasAttachmentParser(spineAtlas)); - var skeletonData = spineJsonParser.readSkeletonData(resource.data); - - resource.spineData = skeletonData; - resource.spineAtlas = spineAtlas; - - // Go through each spineAtlas.pages and wait for page.rendererObject (a baseTexture) to - // load. Once all loaded, then call the next function. - async.each(spineAtlas.pages, function (page, done) + if (page.rendererObject.hasLoaded) { - if (page.rendererObject.hasLoaded) - { - done(); - } - else - { - page.rendererObject.once('loaded', done); - } - }, next); - }); - } - else { - next(); - } + done(); + } + else + { + page.rendererObject.once('loaded', done); + } + }, next); + }); }; }; diff --git a/src/loaders/bitmapFontParser.js b/src/loaders/bitmapFontParser.js index fa6a4b8..9e2206d 100644 --- a/src/loaders/bitmapFontParser.js +++ b/src/loaders/bitmapFontParser.js @@ -7,25 +7,16 @@ { return function (resource, next) { - if (!resource.data || navigator.isCocoonJS) + // skip if no data + if (!resource.data) { - if (window.DOMParser) - { - var domparser = new DOMParser(); - resource.data = domparser.parseFromString(this.xhr.responseText, 'text/xml'); - } - else - { - var div = document.createElement('div'); - div.innerHTML = this.xhr.responseText; - resource.data = div; - } + return next(); } - var name = resource.data.nodeName; + var name = resource.data.nodeName && resource.data.nodeName.toLowerCase(); // skip if not xml data - if (!resource.data || !name || (name.toLowerCase() !== '#document' && name.toLowerCase() !== 'div')) + if (!name || (name !== '#document' && name !== 'div')) { return next(); } diff --git a/src/loaders/loader.js b/src/loaders/loader.js index d022f1e..3d96fbf 100644 --- a/src/loaders/loader.js +++ b/src/loaders/loader.js @@ -21,29 +21,28 @@ * @class * @extends ResourceLoader * @memberof PIXI.loaders + * @param [baseUrl=''] {string} The base url for all resources loaded by this loader. + * @param [concurrency=10] {number} The number of resources to load concurrently. */ -var Loader = function() +function Loader(baseUrl, concurrency) { - ResourceLoader.call(this); - - // parse any json strings into objects - this.use(ResourceLoader.middleware.parsing.json()) + ResourceLoader.call(this, baseUrl, concurrency); // parse any blob into more usable objects (e.g. Image) - .use(ResourceLoader.middleware.parsing.blob()) + this.use(ResourceLoader.middleware.parsing.blob()); // parse any Image objects into textures - .use(textureParser()) + this.use(textureParser()); // parse any spritesheet data into multiple textures - .use(spritesheetParser()) + this.use(spritesheetParser()); // parse any spine data into a spine object - .use(spineAtlasParser()) + this.use(spineAtlasParser()); // parse any spritesheet data into multiple textures - .use(bitmapFontParser()); -}; + this.use(bitmapFontParser()); +} Loader.prototype = Object.create(ResourceLoader.prototype); Loader.prototype.constructor = Loader; diff --git a/src/loaders/spineAtlasParser.js b/src/loaders/spineAtlasParser.js index 2a42091..c6d2d3f 100644 --- a/src/loaders/spineAtlasParser.js +++ b/src/loaders/spineAtlasParser.js @@ -6,51 +6,50 @@ { return function (resource, next) { - // if this is a spritesheet object - if (resource.data && resource.data.bones) + // skip if no data + if (!resource.data || !resource.data.bones) { - /** - * use a bit of hackery to load the atlas file, here we assume that the .json, .atlas and .png files - * that correspond to the spine file are in the same base URL and that the .json and .atlas files - * have the same name - */ - var atlasPath = resource.url.substr(0, resource.url.lastIndexOf('.')) + '.atlas'; - var atlasOptions = { - crossOrigin: resource.crossOrigin, - xhrType: Resource.XHR_RESPONSE_TYPE.TEXT - }; - var baseUrl = resource.url.substr(0, resource.url.lastIndexOf('/') + 1); + return next(); + } + + /** + * use a bit of hackery to load the atlas file, here we assume that the .json, .atlas and .png files + * that correspond to the spine file are in the same base URL and that the .json and .atlas files + * have the same name + */ + var atlasPath = resource.url.substr(0, resource.url.lastIndexOf('.')) + '.atlas'; + var atlasOptions = { + crossOrigin: resource.crossOrigin, + xhrType: Resource.XHR_RESPONSE_TYPE.TEXT + }; + var baseUrl = resource.url.substr(0, resource.url.lastIndexOf('/') + 1); - this.add(resource.name + '_atlas', atlasPath, atlasOptions, function (res) + this.add(resource.name + '_atlas', atlasPath, atlasOptions, function (res) + { + // create a spine atlas using the loaded text + var spineAtlas = new spine.SpineRuntime.Atlas(this.xhr.responseText, baseUrl, res.crossOrigin); + + // spine animation + var spineJsonParser = new spine.SpineRuntime.SkeletonJsonParser(new spine.SpineRuntime.AtlasAttachmentParser(spineAtlas)); + var skeletonData = spineJsonParser.readSkeletonData(resource.data); + + resource.spineData = skeletonData; + resource.spineAtlas = spineAtlas; + + // Go through each spineAtlas.pages and wait for page.rendererObject (a baseTexture) to + // load. Once all loaded, then call the next function. + async.each(spineAtlas.pages, function (page, done) { - // create a spine atlas using the loaded text - var spineAtlas = new spine.SpineRuntime.Atlas(this.xhr.responseText, baseUrl, res.crossOrigin); - - // spine animation - var spineJsonParser = new spine.SpineRuntime.SkeletonJsonParser(new spine.SpineRuntime.AtlasAttachmentParser(spineAtlas)); - var skeletonData = spineJsonParser.readSkeletonData(resource.data); - - resource.spineData = skeletonData; - resource.spineAtlas = spineAtlas; - - // Go through each spineAtlas.pages and wait for page.rendererObject (a baseTexture) to - // load. Once all loaded, then call the next function. - async.each(spineAtlas.pages, function (page, done) + if (page.rendererObject.hasLoaded) { - if (page.rendererObject.hasLoaded) - { - done(); - } - else - { - page.rendererObject.once('loaded', done); - } - }, next); - }); - } - else { - next(); - } + done(); + } + else + { + page.rendererObject.once('loaded', done); + } + }, next); + }); }; }; diff --git a/src/loaders/spritesheetParser.js b/src/loaders/spritesheetParser.js index 1d6f876..685551c 100644 --- a/src/loaders/spritesheetParser.js +++ b/src/loaders/spritesheetParser.js @@ -6,77 +6,76 @@ { return function (resource, next) { - // if this is a spritesheet object - if (resource.data && resource.data.frames) + // skip if no data + if (!resource.data || !resource.data.frames) { - var loadOptions = { - crossOrigin: resource.crossOrigin, - loadType: Resource.LOAD_TYPE.IMAGE - }; + return next(); + } - var route = path.dirname(resource.url.replace(this.baseUrl, '')); + var loadOptions = { + crossOrigin: resource.crossOrigin, + loadType: Resource.LOAD_TYPE.IMAGE + }; - var resolution = core.utils.getResolutionOfUrl( resource.url ); + var route = path.dirname(resource.url.replace(this.baseUrl, '')); - // load the image for this sheet - this.add(resource.name + '_image', route + '/' + resource.data.meta.image, loadOptions, function (res) + var resolution = core.utils.getResolutionOfUrl( resource.url ); + + // load the image for this sheet + this.add(resource.name + '_image', route + '/' + resource.data.meta.image, loadOptions, function (res) + { + resource.textures = {}; + + var frames = resource.data.frames; + + for (var i in frames) { - resource.textures = {}; + var rect = frames[i].frame; - var frames = resource.data.frames; - - for (var i in frames) + if (rect) { - var rect = frames[i].frame; + var size = null; + var trim = null; - 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]; + 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); + } - next(); - }); - } - else { + // 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(); - } + }); }; };