diff --git a/src/loaders/AtlasLoader.js b/src/loaders/AtlasLoader.js deleted file mode 100644 index b46e7f1..0000000 --- a/src/loaders/AtlasLoader.js +++ /dev/null @@ -1,217 +0,0 @@ -var core = require('../core'), - ImageLoader = require('./ImageLoader'); - -/** - * The atlas file loader is used to load in Texture Atlas data and parse it. When loaded this class will dispatch a 'loaded' event. If loading fails this class will dispatch an 'error' event. - * - * To generate the data you can use http://www.codeandweb.com/texturepacker and publish in the 'JSON' format. - * - * It is highly recommended to use texture atlases (also know as 'sprite sheets') as it allowed sprites to be batched and drawn together for highly increased rendering speed. - * Once the data has been loaded the frames are stored in the PIXI texture cache and can be accessed though Texture.fromFrameId() and Sprite.fromFrameId() - * - * @class - * @mixes eventTarget - * @namespace PIXI - * @param url {String} The url of the JSON file - * @param crossorigin {boolean} Whether requests should be treated as crossorigin - */ -function AtlasLoader(url, crossorigin) -{ - this.url = url; - this.baseUrl = url.replace(/[^\/]*$/, ''); - this.crossorigin = crossorigin; - this.loaded = false; -} - -AtlasLoader.prototype.constructor = AtlasLoader; -module.exports = AtlasLoader; - -core.utils.eventTarget.mixin(AtlasLoader.prototype); - - /** - * Starts loading the JSON file - * - */ -AtlasLoader.prototype.load = function () -{ - this.ajaxRequest = new core.utils.AjaxRequest(); - this.ajaxRequest.onreadystatechange = this.onAtlasLoaded.bind(this); - - this.ajaxRequest.open('GET', this.url, true); - - if (this.ajaxRequest.overrideMimeType) - { - this.ajaxRequest.overrideMimeType('application/json'); - } - - this.ajaxRequest.send(null); -}; - -/** - * Invoked when the Atlas has fully loaded. Parses the JSON and builds the texture frames. - * - * @private - */ -AtlasLoader.prototype.onAtlasLoaded = function () -{ - if (this.ajaxRequest.readyState === 4) - { - if (this.ajaxRequest.status === 200 || window.location.href.indexOf('http') === -1) - { - this.atlas = { - meta : { - image : [] - }, - frames : [] - }; - var result = this.ajaxRequest.responseText.split(/\r?\n/); - var lineCount = -3; - - var currentImageId = 0; - var currentFrame = null; - var nameInNextLine = false; - - var i = 0, - j = 0, - selfOnLoaded = this.onLoaded.bind(this); - - // parser without rotation support yet! - for (i = 0; i < result.length; i++) - { - result[i] = result[i].replace(/^\s+|\s+$/g, ''); - - if (result[i] === '') - { - nameInNextLine = i+1; - } - - if (result[i].length > 0) - { - if (nameInNextLine === i) - { - this.atlas.meta.image.push(result[i]); - currentImageId = this.atlas.meta.image.length - 1; - this.atlas.frames.push({}); - lineCount = -3; - } else if (lineCount > 0) - { - if (lineCount % 7 === 1) - { // frame name - if (currentFrame) - { - this.atlas.frames[currentImageId][currentFrame.name] = currentFrame; - } - currentFrame = { name: result[i], frame : {} }; - } else { - var text = result[i].split(' '); - if (lineCount % 7 === 3) - { // position - currentFrame.frame.x = Number(text[1].replace(',', '')); - currentFrame.frame.y = Number(text[2]); - } else if (lineCount % 7 === 4) - { // size - currentFrame.frame.w = Number(text[1].replace(',', '')); - currentFrame.frame.h = Number(text[2]); - } else if (lineCount % 7 === 5) - { // real size - var realSize = { - x : 0, - y : 0, - w : Number(text[1].replace(',', '')), - h : Number(text[2]) - }; - - if (realSize.w > currentFrame.frame.w || realSize.h > currentFrame.frame.h) - { - currentFrame.trimmed = true; - currentFrame.realSize = realSize; - } else { - currentFrame.trimmed = false; - } - } - } - } - lineCount++; - } - } - - if (currentFrame) - { - this.atlas.frames[currentImageId][currentFrame.name] = currentFrame; - } - - if (this.atlas.meta.image.length > 0) - { - this.images = []; - for (j = 0; j < this.atlas.meta.image.length; j++) - { - // sprite sheet - var textureUrl = this.baseUrl + this.atlas.meta.image[j]; - var frameData = this.atlas.frames[j]; - this.images.push(new ImageLoader(textureUrl, this.crossorigin)); - - for (i in frameData) - { - var rect = frameData[i].frame; - if (rect) - { - core.utils.TextureCache[i] = new core.Texture(this.images[j].texture.baseTexture, { - x: rect.x, - y: rect.y, - width: rect.w, - height: rect.h - }); - if (frameData[i].trimmed) - { - core.utils.TextureCache[i].realSize = frameData[i].realSize; - // trim in pixi not supported yet, todo update trim properties if it is done ... - core.utils.TextureCache[i].trim.x = 0; - core.utils.TextureCache[i].trim.y = 0; - } - } - } - } - - this.currentImageId = 0; - for (j = 0; j < this.images.length; j++) - { - this.images[j].on('loaded', selfOnLoaded); - } - this.images[this.currentImageId].load(); - - } else { - this.onLoaded(); - } - - } else { - this.onError(); - } - } -}; - -/** - * Invoked when json file has loaded. - * - * @private - */ -AtlasLoader.prototype.onLoaded = function () -{ - if (this.images.length - 1 > this.currentImageId) - { - this.currentImageId++; - this.images[this.currentImageId].load(); - } else { - this.loaded = true; - this.emit('loaded', { content: this }); - } -}; - -/** - * Invoked when an error occurs. - * - * @private - */ -AtlasLoader.prototype.onError = function () -{ - this.emit('error', { content: this }); -}; diff --git a/src/loaders/AtlasLoader.js b/src/loaders/AtlasLoader.js deleted file mode 100644 index b46e7f1..0000000 --- a/src/loaders/AtlasLoader.js +++ /dev/null @@ -1,217 +0,0 @@ -var core = require('../core'), - ImageLoader = require('./ImageLoader'); - -/** - * The atlas file loader is used to load in Texture Atlas data and parse it. When loaded this class will dispatch a 'loaded' event. If loading fails this class will dispatch an 'error' event. - * - * To generate the data you can use http://www.codeandweb.com/texturepacker and publish in the 'JSON' format. - * - * It is highly recommended to use texture atlases (also know as 'sprite sheets') as it allowed sprites to be batched and drawn together for highly increased rendering speed. - * Once the data has been loaded the frames are stored in the PIXI texture cache and can be accessed though Texture.fromFrameId() and Sprite.fromFrameId() - * - * @class - * @mixes eventTarget - * @namespace PIXI - * @param url {String} The url of the JSON file - * @param crossorigin {boolean} Whether requests should be treated as crossorigin - */ -function AtlasLoader(url, crossorigin) -{ - this.url = url; - this.baseUrl = url.replace(/[^\/]*$/, ''); - this.crossorigin = crossorigin; - this.loaded = false; -} - -AtlasLoader.prototype.constructor = AtlasLoader; -module.exports = AtlasLoader; - -core.utils.eventTarget.mixin(AtlasLoader.prototype); - - /** - * Starts loading the JSON file - * - */ -AtlasLoader.prototype.load = function () -{ - this.ajaxRequest = new core.utils.AjaxRequest(); - this.ajaxRequest.onreadystatechange = this.onAtlasLoaded.bind(this); - - this.ajaxRequest.open('GET', this.url, true); - - if (this.ajaxRequest.overrideMimeType) - { - this.ajaxRequest.overrideMimeType('application/json'); - } - - this.ajaxRequest.send(null); -}; - -/** - * Invoked when the Atlas has fully loaded. Parses the JSON and builds the texture frames. - * - * @private - */ -AtlasLoader.prototype.onAtlasLoaded = function () -{ - if (this.ajaxRequest.readyState === 4) - { - if (this.ajaxRequest.status === 200 || window.location.href.indexOf('http') === -1) - { - this.atlas = { - meta : { - image : [] - }, - frames : [] - }; - var result = this.ajaxRequest.responseText.split(/\r?\n/); - var lineCount = -3; - - var currentImageId = 0; - var currentFrame = null; - var nameInNextLine = false; - - var i = 0, - j = 0, - selfOnLoaded = this.onLoaded.bind(this); - - // parser without rotation support yet! - for (i = 0; i < result.length; i++) - { - result[i] = result[i].replace(/^\s+|\s+$/g, ''); - - if (result[i] === '') - { - nameInNextLine = i+1; - } - - if (result[i].length > 0) - { - if (nameInNextLine === i) - { - this.atlas.meta.image.push(result[i]); - currentImageId = this.atlas.meta.image.length - 1; - this.atlas.frames.push({}); - lineCount = -3; - } else if (lineCount > 0) - { - if (lineCount % 7 === 1) - { // frame name - if (currentFrame) - { - this.atlas.frames[currentImageId][currentFrame.name] = currentFrame; - } - currentFrame = { name: result[i], frame : {} }; - } else { - var text = result[i].split(' '); - if (lineCount % 7 === 3) - { // position - currentFrame.frame.x = Number(text[1].replace(',', '')); - currentFrame.frame.y = Number(text[2]); - } else if (lineCount % 7 === 4) - { // size - currentFrame.frame.w = Number(text[1].replace(',', '')); - currentFrame.frame.h = Number(text[2]); - } else if (lineCount % 7 === 5) - { // real size - var realSize = { - x : 0, - y : 0, - w : Number(text[1].replace(',', '')), - h : Number(text[2]) - }; - - if (realSize.w > currentFrame.frame.w || realSize.h > currentFrame.frame.h) - { - currentFrame.trimmed = true; - currentFrame.realSize = realSize; - } else { - currentFrame.trimmed = false; - } - } - } - } - lineCount++; - } - } - - if (currentFrame) - { - this.atlas.frames[currentImageId][currentFrame.name] = currentFrame; - } - - if (this.atlas.meta.image.length > 0) - { - this.images = []; - for (j = 0; j < this.atlas.meta.image.length; j++) - { - // sprite sheet - var textureUrl = this.baseUrl + this.atlas.meta.image[j]; - var frameData = this.atlas.frames[j]; - this.images.push(new ImageLoader(textureUrl, this.crossorigin)); - - for (i in frameData) - { - var rect = frameData[i].frame; - if (rect) - { - core.utils.TextureCache[i] = new core.Texture(this.images[j].texture.baseTexture, { - x: rect.x, - y: rect.y, - width: rect.w, - height: rect.h - }); - if (frameData[i].trimmed) - { - core.utils.TextureCache[i].realSize = frameData[i].realSize; - // trim in pixi not supported yet, todo update trim properties if it is done ... - core.utils.TextureCache[i].trim.x = 0; - core.utils.TextureCache[i].trim.y = 0; - } - } - } - } - - this.currentImageId = 0; - for (j = 0; j < this.images.length; j++) - { - this.images[j].on('loaded', selfOnLoaded); - } - this.images[this.currentImageId].load(); - - } else { - this.onLoaded(); - } - - } else { - this.onError(); - } - } -}; - -/** - * Invoked when json file has loaded. - * - * @private - */ -AtlasLoader.prototype.onLoaded = function () -{ - if (this.images.length - 1 > this.currentImageId) - { - this.currentImageId++; - this.images[this.currentImageId].load(); - } else { - this.loaded = true; - this.emit('loaded', { content: this }); - } -}; - -/** - * Invoked when an error occurs. - * - * @private - */ -AtlasLoader.prototype.onError = function () -{ - this.emit('error', { content: this }); -}; diff --git a/src/loaders/SpineTextureLoader.js b/src/loaders/SpineTextureLoader.js deleted file mode 100644 index c09a548..0000000 --- a/src/loaders/SpineTextureLoader.js +++ /dev/null @@ -1,56 +0,0 @@ -var core = require('../core'); - -/** - * Supporting class to load images from spine atlases as per spine spec. - * - * @class - * @mixes eventTarget - * @namespace PIXI - * @param basePath {string} Tha base path where to look for the images to be loaded - * @param crossorigin {boolean} Whether requests should be treated as crossorigin - */ -function SpineTextureLoader(basePath, crossorigin) -{ - this.basePath = basePath; - this.crossorigin = crossorigin; - this.loadingCount = 0; -} - -SpineTextureLoader.prototype.constructor = SpineTextureLoader; -module.exports = SpineTextureLoader; - -core.utils.eventTarget.mixin(SpineTextureLoader.prototype); - -/** - * Starts loading a base texture as per spine specification - * - * @param page {spine.AtlasPage} Atlas page to which texture belongs - * @param file {string} The file to load, this is just the file path relative to the base path configured in the constructor - */ -SpineTextureLoader.prototype.load = function (page, file) -{ - page.rendererObject = core.BaseTexture.fromImage(this.basePath + '/' + file, this.crossorigin); - if (!page.rendererObject.hasLoaded) - { - var scope = this; - ++scope.loadingCount; - page.rendererObject.addEventListener('loaded', function () - { - --scope.loadingCount; - scope.dispatchEvent({ - type: 'loadedBaseTexture', - content: scope - }); - }); - } -}; - -/** - * Unloads a previously loaded texture as per spine specification - * - * @param texture {BaseTexture} Texture object to destroy - */ -SpineTextureLoader.prototype.unload = function (texture) -{ - texture.destroy(true); -}; diff --git a/src/loaders/AtlasLoader.js b/src/loaders/AtlasLoader.js deleted file mode 100644 index b46e7f1..0000000 --- a/src/loaders/AtlasLoader.js +++ /dev/null @@ -1,217 +0,0 @@ -var core = require('../core'), - ImageLoader = require('./ImageLoader'); - -/** - * The atlas file loader is used to load in Texture Atlas data and parse it. When loaded this class will dispatch a 'loaded' event. If loading fails this class will dispatch an 'error' event. - * - * To generate the data you can use http://www.codeandweb.com/texturepacker and publish in the 'JSON' format. - * - * It is highly recommended to use texture atlases (also know as 'sprite sheets') as it allowed sprites to be batched and drawn together for highly increased rendering speed. - * Once the data has been loaded the frames are stored in the PIXI texture cache and can be accessed though Texture.fromFrameId() and Sprite.fromFrameId() - * - * @class - * @mixes eventTarget - * @namespace PIXI - * @param url {String} The url of the JSON file - * @param crossorigin {boolean} Whether requests should be treated as crossorigin - */ -function AtlasLoader(url, crossorigin) -{ - this.url = url; - this.baseUrl = url.replace(/[^\/]*$/, ''); - this.crossorigin = crossorigin; - this.loaded = false; -} - -AtlasLoader.prototype.constructor = AtlasLoader; -module.exports = AtlasLoader; - -core.utils.eventTarget.mixin(AtlasLoader.prototype); - - /** - * Starts loading the JSON file - * - */ -AtlasLoader.prototype.load = function () -{ - this.ajaxRequest = new core.utils.AjaxRequest(); - this.ajaxRequest.onreadystatechange = this.onAtlasLoaded.bind(this); - - this.ajaxRequest.open('GET', this.url, true); - - if (this.ajaxRequest.overrideMimeType) - { - this.ajaxRequest.overrideMimeType('application/json'); - } - - this.ajaxRequest.send(null); -}; - -/** - * Invoked when the Atlas has fully loaded. Parses the JSON and builds the texture frames. - * - * @private - */ -AtlasLoader.prototype.onAtlasLoaded = function () -{ - if (this.ajaxRequest.readyState === 4) - { - if (this.ajaxRequest.status === 200 || window.location.href.indexOf('http') === -1) - { - this.atlas = { - meta : { - image : [] - }, - frames : [] - }; - var result = this.ajaxRequest.responseText.split(/\r?\n/); - var lineCount = -3; - - var currentImageId = 0; - var currentFrame = null; - var nameInNextLine = false; - - var i = 0, - j = 0, - selfOnLoaded = this.onLoaded.bind(this); - - // parser without rotation support yet! - for (i = 0; i < result.length; i++) - { - result[i] = result[i].replace(/^\s+|\s+$/g, ''); - - if (result[i] === '') - { - nameInNextLine = i+1; - } - - if (result[i].length > 0) - { - if (nameInNextLine === i) - { - this.atlas.meta.image.push(result[i]); - currentImageId = this.atlas.meta.image.length - 1; - this.atlas.frames.push({}); - lineCount = -3; - } else if (lineCount > 0) - { - if (lineCount % 7 === 1) - { // frame name - if (currentFrame) - { - this.atlas.frames[currentImageId][currentFrame.name] = currentFrame; - } - currentFrame = { name: result[i], frame : {} }; - } else { - var text = result[i].split(' '); - if (lineCount % 7 === 3) - { // position - currentFrame.frame.x = Number(text[1].replace(',', '')); - currentFrame.frame.y = Number(text[2]); - } else if (lineCount % 7 === 4) - { // size - currentFrame.frame.w = Number(text[1].replace(',', '')); - currentFrame.frame.h = Number(text[2]); - } else if (lineCount % 7 === 5) - { // real size - var realSize = { - x : 0, - y : 0, - w : Number(text[1].replace(',', '')), - h : Number(text[2]) - }; - - if (realSize.w > currentFrame.frame.w || realSize.h > currentFrame.frame.h) - { - currentFrame.trimmed = true; - currentFrame.realSize = realSize; - } else { - currentFrame.trimmed = false; - } - } - } - } - lineCount++; - } - } - - if (currentFrame) - { - this.atlas.frames[currentImageId][currentFrame.name] = currentFrame; - } - - if (this.atlas.meta.image.length > 0) - { - this.images = []; - for (j = 0; j < this.atlas.meta.image.length; j++) - { - // sprite sheet - var textureUrl = this.baseUrl + this.atlas.meta.image[j]; - var frameData = this.atlas.frames[j]; - this.images.push(new ImageLoader(textureUrl, this.crossorigin)); - - for (i in frameData) - { - var rect = frameData[i].frame; - if (rect) - { - core.utils.TextureCache[i] = new core.Texture(this.images[j].texture.baseTexture, { - x: rect.x, - y: rect.y, - width: rect.w, - height: rect.h - }); - if (frameData[i].trimmed) - { - core.utils.TextureCache[i].realSize = frameData[i].realSize; - // trim in pixi not supported yet, todo update trim properties if it is done ... - core.utils.TextureCache[i].trim.x = 0; - core.utils.TextureCache[i].trim.y = 0; - } - } - } - } - - this.currentImageId = 0; - for (j = 0; j < this.images.length; j++) - { - this.images[j].on('loaded', selfOnLoaded); - } - this.images[this.currentImageId].load(); - - } else { - this.onLoaded(); - } - - } else { - this.onError(); - } - } -}; - -/** - * Invoked when json file has loaded. - * - * @private - */ -AtlasLoader.prototype.onLoaded = function () -{ - if (this.images.length - 1 > this.currentImageId) - { - this.currentImageId++; - this.images[this.currentImageId].load(); - } else { - this.loaded = true; - this.emit('loaded', { content: this }); - } -}; - -/** - * Invoked when an error occurs. - * - * @private - */ -AtlasLoader.prototype.onError = function () -{ - this.emit('error', { content: this }); -}; diff --git a/src/loaders/SpineTextureLoader.js b/src/loaders/SpineTextureLoader.js deleted file mode 100644 index c09a548..0000000 --- a/src/loaders/SpineTextureLoader.js +++ /dev/null @@ -1,56 +0,0 @@ -var core = require('../core'); - -/** - * Supporting class to load images from spine atlases as per spine spec. - * - * @class - * @mixes eventTarget - * @namespace PIXI - * @param basePath {string} Tha base path where to look for the images to be loaded - * @param crossorigin {boolean} Whether requests should be treated as crossorigin - */ -function SpineTextureLoader(basePath, crossorigin) -{ - this.basePath = basePath; - this.crossorigin = crossorigin; - this.loadingCount = 0; -} - -SpineTextureLoader.prototype.constructor = SpineTextureLoader; -module.exports = SpineTextureLoader; - -core.utils.eventTarget.mixin(SpineTextureLoader.prototype); - -/** - * Starts loading a base texture as per spine specification - * - * @param page {spine.AtlasPage} Atlas page to which texture belongs - * @param file {string} The file to load, this is just the file path relative to the base path configured in the constructor - */ -SpineTextureLoader.prototype.load = function (page, file) -{ - page.rendererObject = core.BaseTexture.fromImage(this.basePath + '/' + file, this.crossorigin); - if (!page.rendererObject.hasLoaded) - { - var scope = this; - ++scope.loadingCount; - page.rendererObject.addEventListener('loaded', function () - { - --scope.loadingCount; - scope.dispatchEvent({ - type: 'loadedBaseTexture', - content: scope - }); - }); - } -}; - -/** - * Unloads a previously loaded texture as per spine specification - * - * @param texture {BaseTexture} Texture object to destroy - */ -SpineTextureLoader.prototype.unload = function (texture) -{ - texture.destroy(true); -}; diff --git a/src/loaders/bitmapFontParser.js b/src/loaders/bitmapFontParser.js index cbc1ff0..6320ae4 100644 --- a/src/loaders/bitmapFontParser.js +++ b/src/loaders/bitmapFontParser.js @@ -21,7 +21,8 @@ } // skip if no data - if (!resource.data) { + if (!resource.data) + { return next(); } @@ -80,6 +81,7 @@ } resource.bitmapFont = data; + next(); }); }; diff --git a/src/loaders/AtlasLoader.js b/src/loaders/AtlasLoader.js deleted file mode 100644 index b46e7f1..0000000 --- a/src/loaders/AtlasLoader.js +++ /dev/null @@ -1,217 +0,0 @@ -var core = require('../core'), - ImageLoader = require('./ImageLoader'); - -/** - * The atlas file loader is used to load in Texture Atlas data and parse it. When loaded this class will dispatch a 'loaded' event. If loading fails this class will dispatch an 'error' event. - * - * To generate the data you can use http://www.codeandweb.com/texturepacker and publish in the 'JSON' format. - * - * It is highly recommended to use texture atlases (also know as 'sprite sheets') as it allowed sprites to be batched and drawn together for highly increased rendering speed. - * Once the data has been loaded the frames are stored in the PIXI texture cache and can be accessed though Texture.fromFrameId() and Sprite.fromFrameId() - * - * @class - * @mixes eventTarget - * @namespace PIXI - * @param url {String} The url of the JSON file - * @param crossorigin {boolean} Whether requests should be treated as crossorigin - */ -function AtlasLoader(url, crossorigin) -{ - this.url = url; - this.baseUrl = url.replace(/[^\/]*$/, ''); - this.crossorigin = crossorigin; - this.loaded = false; -} - -AtlasLoader.prototype.constructor = AtlasLoader; -module.exports = AtlasLoader; - -core.utils.eventTarget.mixin(AtlasLoader.prototype); - - /** - * Starts loading the JSON file - * - */ -AtlasLoader.prototype.load = function () -{ - this.ajaxRequest = new core.utils.AjaxRequest(); - this.ajaxRequest.onreadystatechange = this.onAtlasLoaded.bind(this); - - this.ajaxRequest.open('GET', this.url, true); - - if (this.ajaxRequest.overrideMimeType) - { - this.ajaxRequest.overrideMimeType('application/json'); - } - - this.ajaxRequest.send(null); -}; - -/** - * Invoked when the Atlas has fully loaded. Parses the JSON and builds the texture frames. - * - * @private - */ -AtlasLoader.prototype.onAtlasLoaded = function () -{ - if (this.ajaxRequest.readyState === 4) - { - if (this.ajaxRequest.status === 200 || window.location.href.indexOf('http') === -1) - { - this.atlas = { - meta : { - image : [] - }, - frames : [] - }; - var result = this.ajaxRequest.responseText.split(/\r?\n/); - var lineCount = -3; - - var currentImageId = 0; - var currentFrame = null; - var nameInNextLine = false; - - var i = 0, - j = 0, - selfOnLoaded = this.onLoaded.bind(this); - - // parser without rotation support yet! - for (i = 0; i < result.length; i++) - { - result[i] = result[i].replace(/^\s+|\s+$/g, ''); - - if (result[i] === '') - { - nameInNextLine = i+1; - } - - if (result[i].length > 0) - { - if (nameInNextLine === i) - { - this.atlas.meta.image.push(result[i]); - currentImageId = this.atlas.meta.image.length - 1; - this.atlas.frames.push({}); - lineCount = -3; - } else if (lineCount > 0) - { - if (lineCount % 7 === 1) - { // frame name - if (currentFrame) - { - this.atlas.frames[currentImageId][currentFrame.name] = currentFrame; - } - currentFrame = { name: result[i], frame : {} }; - } else { - var text = result[i].split(' '); - if (lineCount % 7 === 3) - { // position - currentFrame.frame.x = Number(text[1].replace(',', '')); - currentFrame.frame.y = Number(text[2]); - } else if (lineCount % 7 === 4) - { // size - currentFrame.frame.w = Number(text[1].replace(',', '')); - currentFrame.frame.h = Number(text[2]); - } else if (lineCount % 7 === 5) - { // real size - var realSize = { - x : 0, - y : 0, - w : Number(text[1].replace(',', '')), - h : Number(text[2]) - }; - - if (realSize.w > currentFrame.frame.w || realSize.h > currentFrame.frame.h) - { - currentFrame.trimmed = true; - currentFrame.realSize = realSize; - } else { - currentFrame.trimmed = false; - } - } - } - } - lineCount++; - } - } - - if (currentFrame) - { - this.atlas.frames[currentImageId][currentFrame.name] = currentFrame; - } - - if (this.atlas.meta.image.length > 0) - { - this.images = []; - for (j = 0; j < this.atlas.meta.image.length; j++) - { - // sprite sheet - var textureUrl = this.baseUrl + this.atlas.meta.image[j]; - var frameData = this.atlas.frames[j]; - this.images.push(new ImageLoader(textureUrl, this.crossorigin)); - - for (i in frameData) - { - var rect = frameData[i].frame; - if (rect) - { - core.utils.TextureCache[i] = new core.Texture(this.images[j].texture.baseTexture, { - x: rect.x, - y: rect.y, - width: rect.w, - height: rect.h - }); - if (frameData[i].trimmed) - { - core.utils.TextureCache[i].realSize = frameData[i].realSize; - // trim in pixi not supported yet, todo update trim properties if it is done ... - core.utils.TextureCache[i].trim.x = 0; - core.utils.TextureCache[i].trim.y = 0; - } - } - } - } - - this.currentImageId = 0; - for (j = 0; j < this.images.length; j++) - { - this.images[j].on('loaded', selfOnLoaded); - } - this.images[this.currentImageId].load(); - - } else { - this.onLoaded(); - } - - } else { - this.onError(); - } - } -}; - -/** - * Invoked when json file has loaded. - * - * @private - */ -AtlasLoader.prototype.onLoaded = function () -{ - if (this.images.length - 1 > this.currentImageId) - { - this.currentImageId++; - this.images[this.currentImageId].load(); - } else { - this.loaded = true; - this.emit('loaded', { content: this }); - } -}; - -/** - * Invoked when an error occurs. - * - * @private - */ -AtlasLoader.prototype.onError = function () -{ - this.emit('error', { content: this }); -}; diff --git a/src/loaders/SpineTextureLoader.js b/src/loaders/SpineTextureLoader.js deleted file mode 100644 index c09a548..0000000 --- a/src/loaders/SpineTextureLoader.js +++ /dev/null @@ -1,56 +0,0 @@ -var core = require('../core'); - -/** - * Supporting class to load images from spine atlases as per spine spec. - * - * @class - * @mixes eventTarget - * @namespace PIXI - * @param basePath {string} Tha base path where to look for the images to be loaded - * @param crossorigin {boolean} Whether requests should be treated as crossorigin - */ -function SpineTextureLoader(basePath, crossorigin) -{ - this.basePath = basePath; - this.crossorigin = crossorigin; - this.loadingCount = 0; -} - -SpineTextureLoader.prototype.constructor = SpineTextureLoader; -module.exports = SpineTextureLoader; - -core.utils.eventTarget.mixin(SpineTextureLoader.prototype); - -/** - * Starts loading a base texture as per spine specification - * - * @param page {spine.AtlasPage} Atlas page to which texture belongs - * @param file {string} The file to load, this is just the file path relative to the base path configured in the constructor - */ -SpineTextureLoader.prototype.load = function (page, file) -{ - page.rendererObject = core.BaseTexture.fromImage(this.basePath + '/' + file, this.crossorigin); - if (!page.rendererObject.hasLoaded) - { - var scope = this; - ++scope.loadingCount; - page.rendererObject.addEventListener('loaded', function () - { - --scope.loadingCount; - scope.dispatchEvent({ - type: 'loadedBaseTexture', - content: scope - }); - }); - } -}; - -/** - * Unloads a previously loaded texture as per spine specification - * - * @param texture {BaseTexture} Texture object to destroy - */ -SpineTextureLoader.prototype.unload = function (texture) -{ - texture.destroy(true); -}; diff --git a/src/loaders/bitmapFontParser.js b/src/loaders/bitmapFontParser.js index cbc1ff0..6320ae4 100644 --- a/src/loaders/bitmapFontParser.js +++ b/src/loaders/bitmapFontParser.js @@ -21,7 +21,8 @@ } // skip if no data - if (!resource.data) { + if (!resource.data) + { return next(); } @@ -80,6 +81,7 @@ } resource.bitmapFont = data; + next(); }); }; diff --git a/src/loaders/spineAtlasParser.js b/src/loaders/spineAtlasParser.js index 85849f2..c2725d2 100644 --- a/src/loaders/spineAtlasParser.js +++ b/src/loaders/spineAtlasParser.js @@ -1,4 +1,5 @@ var Resource = require('asset-loader').Resource, + async = require('async'), core = require('../core'), spine = require('../spine'); @@ -15,51 +16,38 @@ * 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 atlasOptions = { + crossOrigin: resource.crossOrigin, + xhrType: Resource.XHR_RESPONSE_TYPE.TEXT + }; - this.loadResource(new Resource(atlasPath, atlasOptions), function (res) { - ///////////////////////////////////////////////// - // TODO: THIS IS OLD STYLE - - - // create a new instance of a spine texture loader for this spine object // - var textureLoader = new SpineTextureLoader(this.url.substring(0, this.url.lastIndexOf('/'))); - - // create a spine atlas using the loaded text and a spine texture loader instance // - var spineAtlas = new spine.Atlas(this.ajaxRequest.responseText, textureLoader); - - // now we use an atlas attachment loader // - var attachmentLoader = new spine.AtlasAttachmentLoader(spineAtlas); + this.loadResource(new Resource(atlasPath, atlasOptions), function (res) + { + // create a spine atlas using the loaded text + var spineAtlas = new spine.Atlas(this.ajaxRequest.responseText, this.baseUrl, res.crossOrigin); // spine animation - var spineJsonParser = new spine.SkeletonJson(attachmentLoader); - var skeletonData = spineJsonParser.readSkeletonData(originalLoader.json); + var spineJsonParser = new spine.SkeletonJsonParser(new spine.AtlasAttachmentParser(spineAtlas)); + var skeletonData = spineJsonParser.readSkeletonData(resource.data); - core.utils.AnimCache[originalLoader.url] = skeletonData; - originalLoader.spine = skeletonData; - originalLoader.spineAtlas = spineAtlas; - originalLoader.spineAtlasLoader = atlasLoader; + // core.utils.AnimCache[originalLoader.url] = skeletonData; - // wait for textures to finish loading if needed - if (textureLoader.loadingCount > 0) + resource.spine = 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) { - textureLoader.addEventListener('loadedBaseTexture', function (evt) + if (page.rendererObject.hasLoaded) { - if (evt.content.content.loadingCount <= 0) - { - originalLoader.onLoaded(); - } - }); - } - else - { - originalLoader.onLoaded(); - } - - - ///////////////////////////////////////////////// - - next(); + done(); + } + else + { + page.rendererObject.once('loaded', done); + } + }, next); }); } else { diff --git a/src/loaders/AtlasLoader.js b/src/loaders/AtlasLoader.js deleted file mode 100644 index b46e7f1..0000000 --- a/src/loaders/AtlasLoader.js +++ /dev/null @@ -1,217 +0,0 @@ -var core = require('../core'), - ImageLoader = require('./ImageLoader'); - -/** - * The atlas file loader is used to load in Texture Atlas data and parse it. When loaded this class will dispatch a 'loaded' event. If loading fails this class will dispatch an 'error' event. - * - * To generate the data you can use http://www.codeandweb.com/texturepacker and publish in the 'JSON' format. - * - * It is highly recommended to use texture atlases (also know as 'sprite sheets') as it allowed sprites to be batched and drawn together for highly increased rendering speed. - * Once the data has been loaded the frames are stored in the PIXI texture cache and can be accessed though Texture.fromFrameId() and Sprite.fromFrameId() - * - * @class - * @mixes eventTarget - * @namespace PIXI - * @param url {String} The url of the JSON file - * @param crossorigin {boolean} Whether requests should be treated as crossorigin - */ -function AtlasLoader(url, crossorigin) -{ - this.url = url; - this.baseUrl = url.replace(/[^\/]*$/, ''); - this.crossorigin = crossorigin; - this.loaded = false; -} - -AtlasLoader.prototype.constructor = AtlasLoader; -module.exports = AtlasLoader; - -core.utils.eventTarget.mixin(AtlasLoader.prototype); - - /** - * Starts loading the JSON file - * - */ -AtlasLoader.prototype.load = function () -{ - this.ajaxRequest = new core.utils.AjaxRequest(); - this.ajaxRequest.onreadystatechange = this.onAtlasLoaded.bind(this); - - this.ajaxRequest.open('GET', this.url, true); - - if (this.ajaxRequest.overrideMimeType) - { - this.ajaxRequest.overrideMimeType('application/json'); - } - - this.ajaxRequest.send(null); -}; - -/** - * Invoked when the Atlas has fully loaded. Parses the JSON and builds the texture frames. - * - * @private - */ -AtlasLoader.prototype.onAtlasLoaded = function () -{ - if (this.ajaxRequest.readyState === 4) - { - if (this.ajaxRequest.status === 200 || window.location.href.indexOf('http') === -1) - { - this.atlas = { - meta : { - image : [] - }, - frames : [] - }; - var result = this.ajaxRequest.responseText.split(/\r?\n/); - var lineCount = -3; - - var currentImageId = 0; - var currentFrame = null; - var nameInNextLine = false; - - var i = 0, - j = 0, - selfOnLoaded = this.onLoaded.bind(this); - - // parser without rotation support yet! - for (i = 0; i < result.length; i++) - { - result[i] = result[i].replace(/^\s+|\s+$/g, ''); - - if (result[i] === '') - { - nameInNextLine = i+1; - } - - if (result[i].length > 0) - { - if (nameInNextLine === i) - { - this.atlas.meta.image.push(result[i]); - currentImageId = this.atlas.meta.image.length - 1; - this.atlas.frames.push({}); - lineCount = -3; - } else if (lineCount > 0) - { - if (lineCount % 7 === 1) - { // frame name - if (currentFrame) - { - this.atlas.frames[currentImageId][currentFrame.name] = currentFrame; - } - currentFrame = { name: result[i], frame : {} }; - } else { - var text = result[i].split(' '); - if (lineCount % 7 === 3) - { // position - currentFrame.frame.x = Number(text[1].replace(',', '')); - currentFrame.frame.y = Number(text[2]); - } else if (lineCount % 7 === 4) - { // size - currentFrame.frame.w = Number(text[1].replace(',', '')); - currentFrame.frame.h = Number(text[2]); - } else if (lineCount % 7 === 5) - { // real size - var realSize = { - x : 0, - y : 0, - w : Number(text[1].replace(',', '')), - h : Number(text[2]) - }; - - if (realSize.w > currentFrame.frame.w || realSize.h > currentFrame.frame.h) - { - currentFrame.trimmed = true; - currentFrame.realSize = realSize; - } else { - currentFrame.trimmed = false; - } - } - } - } - lineCount++; - } - } - - if (currentFrame) - { - this.atlas.frames[currentImageId][currentFrame.name] = currentFrame; - } - - if (this.atlas.meta.image.length > 0) - { - this.images = []; - for (j = 0; j < this.atlas.meta.image.length; j++) - { - // sprite sheet - var textureUrl = this.baseUrl + this.atlas.meta.image[j]; - var frameData = this.atlas.frames[j]; - this.images.push(new ImageLoader(textureUrl, this.crossorigin)); - - for (i in frameData) - { - var rect = frameData[i].frame; - if (rect) - { - core.utils.TextureCache[i] = new core.Texture(this.images[j].texture.baseTexture, { - x: rect.x, - y: rect.y, - width: rect.w, - height: rect.h - }); - if (frameData[i].trimmed) - { - core.utils.TextureCache[i].realSize = frameData[i].realSize; - // trim in pixi not supported yet, todo update trim properties if it is done ... - core.utils.TextureCache[i].trim.x = 0; - core.utils.TextureCache[i].trim.y = 0; - } - } - } - } - - this.currentImageId = 0; - for (j = 0; j < this.images.length; j++) - { - this.images[j].on('loaded', selfOnLoaded); - } - this.images[this.currentImageId].load(); - - } else { - this.onLoaded(); - } - - } else { - this.onError(); - } - } -}; - -/** - * Invoked when json file has loaded. - * - * @private - */ -AtlasLoader.prototype.onLoaded = function () -{ - if (this.images.length - 1 > this.currentImageId) - { - this.currentImageId++; - this.images[this.currentImageId].load(); - } else { - this.loaded = true; - this.emit('loaded', { content: this }); - } -}; - -/** - * Invoked when an error occurs. - * - * @private - */ -AtlasLoader.prototype.onError = function () -{ - this.emit('error', { content: this }); -}; diff --git a/src/loaders/SpineTextureLoader.js b/src/loaders/SpineTextureLoader.js deleted file mode 100644 index c09a548..0000000 --- a/src/loaders/SpineTextureLoader.js +++ /dev/null @@ -1,56 +0,0 @@ -var core = require('../core'); - -/** - * Supporting class to load images from spine atlases as per spine spec. - * - * @class - * @mixes eventTarget - * @namespace PIXI - * @param basePath {string} Tha base path where to look for the images to be loaded - * @param crossorigin {boolean} Whether requests should be treated as crossorigin - */ -function SpineTextureLoader(basePath, crossorigin) -{ - this.basePath = basePath; - this.crossorigin = crossorigin; - this.loadingCount = 0; -} - -SpineTextureLoader.prototype.constructor = SpineTextureLoader; -module.exports = SpineTextureLoader; - -core.utils.eventTarget.mixin(SpineTextureLoader.prototype); - -/** - * Starts loading a base texture as per spine specification - * - * @param page {spine.AtlasPage} Atlas page to which texture belongs - * @param file {string} The file to load, this is just the file path relative to the base path configured in the constructor - */ -SpineTextureLoader.prototype.load = function (page, file) -{ - page.rendererObject = core.BaseTexture.fromImage(this.basePath + '/' + file, this.crossorigin); - if (!page.rendererObject.hasLoaded) - { - var scope = this; - ++scope.loadingCount; - page.rendererObject.addEventListener('loaded', function () - { - --scope.loadingCount; - scope.dispatchEvent({ - type: 'loadedBaseTexture', - content: scope - }); - }); - } -}; - -/** - * Unloads a previously loaded texture as per spine specification - * - * @param texture {BaseTexture} Texture object to destroy - */ -SpineTextureLoader.prototype.unload = function (texture) -{ - texture.destroy(true); -}; diff --git a/src/loaders/bitmapFontParser.js b/src/loaders/bitmapFontParser.js index cbc1ff0..6320ae4 100644 --- a/src/loaders/bitmapFontParser.js +++ b/src/loaders/bitmapFontParser.js @@ -21,7 +21,8 @@ } // skip if no data - if (!resource.data) { + if (!resource.data) + { return next(); } @@ -80,6 +81,7 @@ } resource.bitmapFont = data; + next(); }); }; diff --git a/src/loaders/spineAtlasParser.js b/src/loaders/spineAtlasParser.js index 85849f2..c2725d2 100644 --- a/src/loaders/spineAtlasParser.js +++ b/src/loaders/spineAtlasParser.js @@ -1,4 +1,5 @@ var Resource = require('asset-loader').Resource, + async = require('async'), core = require('../core'), spine = require('../spine'); @@ -15,51 +16,38 @@ * 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 atlasOptions = { + crossOrigin: resource.crossOrigin, + xhrType: Resource.XHR_RESPONSE_TYPE.TEXT + }; - this.loadResource(new Resource(atlasPath, atlasOptions), function (res) { - ///////////////////////////////////////////////// - // TODO: THIS IS OLD STYLE - - - // create a new instance of a spine texture loader for this spine object // - var textureLoader = new SpineTextureLoader(this.url.substring(0, this.url.lastIndexOf('/'))); - - // create a spine atlas using the loaded text and a spine texture loader instance // - var spineAtlas = new spine.Atlas(this.ajaxRequest.responseText, textureLoader); - - // now we use an atlas attachment loader // - var attachmentLoader = new spine.AtlasAttachmentLoader(spineAtlas); + this.loadResource(new Resource(atlasPath, atlasOptions), function (res) + { + // create a spine atlas using the loaded text + var spineAtlas = new spine.Atlas(this.ajaxRequest.responseText, this.baseUrl, res.crossOrigin); // spine animation - var spineJsonParser = new spine.SkeletonJson(attachmentLoader); - var skeletonData = spineJsonParser.readSkeletonData(originalLoader.json); + var spineJsonParser = new spine.SkeletonJsonParser(new spine.AtlasAttachmentParser(spineAtlas)); + var skeletonData = spineJsonParser.readSkeletonData(resource.data); - core.utils.AnimCache[originalLoader.url] = skeletonData; - originalLoader.spine = skeletonData; - originalLoader.spineAtlas = spineAtlas; - originalLoader.spineAtlasLoader = atlasLoader; + // core.utils.AnimCache[originalLoader.url] = skeletonData; - // wait for textures to finish loading if needed - if (textureLoader.loadingCount > 0) + resource.spine = 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) { - textureLoader.addEventListener('loadedBaseTexture', function (evt) + if (page.rendererObject.hasLoaded) { - if (evt.content.content.loadingCount <= 0) - { - originalLoader.onLoaded(); - } - }); - } - else - { - originalLoader.onLoaded(); - } - - - ///////////////////////////////////////////////// - - next(); + done(); + } + else + { + page.rendererObject.once('loaded', done); + } + }, next); }); } else { diff --git a/src/spine/SpineRuntime.js b/src/spine/SpineRuntime.js index fadd1b5..80789f7 100644 --- a/src/spine/SpineRuntime.js +++ b/src/spine/SpineRuntime.js @@ -27,6 +27,8 @@ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *****************************************************************************/ +var core = require('../core'); + var spine = module.exports = { radDeg: 180 / Math.PI, @@ -1905,11 +1907,11 @@ } }; -spine.SkeletonJson = function (attachmentLoader) +spine.SkeletonJsonParser = function (attachmentLoader) { this.attachmentLoader = attachmentLoader; }; -spine.SkeletonJson.prototype = { +spine.SkeletonJsonParser.prototype = { scale: 1, readSkeletonData: function (root, name) { @@ -2486,12 +2488,20 @@ } }; -spine.Atlas = function (atlasText, textureLoader) +spine.Atlas = function (atlasText, baseUrl, crossOrigin) { - this.textureLoader = textureLoader; + if (baseUrl && baseUrl.indexOf('/') !== baseUrl.length) + { + baseUrl += '/'; + } + this.pages = []; this.regions = []; + this.texturesLoading = 0; + + var self = this; + var reader = new spine.AtlasReader(atlasText); var tuple = []; tuple.length = 4; @@ -2530,7 +2540,7 @@ else if (direction == "xy") page.uWrap = page.vWrap = spine.Atlas.TextureWrap.repeat; - textureLoader.load(page, line, this); + page.rendererObject = core.BaseTexture.fromImage(baseUrl + line, crossOrigin); this.pages.push(page); @@ -2601,7 +2611,7 @@ { var pages = this.pages; for (var i = 0, n = pages.length; i < n; i++) - this.textureLoader.unload(pages[i].rendererObject); + pages[i].rendererObject.destroy(true); }, updateUVs: function (page) { @@ -2721,11 +2731,11 @@ } }; -spine.AtlasAttachmentLoader = function (atlas) +spine.AtlasAttachmentParser = function (atlas) { this.atlas = atlas; }; -spine.AtlasAttachmentLoader.prototype = { +spine.AtlasAttachmentParser.prototype = { newRegionAttachment: function (skin, name, path) { var region = this.atlas.findRegion(path);