diff --git a/src/pixi/display/Sprite.js b/src/pixi/display/Sprite.js index 5cf3105..cffecce 100644 --- a/src/pixi/display/Sprite.js +++ b/src/pixi/display/Sprite.js @@ -82,7 +82,7 @@ else { this.onTextureUpdateBind = this.onTextureUpdate.bind(this); - this.texture.addEventListener( 'update', this.onTextureUpdateBind ); + this.texture.on( 'update', this.onTextureUpdateBind ); } this.renderable = true; diff --git a/src/pixi/display/Sprite.js b/src/pixi/display/Sprite.js index 5cf3105..cffecce 100644 --- a/src/pixi/display/Sprite.js +++ b/src/pixi/display/Sprite.js @@ -82,7 +82,7 @@ else { this.onTextureUpdateBind = this.onTextureUpdate.bind(this); - this.texture.addEventListener( 'update', this.onTextureUpdateBind ); + this.texture.on( 'update', this.onTextureUpdateBind ); } this.renderable = true; diff --git a/src/pixi/loaders/AssetLoader.js b/src/pixi/loaders/AssetLoader.js index feab211..cf6d3d2 100644 --- a/src/pixi/loaders/AssetLoader.js +++ b/src/pixi/loaders/AssetLoader.js @@ -20,8 +20,6 @@ */ PIXI.AssetLoader = function(assetURLs, crossorigin) { - PIXI.EventTarget.call(this); - /** * The array of asset URLs that are going to be loaded * @@ -58,6 +56,8 @@ }; }; +PIXI.EventTarget.mixin(PIXI.AssetLoader.prototype); + /** * Fired when an item has loaded * @event onProgress @@ -114,7 +114,7 @@ var scope = this; function onLoad(evt) { - scope.onAssetLoaded(evt.content); + scope.onAssetLoaded(evt.data.content); } this.loadCount = this.assetURLs.length; @@ -135,7 +135,7 @@ var loader = new Constructor(fileName, this.crossorigin); - loader.addEventListener('loaded', onLoad); + loader.on('loaded', onLoad); loader.load(); } }; @@ -149,12 +149,12 @@ PIXI.AssetLoader.prototype.onAssetLoaded = function(loader) { this.loadCount--; - this.dispatchEvent({ type: 'onProgress', content: this, loader: loader }); + this.emit('onProgress', { content: this, loader: loader }); if (this.onProgress) this.onProgress(loader); if (!this.loadCount) { - this.dispatchEvent({type: 'onComplete', content: this}); + this.emit('onComplete', { content: this }); if(this.onComplete) this.onComplete(); } }; diff --git a/src/pixi/display/Sprite.js b/src/pixi/display/Sprite.js index 5cf3105..cffecce 100644 --- a/src/pixi/display/Sprite.js +++ b/src/pixi/display/Sprite.js @@ -82,7 +82,7 @@ else { this.onTextureUpdateBind = this.onTextureUpdate.bind(this); - this.texture.addEventListener( 'update', this.onTextureUpdateBind ); + this.texture.on( 'update', this.onTextureUpdateBind ); } this.renderable = true; diff --git a/src/pixi/loaders/AssetLoader.js b/src/pixi/loaders/AssetLoader.js index feab211..cf6d3d2 100644 --- a/src/pixi/loaders/AssetLoader.js +++ b/src/pixi/loaders/AssetLoader.js @@ -20,8 +20,6 @@ */ PIXI.AssetLoader = function(assetURLs, crossorigin) { - PIXI.EventTarget.call(this); - /** * The array of asset URLs that are going to be loaded * @@ -58,6 +56,8 @@ }; }; +PIXI.EventTarget.mixin(PIXI.AssetLoader.prototype); + /** * Fired when an item has loaded * @event onProgress @@ -114,7 +114,7 @@ var scope = this; function onLoad(evt) { - scope.onAssetLoaded(evt.content); + scope.onAssetLoaded(evt.data.content); } this.loadCount = this.assetURLs.length; @@ -135,7 +135,7 @@ var loader = new Constructor(fileName, this.crossorigin); - loader.addEventListener('loaded', onLoad); + loader.on('loaded', onLoad); loader.load(); } }; @@ -149,12 +149,12 @@ PIXI.AssetLoader.prototype.onAssetLoaded = function(loader) { this.loadCount--; - this.dispatchEvent({ type: 'onProgress', content: this, loader: loader }); + this.emit('onProgress', { content: this, loader: loader }); if (this.onProgress) this.onProgress(loader); if (!this.loadCount) { - this.dispatchEvent({type: 'onComplete', content: this}); + this.emit('onComplete', { content: this }); if(this.onComplete) this.onComplete(); } }; diff --git a/src/pixi/loaders/AtlasLoader.js b/src/pixi/loaders/AtlasLoader.js index 98e32c9..aa6c381 100644 --- a/src/pixi/loaders/AtlasLoader.js +++ b/src/pixi/loaders/AtlasLoader.js @@ -7,14 +7,13 @@ * When loaded this class will dispatch a 'loaded' event * If loading fails this class will dispatch an 'error' event * @class AtlasLoader - * @extends EventTarget + * @uses EventTarget * @constructor * @param {String} url the url of the JSON file * @param {Boolean} crossorigin */ PIXI.AtlasLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); this.url = url; this.baseUrl = url.replace(/[^\/]*$/, ''); this.crossorigin = crossorigin; @@ -25,6 +24,7 @@ // constructor PIXI.AtlasLoader.constructor = PIXI.AtlasLoader; +PIXI.EventTarget.mixin(PIXI.AtlasLoader.prototype); /** * Starts loading the JSON file @@ -145,7 +145,7 @@ this.currentImageId = 0; for (j = 0; j < this.images.length; j++) { - this.images[j].addEventListener('loaded', selfOnLoaded); + this.images[j].on('loaded', selfOnLoaded); } this.images[this.currentImageId].load(); @@ -170,10 +170,7 @@ this.images[this.currentImageId].load(); } else { this.loaded = true; - this.dispatchEvent({ - type: 'loaded', - content: this - }); + this.emit('loaded', { content: this }); } }; @@ -183,8 +180,5 @@ * @private */ PIXI.AtlasLoader.prototype.onError = function () { - this.dispatchEvent({ - type: 'error', - content: this - }); + this.emit('error', { content: this }); }; diff --git a/src/pixi/display/Sprite.js b/src/pixi/display/Sprite.js index 5cf3105..cffecce 100644 --- a/src/pixi/display/Sprite.js +++ b/src/pixi/display/Sprite.js @@ -82,7 +82,7 @@ else { this.onTextureUpdateBind = this.onTextureUpdate.bind(this); - this.texture.addEventListener( 'update', this.onTextureUpdateBind ); + this.texture.on( 'update', this.onTextureUpdateBind ); } this.renderable = true; diff --git a/src/pixi/loaders/AssetLoader.js b/src/pixi/loaders/AssetLoader.js index feab211..cf6d3d2 100644 --- a/src/pixi/loaders/AssetLoader.js +++ b/src/pixi/loaders/AssetLoader.js @@ -20,8 +20,6 @@ */ PIXI.AssetLoader = function(assetURLs, crossorigin) { - PIXI.EventTarget.call(this); - /** * The array of asset URLs that are going to be loaded * @@ -58,6 +56,8 @@ }; }; +PIXI.EventTarget.mixin(PIXI.AssetLoader.prototype); + /** * Fired when an item has loaded * @event onProgress @@ -114,7 +114,7 @@ var scope = this; function onLoad(evt) { - scope.onAssetLoaded(evt.content); + scope.onAssetLoaded(evt.data.content); } this.loadCount = this.assetURLs.length; @@ -135,7 +135,7 @@ var loader = new Constructor(fileName, this.crossorigin); - loader.addEventListener('loaded', onLoad); + loader.on('loaded', onLoad); loader.load(); } }; @@ -149,12 +149,12 @@ PIXI.AssetLoader.prototype.onAssetLoaded = function(loader) { this.loadCount--; - this.dispatchEvent({ type: 'onProgress', content: this, loader: loader }); + this.emit('onProgress', { content: this, loader: loader }); if (this.onProgress) this.onProgress(loader); if (!this.loadCount) { - this.dispatchEvent({type: 'onComplete', content: this}); + this.emit('onComplete', { content: this }); if(this.onComplete) this.onComplete(); } }; diff --git a/src/pixi/loaders/AtlasLoader.js b/src/pixi/loaders/AtlasLoader.js index 98e32c9..aa6c381 100644 --- a/src/pixi/loaders/AtlasLoader.js +++ b/src/pixi/loaders/AtlasLoader.js @@ -7,14 +7,13 @@ * When loaded this class will dispatch a 'loaded' event * If loading fails this class will dispatch an 'error' event * @class AtlasLoader - * @extends EventTarget + * @uses EventTarget * @constructor * @param {String} url the url of the JSON file * @param {Boolean} crossorigin */ PIXI.AtlasLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); this.url = url; this.baseUrl = url.replace(/[^\/]*$/, ''); this.crossorigin = crossorigin; @@ -25,6 +24,7 @@ // constructor PIXI.AtlasLoader.constructor = PIXI.AtlasLoader; +PIXI.EventTarget.mixin(PIXI.AtlasLoader.prototype); /** * Starts loading the JSON file @@ -145,7 +145,7 @@ this.currentImageId = 0; for (j = 0; j < this.images.length; j++) { - this.images[j].addEventListener('loaded', selfOnLoaded); + this.images[j].on('loaded', selfOnLoaded); } this.images[this.currentImageId].load(); @@ -170,10 +170,7 @@ this.images[this.currentImageId].load(); } else { this.loaded = true; - this.dispatchEvent({ - type: 'loaded', - content: this - }); + this.emit('loaded', { content: this }); } }; @@ -183,8 +180,5 @@ * @private */ PIXI.AtlasLoader.prototype.onError = function () { - this.dispatchEvent({ - type: 'error', - content: this - }); + this.emit('error', { content: this }); }; diff --git a/src/pixi/loaders/BitmapFontLoader.js b/src/pixi/loaders/BitmapFontLoader.js index f87fbdd..bbd1fed 100644 --- a/src/pixi/loaders/BitmapFontLoader.js +++ b/src/pixi/loaders/BitmapFontLoader.js @@ -21,7 +21,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -59,6 +58,7 @@ // constructor PIXI.BitmapFontLoader.prototype.constructor = PIXI.BitmapFontLoader; +PIXI.EventTarget.mixin(PIXI.BitmapFontLoader.prototype); /** * Loads the XML font data @@ -163,5 +163,5 @@ */ PIXI.BitmapFontLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/display/Sprite.js b/src/pixi/display/Sprite.js index 5cf3105..cffecce 100644 --- a/src/pixi/display/Sprite.js +++ b/src/pixi/display/Sprite.js @@ -82,7 +82,7 @@ else { this.onTextureUpdateBind = this.onTextureUpdate.bind(this); - this.texture.addEventListener( 'update', this.onTextureUpdateBind ); + this.texture.on( 'update', this.onTextureUpdateBind ); } this.renderable = true; diff --git a/src/pixi/loaders/AssetLoader.js b/src/pixi/loaders/AssetLoader.js index feab211..cf6d3d2 100644 --- a/src/pixi/loaders/AssetLoader.js +++ b/src/pixi/loaders/AssetLoader.js @@ -20,8 +20,6 @@ */ PIXI.AssetLoader = function(assetURLs, crossorigin) { - PIXI.EventTarget.call(this); - /** * The array of asset URLs that are going to be loaded * @@ -58,6 +56,8 @@ }; }; +PIXI.EventTarget.mixin(PIXI.AssetLoader.prototype); + /** * Fired when an item has loaded * @event onProgress @@ -114,7 +114,7 @@ var scope = this; function onLoad(evt) { - scope.onAssetLoaded(evt.content); + scope.onAssetLoaded(evt.data.content); } this.loadCount = this.assetURLs.length; @@ -135,7 +135,7 @@ var loader = new Constructor(fileName, this.crossorigin); - loader.addEventListener('loaded', onLoad); + loader.on('loaded', onLoad); loader.load(); } }; @@ -149,12 +149,12 @@ PIXI.AssetLoader.prototype.onAssetLoaded = function(loader) { this.loadCount--; - this.dispatchEvent({ type: 'onProgress', content: this, loader: loader }); + this.emit('onProgress', { content: this, loader: loader }); if (this.onProgress) this.onProgress(loader); if (!this.loadCount) { - this.dispatchEvent({type: 'onComplete', content: this}); + this.emit('onComplete', { content: this }); if(this.onComplete) this.onComplete(); } }; diff --git a/src/pixi/loaders/AtlasLoader.js b/src/pixi/loaders/AtlasLoader.js index 98e32c9..aa6c381 100644 --- a/src/pixi/loaders/AtlasLoader.js +++ b/src/pixi/loaders/AtlasLoader.js @@ -7,14 +7,13 @@ * When loaded this class will dispatch a 'loaded' event * If loading fails this class will dispatch an 'error' event * @class AtlasLoader - * @extends EventTarget + * @uses EventTarget * @constructor * @param {String} url the url of the JSON file * @param {Boolean} crossorigin */ PIXI.AtlasLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); this.url = url; this.baseUrl = url.replace(/[^\/]*$/, ''); this.crossorigin = crossorigin; @@ -25,6 +24,7 @@ // constructor PIXI.AtlasLoader.constructor = PIXI.AtlasLoader; +PIXI.EventTarget.mixin(PIXI.AtlasLoader.prototype); /** * Starts loading the JSON file @@ -145,7 +145,7 @@ this.currentImageId = 0; for (j = 0; j < this.images.length; j++) { - this.images[j].addEventListener('loaded', selfOnLoaded); + this.images[j].on('loaded', selfOnLoaded); } this.images[this.currentImageId].load(); @@ -170,10 +170,7 @@ this.images[this.currentImageId].load(); } else { this.loaded = true; - this.dispatchEvent({ - type: 'loaded', - content: this - }); + this.emit('loaded', { content: this }); } }; @@ -183,8 +180,5 @@ * @private */ PIXI.AtlasLoader.prototype.onError = function () { - this.dispatchEvent({ - type: 'error', - content: this - }); + this.emit('error', { content: this }); }; diff --git a/src/pixi/loaders/BitmapFontLoader.js b/src/pixi/loaders/BitmapFontLoader.js index f87fbdd..bbd1fed 100644 --- a/src/pixi/loaders/BitmapFontLoader.js +++ b/src/pixi/loaders/BitmapFontLoader.js @@ -21,7 +21,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -59,6 +58,7 @@ // constructor PIXI.BitmapFontLoader.prototype.constructor = PIXI.BitmapFontLoader; +PIXI.EventTarget.mixin(PIXI.BitmapFontLoader.prototype); /** * Loads the XML font data @@ -163,5 +163,5 @@ */ PIXI.BitmapFontLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/loaders/ImageLoader.js b/src/pixi/loaders/ImageLoader.js index ad4c61c..f1606f1 100644 --- a/src/pixi/loaders/ImageLoader.js +++ b/src/pixi/loaders/ImageLoader.js @@ -15,8 +15,6 @@ */ PIXI.ImageLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The texture being loaded * @@ -35,6 +33,7 @@ // constructor PIXI.ImageLoader.prototype.constructor = PIXI.ImageLoader; +PIXI.EventTarget.mixin(PIXI.ImageLoader.prototype); /** * Loads image or takes it from cache @@ -45,7 +44,7 @@ { if(!this.texture.baseTexture.hasLoaded) { - this.texture.baseTexture.addEventListener('loaded', this.onLoaded.bind(this)); + this.texture.baseTexture.on('loaded', this.onLoaded.bind(this)); } else { @@ -61,7 +60,7 @@ */ PIXI.ImageLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; /** diff --git a/src/pixi/display/Sprite.js b/src/pixi/display/Sprite.js index 5cf3105..cffecce 100644 --- a/src/pixi/display/Sprite.js +++ b/src/pixi/display/Sprite.js @@ -82,7 +82,7 @@ else { this.onTextureUpdateBind = this.onTextureUpdate.bind(this); - this.texture.addEventListener( 'update', this.onTextureUpdateBind ); + this.texture.on( 'update', this.onTextureUpdateBind ); } this.renderable = true; diff --git a/src/pixi/loaders/AssetLoader.js b/src/pixi/loaders/AssetLoader.js index feab211..cf6d3d2 100644 --- a/src/pixi/loaders/AssetLoader.js +++ b/src/pixi/loaders/AssetLoader.js @@ -20,8 +20,6 @@ */ PIXI.AssetLoader = function(assetURLs, crossorigin) { - PIXI.EventTarget.call(this); - /** * The array of asset URLs that are going to be loaded * @@ -58,6 +56,8 @@ }; }; +PIXI.EventTarget.mixin(PIXI.AssetLoader.prototype); + /** * Fired when an item has loaded * @event onProgress @@ -114,7 +114,7 @@ var scope = this; function onLoad(evt) { - scope.onAssetLoaded(evt.content); + scope.onAssetLoaded(evt.data.content); } this.loadCount = this.assetURLs.length; @@ -135,7 +135,7 @@ var loader = new Constructor(fileName, this.crossorigin); - loader.addEventListener('loaded', onLoad); + loader.on('loaded', onLoad); loader.load(); } }; @@ -149,12 +149,12 @@ PIXI.AssetLoader.prototype.onAssetLoaded = function(loader) { this.loadCount--; - this.dispatchEvent({ type: 'onProgress', content: this, loader: loader }); + this.emit('onProgress', { content: this, loader: loader }); if (this.onProgress) this.onProgress(loader); if (!this.loadCount) { - this.dispatchEvent({type: 'onComplete', content: this}); + this.emit('onComplete', { content: this }); if(this.onComplete) this.onComplete(); } }; diff --git a/src/pixi/loaders/AtlasLoader.js b/src/pixi/loaders/AtlasLoader.js index 98e32c9..aa6c381 100644 --- a/src/pixi/loaders/AtlasLoader.js +++ b/src/pixi/loaders/AtlasLoader.js @@ -7,14 +7,13 @@ * When loaded this class will dispatch a 'loaded' event * If loading fails this class will dispatch an 'error' event * @class AtlasLoader - * @extends EventTarget + * @uses EventTarget * @constructor * @param {String} url the url of the JSON file * @param {Boolean} crossorigin */ PIXI.AtlasLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); this.url = url; this.baseUrl = url.replace(/[^\/]*$/, ''); this.crossorigin = crossorigin; @@ -25,6 +24,7 @@ // constructor PIXI.AtlasLoader.constructor = PIXI.AtlasLoader; +PIXI.EventTarget.mixin(PIXI.AtlasLoader.prototype); /** * Starts loading the JSON file @@ -145,7 +145,7 @@ this.currentImageId = 0; for (j = 0; j < this.images.length; j++) { - this.images[j].addEventListener('loaded', selfOnLoaded); + this.images[j].on('loaded', selfOnLoaded); } this.images[this.currentImageId].load(); @@ -170,10 +170,7 @@ this.images[this.currentImageId].load(); } else { this.loaded = true; - this.dispatchEvent({ - type: 'loaded', - content: this - }); + this.emit('loaded', { content: this }); } }; @@ -183,8 +180,5 @@ * @private */ PIXI.AtlasLoader.prototype.onError = function () { - this.dispatchEvent({ - type: 'error', - content: this - }); + this.emit('error', { content: this }); }; diff --git a/src/pixi/loaders/BitmapFontLoader.js b/src/pixi/loaders/BitmapFontLoader.js index f87fbdd..bbd1fed 100644 --- a/src/pixi/loaders/BitmapFontLoader.js +++ b/src/pixi/loaders/BitmapFontLoader.js @@ -21,7 +21,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -59,6 +58,7 @@ // constructor PIXI.BitmapFontLoader.prototype.constructor = PIXI.BitmapFontLoader; +PIXI.EventTarget.mixin(PIXI.BitmapFontLoader.prototype); /** * Loads the XML font data @@ -163,5 +163,5 @@ */ PIXI.BitmapFontLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/loaders/ImageLoader.js b/src/pixi/loaders/ImageLoader.js index ad4c61c..f1606f1 100644 --- a/src/pixi/loaders/ImageLoader.js +++ b/src/pixi/loaders/ImageLoader.js @@ -15,8 +15,6 @@ */ PIXI.ImageLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The texture being loaded * @@ -35,6 +33,7 @@ // constructor PIXI.ImageLoader.prototype.constructor = PIXI.ImageLoader; +PIXI.EventTarget.mixin(PIXI.ImageLoader.prototype); /** * Loads image or takes it from cache @@ -45,7 +44,7 @@ { if(!this.texture.baseTexture.hasLoaded) { - this.texture.baseTexture.addEventListener('loaded', this.onLoaded.bind(this)); + this.texture.baseTexture.on('loaded', this.onLoaded.bind(this)); } else { @@ -61,7 +60,7 @@ */ PIXI.ImageLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; /** diff --git a/src/pixi/loaders/JsonLoader.js b/src/pixi/loaders/JsonLoader.js index bdfd6e0..17a2a95 100644 --- a/src/pixi/loaders/JsonLoader.js +++ b/src/pixi/loaders/JsonLoader.js @@ -14,8 +14,6 @@ * @param crossorigin {Boolean} Whether requests should be treated as crossorigin */ PIXI.JsonLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The url of the bitmap font data * @@ -54,6 +52,7 @@ // constructor PIXI.JsonLoader.prototype.constructor = PIXI.JsonLoader; +PIXI.EventTarget.mixin(PIXI.JsonLoader.prototype); /** * Loads the JSON data diff --git a/src/pixi/display/Sprite.js b/src/pixi/display/Sprite.js index 5cf3105..cffecce 100644 --- a/src/pixi/display/Sprite.js +++ b/src/pixi/display/Sprite.js @@ -82,7 +82,7 @@ else { this.onTextureUpdateBind = this.onTextureUpdate.bind(this); - this.texture.addEventListener( 'update', this.onTextureUpdateBind ); + this.texture.on( 'update', this.onTextureUpdateBind ); } this.renderable = true; diff --git a/src/pixi/loaders/AssetLoader.js b/src/pixi/loaders/AssetLoader.js index feab211..cf6d3d2 100644 --- a/src/pixi/loaders/AssetLoader.js +++ b/src/pixi/loaders/AssetLoader.js @@ -20,8 +20,6 @@ */ PIXI.AssetLoader = function(assetURLs, crossorigin) { - PIXI.EventTarget.call(this); - /** * The array of asset URLs that are going to be loaded * @@ -58,6 +56,8 @@ }; }; +PIXI.EventTarget.mixin(PIXI.AssetLoader.prototype); + /** * Fired when an item has loaded * @event onProgress @@ -114,7 +114,7 @@ var scope = this; function onLoad(evt) { - scope.onAssetLoaded(evt.content); + scope.onAssetLoaded(evt.data.content); } this.loadCount = this.assetURLs.length; @@ -135,7 +135,7 @@ var loader = new Constructor(fileName, this.crossorigin); - loader.addEventListener('loaded', onLoad); + loader.on('loaded', onLoad); loader.load(); } }; @@ -149,12 +149,12 @@ PIXI.AssetLoader.prototype.onAssetLoaded = function(loader) { this.loadCount--; - this.dispatchEvent({ type: 'onProgress', content: this, loader: loader }); + this.emit('onProgress', { content: this, loader: loader }); if (this.onProgress) this.onProgress(loader); if (!this.loadCount) { - this.dispatchEvent({type: 'onComplete', content: this}); + this.emit('onComplete', { content: this }); if(this.onComplete) this.onComplete(); } }; diff --git a/src/pixi/loaders/AtlasLoader.js b/src/pixi/loaders/AtlasLoader.js index 98e32c9..aa6c381 100644 --- a/src/pixi/loaders/AtlasLoader.js +++ b/src/pixi/loaders/AtlasLoader.js @@ -7,14 +7,13 @@ * When loaded this class will dispatch a 'loaded' event * If loading fails this class will dispatch an 'error' event * @class AtlasLoader - * @extends EventTarget + * @uses EventTarget * @constructor * @param {String} url the url of the JSON file * @param {Boolean} crossorigin */ PIXI.AtlasLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); this.url = url; this.baseUrl = url.replace(/[^\/]*$/, ''); this.crossorigin = crossorigin; @@ -25,6 +24,7 @@ // constructor PIXI.AtlasLoader.constructor = PIXI.AtlasLoader; +PIXI.EventTarget.mixin(PIXI.AtlasLoader.prototype); /** * Starts loading the JSON file @@ -145,7 +145,7 @@ this.currentImageId = 0; for (j = 0; j < this.images.length; j++) { - this.images[j].addEventListener('loaded', selfOnLoaded); + this.images[j].on('loaded', selfOnLoaded); } this.images[this.currentImageId].load(); @@ -170,10 +170,7 @@ this.images[this.currentImageId].load(); } else { this.loaded = true; - this.dispatchEvent({ - type: 'loaded', - content: this - }); + this.emit('loaded', { content: this }); } }; @@ -183,8 +180,5 @@ * @private */ PIXI.AtlasLoader.prototype.onError = function () { - this.dispatchEvent({ - type: 'error', - content: this - }); + this.emit('error', { content: this }); }; diff --git a/src/pixi/loaders/BitmapFontLoader.js b/src/pixi/loaders/BitmapFontLoader.js index f87fbdd..bbd1fed 100644 --- a/src/pixi/loaders/BitmapFontLoader.js +++ b/src/pixi/loaders/BitmapFontLoader.js @@ -21,7 +21,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -59,6 +58,7 @@ // constructor PIXI.BitmapFontLoader.prototype.constructor = PIXI.BitmapFontLoader; +PIXI.EventTarget.mixin(PIXI.BitmapFontLoader.prototype); /** * Loads the XML font data @@ -163,5 +163,5 @@ */ PIXI.BitmapFontLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/loaders/ImageLoader.js b/src/pixi/loaders/ImageLoader.js index ad4c61c..f1606f1 100644 --- a/src/pixi/loaders/ImageLoader.js +++ b/src/pixi/loaders/ImageLoader.js @@ -15,8 +15,6 @@ */ PIXI.ImageLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The texture being loaded * @@ -35,6 +33,7 @@ // constructor PIXI.ImageLoader.prototype.constructor = PIXI.ImageLoader; +PIXI.EventTarget.mixin(PIXI.ImageLoader.prototype); /** * Loads image or takes it from cache @@ -45,7 +44,7 @@ { if(!this.texture.baseTexture.hasLoaded) { - this.texture.baseTexture.addEventListener('loaded', this.onLoaded.bind(this)); + this.texture.baseTexture.on('loaded', this.onLoaded.bind(this)); } else { @@ -61,7 +60,7 @@ */ PIXI.ImageLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; /** diff --git a/src/pixi/loaders/JsonLoader.js b/src/pixi/loaders/JsonLoader.js index bdfd6e0..17a2a95 100644 --- a/src/pixi/loaders/JsonLoader.js +++ b/src/pixi/loaders/JsonLoader.js @@ -14,8 +14,6 @@ * @param crossorigin {Boolean} Whether requests should be treated as crossorigin */ PIXI.JsonLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The url of the bitmap font data * @@ -54,6 +52,7 @@ // constructor PIXI.JsonLoader.prototype.constructor = PIXI.JsonLoader; +PIXI.EventTarget.mixin(PIXI.JsonLoader.prototype); /** * Loads the JSON data diff --git a/src/pixi/loaders/SpineLoader.js b/src/pixi/loaders/SpineLoader.js index 23072f1..027a980 100644 --- a/src/pixi/loaders/SpineLoader.js +++ b/src/pixi/loaders/SpineLoader.js @@ -23,8 +23,6 @@ */ PIXI.SpineLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The url of the bitmap font data * @@ -53,6 +51,8 @@ PIXI.SpineLoader.prototype.constructor = PIXI.SpineLoader; +PIXI.EventTarget.mixin(PIXI.SpineLoader.prototype); + /** * Loads the JSON data * @@ -62,8 +62,8 @@ var scope = this; var jsonLoader = new PIXI.JsonLoader(this.url, this.crossorigin); - jsonLoader.addEventListener("loaded", function (event) { - scope.json = event.content.json; + jsonLoader.on('loaded', function (event) { + scope.json = event.data.content.json; scope.onLoaded(); }); jsonLoader.load(); @@ -77,6 +77,6 @@ */ PIXI.SpineLoader.prototype.onLoaded = function () { this.loaded = true; - this.dispatchEvent({type: "loaded", content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/display/Sprite.js b/src/pixi/display/Sprite.js index 5cf3105..cffecce 100644 --- a/src/pixi/display/Sprite.js +++ b/src/pixi/display/Sprite.js @@ -82,7 +82,7 @@ else { this.onTextureUpdateBind = this.onTextureUpdate.bind(this); - this.texture.addEventListener( 'update', this.onTextureUpdateBind ); + this.texture.on( 'update', this.onTextureUpdateBind ); } this.renderable = true; diff --git a/src/pixi/loaders/AssetLoader.js b/src/pixi/loaders/AssetLoader.js index feab211..cf6d3d2 100644 --- a/src/pixi/loaders/AssetLoader.js +++ b/src/pixi/loaders/AssetLoader.js @@ -20,8 +20,6 @@ */ PIXI.AssetLoader = function(assetURLs, crossorigin) { - PIXI.EventTarget.call(this); - /** * The array of asset URLs that are going to be loaded * @@ -58,6 +56,8 @@ }; }; +PIXI.EventTarget.mixin(PIXI.AssetLoader.prototype); + /** * Fired when an item has loaded * @event onProgress @@ -114,7 +114,7 @@ var scope = this; function onLoad(evt) { - scope.onAssetLoaded(evt.content); + scope.onAssetLoaded(evt.data.content); } this.loadCount = this.assetURLs.length; @@ -135,7 +135,7 @@ var loader = new Constructor(fileName, this.crossorigin); - loader.addEventListener('loaded', onLoad); + loader.on('loaded', onLoad); loader.load(); } }; @@ -149,12 +149,12 @@ PIXI.AssetLoader.prototype.onAssetLoaded = function(loader) { this.loadCount--; - this.dispatchEvent({ type: 'onProgress', content: this, loader: loader }); + this.emit('onProgress', { content: this, loader: loader }); if (this.onProgress) this.onProgress(loader); if (!this.loadCount) { - this.dispatchEvent({type: 'onComplete', content: this}); + this.emit('onComplete', { content: this }); if(this.onComplete) this.onComplete(); } }; diff --git a/src/pixi/loaders/AtlasLoader.js b/src/pixi/loaders/AtlasLoader.js index 98e32c9..aa6c381 100644 --- a/src/pixi/loaders/AtlasLoader.js +++ b/src/pixi/loaders/AtlasLoader.js @@ -7,14 +7,13 @@ * When loaded this class will dispatch a 'loaded' event * If loading fails this class will dispatch an 'error' event * @class AtlasLoader - * @extends EventTarget + * @uses EventTarget * @constructor * @param {String} url the url of the JSON file * @param {Boolean} crossorigin */ PIXI.AtlasLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); this.url = url; this.baseUrl = url.replace(/[^\/]*$/, ''); this.crossorigin = crossorigin; @@ -25,6 +24,7 @@ // constructor PIXI.AtlasLoader.constructor = PIXI.AtlasLoader; +PIXI.EventTarget.mixin(PIXI.AtlasLoader.prototype); /** * Starts loading the JSON file @@ -145,7 +145,7 @@ this.currentImageId = 0; for (j = 0; j < this.images.length; j++) { - this.images[j].addEventListener('loaded', selfOnLoaded); + this.images[j].on('loaded', selfOnLoaded); } this.images[this.currentImageId].load(); @@ -170,10 +170,7 @@ this.images[this.currentImageId].load(); } else { this.loaded = true; - this.dispatchEvent({ - type: 'loaded', - content: this - }); + this.emit('loaded', { content: this }); } }; @@ -183,8 +180,5 @@ * @private */ PIXI.AtlasLoader.prototype.onError = function () { - this.dispatchEvent({ - type: 'error', - content: this - }); + this.emit('error', { content: this }); }; diff --git a/src/pixi/loaders/BitmapFontLoader.js b/src/pixi/loaders/BitmapFontLoader.js index f87fbdd..bbd1fed 100644 --- a/src/pixi/loaders/BitmapFontLoader.js +++ b/src/pixi/loaders/BitmapFontLoader.js @@ -21,7 +21,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -59,6 +58,7 @@ // constructor PIXI.BitmapFontLoader.prototype.constructor = PIXI.BitmapFontLoader; +PIXI.EventTarget.mixin(PIXI.BitmapFontLoader.prototype); /** * Loads the XML font data @@ -163,5 +163,5 @@ */ PIXI.BitmapFontLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/loaders/ImageLoader.js b/src/pixi/loaders/ImageLoader.js index ad4c61c..f1606f1 100644 --- a/src/pixi/loaders/ImageLoader.js +++ b/src/pixi/loaders/ImageLoader.js @@ -15,8 +15,6 @@ */ PIXI.ImageLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The texture being loaded * @@ -35,6 +33,7 @@ // constructor PIXI.ImageLoader.prototype.constructor = PIXI.ImageLoader; +PIXI.EventTarget.mixin(PIXI.ImageLoader.prototype); /** * Loads image or takes it from cache @@ -45,7 +44,7 @@ { if(!this.texture.baseTexture.hasLoaded) { - this.texture.baseTexture.addEventListener('loaded', this.onLoaded.bind(this)); + this.texture.baseTexture.on('loaded', this.onLoaded.bind(this)); } else { @@ -61,7 +60,7 @@ */ PIXI.ImageLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; /** diff --git a/src/pixi/loaders/JsonLoader.js b/src/pixi/loaders/JsonLoader.js index bdfd6e0..17a2a95 100644 --- a/src/pixi/loaders/JsonLoader.js +++ b/src/pixi/loaders/JsonLoader.js @@ -14,8 +14,6 @@ * @param crossorigin {Boolean} Whether requests should be treated as crossorigin */ PIXI.JsonLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The url of the bitmap font data * @@ -54,6 +52,7 @@ // constructor PIXI.JsonLoader.prototype.constructor = PIXI.JsonLoader; +PIXI.EventTarget.mixin(PIXI.JsonLoader.prototype); /** * Loads the JSON data diff --git a/src/pixi/loaders/SpineLoader.js b/src/pixi/loaders/SpineLoader.js index 23072f1..027a980 100644 --- a/src/pixi/loaders/SpineLoader.js +++ b/src/pixi/loaders/SpineLoader.js @@ -23,8 +23,6 @@ */ PIXI.SpineLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The url of the bitmap font data * @@ -53,6 +51,8 @@ PIXI.SpineLoader.prototype.constructor = PIXI.SpineLoader; +PIXI.EventTarget.mixin(PIXI.SpineLoader.prototype); + /** * Loads the JSON data * @@ -62,8 +62,8 @@ var scope = this; var jsonLoader = new PIXI.JsonLoader(this.url, this.crossorigin); - jsonLoader.addEventListener("loaded", function (event) { - scope.json = event.content.json; + jsonLoader.on('loaded', function (event) { + scope.json = event.data.content.json; scope.onLoaded(); }); jsonLoader.load(); @@ -77,6 +77,6 @@ */ PIXI.SpineLoader.prototype.onLoaded = function () { this.loaded = true; - this.dispatchEvent({type: "loaded", content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/loaders/SpriteSheetLoader.js b/src/pixi/loaders/SpriteSheetLoader.js index bfb827a..971724f 100644 --- a/src/pixi/loaders/SpriteSheetLoader.js +++ b/src/pixi/loaders/SpriteSheetLoader.js @@ -23,7 +23,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -70,6 +69,8 @@ // constructor PIXI.SpriteSheetLoader.prototype.constructor = PIXI.SpriteSheetLoader; +PIXI.EventTarget.mixin(PIXI.SpriteSheetLoader.prototype); + /** * This will begin loading the JSON file * @@ -78,8 +79,8 @@ PIXI.SpriteSheetLoader.prototype.load = function () { var scope = this; var jsonLoader = new PIXI.JsonLoader(this.url, this.crossorigin); - jsonLoader.addEventListener('loaded', function (event) { - scope.json = event.content.json; + jsonLoader.on('loaded', function (event) { + scope.json = event.data.content.json; scope.onLoaded(); }); jsonLoader.load(); @@ -92,8 +93,7 @@ * @private */ PIXI.SpriteSheetLoader.prototype.onLoaded = function () { - this.dispatchEvent({ - type: 'loaded', + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/display/Sprite.js b/src/pixi/display/Sprite.js index 5cf3105..cffecce 100644 --- a/src/pixi/display/Sprite.js +++ b/src/pixi/display/Sprite.js @@ -82,7 +82,7 @@ else { this.onTextureUpdateBind = this.onTextureUpdate.bind(this); - this.texture.addEventListener( 'update', this.onTextureUpdateBind ); + this.texture.on( 'update', this.onTextureUpdateBind ); } this.renderable = true; diff --git a/src/pixi/loaders/AssetLoader.js b/src/pixi/loaders/AssetLoader.js index feab211..cf6d3d2 100644 --- a/src/pixi/loaders/AssetLoader.js +++ b/src/pixi/loaders/AssetLoader.js @@ -20,8 +20,6 @@ */ PIXI.AssetLoader = function(assetURLs, crossorigin) { - PIXI.EventTarget.call(this); - /** * The array of asset URLs that are going to be loaded * @@ -58,6 +56,8 @@ }; }; +PIXI.EventTarget.mixin(PIXI.AssetLoader.prototype); + /** * Fired when an item has loaded * @event onProgress @@ -114,7 +114,7 @@ var scope = this; function onLoad(evt) { - scope.onAssetLoaded(evt.content); + scope.onAssetLoaded(evt.data.content); } this.loadCount = this.assetURLs.length; @@ -135,7 +135,7 @@ var loader = new Constructor(fileName, this.crossorigin); - loader.addEventListener('loaded', onLoad); + loader.on('loaded', onLoad); loader.load(); } }; @@ -149,12 +149,12 @@ PIXI.AssetLoader.prototype.onAssetLoaded = function(loader) { this.loadCount--; - this.dispatchEvent({ type: 'onProgress', content: this, loader: loader }); + this.emit('onProgress', { content: this, loader: loader }); if (this.onProgress) this.onProgress(loader); if (!this.loadCount) { - this.dispatchEvent({type: 'onComplete', content: this}); + this.emit('onComplete', { content: this }); if(this.onComplete) this.onComplete(); } }; diff --git a/src/pixi/loaders/AtlasLoader.js b/src/pixi/loaders/AtlasLoader.js index 98e32c9..aa6c381 100644 --- a/src/pixi/loaders/AtlasLoader.js +++ b/src/pixi/loaders/AtlasLoader.js @@ -7,14 +7,13 @@ * When loaded this class will dispatch a 'loaded' event * If loading fails this class will dispatch an 'error' event * @class AtlasLoader - * @extends EventTarget + * @uses EventTarget * @constructor * @param {String} url the url of the JSON file * @param {Boolean} crossorigin */ PIXI.AtlasLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); this.url = url; this.baseUrl = url.replace(/[^\/]*$/, ''); this.crossorigin = crossorigin; @@ -25,6 +24,7 @@ // constructor PIXI.AtlasLoader.constructor = PIXI.AtlasLoader; +PIXI.EventTarget.mixin(PIXI.AtlasLoader.prototype); /** * Starts loading the JSON file @@ -145,7 +145,7 @@ this.currentImageId = 0; for (j = 0; j < this.images.length; j++) { - this.images[j].addEventListener('loaded', selfOnLoaded); + this.images[j].on('loaded', selfOnLoaded); } this.images[this.currentImageId].load(); @@ -170,10 +170,7 @@ this.images[this.currentImageId].load(); } else { this.loaded = true; - this.dispatchEvent({ - type: 'loaded', - content: this - }); + this.emit('loaded', { content: this }); } }; @@ -183,8 +180,5 @@ * @private */ PIXI.AtlasLoader.prototype.onError = function () { - this.dispatchEvent({ - type: 'error', - content: this - }); + this.emit('error', { content: this }); }; diff --git a/src/pixi/loaders/BitmapFontLoader.js b/src/pixi/loaders/BitmapFontLoader.js index f87fbdd..bbd1fed 100644 --- a/src/pixi/loaders/BitmapFontLoader.js +++ b/src/pixi/loaders/BitmapFontLoader.js @@ -21,7 +21,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -59,6 +58,7 @@ // constructor PIXI.BitmapFontLoader.prototype.constructor = PIXI.BitmapFontLoader; +PIXI.EventTarget.mixin(PIXI.BitmapFontLoader.prototype); /** * Loads the XML font data @@ -163,5 +163,5 @@ */ PIXI.BitmapFontLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/loaders/ImageLoader.js b/src/pixi/loaders/ImageLoader.js index ad4c61c..f1606f1 100644 --- a/src/pixi/loaders/ImageLoader.js +++ b/src/pixi/loaders/ImageLoader.js @@ -15,8 +15,6 @@ */ PIXI.ImageLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The texture being loaded * @@ -35,6 +33,7 @@ // constructor PIXI.ImageLoader.prototype.constructor = PIXI.ImageLoader; +PIXI.EventTarget.mixin(PIXI.ImageLoader.prototype); /** * Loads image or takes it from cache @@ -45,7 +44,7 @@ { if(!this.texture.baseTexture.hasLoaded) { - this.texture.baseTexture.addEventListener('loaded', this.onLoaded.bind(this)); + this.texture.baseTexture.on('loaded', this.onLoaded.bind(this)); } else { @@ -61,7 +60,7 @@ */ PIXI.ImageLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; /** diff --git a/src/pixi/loaders/JsonLoader.js b/src/pixi/loaders/JsonLoader.js index bdfd6e0..17a2a95 100644 --- a/src/pixi/loaders/JsonLoader.js +++ b/src/pixi/loaders/JsonLoader.js @@ -14,8 +14,6 @@ * @param crossorigin {Boolean} Whether requests should be treated as crossorigin */ PIXI.JsonLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The url of the bitmap font data * @@ -54,6 +52,7 @@ // constructor PIXI.JsonLoader.prototype.constructor = PIXI.JsonLoader; +PIXI.EventTarget.mixin(PIXI.JsonLoader.prototype); /** * Loads the JSON data diff --git a/src/pixi/loaders/SpineLoader.js b/src/pixi/loaders/SpineLoader.js index 23072f1..027a980 100644 --- a/src/pixi/loaders/SpineLoader.js +++ b/src/pixi/loaders/SpineLoader.js @@ -23,8 +23,6 @@ */ PIXI.SpineLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The url of the bitmap font data * @@ -53,6 +51,8 @@ PIXI.SpineLoader.prototype.constructor = PIXI.SpineLoader; +PIXI.EventTarget.mixin(PIXI.SpineLoader.prototype); + /** * Loads the JSON data * @@ -62,8 +62,8 @@ var scope = this; var jsonLoader = new PIXI.JsonLoader(this.url, this.crossorigin); - jsonLoader.addEventListener("loaded", function (event) { - scope.json = event.content.json; + jsonLoader.on('loaded', function (event) { + scope.json = event.data.content.json; scope.onLoaded(); }); jsonLoader.load(); @@ -77,6 +77,6 @@ */ PIXI.SpineLoader.prototype.onLoaded = function () { this.loaded = true; - this.dispatchEvent({type: "loaded", content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/loaders/SpriteSheetLoader.js b/src/pixi/loaders/SpriteSheetLoader.js index bfb827a..971724f 100644 --- a/src/pixi/loaders/SpriteSheetLoader.js +++ b/src/pixi/loaders/SpriteSheetLoader.js @@ -23,7 +23,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -70,6 +69,8 @@ // constructor PIXI.SpriteSheetLoader.prototype.constructor = PIXI.SpriteSheetLoader; +PIXI.EventTarget.mixin(PIXI.SpriteSheetLoader.prototype); + /** * This will begin loading the JSON file * @@ -78,8 +79,8 @@ PIXI.SpriteSheetLoader.prototype.load = function () { var scope = this; var jsonLoader = new PIXI.JsonLoader(this.url, this.crossorigin); - jsonLoader.addEventListener('loaded', function (event) { - scope.json = event.content.json; + jsonLoader.on('loaded', function (event) { + scope.json = event.data.content.json; scope.onLoaded(); }); jsonLoader.load(); @@ -92,8 +93,7 @@ * @private */ PIXI.SpriteSheetLoader.prototype.onLoaded = function () { - this.dispatchEvent({ - type: 'loaded', + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/renderers/webgl/WebGLRenderer.js b/src/pixi/renderers/webgl/WebGLRenderer.js index e333d78..20c7b26 100644 --- a/src/pixi/renderers/webgl/WebGLRenderer.js +++ b/src/pixi/renderers/webgl/WebGLRenderer.js @@ -21,7 +21,7 @@ * @param [options.antialias=false] {Boolean} sets antialias (only applicable in chrome at the moment) * @param [options.preserveDrawingBuffer=false] {Boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context * @param [options.resolution=1] {Number} the resolution of the renderer retina would be 2 - * + * */ PIXI.WebGLRenderer = function(width, height, options) { @@ -37,7 +37,7 @@ options = PIXI.defaultRenderOptions; } - + if(!PIXI.defaultRenderer) { PIXI.sayHello('webGL'); @@ -581,8 +581,8 @@ // deal with losing context.. // remove listeners - this.view.removeEventListener('webglcontextlost', this.contextLostFunction); - this.view.removeEventListener('webglcontextrestored', this.contextRestoredFunction); + this.view.off('webglcontextlost', this.contextLostFunction); + this.view.off('webglcontextrestored', this.contextRestoredFunction); PIXI.glContexts[this.glContextId] = null; diff --git a/src/pixi/display/Sprite.js b/src/pixi/display/Sprite.js index 5cf3105..cffecce 100644 --- a/src/pixi/display/Sprite.js +++ b/src/pixi/display/Sprite.js @@ -82,7 +82,7 @@ else { this.onTextureUpdateBind = this.onTextureUpdate.bind(this); - this.texture.addEventListener( 'update', this.onTextureUpdateBind ); + this.texture.on( 'update', this.onTextureUpdateBind ); } this.renderable = true; diff --git a/src/pixi/loaders/AssetLoader.js b/src/pixi/loaders/AssetLoader.js index feab211..cf6d3d2 100644 --- a/src/pixi/loaders/AssetLoader.js +++ b/src/pixi/loaders/AssetLoader.js @@ -20,8 +20,6 @@ */ PIXI.AssetLoader = function(assetURLs, crossorigin) { - PIXI.EventTarget.call(this); - /** * The array of asset URLs that are going to be loaded * @@ -58,6 +56,8 @@ }; }; +PIXI.EventTarget.mixin(PIXI.AssetLoader.prototype); + /** * Fired when an item has loaded * @event onProgress @@ -114,7 +114,7 @@ var scope = this; function onLoad(evt) { - scope.onAssetLoaded(evt.content); + scope.onAssetLoaded(evt.data.content); } this.loadCount = this.assetURLs.length; @@ -135,7 +135,7 @@ var loader = new Constructor(fileName, this.crossorigin); - loader.addEventListener('loaded', onLoad); + loader.on('loaded', onLoad); loader.load(); } }; @@ -149,12 +149,12 @@ PIXI.AssetLoader.prototype.onAssetLoaded = function(loader) { this.loadCount--; - this.dispatchEvent({ type: 'onProgress', content: this, loader: loader }); + this.emit('onProgress', { content: this, loader: loader }); if (this.onProgress) this.onProgress(loader); if (!this.loadCount) { - this.dispatchEvent({type: 'onComplete', content: this}); + this.emit('onComplete', { content: this }); if(this.onComplete) this.onComplete(); } }; diff --git a/src/pixi/loaders/AtlasLoader.js b/src/pixi/loaders/AtlasLoader.js index 98e32c9..aa6c381 100644 --- a/src/pixi/loaders/AtlasLoader.js +++ b/src/pixi/loaders/AtlasLoader.js @@ -7,14 +7,13 @@ * When loaded this class will dispatch a 'loaded' event * If loading fails this class will dispatch an 'error' event * @class AtlasLoader - * @extends EventTarget + * @uses EventTarget * @constructor * @param {String} url the url of the JSON file * @param {Boolean} crossorigin */ PIXI.AtlasLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); this.url = url; this.baseUrl = url.replace(/[^\/]*$/, ''); this.crossorigin = crossorigin; @@ -25,6 +24,7 @@ // constructor PIXI.AtlasLoader.constructor = PIXI.AtlasLoader; +PIXI.EventTarget.mixin(PIXI.AtlasLoader.prototype); /** * Starts loading the JSON file @@ -145,7 +145,7 @@ this.currentImageId = 0; for (j = 0; j < this.images.length; j++) { - this.images[j].addEventListener('loaded', selfOnLoaded); + this.images[j].on('loaded', selfOnLoaded); } this.images[this.currentImageId].load(); @@ -170,10 +170,7 @@ this.images[this.currentImageId].load(); } else { this.loaded = true; - this.dispatchEvent({ - type: 'loaded', - content: this - }); + this.emit('loaded', { content: this }); } }; @@ -183,8 +180,5 @@ * @private */ PIXI.AtlasLoader.prototype.onError = function () { - this.dispatchEvent({ - type: 'error', - content: this - }); + this.emit('error', { content: this }); }; diff --git a/src/pixi/loaders/BitmapFontLoader.js b/src/pixi/loaders/BitmapFontLoader.js index f87fbdd..bbd1fed 100644 --- a/src/pixi/loaders/BitmapFontLoader.js +++ b/src/pixi/loaders/BitmapFontLoader.js @@ -21,7 +21,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -59,6 +58,7 @@ // constructor PIXI.BitmapFontLoader.prototype.constructor = PIXI.BitmapFontLoader; +PIXI.EventTarget.mixin(PIXI.BitmapFontLoader.prototype); /** * Loads the XML font data @@ -163,5 +163,5 @@ */ PIXI.BitmapFontLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/loaders/ImageLoader.js b/src/pixi/loaders/ImageLoader.js index ad4c61c..f1606f1 100644 --- a/src/pixi/loaders/ImageLoader.js +++ b/src/pixi/loaders/ImageLoader.js @@ -15,8 +15,6 @@ */ PIXI.ImageLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The texture being loaded * @@ -35,6 +33,7 @@ // constructor PIXI.ImageLoader.prototype.constructor = PIXI.ImageLoader; +PIXI.EventTarget.mixin(PIXI.ImageLoader.prototype); /** * Loads image or takes it from cache @@ -45,7 +44,7 @@ { if(!this.texture.baseTexture.hasLoaded) { - this.texture.baseTexture.addEventListener('loaded', this.onLoaded.bind(this)); + this.texture.baseTexture.on('loaded', this.onLoaded.bind(this)); } else { @@ -61,7 +60,7 @@ */ PIXI.ImageLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; /** diff --git a/src/pixi/loaders/JsonLoader.js b/src/pixi/loaders/JsonLoader.js index bdfd6e0..17a2a95 100644 --- a/src/pixi/loaders/JsonLoader.js +++ b/src/pixi/loaders/JsonLoader.js @@ -14,8 +14,6 @@ * @param crossorigin {Boolean} Whether requests should be treated as crossorigin */ PIXI.JsonLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The url of the bitmap font data * @@ -54,6 +52,7 @@ // constructor PIXI.JsonLoader.prototype.constructor = PIXI.JsonLoader; +PIXI.EventTarget.mixin(PIXI.JsonLoader.prototype); /** * Loads the JSON data diff --git a/src/pixi/loaders/SpineLoader.js b/src/pixi/loaders/SpineLoader.js index 23072f1..027a980 100644 --- a/src/pixi/loaders/SpineLoader.js +++ b/src/pixi/loaders/SpineLoader.js @@ -23,8 +23,6 @@ */ PIXI.SpineLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The url of the bitmap font data * @@ -53,6 +51,8 @@ PIXI.SpineLoader.prototype.constructor = PIXI.SpineLoader; +PIXI.EventTarget.mixin(PIXI.SpineLoader.prototype); + /** * Loads the JSON data * @@ -62,8 +62,8 @@ var scope = this; var jsonLoader = new PIXI.JsonLoader(this.url, this.crossorigin); - jsonLoader.addEventListener("loaded", function (event) { - scope.json = event.content.json; + jsonLoader.on('loaded', function (event) { + scope.json = event.data.content.json; scope.onLoaded(); }); jsonLoader.load(); @@ -77,6 +77,6 @@ */ PIXI.SpineLoader.prototype.onLoaded = function () { this.loaded = true; - this.dispatchEvent({type: "loaded", content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/loaders/SpriteSheetLoader.js b/src/pixi/loaders/SpriteSheetLoader.js index bfb827a..971724f 100644 --- a/src/pixi/loaders/SpriteSheetLoader.js +++ b/src/pixi/loaders/SpriteSheetLoader.js @@ -23,7 +23,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -70,6 +69,8 @@ // constructor PIXI.SpriteSheetLoader.prototype.constructor = PIXI.SpriteSheetLoader; +PIXI.EventTarget.mixin(PIXI.SpriteSheetLoader.prototype); + /** * This will begin loading the JSON file * @@ -78,8 +79,8 @@ PIXI.SpriteSheetLoader.prototype.load = function () { var scope = this; var jsonLoader = new PIXI.JsonLoader(this.url, this.crossorigin); - jsonLoader.addEventListener('loaded', function (event) { - scope.json = event.content.json; + jsonLoader.on('loaded', function (event) { + scope.json = event.data.content.json; scope.onLoaded(); }); jsonLoader.load(); @@ -92,8 +93,7 @@ * @private */ PIXI.SpriteSheetLoader.prototype.onLoaded = function () { - this.dispatchEvent({ - type: 'loaded', + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/renderers/webgl/WebGLRenderer.js b/src/pixi/renderers/webgl/WebGLRenderer.js index e333d78..20c7b26 100644 --- a/src/pixi/renderers/webgl/WebGLRenderer.js +++ b/src/pixi/renderers/webgl/WebGLRenderer.js @@ -21,7 +21,7 @@ * @param [options.antialias=false] {Boolean} sets antialias (only applicable in chrome at the moment) * @param [options.preserveDrawingBuffer=false] {Boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context * @param [options.resolution=1] {Number} the resolution of the renderer retina would be 2 - * + * */ PIXI.WebGLRenderer = function(width, height, options) { @@ -37,7 +37,7 @@ options = PIXI.defaultRenderOptions; } - + if(!PIXI.defaultRenderer) { PIXI.sayHello('webGL'); @@ -581,8 +581,8 @@ // deal with losing context.. // remove listeners - this.view.removeEventListener('webglcontextlost', this.contextLostFunction); - this.view.removeEventListener('webglcontextrestored', this.contextRestoredFunction); + this.view.off('webglcontextlost', this.contextLostFunction); + this.view.off('webglcontextrestored', this.contextRestoredFunction); PIXI.glContexts[this.glContextId] = null; diff --git a/src/pixi/textures/BaseTexture.js b/src/pixi/textures/BaseTexture.js index a41b86f..5f954e2 100644 --- a/src/pixi/textures/BaseTexture.js +++ b/src/pixi/textures/BaseTexture.js @@ -19,8 +19,6 @@ */ PIXI.BaseTexture = function(source, scaleMode) { - PIXI.EventTarget.call( this ); - /** * The Resolution of the texture. * @@ -134,6 +132,7 @@ }; PIXI.BaseTexture.prototype.constructor = PIXI.BaseTexture; +PIXI.EventTarget.mixin(PIXI.BaseTexture.prototype); /** * Destroys this base texture diff --git a/src/pixi/display/Sprite.js b/src/pixi/display/Sprite.js index 5cf3105..cffecce 100644 --- a/src/pixi/display/Sprite.js +++ b/src/pixi/display/Sprite.js @@ -82,7 +82,7 @@ else { this.onTextureUpdateBind = this.onTextureUpdate.bind(this); - this.texture.addEventListener( 'update', this.onTextureUpdateBind ); + this.texture.on( 'update', this.onTextureUpdateBind ); } this.renderable = true; diff --git a/src/pixi/loaders/AssetLoader.js b/src/pixi/loaders/AssetLoader.js index feab211..cf6d3d2 100644 --- a/src/pixi/loaders/AssetLoader.js +++ b/src/pixi/loaders/AssetLoader.js @@ -20,8 +20,6 @@ */ PIXI.AssetLoader = function(assetURLs, crossorigin) { - PIXI.EventTarget.call(this); - /** * The array of asset URLs that are going to be loaded * @@ -58,6 +56,8 @@ }; }; +PIXI.EventTarget.mixin(PIXI.AssetLoader.prototype); + /** * Fired when an item has loaded * @event onProgress @@ -114,7 +114,7 @@ var scope = this; function onLoad(evt) { - scope.onAssetLoaded(evt.content); + scope.onAssetLoaded(evt.data.content); } this.loadCount = this.assetURLs.length; @@ -135,7 +135,7 @@ var loader = new Constructor(fileName, this.crossorigin); - loader.addEventListener('loaded', onLoad); + loader.on('loaded', onLoad); loader.load(); } }; @@ -149,12 +149,12 @@ PIXI.AssetLoader.prototype.onAssetLoaded = function(loader) { this.loadCount--; - this.dispatchEvent({ type: 'onProgress', content: this, loader: loader }); + this.emit('onProgress', { content: this, loader: loader }); if (this.onProgress) this.onProgress(loader); if (!this.loadCount) { - this.dispatchEvent({type: 'onComplete', content: this}); + this.emit('onComplete', { content: this }); if(this.onComplete) this.onComplete(); } }; diff --git a/src/pixi/loaders/AtlasLoader.js b/src/pixi/loaders/AtlasLoader.js index 98e32c9..aa6c381 100644 --- a/src/pixi/loaders/AtlasLoader.js +++ b/src/pixi/loaders/AtlasLoader.js @@ -7,14 +7,13 @@ * When loaded this class will dispatch a 'loaded' event * If loading fails this class will dispatch an 'error' event * @class AtlasLoader - * @extends EventTarget + * @uses EventTarget * @constructor * @param {String} url the url of the JSON file * @param {Boolean} crossorigin */ PIXI.AtlasLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); this.url = url; this.baseUrl = url.replace(/[^\/]*$/, ''); this.crossorigin = crossorigin; @@ -25,6 +24,7 @@ // constructor PIXI.AtlasLoader.constructor = PIXI.AtlasLoader; +PIXI.EventTarget.mixin(PIXI.AtlasLoader.prototype); /** * Starts loading the JSON file @@ -145,7 +145,7 @@ this.currentImageId = 0; for (j = 0; j < this.images.length; j++) { - this.images[j].addEventListener('loaded', selfOnLoaded); + this.images[j].on('loaded', selfOnLoaded); } this.images[this.currentImageId].load(); @@ -170,10 +170,7 @@ this.images[this.currentImageId].load(); } else { this.loaded = true; - this.dispatchEvent({ - type: 'loaded', - content: this - }); + this.emit('loaded', { content: this }); } }; @@ -183,8 +180,5 @@ * @private */ PIXI.AtlasLoader.prototype.onError = function () { - this.dispatchEvent({ - type: 'error', - content: this - }); + this.emit('error', { content: this }); }; diff --git a/src/pixi/loaders/BitmapFontLoader.js b/src/pixi/loaders/BitmapFontLoader.js index f87fbdd..bbd1fed 100644 --- a/src/pixi/loaders/BitmapFontLoader.js +++ b/src/pixi/loaders/BitmapFontLoader.js @@ -21,7 +21,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -59,6 +58,7 @@ // constructor PIXI.BitmapFontLoader.prototype.constructor = PIXI.BitmapFontLoader; +PIXI.EventTarget.mixin(PIXI.BitmapFontLoader.prototype); /** * Loads the XML font data @@ -163,5 +163,5 @@ */ PIXI.BitmapFontLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/loaders/ImageLoader.js b/src/pixi/loaders/ImageLoader.js index ad4c61c..f1606f1 100644 --- a/src/pixi/loaders/ImageLoader.js +++ b/src/pixi/loaders/ImageLoader.js @@ -15,8 +15,6 @@ */ PIXI.ImageLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The texture being loaded * @@ -35,6 +33,7 @@ // constructor PIXI.ImageLoader.prototype.constructor = PIXI.ImageLoader; +PIXI.EventTarget.mixin(PIXI.ImageLoader.prototype); /** * Loads image or takes it from cache @@ -45,7 +44,7 @@ { if(!this.texture.baseTexture.hasLoaded) { - this.texture.baseTexture.addEventListener('loaded', this.onLoaded.bind(this)); + this.texture.baseTexture.on('loaded', this.onLoaded.bind(this)); } else { @@ -61,7 +60,7 @@ */ PIXI.ImageLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; /** diff --git a/src/pixi/loaders/JsonLoader.js b/src/pixi/loaders/JsonLoader.js index bdfd6e0..17a2a95 100644 --- a/src/pixi/loaders/JsonLoader.js +++ b/src/pixi/loaders/JsonLoader.js @@ -14,8 +14,6 @@ * @param crossorigin {Boolean} Whether requests should be treated as crossorigin */ PIXI.JsonLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The url of the bitmap font data * @@ -54,6 +52,7 @@ // constructor PIXI.JsonLoader.prototype.constructor = PIXI.JsonLoader; +PIXI.EventTarget.mixin(PIXI.JsonLoader.prototype); /** * Loads the JSON data diff --git a/src/pixi/loaders/SpineLoader.js b/src/pixi/loaders/SpineLoader.js index 23072f1..027a980 100644 --- a/src/pixi/loaders/SpineLoader.js +++ b/src/pixi/loaders/SpineLoader.js @@ -23,8 +23,6 @@ */ PIXI.SpineLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The url of the bitmap font data * @@ -53,6 +51,8 @@ PIXI.SpineLoader.prototype.constructor = PIXI.SpineLoader; +PIXI.EventTarget.mixin(PIXI.SpineLoader.prototype); + /** * Loads the JSON data * @@ -62,8 +62,8 @@ var scope = this; var jsonLoader = new PIXI.JsonLoader(this.url, this.crossorigin); - jsonLoader.addEventListener("loaded", function (event) { - scope.json = event.content.json; + jsonLoader.on('loaded', function (event) { + scope.json = event.data.content.json; scope.onLoaded(); }); jsonLoader.load(); @@ -77,6 +77,6 @@ */ PIXI.SpineLoader.prototype.onLoaded = function () { this.loaded = true; - this.dispatchEvent({type: "loaded", content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/loaders/SpriteSheetLoader.js b/src/pixi/loaders/SpriteSheetLoader.js index bfb827a..971724f 100644 --- a/src/pixi/loaders/SpriteSheetLoader.js +++ b/src/pixi/loaders/SpriteSheetLoader.js @@ -23,7 +23,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -70,6 +69,8 @@ // constructor PIXI.SpriteSheetLoader.prototype.constructor = PIXI.SpriteSheetLoader; +PIXI.EventTarget.mixin(PIXI.SpriteSheetLoader.prototype); + /** * This will begin loading the JSON file * @@ -78,8 +79,8 @@ PIXI.SpriteSheetLoader.prototype.load = function () { var scope = this; var jsonLoader = new PIXI.JsonLoader(this.url, this.crossorigin); - jsonLoader.addEventListener('loaded', function (event) { - scope.json = event.content.json; + jsonLoader.on('loaded', function (event) { + scope.json = event.data.content.json; scope.onLoaded(); }); jsonLoader.load(); @@ -92,8 +93,7 @@ * @private */ PIXI.SpriteSheetLoader.prototype.onLoaded = function () { - this.dispatchEvent({ - type: 'loaded', + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/renderers/webgl/WebGLRenderer.js b/src/pixi/renderers/webgl/WebGLRenderer.js index e333d78..20c7b26 100644 --- a/src/pixi/renderers/webgl/WebGLRenderer.js +++ b/src/pixi/renderers/webgl/WebGLRenderer.js @@ -21,7 +21,7 @@ * @param [options.antialias=false] {Boolean} sets antialias (only applicable in chrome at the moment) * @param [options.preserveDrawingBuffer=false] {Boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context * @param [options.resolution=1] {Number} the resolution of the renderer retina would be 2 - * + * */ PIXI.WebGLRenderer = function(width, height, options) { @@ -37,7 +37,7 @@ options = PIXI.defaultRenderOptions; } - + if(!PIXI.defaultRenderer) { PIXI.sayHello('webGL'); @@ -581,8 +581,8 @@ // deal with losing context.. // remove listeners - this.view.removeEventListener('webglcontextlost', this.contextLostFunction); - this.view.removeEventListener('webglcontextrestored', this.contextRestoredFunction); + this.view.off('webglcontextlost', this.contextLostFunction); + this.view.off('webglcontextrestored', this.contextRestoredFunction); PIXI.glContexts[this.glContextId] = null; diff --git a/src/pixi/textures/BaseTexture.js b/src/pixi/textures/BaseTexture.js index a41b86f..5f954e2 100644 --- a/src/pixi/textures/BaseTexture.js +++ b/src/pixi/textures/BaseTexture.js @@ -19,8 +19,6 @@ */ PIXI.BaseTexture = function(source, scaleMode) { - PIXI.EventTarget.call( this ); - /** * The Resolution of the texture. * @@ -134,6 +132,7 @@ }; PIXI.BaseTexture.prototype.constructor = PIXI.BaseTexture; +PIXI.EventTarget.mixin(PIXI.BaseTexture.prototype); /** * Destroys this base texture diff --git a/src/pixi/textures/RenderTexture.js b/src/pixi/textures/RenderTexture.js index f1e4ad1..021e97a 100644 --- a/src/pixi/textures/RenderTexture.js +++ b/src/pixi/textures/RenderTexture.js @@ -34,9 +34,6 @@ */ PIXI.RenderTexture = function(width, height, renderer, scaleMode, resolution) { - PIXI.EventTarget.call( this ); - - /** * The with of the render texture * @@ -44,6 +41,7 @@ * @type Number */ this.width = width || 100; + /** * The height of the render texture * @@ -51,9 +49,9 @@ * @type Number */ this.height = height || 100; - + /** - * The Resolution of the texture. + * The Resolution of the texture. * * @property resolution * @type Number @@ -76,7 +74,6 @@ * @type Rectangle */ this.crop = new PIXI.Rectangle(0, 0, this.width * this.resolution, this.height * this.resolution); - /** * The base texture object that this texture uses @@ -95,6 +92,11 @@ this.baseTexture.hasLoaded = true; + PIXI.Texture.call(this, + this.baseTexture, + new PIXI.Rectangle(0, 0, this.width, this.height) + ); + // each render texture can only belong to one renderer at the moment if its webGL this.renderer = renderer || PIXI.defaultRenderer; @@ -156,10 +158,10 @@ this.projection.x = this.width / 2; this.projection.y = -this.height / 2; } - + if(!this.valid)return; + this.textureBuffer.resize(this.width * this.resolution, this.height * this.resolution); - }; /** @@ -175,7 +177,7 @@ { this.renderer.gl.bindFramebuffer(this.renderer.gl.FRAMEBUFFER, this.textureBuffer.frameBuffer); } - + this.textureBuffer.clear(); }; @@ -228,7 +230,7 @@ PIXI.WebGLRenderer.updateTextures(); this.renderer.spriteBatch.dirty = true; - + this.renderer.renderDisplayObject(displayObject, this.projection, this.textureBuffer.frameBuffer); displayObject.worldTransform = originalWorldTransform; @@ -254,7 +256,7 @@ var originalWorldTransform = displayObject.worldTransform; displayObject.worldTransform = PIXI.RenderTexture.tempMatrix; - + if(position) { displayObject.worldTransform.tx = position.x; @@ -284,9 +286,9 @@ //this.baseTexture.resolution = 2; this.renderer.renderDisplayObject(displayObject, context); - + this.renderer.resolution = realResolution; - + context.setTransform(1,0,0,1,0,0); displayObject.worldTransform = originalWorldTransform; @@ -342,7 +344,7 @@ } tempCanvas.context.putImageData(canvasData, 0, 0); - + return tempCanvas.canvas; } else diff --git a/src/pixi/display/Sprite.js b/src/pixi/display/Sprite.js index 5cf3105..cffecce 100644 --- a/src/pixi/display/Sprite.js +++ b/src/pixi/display/Sprite.js @@ -82,7 +82,7 @@ else { this.onTextureUpdateBind = this.onTextureUpdate.bind(this); - this.texture.addEventListener( 'update', this.onTextureUpdateBind ); + this.texture.on( 'update', this.onTextureUpdateBind ); } this.renderable = true; diff --git a/src/pixi/loaders/AssetLoader.js b/src/pixi/loaders/AssetLoader.js index feab211..cf6d3d2 100644 --- a/src/pixi/loaders/AssetLoader.js +++ b/src/pixi/loaders/AssetLoader.js @@ -20,8 +20,6 @@ */ PIXI.AssetLoader = function(assetURLs, crossorigin) { - PIXI.EventTarget.call(this); - /** * The array of asset URLs that are going to be loaded * @@ -58,6 +56,8 @@ }; }; +PIXI.EventTarget.mixin(PIXI.AssetLoader.prototype); + /** * Fired when an item has loaded * @event onProgress @@ -114,7 +114,7 @@ var scope = this; function onLoad(evt) { - scope.onAssetLoaded(evt.content); + scope.onAssetLoaded(evt.data.content); } this.loadCount = this.assetURLs.length; @@ -135,7 +135,7 @@ var loader = new Constructor(fileName, this.crossorigin); - loader.addEventListener('loaded', onLoad); + loader.on('loaded', onLoad); loader.load(); } }; @@ -149,12 +149,12 @@ PIXI.AssetLoader.prototype.onAssetLoaded = function(loader) { this.loadCount--; - this.dispatchEvent({ type: 'onProgress', content: this, loader: loader }); + this.emit('onProgress', { content: this, loader: loader }); if (this.onProgress) this.onProgress(loader); if (!this.loadCount) { - this.dispatchEvent({type: 'onComplete', content: this}); + this.emit('onComplete', { content: this }); if(this.onComplete) this.onComplete(); } }; diff --git a/src/pixi/loaders/AtlasLoader.js b/src/pixi/loaders/AtlasLoader.js index 98e32c9..aa6c381 100644 --- a/src/pixi/loaders/AtlasLoader.js +++ b/src/pixi/loaders/AtlasLoader.js @@ -7,14 +7,13 @@ * When loaded this class will dispatch a 'loaded' event * If loading fails this class will dispatch an 'error' event * @class AtlasLoader - * @extends EventTarget + * @uses EventTarget * @constructor * @param {String} url the url of the JSON file * @param {Boolean} crossorigin */ PIXI.AtlasLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); this.url = url; this.baseUrl = url.replace(/[^\/]*$/, ''); this.crossorigin = crossorigin; @@ -25,6 +24,7 @@ // constructor PIXI.AtlasLoader.constructor = PIXI.AtlasLoader; +PIXI.EventTarget.mixin(PIXI.AtlasLoader.prototype); /** * Starts loading the JSON file @@ -145,7 +145,7 @@ this.currentImageId = 0; for (j = 0; j < this.images.length; j++) { - this.images[j].addEventListener('loaded', selfOnLoaded); + this.images[j].on('loaded', selfOnLoaded); } this.images[this.currentImageId].load(); @@ -170,10 +170,7 @@ this.images[this.currentImageId].load(); } else { this.loaded = true; - this.dispatchEvent({ - type: 'loaded', - content: this - }); + this.emit('loaded', { content: this }); } }; @@ -183,8 +180,5 @@ * @private */ PIXI.AtlasLoader.prototype.onError = function () { - this.dispatchEvent({ - type: 'error', - content: this - }); + this.emit('error', { content: this }); }; diff --git a/src/pixi/loaders/BitmapFontLoader.js b/src/pixi/loaders/BitmapFontLoader.js index f87fbdd..bbd1fed 100644 --- a/src/pixi/loaders/BitmapFontLoader.js +++ b/src/pixi/loaders/BitmapFontLoader.js @@ -21,7 +21,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -59,6 +58,7 @@ // constructor PIXI.BitmapFontLoader.prototype.constructor = PIXI.BitmapFontLoader; +PIXI.EventTarget.mixin(PIXI.BitmapFontLoader.prototype); /** * Loads the XML font data @@ -163,5 +163,5 @@ */ PIXI.BitmapFontLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/loaders/ImageLoader.js b/src/pixi/loaders/ImageLoader.js index ad4c61c..f1606f1 100644 --- a/src/pixi/loaders/ImageLoader.js +++ b/src/pixi/loaders/ImageLoader.js @@ -15,8 +15,6 @@ */ PIXI.ImageLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The texture being loaded * @@ -35,6 +33,7 @@ // constructor PIXI.ImageLoader.prototype.constructor = PIXI.ImageLoader; +PIXI.EventTarget.mixin(PIXI.ImageLoader.prototype); /** * Loads image or takes it from cache @@ -45,7 +44,7 @@ { if(!this.texture.baseTexture.hasLoaded) { - this.texture.baseTexture.addEventListener('loaded', this.onLoaded.bind(this)); + this.texture.baseTexture.on('loaded', this.onLoaded.bind(this)); } else { @@ -61,7 +60,7 @@ */ PIXI.ImageLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; /** diff --git a/src/pixi/loaders/JsonLoader.js b/src/pixi/loaders/JsonLoader.js index bdfd6e0..17a2a95 100644 --- a/src/pixi/loaders/JsonLoader.js +++ b/src/pixi/loaders/JsonLoader.js @@ -14,8 +14,6 @@ * @param crossorigin {Boolean} Whether requests should be treated as crossorigin */ PIXI.JsonLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The url of the bitmap font data * @@ -54,6 +52,7 @@ // constructor PIXI.JsonLoader.prototype.constructor = PIXI.JsonLoader; +PIXI.EventTarget.mixin(PIXI.JsonLoader.prototype); /** * Loads the JSON data diff --git a/src/pixi/loaders/SpineLoader.js b/src/pixi/loaders/SpineLoader.js index 23072f1..027a980 100644 --- a/src/pixi/loaders/SpineLoader.js +++ b/src/pixi/loaders/SpineLoader.js @@ -23,8 +23,6 @@ */ PIXI.SpineLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The url of the bitmap font data * @@ -53,6 +51,8 @@ PIXI.SpineLoader.prototype.constructor = PIXI.SpineLoader; +PIXI.EventTarget.mixin(PIXI.SpineLoader.prototype); + /** * Loads the JSON data * @@ -62,8 +62,8 @@ var scope = this; var jsonLoader = new PIXI.JsonLoader(this.url, this.crossorigin); - jsonLoader.addEventListener("loaded", function (event) { - scope.json = event.content.json; + jsonLoader.on('loaded', function (event) { + scope.json = event.data.content.json; scope.onLoaded(); }); jsonLoader.load(); @@ -77,6 +77,6 @@ */ PIXI.SpineLoader.prototype.onLoaded = function () { this.loaded = true; - this.dispatchEvent({type: "loaded", content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/loaders/SpriteSheetLoader.js b/src/pixi/loaders/SpriteSheetLoader.js index bfb827a..971724f 100644 --- a/src/pixi/loaders/SpriteSheetLoader.js +++ b/src/pixi/loaders/SpriteSheetLoader.js @@ -23,7 +23,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -70,6 +69,8 @@ // constructor PIXI.SpriteSheetLoader.prototype.constructor = PIXI.SpriteSheetLoader; +PIXI.EventTarget.mixin(PIXI.SpriteSheetLoader.prototype); + /** * This will begin loading the JSON file * @@ -78,8 +79,8 @@ PIXI.SpriteSheetLoader.prototype.load = function () { var scope = this; var jsonLoader = new PIXI.JsonLoader(this.url, this.crossorigin); - jsonLoader.addEventListener('loaded', function (event) { - scope.json = event.content.json; + jsonLoader.on('loaded', function (event) { + scope.json = event.data.content.json; scope.onLoaded(); }); jsonLoader.load(); @@ -92,8 +93,7 @@ * @private */ PIXI.SpriteSheetLoader.prototype.onLoaded = function () { - this.dispatchEvent({ - type: 'loaded', + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/renderers/webgl/WebGLRenderer.js b/src/pixi/renderers/webgl/WebGLRenderer.js index e333d78..20c7b26 100644 --- a/src/pixi/renderers/webgl/WebGLRenderer.js +++ b/src/pixi/renderers/webgl/WebGLRenderer.js @@ -21,7 +21,7 @@ * @param [options.antialias=false] {Boolean} sets antialias (only applicable in chrome at the moment) * @param [options.preserveDrawingBuffer=false] {Boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context * @param [options.resolution=1] {Number} the resolution of the renderer retina would be 2 - * + * */ PIXI.WebGLRenderer = function(width, height, options) { @@ -37,7 +37,7 @@ options = PIXI.defaultRenderOptions; } - + if(!PIXI.defaultRenderer) { PIXI.sayHello('webGL'); @@ -581,8 +581,8 @@ // deal with losing context.. // remove listeners - this.view.removeEventListener('webglcontextlost', this.contextLostFunction); - this.view.removeEventListener('webglcontextrestored', this.contextRestoredFunction); + this.view.off('webglcontextlost', this.contextLostFunction); + this.view.off('webglcontextrestored', this.contextRestoredFunction); PIXI.glContexts[this.glContextId] = null; diff --git a/src/pixi/textures/BaseTexture.js b/src/pixi/textures/BaseTexture.js index a41b86f..5f954e2 100644 --- a/src/pixi/textures/BaseTexture.js +++ b/src/pixi/textures/BaseTexture.js @@ -19,8 +19,6 @@ */ PIXI.BaseTexture = function(source, scaleMode) { - PIXI.EventTarget.call( this ); - /** * The Resolution of the texture. * @@ -134,6 +132,7 @@ }; PIXI.BaseTexture.prototype.constructor = PIXI.BaseTexture; +PIXI.EventTarget.mixin(PIXI.BaseTexture.prototype); /** * Destroys this base texture diff --git a/src/pixi/textures/RenderTexture.js b/src/pixi/textures/RenderTexture.js index f1e4ad1..021e97a 100644 --- a/src/pixi/textures/RenderTexture.js +++ b/src/pixi/textures/RenderTexture.js @@ -34,9 +34,6 @@ */ PIXI.RenderTexture = function(width, height, renderer, scaleMode, resolution) { - PIXI.EventTarget.call( this ); - - /** * The with of the render texture * @@ -44,6 +41,7 @@ * @type Number */ this.width = width || 100; + /** * The height of the render texture * @@ -51,9 +49,9 @@ * @type Number */ this.height = height || 100; - + /** - * The Resolution of the texture. + * The Resolution of the texture. * * @property resolution * @type Number @@ -76,7 +74,6 @@ * @type Rectangle */ this.crop = new PIXI.Rectangle(0, 0, this.width * this.resolution, this.height * this.resolution); - /** * The base texture object that this texture uses @@ -95,6 +92,11 @@ this.baseTexture.hasLoaded = true; + PIXI.Texture.call(this, + this.baseTexture, + new PIXI.Rectangle(0, 0, this.width, this.height) + ); + // each render texture can only belong to one renderer at the moment if its webGL this.renderer = renderer || PIXI.defaultRenderer; @@ -156,10 +158,10 @@ this.projection.x = this.width / 2; this.projection.y = -this.height / 2; } - + if(!this.valid)return; + this.textureBuffer.resize(this.width * this.resolution, this.height * this.resolution); - }; /** @@ -175,7 +177,7 @@ { this.renderer.gl.bindFramebuffer(this.renderer.gl.FRAMEBUFFER, this.textureBuffer.frameBuffer); } - + this.textureBuffer.clear(); }; @@ -228,7 +230,7 @@ PIXI.WebGLRenderer.updateTextures(); this.renderer.spriteBatch.dirty = true; - + this.renderer.renderDisplayObject(displayObject, this.projection, this.textureBuffer.frameBuffer); displayObject.worldTransform = originalWorldTransform; @@ -254,7 +256,7 @@ var originalWorldTransform = displayObject.worldTransform; displayObject.worldTransform = PIXI.RenderTexture.tempMatrix; - + if(position) { displayObject.worldTransform.tx = position.x; @@ -284,9 +286,9 @@ //this.baseTexture.resolution = 2; this.renderer.renderDisplayObject(displayObject, context); - + this.renderer.resolution = realResolution; - + context.setTransform(1,0,0,1,0,0); displayObject.worldTransform = originalWorldTransform; @@ -342,7 +344,7 @@ } tempCanvas.context.putImageData(canvasData, 0, 0); - + return tempCanvas.canvas; } else diff --git a/src/pixi/textures/Texture.js b/src/pixi/textures/Texture.js index f2e5a0f..112875b 100644 --- a/src/pixi/textures/Texture.js +++ b/src/pixi/textures/Texture.js @@ -19,8 +19,6 @@ */ PIXI.Texture = function(baseTexture, frame) { - PIXI.EventTarget.call( this ); - /** * Does this Texture have any frame data assigned to it? * @@ -118,6 +116,7 @@ }; PIXI.Texture.prototype.constructor = PIXI.Texture; +PIXI.EventTarget.mixin(PIXI.Texture.prototype); /** * Called when the base texture is loaded diff --git a/src/pixi/display/Sprite.js b/src/pixi/display/Sprite.js index 5cf3105..cffecce 100644 --- a/src/pixi/display/Sprite.js +++ b/src/pixi/display/Sprite.js @@ -82,7 +82,7 @@ else { this.onTextureUpdateBind = this.onTextureUpdate.bind(this); - this.texture.addEventListener( 'update', this.onTextureUpdateBind ); + this.texture.on( 'update', this.onTextureUpdateBind ); } this.renderable = true; diff --git a/src/pixi/loaders/AssetLoader.js b/src/pixi/loaders/AssetLoader.js index feab211..cf6d3d2 100644 --- a/src/pixi/loaders/AssetLoader.js +++ b/src/pixi/loaders/AssetLoader.js @@ -20,8 +20,6 @@ */ PIXI.AssetLoader = function(assetURLs, crossorigin) { - PIXI.EventTarget.call(this); - /** * The array of asset URLs that are going to be loaded * @@ -58,6 +56,8 @@ }; }; +PIXI.EventTarget.mixin(PIXI.AssetLoader.prototype); + /** * Fired when an item has loaded * @event onProgress @@ -114,7 +114,7 @@ var scope = this; function onLoad(evt) { - scope.onAssetLoaded(evt.content); + scope.onAssetLoaded(evt.data.content); } this.loadCount = this.assetURLs.length; @@ -135,7 +135,7 @@ var loader = new Constructor(fileName, this.crossorigin); - loader.addEventListener('loaded', onLoad); + loader.on('loaded', onLoad); loader.load(); } }; @@ -149,12 +149,12 @@ PIXI.AssetLoader.prototype.onAssetLoaded = function(loader) { this.loadCount--; - this.dispatchEvent({ type: 'onProgress', content: this, loader: loader }); + this.emit('onProgress', { content: this, loader: loader }); if (this.onProgress) this.onProgress(loader); if (!this.loadCount) { - this.dispatchEvent({type: 'onComplete', content: this}); + this.emit('onComplete', { content: this }); if(this.onComplete) this.onComplete(); } }; diff --git a/src/pixi/loaders/AtlasLoader.js b/src/pixi/loaders/AtlasLoader.js index 98e32c9..aa6c381 100644 --- a/src/pixi/loaders/AtlasLoader.js +++ b/src/pixi/loaders/AtlasLoader.js @@ -7,14 +7,13 @@ * When loaded this class will dispatch a 'loaded' event * If loading fails this class will dispatch an 'error' event * @class AtlasLoader - * @extends EventTarget + * @uses EventTarget * @constructor * @param {String} url the url of the JSON file * @param {Boolean} crossorigin */ PIXI.AtlasLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); this.url = url; this.baseUrl = url.replace(/[^\/]*$/, ''); this.crossorigin = crossorigin; @@ -25,6 +24,7 @@ // constructor PIXI.AtlasLoader.constructor = PIXI.AtlasLoader; +PIXI.EventTarget.mixin(PIXI.AtlasLoader.prototype); /** * Starts loading the JSON file @@ -145,7 +145,7 @@ this.currentImageId = 0; for (j = 0; j < this.images.length; j++) { - this.images[j].addEventListener('loaded', selfOnLoaded); + this.images[j].on('loaded', selfOnLoaded); } this.images[this.currentImageId].load(); @@ -170,10 +170,7 @@ this.images[this.currentImageId].load(); } else { this.loaded = true; - this.dispatchEvent({ - type: 'loaded', - content: this - }); + this.emit('loaded', { content: this }); } }; @@ -183,8 +180,5 @@ * @private */ PIXI.AtlasLoader.prototype.onError = function () { - this.dispatchEvent({ - type: 'error', - content: this - }); + this.emit('error', { content: this }); }; diff --git a/src/pixi/loaders/BitmapFontLoader.js b/src/pixi/loaders/BitmapFontLoader.js index f87fbdd..bbd1fed 100644 --- a/src/pixi/loaders/BitmapFontLoader.js +++ b/src/pixi/loaders/BitmapFontLoader.js @@ -21,7 +21,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -59,6 +58,7 @@ // constructor PIXI.BitmapFontLoader.prototype.constructor = PIXI.BitmapFontLoader; +PIXI.EventTarget.mixin(PIXI.BitmapFontLoader.prototype); /** * Loads the XML font data @@ -163,5 +163,5 @@ */ PIXI.BitmapFontLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/loaders/ImageLoader.js b/src/pixi/loaders/ImageLoader.js index ad4c61c..f1606f1 100644 --- a/src/pixi/loaders/ImageLoader.js +++ b/src/pixi/loaders/ImageLoader.js @@ -15,8 +15,6 @@ */ PIXI.ImageLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The texture being loaded * @@ -35,6 +33,7 @@ // constructor PIXI.ImageLoader.prototype.constructor = PIXI.ImageLoader; +PIXI.EventTarget.mixin(PIXI.ImageLoader.prototype); /** * Loads image or takes it from cache @@ -45,7 +44,7 @@ { if(!this.texture.baseTexture.hasLoaded) { - this.texture.baseTexture.addEventListener('loaded', this.onLoaded.bind(this)); + this.texture.baseTexture.on('loaded', this.onLoaded.bind(this)); } else { @@ -61,7 +60,7 @@ */ PIXI.ImageLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; /** diff --git a/src/pixi/loaders/JsonLoader.js b/src/pixi/loaders/JsonLoader.js index bdfd6e0..17a2a95 100644 --- a/src/pixi/loaders/JsonLoader.js +++ b/src/pixi/loaders/JsonLoader.js @@ -14,8 +14,6 @@ * @param crossorigin {Boolean} Whether requests should be treated as crossorigin */ PIXI.JsonLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The url of the bitmap font data * @@ -54,6 +52,7 @@ // constructor PIXI.JsonLoader.prototype.constructor = PIXI.JsonLoader; +PIXI.EventTarget.mixin(PIXI.JsonLoader.prototype); /** * Loads the JSON data diff --git a/src/pixi/loaders/SpineLoader.js b/src/pixi/loaders/SpineLoader.js index 23072f1..027a980 100644 --- a/src/pixi/loaders/SpineLoader.js +++ b/src/pixi/loaders/SpineLoader.js @@ -23,8 +23,6 @@ */ PIXI.SpineLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The url of the bitmap font data * @@ -53,6 +51,8 @@ PIXI.SpineLoader.prototype.constructor = PIXI.SpineLoader; +PIXI.EventTarget.mixin(PIXI.SpineLoader.prototype); + /** * Loads the JSON data * @@ -62,8 +62,8 @@ var scope = this; var jsonLoader = new PIXI.JsonLoader(this.url, this.crossorigin); - jsonLoader.addEventListener("loaded", function (event) { - scope.json = event.content.json; + jsonLoader.on('loaded', function (event) { + scope.json = event.data.content.json; scope.onLoaded(); }); jsonLoader.load(); @@ -77,6 +77,6 @@ */ PIXI.SpineLoader.prototype.onLoaded = function () { this.loaded = true; - this.dispatchEvent({type: "loaded", content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/loaders/SpriteSheetLoader.js b/src/pixi/loaders/SpriteSheetLoader.js index bfb827a..971724f 100644 --- a/src/pixi/loaders/SpriteSheetLoader.js +++ b/src/pixi/loaders/SpriteSheetLoader.js @@ -23,7 +23,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -70,6 +69,8 @@ // constructor PIXI.SpriteSheetLoader.prototype.constructor = PIXI.SpriteSheetLoader; +PIXI.EventTarget.mixin(PIXI.SpriteSheetLoader.prototype); + /** * This will begin loading the JSON file * @@ -78,8 +79,8 @@ PIXI.SpriteSheetLoader.prototype.load = function () { var scope = this; var jsonLoader = new PIXI.JsonLoader(this.url, this.crossorigin); - jsonLoader.addEventListener('loaded', function (event) { - scope.json = event.content.json; + jsonLoader.on('loaded', function (event) { + scope.json = event.data.content.json; scope.onLoaded(); }); jsonLoader.load(); @@ -92,8 +93,7 @@ * @private */ PIXI.SpriteSheetLoader.prototype.onLoaded = function () { - this.dispatchEvent({ - type: 'loaded', + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/renderers/webgl/WebGLRenderer.js b/src/pixi/renderers/webgl/WebGLRenderer.js index e333d78..20c7b26 100644 --- a/src/pixi/renderers/webgl/WebGLRenderer.js +++ b/src/pixi/renderers/webgl/WebGLRenderer.js @@ -21,7 +21,7 @@ * @param [options.antialias=false] {Boolean} sets antialias (only applicable in chrome at the moment) * @param [options.preserveDrawingBuffer=false] {Boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context * @param [options.resolution=1] {Number} the resolution of the renderer retina would be 2 - * + * */ PIXI.WebGLRenderer = function(width, height, options) { @@ -37,7 +37,7 @@ options = PIXI.defaultRenderOptions; } - + if(!PIXI.defaultRenderer) { PIXI.sayHello('webGL'); @@ -581,8 +581,8 @@ // deal with losing context.. // remove listeners - this.view.removeEventListener('webglcontextlost', this.contextLostFunction); - this.view.removeEventListener('webglcontextrestored', this.contextRestoredFunction); + this.view.off('webglcontextlost', this.contextLostFunction); + this.view.off('webglcontextrestored', this.contextRestoredFunction); PIXI.glContexts[this.glContextId] = null; diff --git a/src/pixi/textures/BaseTexture.js b/src/pixi/textures/BaseTexture.js index a41b86f..5f954e2 100644 --- a/src/pixi/textures/BaseTexture.js +++ b/src/pixi/textures/BaseTexture.js @@ -19,8 +19,6 @@ */ PIXI.BaseTexture = function(source, scaleMode) { - PIXI.EventTarget.call( this ); - /** * The Resolution of the texture. * @@ -134,6 +132,7 @@ }; PIXI.BaseTexture.prototype.constructor = PIXI.BaseTexture; +PIXI.EventTarget.mixin(PIXI.BaseTexture.prototype); /** * Destroys this base texture diff --git a/src/pixi/textures/RenderTexture.js b/src/pixi/textures/RenderTexture.js index f1e4ad1..021e97a 100644 --- a/src/pixi/textures/RenderTexture.js +++ b/src/pixi/textures/RenderTexture.js @@ -34,9 +34,6 @@ */ PIXI.RenderTexture = function(width, height, renderer, scaleMode, resolution) { - PIXI.EventTarget.call( this ); - - /** * The with of the render texture * @@ -44,6 +41,7 @@ * @type Number */ this.width = width || 100; + /** * The height of the render texture * @@ -51,9 +49,9 @@ * @type Number */ this.height = height || 100; - + /** - * The Resolution of the texture. + * The Resolution of the texture. * * @property resolution * @type Number @@ -76,7 +74,6 @@ * @type Rectangle */ this.crop = new PIXI.Rectangle(0, 0, this.width * this.resolution, this.height * this.resolution); - /** * The base texture object that this texture uses @@ -95,6 +92,11 @@ this.baseTexture.hasLoaded = true; + PIXI.Texture.call(this, + this.baseTexture, + new PIXI.Rectangle(0, 0, this.width, this.height) + ); + // each render texture can only belong to one renderer at the moment if its webGL this.renderer = renderer || PIXI.defaultRenderer; @@ -156,10 +158,10 @@ this.projection.x = this.width / 2; this.projection.y = -this.height / 2; } - + if(!this.valid)return; + this.textureBuffer.resize(this.width * this.resolution, this.height * this.resolution); - }; /** @@ -175,7 +177,7 @@ { this.renderer.gl.bindFramebuffer(this.renderer.gl.FRAMEBUFFER, this.textureBuffer.frameBuffer); } - + this.textureBuffer.clear(); }; @@ -228,7 +230,7 @@ PIXI.WebGLRenderer.updateTextures(); this.renderer.spriteBatch.dirty = true; - + this.renderer.renderDisplayObject(displayObject, this.projection, this.textureBuffer.frameBuffer); displayObject.worldTransform = originalWorldTransform; @@ -254,7 +256,7 @@ var originalWorldTransform = displayObject.worldTransform; displayObject.worldTransform = PIXI.RenderTexture.tempMatrix; - + if(position) { displayObject.worldTransform.tx = position.x; @@ -284,9 +286,9 @@ //this.baseTexture.resolution = 2; this.renderer.renderDisplayObject(displayObject, context); - + this.renderer.resolution = realResolution; - + context.setTransform(1,0,0,1,0,0); displayObject.worldTransform = originalWorldTransform; @@ -342,7 +344,7 @@ } tempCanvas.context.putImageData(canvasData, 0, 0); - + return tempCanvas.canvas; } else diff --git a/src/pixi/textures/Texture.js b/src/pixi/textures/Texture.js index f2e5a0f..112875b 100644 --- a/src/pixi/textures/Texture.js +++ b/src/pixi/textures/Texture.js @@ -19,8 +19,6 @@ */ PIXI.Texture = function(baseTexture, frame) { - PIXI.EventTarget.call( this ); - /** * Does this Texture have any frame data assigned to it? * @@ -118,6 +116,7 @@ }; PIXI.Texture.prototype.constructor = PIXI.Texture; +PIXI.EventTarget.mixin(PIXI.Texture.prototype); /** * Called when the base texture is loaded diff --git a/src/pixi/utils/EventTarget.js b/src/pixi/utils/EventTarget.js index ada6f59..39c04a7 100644 --- a/src/pixi/utils/EventTarget.js +++ b/src/pixi/utils/EventTarget.js @@ -1,112 +1,284 @@ /** * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * https://github.com/mrdoob/eventtarget.js/ - * THankS mr DOob! + * @author Chad Engler https://github.com/englercj @Rolnaaba */ /** - * Adds event emitter functionality to a class + * Originally based on https://github.com/mrdoob/eventtarget.js/ from mr DOob. + * Currently takes inspiration from the nodejs EventEmitter, EventEmitter3, and smokesignals + */ + +/** + * Mixins event emitter functionality to a class * * @class EventTarget * @example - * function MyEmitter() { - * PIXI.EventTarget.call(this); //mixes in event target stuff - * } + * function MyEmitter() {} + * + * PIXI.EventTarget.mixin(MyEmitter.prototype); * * var em = new MyEmitter(); - * em.emit({ type: 'eventName', data: 'some data' }); + * em.emit('eventName', 'some data', 'some moar data', {}, null, ...); */ -PIXI.EventTarget = function () { +PIXI.EventTarget = { + /** + * Backward compat from when this used to be a function + */ + call: function callCompat(obj) { + if(obj) { + obj = obj.prototype || obj; + PIXI.EventTarget.mixin(obj); + } + }, /** - * Holds all the listeners + * Mixes in the properties of the EventTarget prototype onto another object * - * @property listeners + * @method mixin + * @param object {Object} The obj to mix into + */ + mixin: function mixin(obj) { + /** + * Return a list of assigned event listeners. + * + * @method listeners + * @param eventName {String} The events that should be listed. + * @returns {Array} An array of listener functions + */ + obj.listeners = function listeners(eventName) { + this._listeners = this._listeners || {}; + + return this._listeners[eventName] ? this._listeners[eventName].slice() : []; + }; + + /** + * Emit an event to all registered event listeners. + * + * @method emit + * @alias dispatchEvent + * @param eventName {String} The name of the event. + * @returns {Boolean} Indication if we've emitted an event. + */ + obj.emit = obj.dispatchEvent = function emit(eventName, data) { + this._listeners = this._listeners || {}; + + //backwards compat with old method ".emit({ type: 'something' })" + if(typeof eventName === 'object') { + data = eventName; + eventName = eventName.type; + } + + //ensure we are using a real pixi event + if(!data || data.__isEventObject !== true) { + data = new PIXI.Event(this, eventName, data); + } + + //iterate the listeners + if(this._listeners && this._listeners[eventName]) { + var listeners = this._listeners[eventName], + length = listeners.length, + fn = listeners[0], + i; + + for(i = 0; i < length; fn = listeners[++i]) { + //call the event listener + fn.call(this, data); + + //if "stopImmediatePropagation" is called, stop calling sibling events + if(data.stoppedImmediate) { + return this; + } + } + + //if "stopPropagation" is called then don't bubble the event + if(data.stopped) { + return this; + } + } + + //bubble this event up the scene graph + if(this.parent && this.parent.emit) { + this.parent.emit.call(this.parent, eventName, data); + } + + return this; + }; + + /** + * Register a new EventListener for the given event. + * + * @method on + * @alias addEventListener + * @param eventName {String} Name of the event. + * @param callback {Functon} fn Callback function. + */ + obj.on = obj.addEventListener = function on(eventName, fn) { + this._listeners = this._listeners || {}; + + (this._listeners[eventName] = this._listeners[eventName] || []) + .push(fn); + + return this; + }; + + /** + * Add an EventListener that's only called once. + * + * @method once + * @param eventName {String} Name of the event. + * @param callback {Function} Callback function. + */ + obj.once = function once(eventName, fn) { + this._listeners = this._listeners || {}; + + var self = this; + function onceHandlerWrapper() { + fn.apply(self.off(eventName, onceHandlerWrapper), arguments); + } + onceHandlerWrapper._originalHandler = fn; + + return this.on(eventName, onceHandlerWrapper); + }; + + /** + * Remove event listeners. + * + * @method off + * @alias removeEventListener + * @param eventName {String} The event we want to remove. + * @param callback {Function} The listener that we need to find. + */ + obj.off = obj.removeEventListener = function off(eventName, fn) { + this._listeners = this._listeners || {}; + + if(!this._listeners[eventName]) + return this; + + var list = this._listeners[eventName], + i = list.length = fn ? list.length : 0; + + while(i-- > 0) { + if(list[i] === fn || list[i]._originalHandler === fn) { + list.splice(i, 1); + } + } + + if(list.length === 0) { + delete this._listeners[eventName]; + } + + return this; + }; + + /** + * Remove all listeners or only the listeners for the specified event. + * + * @method removeAllListeners + * @param eventName {String} The event you want to remove all listeners for. + */ + obj.removeAllListeners = function removeAllListeners(eventName) { + this._listeners = this._listeners || {}; + + if(!this._listeners[eventName]) + return this; + + delete this._listeners[eventName]; + + return this; + }; + } +}; + +/** + * Creates an homogenous object for tracking events so users can know what to expect. + * + * @class Event + * @extends Object + * @constructor + * @param target {Object} The target object that the event is called on + * @param name {String} The string name of the event that was triggered + * @param data {Object} Arbitrary event data to pass along + */ +PIXI.Event = function(target, name, data) { + //for duck typing in the ".on()" function + this.__isEventObject = true; + + /** + * Tracks the state of bubbling propagation. Do not + * set this directly, instead use `event.stopPropagation()` + * + * @property stopped + * @type Boolean + * @private + * @readOnly + */ + this.stopped = false; + + /** + * Tracks the state of sibling listener propagation. Do not + * set this directly, instead use `event.stopImmediatePropagation()` + * + * @property stoppedImmediate + * @type Boolean + * @private + * @readOnly + */ + this.stoppedImmediate = false; + + /** + * The original target the event triggered on. + * + * @property target * @type Object + * @readOnly */ - var listeners = {}; + this.target = target; /** - * Adds a listener for a specific event + * The string name of the event that this represents. * - * @method addEventListener - * @param type {string} A string representing the event type to listen for. - * @param listener {function} The callback function that will be fired when the event occurs + * @property type + * @type String + * @readOnly */ - this.addEventListener = this.on = function ( type, listener ) { - - - if ( listeners[ type ] === undefined ) { - - listeners[ type ] = []; - - } - - if ( listeners[ type ].indexOf( listener ) === - 1 ) { - - listeners[ type ].unshift( listener ); - } - - }; + this.type = name; /** - * Fires the event, ie pretends that the event has happened + * The data that was passed in with this event. * - * @method dispatchEvent - * @param event {Event} the event object + * @property data + * @type Object + * @readOnly */ - this.dispatchEvent = this.emit = function ( event ) { + this.data = data; - if ( !listeners[ event.type ] || !listeners[ event.type ].length ) { - - return; - - } - - - for(var i = listeners[ event.type ].length-1; i >= 0; i--) { -// for(var i = 0, l=listeners[ event.type ].length; i < l; i++) { - - - listeners[ event.type ][ i ]( event ); - - } - - }; + //backwards compat with older version of events + this.content = data; /** - * Removes the specified listener that was assigned to the specified event type + * The timestamp when the event occurred. * - * @method removeEventListener - * @param type {string} A string representing the event type which will have its listener removed - * @param listener {function} The callback function that was be fired when the event occured + * @property timeStamp + * @type Number + * @readOnly */ - this.removeEventListener = this.off = function ( type, listener ) { + this.timeStamp = Date.now(); +}; - if ( listeners[ type ] === undefined ) return; +/** + * Stops the propagation of events up the scene graph (prevents bubbling). + * + * @method stopPropagation + */ +PIXI.Event.prototype.stopPropagation = function stopPropagation() { + this.stopped = true; +}; - var index = listeners[ type ].indexOf( listener ); - - if ( index !== - 1 ) { - - listeners[ type ].splice( index, 1 ); - - } - - }; - - /** - * Removes all the listeners that were active for the specified event type - * - * @method removeAllEventListeners - * @param type {string} A string representing the event type which will have all its listeners removed - */ - this.removeAllEventListeners = function( type ) { - var a = listeners[type]; - if (a) - a.length = 0; - }; +/** + * Stops the propagation of events to sibling listeners (no longer calls any listeners). + * + * @method stopImmediatePropagation + */ +PIXI.Event.prototype.stopImmediatePropagation = function stopImmediatePropagation() { + this.stoppedImmediate = true; }; diff --git a/src/pixi/display/Sprite.js b/src/pixi/display/Sprite.js index 5cf3105..cffecce 100644 --- a/src/pixi/display/Sprite.js +++ b/src/pixi/display/Sprite.js @@ -82,7 +82,7 @@ else { this.onTextureUpdateBind = this.onTextureUpdate.bind(this); - this.texture.addEventListener( 'update', this.onTextureUpdateBind ); + this.texture.on( 'update', this.onTextureUpdateBind ); } this.renderable = true; diff --git a/src/pixi/loaders/AssetLoader.js b/src/pixi/loaders/AssetLoader.js index feab211..cf6d3d2 100644 --- a/src/pixi/loaders/AssetLoader.js +++ b/src/pixi/loaders/AssetLoader.js @@ -20,8 +20,6 @@ */ PIXI.AssetLoader = function(assetURLs, crossorigin) { - PIXI.EventTarget.call(this); - /** * The array of asset URLs that are going to be loaded * @@ -58,6 +56,8 @@ }; }; +PIXI.EventTarget.mixin(PIXI.AssetLoader.prototype); + /** * Fired when an item has loaded * @event onProgress @@ -114,7 +114,7 @@ var scope = this; function onLoad(evt) { - scope.onAssetLoaded(evt.content); + scope.onAssetLoaded(evt.data.content); } this.loadCount = this.assetURLs.length; @@ -135,7 +135,7 @@ var loader = new Constructor(fileName, this.crossorigin); - loader.addEventListener('loaded', onLoad); + loader.on('loaded', onLoad); loader.load(); } }; @@ -149,12 +149,12 @@ PIXI.AssetLoader.prototype.onAssetLoaded = function(loader) { this.loadCount--; - this.dispatchEvent({ type: 'onProgress', content: this, loader: loader }); + this.emit('onProgress', { content: this, loader: loader }); if (this.onProgress) this.onProgress(loader); if (!this.loadCount) { - this.dispatchEvent({type: 'onComplete', content: this}); + this.emit('onComplete', { content: this }); if(this.onComplete) this.onComplete(); } }; diff --git a/src/pixi/loaders/AtlasLoader.js b/src/pixi/loaders/AtlasLoader.js index 98e32c9..aa6c381 100644 --- a/src/pixi/loaders/AtlasLoader.js +++ b/src/pixi/loaders/AtlasLoader.js @@ -7,14 +7,13 @@ * When loaded this class will dispatch a 'loaded' event * If loading fails this class will dispatch an 'error' event * @class AtlasLoader - * @extends EventTarget + * @uses EventTarget * @constructor * @param {String} url the url of the JSON file * @param {Boolean} crossorigin */ PIXI.AtlasLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); this.url = url; this.baseUrl = url.replace(/[^\/]*$/, ''); this.crossorigin = crossorigin; @@ -25,6 +24,7 @@ // constructor PIXI.AtlasLoader.constructor = PIXI.AtlasLoader; +PIXI.EventTarget.mixin(PIXI.AtlasLoader.prototype); /** * Starts loading the JSON file @@ -145,7 +145,7 @@ this.currentImageId = 0; for (j = 0; j < this.images.length; j++) { - this.images[j].addEventListener('loaded', selfOnLoaded); + this.images[j].on('loaded', selfOnLoaded); } this.images[this.currentImageId].load(); @@ -170,10 +170,7 @@ this.images[this.currentImageId].load(); } else { this.loaded = true; - this.dispatchEvent({ - type: 'loaded', - content: this - }); + this.emit('loaded', { content: this }); } }; @@ -183,8 +180,5 @@ * @private */ PIXI.AtlasLoader.prototype.onError = function () { - this.dispatchEvent({ - type: 'error', - content: this - }); + this.emit('error', { content: this }); }; diff --git a/src/pixi/loaders/BitmapFontLoader.js b/src/pixi/loaders/BitmapFontLoader.js index f87fbdd..bbd1fed 100644 --- a/src/pixi/loaders/BitmapFontLoader.js +++ b/src/pixi/loaders/BitmapFontLoader.js @@ -21,7 +21,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -59,6 +58,7 @@ // constructor PIXI.BitmapFontLoader.prototype.constructor = PIXI.BitmapFontLoader; +PIXI.EventTarget.mixin(PIXI.BitmapFontLoader.prototype); /** * Loads the XML font data @@ -163,5 +163,5 @@ */ PIXI.BitmapFontLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/loaders/ImageLoader.js b/src/pixi/loaders/ImageLoader.js index ad4c61c..f1606f1 100644 --- a/src/pixi/loaders/ImageLoader.js +++ b/src/pixi/loaders/ImageLoader.js @@ -15,8 +15,6 @@ */ PIXI.ImageLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The texture being loaded * @@ -35,6 +33,7 @@ // constructor PIXI.ImageLoader.prototype.constructor = PIXI.ImageLoader; +PIXI.EventTarget.mixin(PIXI.ImageLoader.prototype); /** * Loads image or takes it from cache @@ -45,7 +44,7 @@ { if(!this.texture.baseTexture.hasLoaded) { - this.texture.baseTexture.addEventListener('loaded', this.onLoaded.bind(this)); + this.texture.baseTexture.on('loaded', this.onLoaded.bind(this)); } else { @@ -61,7 +60,7 @@ */ PIXI.ImageLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; /** diff --git a/src/pixi/loaders/JsonLoader.js b/src/pixi/loaders/JsonLoader.js index bdfd6e0..17a2a95 100644 --- a/src/pixi/loaders/JsonLoader.js +++ b/src/pixi/loaders/JsonLoader.js @@ -14,8 +14,6 @@ * @param crossorigin {Boolean} Whether requests should be treated as crossorigin */ PIXI.JsonLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The url of the bitmap font data * @@ -54,6 +52,7 @@ // constructor PIXI.JsonLoader.prototype.constructor = PIXI.JsonLoader; +PIXI.EventTarget.mixin(PIXI.JsonLoader.prototype); /** * Loads the JSON data diff --git a/src/pixi/loaders/SpineLoader.js b/src/pixi/loaders/SpineLoader.js index 23072f1..027a980 100644 --- a/src/pixi/loaders/SpineLoader.js +++ b/src/pixi/loaders/SpineLoader.js @@ -23,8 +23,6 @@ */ PIXI.SpineLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The url of the bitmap font data * @@ -53,6 +51,8 @@ PIXI.SpineLoader.prototype.constructor = PIXI.SpineLoader; +PIXI.EventTarget.mixin(PIXI.SpineLoader.prototype); + /** * Loads the JSON data * @@ -62,8 +62,8 @@ var scope = this; var jsonLoader = new PIXI.JsonLoader(this.url, this.crossorigin); - jsonLoader.addEventListener("loaded", function (event) { - scope.json = event.content.json; + jsonLoader.on('loaded', function (event) { + scope.json = event.data.content.json; scope.onLoaded(); }); jsonLoader.load(); @@ -77,6 +77,6 @@ */ PIXI.SpineLoader.prototype.onLoaded = function () { this.loaded = true; - this.dispatchEvent({type: "loaded", content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/loaders/SpriteSheetLoader.js b/src/pixi/loaders/SpriteSheetLoader.js index bfb827a..971724f 100644 --- a/src/pixi/loaders/SpriteSheetLoader.js +++ b/src/pixi/loaders/SpriteSheetLoader.js @@ -23,7 +23,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -70,6 +69,8 @@ // constructor PIXI.SpriteSheetLoader.prototype.constructor = PIXI.SpriteSheetLoader; +PIXI.EventTarget.mixin(PIXI.SpriteSheetLoader.prototype); + /** * This will begin loading the JSON file * @@ -78,8 +79,8 @@ PIXI.SpriteSheetLoader.prototype.load = function () { var scope = this; var jsonLoader = new PIXI.JsonLoader(this.url, this.crossorigin); - jsonLoader.addEventListener('loaded', function (event) { - scope.json = event.content.json; + jsonLoader.on('loaded', function (event) { + scope.json = event.data.content.json; scope.onLoaded(); }); jsonLoader.load(); @@ -92,8 +93,7 @@ * @private */ PIXI.SpriteSheetLoader.prototype.onLoaded = function () { - this.dispatchEvent({ - type: 'loaded', + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/renderers/webgl/WebGLRenderer.js b/src/pixi/renderers/webgl/WebGLRenderer.js index e333d78..20c7b26 100644 --- a/src/pixi/renderers/webgl/WebGLRenderer.js +++ b/src/pixi/renderers/webgl/WebGLRenderer.js @@ -21,7 +21,7 @@ * @param [options.antialias=false] {Boolean} sets antialias (only applicable in chrome at the moment) * @param [options.preserveDrawingBuffer=false] {Boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context * @param [options.resolution=1] {Number} the resolution of the renderer retina would be 2 - * + * */ PIXI.WebGLRenderer = function(width, height, options) { @@ -37,7 +37,7 @@ options = PIXI.defaultRenderOptions; } - + if(!PIXI.defaultRenderer) { PIXI.sayHello('webGL'); @@ -581,8 +581,8 @@ // deal with losing context.. // remove listeners - this.view.removeEventListener('webglcontextlost', this.contextLostFunction); - this.view.removeEventListener('webglcontextrestored', this.contextRestoredFunction); + this.view.off('webglcontextlost', this.contextLostFunction); + this.view.off('webglcontextrestored', this.contextRestoredFunction); PIXI.glContexts[this.glContextId] = null; diff --git a/src/pixi/textures/BaseTexture.js b/src/pixi/textures/BaseTexture.js index a41b86f..5f954e2 100644 --- a/src/pixi/textures/BaseTexture.js +++ b/src/pixi/textures/BaseTexture.js @@ -19,8 +19,6 @@ */ PIXI.BaseTexture = function(source, scaleMode) { - PIXI.EventTarget.call( this ); - /** * The Resolution of the texture. * @@ -134,6 +132,7 @@ }; PIXI.BaseTexture.prototype.constructor = PIXI.BaseTexture; +PIXI.EventTarget.mixin(PIXI.BaseTexture.prototype); /** * Destroys this base texture diff --git a/src/pixi/textures/RenderTexture.js b/src/pixi/textures/RenderTexture.js index f1e4ad1..021e97a 100644 --- a/src/pixi/textures/RenderTexture.js +++ b/src/pixi/textures/RenderTexture.js @@ -34,9 +34,6 @@ */ PIXI.RenderTexture = function(width, height, renderer, scaleMode, resolution) { - PIXI.EventTarget.call( this ); - - /** * The with of the render texture * @@ -44,6 +41,7 @@ * @type Number */ this.width = width || 100; + /** * The height of the render texture * @@ -51,9 +49,9 @@ * @type Number */ this.height = height || 100; - + /** - * The Resolution of the texture. + * The Resolution of the texture. * * @property resolution * @type Number @@ -76,7 +74,6 @@ * @type Rectangle */ this.crop = new PIXI.Rectangle(0, 0, this.width * this.resolution, this.height * this.resolution); - /** * The base texture object that this texture uses @@ -95,6 +92,11 @@ this.baseTexture.hasLoaded = true; + PIXI.Texture.call(this, + this.baseTexture, + new PIXI.Rectangle(0, 0, this.width, this.height) + ); + // each render texture can only belong to one renderer at the moment if its webGL this.renderer = renderer || PIXI.defaultRenderer; @@ -156,10 +158,10 @@ this.projection.x = this.width / 2; this.projection.y = -this.height / 2; } - + if(!this.valid)return; + this.textureBuffer.resize(this.width * this.resolution, this.height * this.resolution); - }; /** @@ -175,7 +177,7 @@ { this.renderer.gl.bindFramebuffer(this.renderer.gl.FRAMEBUFFER, this.textureBuffer.frameBuffer); } - + this.textureBuffer.clear(); }; @@ -228,7 +230,7 @@ PIXI.WebGLRenderer.updateTextures(); this.renderer.spriteBatch.dirty = true; - + this.renderer.renderDisplayObject(displayObject, this.projection, this.textureBuffer.frameBuffer); displayObject.worldTransform = originalWorldTransform; @@ -254,7 +256,7 @@ var originalWorldTransform = displayObject.worldTransform; displayObject.worldTransform = PIXI.RenderTexture.tempMatrix; - + if(position) { displayObject.worldTransform.tx = position.x; @@ -284,9 +286,9 @@ //this.baseTexture.resolution = 2; this.renderer.renderDisplayObject(displayObject, context); - + this.renderer.resolution = realResolution; - + context.setTransform(1,0,0,1,0,0); displayObject.worldTransform = originalWorldTransform; @@ -342,7 +344,7 @@ } tempCanvas.context.putImageData(canvasData, 0, 0); - + return tempCanvas.canvas; } else diff --git a/src/pixi/textures/Texture.js b/src/pixi/textures/Texture.js index f2e5a0f..112875b 100644 --- a/src/pixi/textures/Texture.js +++ b/src/pixi/textures/Texture.js @@ -19,8 +19,6 @@ */ PIXI.Texture = function(baseTexture, frame) { - PIXI.EventTarget.call( this ); - /** * Does this Texture have any frame data assigned to it? * @@ -118,6 +116,7 @@ }; PIXI.Texture.prototype.constructor = PIXI.Texture; +PIXI.EventTarget.mixin(PIXI.Texture.prototype); /** * Called when the base texture is loaded diff --git a/src/pixi/utils/EventTarget.js b/src/pixi/utils/EventTarget.js index ada6f59..39c04a7 100644 --- a/src/pixi/utils/EventTarget.js +++ b/src/pixi/utils/EventTarget.js @@ -1,112 +1,284 @@ /** * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * https://github.com/mrdoob/eventtarget.js/ - * THankS mr DOob! + * @author Chad Engler https://github.com/englercj @Rolnaaba */ /** - * Adds event emitter functionality to a class + * Originally based on https://github.com/mrdoob/eventtarget.js/ from mr DOob. + * Currently takes inspiration from the nodejs EventEmitter, EventEmitter3, and smokesignals + */ + +/** + * Mixins event emitter functionality to a class * * @class EventTarget * @example - * function MyEmitter() { - * PIXI.EventTarget.call(this); //mixes in event target stuff - * } + * function MyEmitter() {} + * + * PIXI.EventTarget.mixin(MyEmitter.prototype); * * var em = new MyEmitter(); - * em.emit({ type: 'eventName', data: 'some data' }); + * em.emit('eventName', 'some data', 'some moar data', {}, null, ...); */ -PIXI.EventTarget = function () { +PIXI.EventTarget = { + /** + * Backward compat from when this used to be a function + */ + call: function callCompat(obj) { + if(obj) { + obj = obj.prototype || obj; + PIXI.EventTarget.mixin(obj); + } + }, /** - * Holds all the listeners + * Mixes in the properties of the EventTarget prototype onto another object * - * @property listeners + * @method mixin + * @param object {Object} The obj to mix into + */ + mixin: function mixin(obj) { + /** + * Return a list of assigned event listeners. + * + * @method listeners + * @param eventName {String} The events that should be listed. + * @returns {Array} An array of listener functions + */ + obj.listeners = function listeners(eventName) { + this._listeners = this._listeners || {}; + + return this._listeners[eventName] ? this._listeners[eventName].slice() : []; + }; + + /** + * Emit an event to all registered event listeners. + * + * @method emit + * @alias dispatchEvent + * @param eventName {String} The name of the event. + * @returns {Boolean} Indication if we've emitted an event. + */ + obj.emit = obj.dispatchEvent = function emit(eventName, data) { + this._listeners = this._listeners || {}; + + //backwards compat with old method ".emit({ type: 'something' })" + if(typeof eventName === 'object') { + data = eventName; + eventName = eventName.type; + } + + //ensure we are using a real pixi event + if(!data || data.__isEventObject !== true) { + data = new PIXI.Event(this, eventName, data); + } + + //iterate the listeners + if(this._listeners && this._listeners[eventName]) { + var listeners = this._listeners[eventName], + length = listeners.length, + fn = listeners[0], + i; + + for(i = 0; i < length; fn = listeners[++i]) { + //call the event listener + fn.call(this, data); + + //if "stopImmediatePropagation" is called, stop calling sibling events + if(data.stoppedImmediate) { + return this; + } + } + + //if "stopPropagation" is called then don't bubble the event + if(data.stopped) { + return this; + } + } + + //bubble this event up the scene graph + if(this.parent && this.parent.emit) { + this.parent.emit.call(this.parent, eventName, data); + } + + return this; + }; + + /** + * Register a new EventListener for the given event. + * + * @method on + * @alias addEventListener + * @param eventName {String} Name of the event. + * @param callback {Functon} fn Callback function. + */ + obj.on = obj.addEventListener = function on(eventName, fn) { + this._listeners = this._listeners || {}; + + (this._listeners[eventName] = this._listeners[eventName] || []) + .push(fn); + + return this; + }; + + /** + * Add an EventListener that's only called once. + * + * @method once + * @param eventName {String} Name of the event. + * @param callback {Function} Callback function. + */ + obj.once = function once(eventName, fn) { + this._listeners = this._listeners || {}; + + var self = this; + function onceHandlerWrapper() { + fn.apply(self.off(eventName, onceHandlerWrapper), arguments); + } + onceHandlerWrapper._originalHandler = fn; + + return this.on(eventName, onceHandlerWrapper); + }; + + /** + * Remove event listeners. + * + * @method off + * @alias removeEventListener + * @param eventName {String} The event we want to remove. + * @param callback {Function} The listener that we need to find. + */ + obj.off = obj.removeEventListener = function off(eventName, fn) { + this._listeners = this._listeners || {}; + + if(!this._listeners[eventName]) + return this; + + var list = this._listeners[eventName], + i = list.length = fn ? list.length : 0; + + while(i-- > 0) { + if(list[i] === fn || list[i]._originalHandler === fn) { + list.splice(i, 1); + } + } + + if(list.length === 0) { + delete this._listeners[eventName]; + } + + return this; + }; + + /** + * Remove all listeners or only the listeners for the specified event. + * + * @method removeAllListeners + * @param eventName {String} The event you want to remove all listeners for. + */ + obj.removeAllListeners = function removeAllListeners(eventName) { + this._listeners = this._listeners || {}; + + if(!this._listeners[eventName]) + return this; + + delete this._listeners[eventName]; + + return this; + }; + } +}; + +/** + * Creates an homogenous object for tracking events so users can know what to expect. + * + * @class Event + * @extends Object + * @constructor + * @param target {Object} The target object that the event is called on + * @param name {String} The string name of the event that was triggered + * @param data {Object} Arbitrary event data to pass along + */ +PIXI.Event = function(target, name, data) { + //for duck typing in the ".on()" function + this.__isEventObject = true; + + /** + * Tracks the state of bubbling propagation. Do not + * set this directly, instead use `event.stopPropagation()` + * + * @property stopped + * @type Boolean + * @private + * @readOnly + */ + this.stopped = false; + + /** + * Tracks the state of sibling listener propagation. Do not + * set this directly, instead use `event.stopImmediatePropagation()` + * + * @property stoppedImmediate + * @type Boolean + * @private + * @readOnly + */ + this.stoppedImmediate = false; + + /** + * The original target the event triggered on. + * + * @property target * @type Object + * @readOnly */ - var listeners = {}; + this.target = target; /** - * Adds a listener for a specific event + * The string name of the event that this represents. * - * @method addEventListener - * @param type {string} A string representing the event type to listen for. - * @param listener {function} The callback function that will be fired when the event occurs + * @property type + * @type String + * @readOnly */ - this.addEventListener = this.on = function ( type, listener ) { - - - if ( listeners[ type ] === undefined ) { - - listeners[ type ] = []; - - } - - if ( listeners[ type ].indexOf( listener ) === - 1 ) { - - listeners[ type ].unshift( listener ); - } - - }; + this.type = name; /** - * Fires the event, ie pretends that the event has happened + * The data that was passed in with this event. * - * @method dispatchEvent - * @param event {Event} the event object + * @property data + * @type Object + * @readOnly */ - this.dispatchEvent = this.emit = function ( event ) { + this.data = data; - if ( !listeners[ event.type ] || !listeners[ event.type ].length ) { - - return; - - } - - - for(var i = listeners[ event.type ].length-1; i >= 0; i--) { -// for(var i = 0, l=listeners[ event.type ].length; i < l; i++) { - - - listeners[ event.type ][ i ]( event ); - - } - - }; + //backwards compat with older version of events + this.content = data; /** - * Removes the specified listener that was assigned to the specified event type + * The timestamp when the event occurred. * - * @method removeEventListener - * @param type {string} A string representing the event type which will have its listener removed - * @param listener {function} The callback function that was be fired when the event occured + * @property timeStamp + * @type Number + * @readOnly */ - this.removeEventListener = this.off = function ( type, listener ) { + this.timeStamp = Date.now(); +}; - if ( listeners[ type ] === undefined ) return; +/** + * Stops the propagation of events up the scene graph (prevents bubbling). + * + * @method stopPropagation + */ +PIXI.Event.prototype.stopPropagation = function stopPropagation() { + this.stopped = true; +}; - var index = listeners[ type ].indexOf( listener ); - - if ( index !== - 1 ) { - - listeners[ type ].splice( index, 1 ); - - } - - }; - - /** - * Removes all the listeners that were active for the specified event type - * - * @method removeAllEventListeners - * @param type {string} A string representing the event type which will have all its listeners removed - */ - this.removeAllEventListeners = function( type ) { - var a = listeners[type]; - if (a) - a.length = 0; - }; +/** + * Stops the propagation of events to sibling listeners (no longer calls any listeners). + * + * @method stopImmediatePropagation + */ +PIXI.Event.prototype.stopImmediatePropagation = function stopImmediatePropagation() { + this.stoppedImmediate = true; }; diff --git a/test/lib/pixi/textures/RenderTexture.js b/test/lib/pixi/textures/RenderTexture.js new file mode 100644 index 0000000..8556501 --- /dev/null +++ b/test/lib/pixi/textures/RenderTexture.js @@ -0,0 +1,14 @@ + +function pixi_textures_RenderTexture_confirmNew(obj, done) { + var expect = chai.expect; + + expect(obj).to.have.property('width'); + expect(obj).to.have.property('height'); + + expect(obj).to.have.property('render'); + expect(obj).to.have.property('renderer'); + // expect(obj).to.have.property('projection'); + expect(obj).to.have.property('textureBuffer'); + + pixi_textures_Texture_confirmNew(obj, done); +} diff --git a/src/pixi/display/Sprite.js b/src/pixi/display/Sprite.js index 5cf3105..cffecce 100644 --- a/src/pixi/display/Sprite.js +++ b/src/pixi/display/Sprite.js @@ -82,7 +82,7 @@ else { this.onTextureUpdateBind = this.onTextureUpdate.bind(this); - this.texture.addEventListener( 'update', this.onTextureUpdateBind ); + this.texture.on( 'update', this.onTextureUpdateBind ); } this.renderable = true; diff --git a/src/pixi/loaders/AssetLoader.js b/src/pixi/loaders/AssetLoader.js index feab211..cf6d3d2 100644 --- a/src/pixi/loaders/AssetLoader.js +++ b/src/pixi/loaders/AssetLoader.js @@ -20,8 +20,6 @@ */ PIXI.AssetLoader = function(assetURLs, crossorigin) { - PIXI.EventTarget.call(this); - /** * The array of asset URLs that are going to be loaded * @@ -58,6 +56,8 @@ }; }; +PIXI.EventTarget.mixin(PIXI.AssetLoader.prototype); + /** * Fired when an item has loaded * @event onProgress @@ -114,7 +114,7 @@ var scope = this; function onLoad(evt) { - scope.onAssetLoaded(evt.content); + scope.onAssetLoaded(evt.data.content); } this.loadCount = this.assetURLs.length; @@ -135,7 +135,7 @@ var loader = new Constructor(fileName, this.crossorigin); - loader.addEventListener('loaded', onLoad); + loader.on('loaded', onLoad); loader.load(); } }; @@ -149,12 +149,12 @@ PIXI.AssetLoader.prototype.onAssetLoaded = function(loader) { this.loadCount--; - this.dispatchEvent({ type: 'onProgress', content: this, loader: loader }); + this.emit('onProgress', { content: this, loader: loader }); if (this.onProgress) this.onProgress(loader); if (!this.loadCount) { - this.dispatchEvent({type: 'onComplete', content: this}); + this.emit('onComplete', { content: this }); if(this.onComplete) this.onComplete(); } }; diff --git a/src/pixi/loaders/AtlasLoader.js b/src/pixi/loaders/AtlasLoader.js index 98e32c9..aa6c381 100644 --- a/src/pixi/loaders/AtlasLoader.js +++ b/src/pixi/loaders/AtlasLoader.js @@ -7,14 +7,13 @@ * When loaded this class will dispatch a 'loaded' event * If loading fails this class will dispatch an 'error' event * @class AtlasLoader - * @extends EventTarget + * @uses EventTarget * @constructor * @param {String} url the url of the JSON file * @param {Boolean} crossorigin */ PIXI.AtlasLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); this.url = url; this.baseUrl = url.replace(/[^\/]*$/, ''); this.crossorigin = crossorigin; @@ -25,6 +24,7 @@ // constructor PIXI.AtlasLoader.constructor = PIXI.AtlasLoader; +PIXI.EventTarget.mixin(PIXI.AtlasLoader.prototype); /** * Starts loading the JSON file @@ -145,7 +145,7 @@ this.currentImageId = 0; for (j = 0; j < this.images.length; j++) { - this.images[j].addEventListener('loaded', selfOnLoaded); + this.images[j].on('loaded', selfOnLoaded); } this.images[this.currentImageId].load(); @@ -170,10 +170,7 @@ this.images[this.currentImageId].load(); } else { this.loaded = true; - this.dispatchEvent({ - type: 'loaded', - content: this - }); + this.emit('loaded', { content: this }); } }; @@ -183,8 +180,5 @@ * @private */ PIXI.AtlasLoader.prototype.onError = function () { - this.dispatchEvent({ - type: 'error', - content: this - }); + this.emit('error', { content: this }); }; diff --git a/src/pixi/loaders/BitmapFontLoader.js b/src/pixi/loaders/BitmapFontLoader.js index f87fbdd..bbd1fed 100644 --- a/src/pixi/loaders/BitmapFontLoader.js +++ b/src/pixi/loaders/BitmapFontLoader.js @@ -21,7 +21,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -59,6 +58,7 @@ // constructor PIXI.BitmapFontLoader.prototype.constructor = PIXI.BitmapFontLoader; +PIXI.EventTarget.mixin(PIXI.BitmapFontLoader.prototype); /** * Loads the XML font data @@ -163,5 +163,5 @@ */ PIXI.BitmapFontLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/loaders/ImageLoader.js b/src/pixi/loaders/ImageLoader.js index ad4c61c..f1606f1 100644 --- a/src/pixi/loaders/ImageLoader.js +++ b/src/pixi/loaders/ImageLoader.js @@ -15,8 +15,6 @@ */ PIXI.ImageLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The texture being loaded * @@ -35,6 +33,7 @@ // constructor PIXI.ImageLoader.prototype.constructor = PIXI.ImageLoader; +PIXI.EventTarget.mixin(PIXI.ImageLoader.prototype); /** * Loads image or takes it from cache @@ -45,7 +44,7 @@ { if(!this.texture.baseTexture.hasLoaded) { - this.texture.baseTexture.addEventListener('loaded', this.onLoaded.bind(this)); + this.texture.baseTexture.on('loaded', this.onLoaded.bind(this)); } else { @@ -61,7 +60,7 @@ */ PIXI.ImageLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; /** diff --git a/src/pixi/loaders/JsonLoader.js b/src/pixi/loaders/JsonLoader.js index bdfd6e0..17a2a95 100644 --- a/src/pixi/loaders/JsonLoader.js +++ b/src/pixi/loaders/JsonLoader.js @@ -14,8 +14,6 @@ * @param crossorigin {Boolean} Whether requests should be treated as crossorigin */ PIXI.JsonLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The url of the bitmap font data * @@ -54,6 +52,7 @@ // constructor PIXI.JsonLoader.prototype.constructor = PIXI.JsonLoader; +PIXI.EventTarget.mixin(PIXI.JsonLoader.prototype); /** * Loads the JSON data diff --git a/src/pixi/loaders/SpineLoader.js b/src/pixi/loaders/SpineLoader.js index 23072f1..027a980 100644 --- a/src/pixi/loaders/SpineLoader.js +++ b/src/pixi/loaders/SpineLoader.js @@ -23,8 +23,6 @@ */ PIXI.SpineLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The url of the bitmap font data * @@ -53,6 +51,8 @@ PIXI.SpineLoader.prototype.constructor = PIXI.SpineLoader; +PIXI.EventTarget.mixin(PIXI.SpineLoader.prototype); + /** * Loads the JSON data * @@ -62,8 +62,8 @@ var scope = this; var jsonLoader = new PIXI.JsonLoader(this.url, this.crossorigin); - jsonLoader.addEventListener("loaded", function (event) { - scope.json = event.content.json; + jsonLoader.on('loaded', function (event) { + scope.json = event.data.content.json; scope.onLoaded(); }); jsonLoader.load(); @@ -77,6 +77,6 @@ */ PIXI.SpineLoader.prototype.onLoaded = function () { this.loaded = true; - this.dispatchEvent({type: "loaded", content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/loaders/SpriteSheetLoader.js b/src/pixi/loaders/SpriteSheetLoader.js index bfb827a..971724f 100644 --- a/src/pixi/loaders/SpriteSheetLoader.js +++ b/src/pixi/loaders/SpriteSheetLoader.js @@ -23,7 +23,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -70,6 +69,8 @@ // constructor PIXI.SpriteSheetLoader.prototype.constructor = PIXI.SpriteSheetLoader; +PIXI.EventTarget.mixin(PIXI.SpriteSheetLoader.prototype); + /** * This will begin loading the JSON file * @@ -78,8 +79,8 @@ PIXI.SpriteSheetLoader.prototype.load = function () { var scope = this; var jsonLoader = new PIXI.JsonLoader(this.url, this.crossorigin); - jsonLoader.addEventListener('loaded', function (event) { - scope.json = event.content.json; + jsonLoader.on('loaded', function (event) { + scope.json = event.data.content.json; scope.onLoaded(); }); jsonLoader.load(); @@ -92,8 +93,7 @@ * @private */ PIXI.SpriteSheetLoader.prototype.onLoaded = function () { - this.dispatchEvent({ - type: 'loaded', + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/renderers/webgl/WebGLRenderer.js b/src/pixi/renderers/webgl/WebGLRenderer.js index e333d78..20c7b26 100644 --- a/src/pixi/renderers/webgl/WebGLRenderer.js +++ b/src/pixi/renderers/webgl/WebGLRenderer.js @@ -21,7 +21,7 @@ * @param [options.antialias=false] {Boolean} sets antialias (only applicable in chrome at the moment) * @param [options.preserveDrawingBuffer=false] {Boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context * @param [options.resolution=1] {Number} the resolution of the renderer retina would be 2 - * + * */ PIXI.WebGLRenderer = function(width, height, options) { @@ -37,7 +37,7 @@ options = PIXI.defaultRenderOptions; } - + if(!PIXI.defaultRenderer) { PIXI.sayHello('webGL'); @@ -581,8 +581,8 @@ // deal with losing context.. // remove listeners - this.view.removeEventListener('webglcontextlost', this.contextLostFunction); - this.view.removeEventListener('webglcontextrestored', this.contextRestoredFunction); + this.view.off('webglcontextlost', this.contextLostFunction); + this.view.off('webglcontextrestored', this.contextRestoredFunction); PIXI.glContexts[this.glContextId] = null; diff --git a/src/pixi/textures/BaseTexture.js b/src/pixi/textures/BaseTexture.js index a41b86f..5f954e2 100644 --- a/src/pixi/textures/BaseTexture.js +++ b/src/pixi/textures/BaseTexture.js @@ -19,8 +19,6 @@ */ PIXI.BaseTexture = function(source, scaleMode) { - PIXI.EventTarget.call( this ); - /** * The Resolution of the texture. * @@ -134,6 +132,7 @@ }; PIXI.BaseTexture.prototype.constructor = PIXI.BaseTexture; +PIXI.EventTarget.mixin(PIXI.BaseTexture.prototype); /** * Destroys this base texture diff --git a/src/pixi/textures/RenderTexture.js b/src/pixi/textures/RenderTexture.js index f1e4ad1..021e97a 100644 --- a/src/pixi/textures/RenderTexture.js +++ b/src/pixi/textures/RenderTexture.js @@ -34,9 +34,6 @@ */ PIXI.RenderTexture = function(width, height, renderer, scaleMode, resolution) { - PIXI.EventTarget.call( this ); - - /** * The with of the render texture * @@ -44,6 +41,7 @@ * @type Number */ this.width = width || 100; + /** * The height of the render texture * @@ -51,9 +49,9 @@ * @type Number */ this.height = height || 100; - + /** - * The Resolution of the texture. + * The Resolution of the texture. * * @property resolution * @type Number @@ -76,7 +74,6 @@ * @type Rectangle */ this.crop = new PIXI.Rectangle(0, 0, this.width * this.resolution, this.height * this.resolution); - /** * The base texture object that this texture uses @@ -95,6 +92,11 @@ this.baseTexture.hasLoaded = true; + PIXI.Texture.call(this, + this.baseTexture, + new PIXI.Rectangle(0, 0, this.width, this.height) + ); + // each render texture can only belong to one renderer at the moment if its webGL this.renderer = renderer || PIXI.defaultRenderer; @@ -156,10 +158,10 @@ this.projection.x = this.width / 2; this.projection.y = -this.height / 2; } - + if(!this.valid)return; + this.textureBuffer.resize(this.width * this.resolution, this.height * this.resolution); - }; /** @@ -175,7 +177,7 @@ { this.renderer.gl.bindFramebuffer(this.renderer.gl.FRAMEBUFFER, this.textureBuffer.frameBuffer); } - + this.textureBuffer.clear(); }; @@ -228,7 +230,7 @@ PIXI.WebGLRenderer.updateTextures(); this.renderer.spriteBatch.dirty = true; - + this.renderer.renderDisplayObject(displayObject, this.projection, this.textureBuffer.frameBuffer); displayObject.worldTransform = originalWorldTransform; @@ -254,7 +256,7 @@ var originalWorldTransform = displayObject.worldTransform; displayObject.worldTransform = PIXI.RenderTexture.tempMatrix; - + if(position) { displayObject.worldTransform.tx = position.x; @@ -284,9 +286,9 @@ //this.baseTexture.resolution = 2; this.renderer.renderDisplayObject(displayObject, context); - + this.renderer.resolution = realResolution; - + context.setTransform(1,0,0,1,0,0); displayObject.worldTransform = originalWorldTransform; @@ -342,7 +344,7 @@ } tempCanvas.context.putImageData(canvasData, 0, 0); - + return tempCanvas.canvas; } else diff --git a/src/pixi/textures/Texture.js b/src/pixi/textures/Texture.js index f2e5a0f..112875b 100644 --- a/src/pixi/textures/Texture.js +++ b/src/pixi/textures/Texture.js @@ -19,8 +19,6 @@ */ PIXI.Texture = function(baseTexture, frame) { - PIXI.EventTarget.call( this ); - /** * Does this Texture have any frame data assigned to it? * @@ -118,6 +116,7 @@ }; PIXI.Texture.prototype.constructor = PIXI.Texture; +PIXI.EventTarget.mixin(PIXI.Texture.prototype); /** * Called when the base texture is loaded diff --git a/src/pixi/utils/EventTarget.js b/src/pixi/utils/EventTarget.js index ada6f59..39c04a7 100644 --- a/src/pixi/utils/EventTarget.js +++ b/src/pixi/utils/EventTarget.js @@ -1,112 +1,284 @@ /** * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * https://github.com/mrdoob/eventtarget.js/ - * THankS mr DOob! + * @author Chad Engler https://github.com/englercj @Rolnaaba */ /** - * Adds event emitter functionality to a class + * Originally based on https://github.com/mrdoob/eventtarget.js/ from mr DOob. + * Currently takes inspiration from the nodejs EventEmitter, EventEmitter3, and smokesignals + */ + +/** + * Mixins event emitter functionality to a class * * @class EventTarget * @example - * function MyEmitter() { - * PIXI.EventTarget.call(this); //mixes in event target stuff - * } + * function MyEmitter() {} + * + * PIXI.EventTarget.mixin(MyEmitter.prototype); * * var em = new MyEmitter(); - * em.emit({ type: 'eventName', data: 'some data' }); + * em.emit('eventName', 'some data', 'some moar data', {}, null, ...); */ -PIXI.EventTarget = function () { +PIXI.EventTarget = { + /** + * Backward compat from when this used to be a function + */ + call: function callCompat(obj) { + if(obj) { + obj = obj.prototype || obj; + PIXI.EventTarget.mixin(obj); + } + }, /** - * Holds all the listeners + * Mixes in the properties of the EventTarget prototype onto another object * - * @property listeners + * @method mixin + * @param object {Object} The obj to mix into + */ + mixin: function mixin(obj) { + /** + * Return a list of assigned event listeners. + * + * @method listeners + * @param eventName {String} The events that should be listed. + * @returns {Array} An array of listener functions + */ + obj.listeners = function listeners(eventName) { + this._listeners = this._listeners || {}; + + return this._listeners[eventName] ? this._listeners[eventName].slice() : []; + }; + + /** + * Emit an event to all registered event listeners. + * + * @method emit + * @alias dispatchEvent + * @param eventName {String} The name of the event. + * @returns {Boolean} Indication if we've emitted an event. + */ + obj.emit = obj.dispatchEvent = function emit(eventName, data) { + this._listeners = this._listeners || {}; + + //backwards compat with old method ".emit({ type: 'something' })" + if(typeof eventName === 'object') { + data = eventName; + eventName = eventName.type; + } + + //ensure we are using a real pixi event + if(!data || data.__isEventObject !== true) { + data = new PIXI.Event(this, eventName, data); + } + + //iterate the listeners + if(this._listeners && this._listeners[eventName]) { + var listeners = this._listeners[eventName], + length = listeners.length, + fn = listeners[0], + i; + + for(i = 0; i < length; fn = listeners[++i]) { + //call the event listener + fn.call(this, data); + + //if "stopImmediatePropagation" is called, stop calling sibling events + if(data.stoppedImmediate) { + return this; + } + } + + //if "stopPropagation" is called then don't bubble the event + if(data.stopped) { + return this; + } + } + + //bubble this event up the scene graph + if(this.parent && this.parent.emit) { + this.parent.emit.call(this.parent, eventName, data); + } + + return this; + }; + + /** + * Register a new EventListener for the given event. + * + * @method on + * @alias addEventListener + * @param eventName {String} Name of the event. + * @param callback {Functon} fn Callback function. + */ + obj.on = obj.addEventListener = function on(eventName, fn) { + this._listeners = this._listeners || {}; + + (this._listeners[eventName] = this._listeners[eventName] || []) + .push(fn); + + return this; + }; + + /** + * Add an EventListener that's only called once. + * + * @method once + * @param eventName {String} Name of the event. + * @param callback {Function} Callback function. + */ + obj.once = function once(eventName, fn) { + this._listeners = this._listeners || {}; + + var self = this; + function onceHandlerWrapper() { + fn.apply(self.off(eventName, onceHandlerWrapper), arguments); + } + onceHandlerWrapper._originalHandler = fn; + + return this.on(eventName, onceHandlerWrapper); + }; + + /** + * Remove event listeners. + * + * @method off + * @alias removeEventListener + * @param eventName {String} The event we want to remove. + * @param callback {Function} The listener that we need to find. + */ + obj.off = obj.removeEventListener = function off(eventName, fn) { + this._listeners = this._listeners || {}; + + if(!this._listeners[eventName]) + return this; + + var list = this._listeners[eventName], + i = list.length = fn ? list.length : 0; + + while(i-- > 0) { + if(list[i] === fn || list[i]._originalHandler === fn) { + list.splice(i, 1); + } + } + + if(list.length === 0) { + delete this._listeners[eventName]; + } + + return this; + }; + + /** + * Remove all listeners or only the listeners for the specified event. + * + * @method removeAllListeners + * @param eventName {String} The event you want to remove all listeners for. + */ + obj.removeAllListeners = function removeAllListeners(eventName) { + this._listeners = this._listeners || {}; + + if(!this._listeners[eventName]) + return this; + + delete this._listeners[eventName]; + + return this; + }; + } +}; + +/** + * Creates an homogenous object for tracking events so users can know what to expect. + * + * @class Event + * @extends Object + * @constructor + * @param target {Object} The target object that the event is called on + * @param name {String} The string name of the event that was triggered + * @param data {Object} Arbitrary event data to pass along + */ +PIXI.Event = function(target, name, data) { + //for duck typing in the ".on()" function + this.__isEventObject = true; + + /** + * Tracks the state of bubbling propagation. Do not + * set this directly, instead use `event.stopPropagation()` + * + * @property stopped + * @type Boolean + * @private + * @readOnly + */ + this.stopped = false; + + /** + * Tracks the state of sibling listener propagation. Do not + * set this directly, instead use `event.stopImmediatePropagation()` + * + * @property stoppedImmediate + * @type Boolean + * @private + * @readOnly + */ + this.stoppedImmediate = false; + + /** + * The original target the event triggered on. + * + * @property target * @type Object + * @readOnly */ - var listeners = {}; + this.target = target; /** - * Adds a listener for a specific event + * The string name of the event that this represents. * - * @method addEventListener - * @param type {string} A string representing the event type to listen for. - * @param listener {function} The callback function that will be fired when the event occurs + * @property type + * @type String + * @readOnly */ - this.addEventListener = this.on = function ( type, listener ) { - - - if ( listeners[ type ] === undefined ) { - - listeners[ type ] = []; - - } - - if ( listeners[ type ].indexOf( listener ) === - 1 ) { - - listeners[ type ].unshift( listener ); - } - - }; + this.type = name; /** - * Fires the event, ie pretends that the event has happened + * The data that was passed in with this event. * - * @method dispatchEvent - * @param event {Event} the event object + * @property data + * @type Object + * @readOnly */ - this.dispatchEvent = this.emit = function ( event ) { + this.data = data; - if ( !listeners[ event.type ] || !listeners[ event.type ].length ) { - - return; - - } - - - for(var i = listeners[ event.type ].length-1; i >= 0; i--) { -// for(var i = 0, l=listeners[ event.type ].length; i < l; i++) { - - - listeners[ event.type ][ i ]( event ); - - } - - }; + //backwards compat with older version of events + this.content = data; /** - * Removes the specified listener that was assigned to the specified event type + * The timestamp when the event occurred. * - * @method removeEventListener - * @param type {string} A string representing the event type which will have its listener removed - * @param listener {function} The callback function that was be fired when the event occured + * @property timeStamp + * @type Number + * @readOnly */ - this.removeEventListener = this.off = function ( type, listener ) { + this.timeStamp = Date.now(); +}; - if ( listeners[ type ] === undefined ) return; +/** + * Stops the propagation of events up the scene graph (prevents bubbling). + * + * @method stopPropagation + */ +PIXI.Event.prototype.stopPropagation = function stopPropagation() { + this.stopped = true; +}; - var index = listeners[ type ].indexOf( listener ); - - if ( index !== - 1 ) { - - listeners[ type ].splice( index, 1 ); - - } - - }; - - /** - * Removes all the listeners that were active for the specified event type - * - * @method removeAllEventListeners - * @param type {string} A string representing the event type which will have all its listeners removed - */ - this.removeAllEventListeners = function( type ) { - var a = listeners[type]; - if (a) - a.length = 0; - }; +/** + * Stops the propagation of events to sibling listeners (no longer calls any listeners). + * + * @method stopImmediatePropagation + */ +PIXI.Event.prototype.stopImmediatePropagation = function stopImmediatePropagation() { + this.stoppedImmediate = true; }; diff --git a/test/lib/pixi/textures/RenderTexture.js b/test/lib/pixi/textures/RenderTexture.js new file mode 100644 index 0000000..8556501 --- /dev/null +++ b/test/lib/pixi/textures/RenderTexture.js @@ -0,0 +1,14 @@ + +function pixi_textures_RenderTexture_confirmNew(obj, done) { + var expect = chai.expect; + + expect(obj).to.have.property('width'); + expect(obj).to.have.property('height'); + + expect(obj).to.have.property('render'); + expect(obj).to.have.property('renderer'); + // expect(obj).to.have.property('projection'); + expect(obj).to.have.property('textureBuffer'); + + pixi_textures_Texture_confirmNew(obj, done); +} diff --git a/test/lib/pixi/textures/Texture.js b/test/lib/pixi/textures/Texture.js index d8eeeb3..2493385 100644 --- a/test/lib/pixi/textures/Texture.js +++ b/test/lib/pixi/textures/Texture.js @@ -11,7 +11,7 @@ } expect(obj).to.be.an.instanceof(PIXI.Texture); - pixi_utils_EventTarget_like(obj); + pixi_utils_EventTarget_confirm(obj); expect(obj).to.have.property('baseTexture') .and.to.be.an.instanceof(PIXI.BaseTexture); @@ -20,7 +20,7 @@ if (obj.baseTexture.hasLoaded) { confirmFrameDone(); } else { - obj.addEventListener('update', confirmFrameDone); + obj.on('update', confirmFrameDone); pixi_core_Rectangle_confirm(obj.frame, 0, 0, 1, 1); } } diff --git a/src/pixi/display/Sprite.js b/src/pixi/display/Sprite.js index 5cf3105..cffecce 100644 --- a/src/pixi/display/Sprite.js +++ b/src/pixi/display/Sprite.js @@ -82,7 +82,7 @@ else { this.onTextureUpdateBind = this.onTextureUpdate.bind(this); - this.texture.addEventListener( 'update', this.onTextureUpdateBind ); + this.texture.on( 'update', this.onTextureUpdateBind ); } this.renderable = true; diff --git a/src/pixi/loaders/AssetLoader.js b/src/pixi/loaders/AssetLoader.js index feab211..cf6d3d2 100644 --- a/src/pixi/loaders/AssetLoader.js +++ b/src/pixi/loaders/AssetLoader.js @@ -20,8 +20,6 @@ */ PIXI.AssetLoader = function(assetURLs, crossorigin) { - PIXI.EventTarget.call(this); - /** * The array of asset URLs that are going to be loaded * @@ -58,6 +56,8 @@ }; }; +PIXI.EventTarget.mixin(PIXI.AssetLoader.prototype); + /** * Fired when an item has loaded * @event onProgress @@ -114,7 +114,7 @@ var scope = this; function onLoad(evt) { - scope.onAssetLoaded(evt.content); + scope.onAssetLoaded(evt.data.content); } this.loadCount = this.assetURLs.length; @@ -135,7 +135,7 @@ var loader = new Constructor(fileName, this.crossorigin); - loader.addEventListener('loaded', onLoad); + loader.on('loaded', onLoad); loader.load(); } }; @@ -149,12 +149,12 @@ PIXI.AssetLoader.prototype.onAssetLoaded = function(loader) { this.loadCount--; - this.dispatchEvent({ type: 'onProgress', content: this, loader: loader }); + this.emit('onProgress', { content: this, loader: loader }); if (this.onProgress) this.onProgress(loader); if (!this.loadCount) { - this.dispatchEvent({type: 'onComplete', content: this}); + this.emit('onComplete', { content: this }); if(this.onComplete) this.onComplete(); } }; diff --git a/src/pixi/loaders/AtlasLoader.js b/src/pixi/loaders/AtlasLoader.js index 98e32c9..aa6c381 100644 --- a/src/pixi/loaders/AtlasLoader.js +++ b/src/pixi/loaders/AtlasLoader.js @@ -7,14 +7,13 @@ * When loaded this class will dispatch a 'loaded' event * If loading fails this class will dispatch an 'error' event * @class AtlasLoader - * @extends EventTarget + * @uses EventTarget * @constructor * @param {String} url the url of the JSON file * @param {Boolean} crossorigin */ PIXI.AtlasLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); this.url = url; this.baseUrl = url.replace(/[^\/]*$/, ''); this.crossorigin = crossorigin; @@ -25,6 +24,7 @@ // constructor PIXI.AtlasLoader.constructor = PIXI.AtlasLoader; +PIXI.EventTarget.mixin(PIXI.AtlasLoader.prototype); /** * Starts loading the JSON file @@ -145,7 +145,7 @@ this.currentImageId = 0; for (j = 0; j < this.images.length; j++) { - this.images[j].addEventListener('loaded', selfOnLoaded); + this.images[j].on('loaded', selfOnLoaded); } this.images[this.currentImageId].load(); @@ -170,10 +170,7 @@ this.images[this.currentImageId].load(); } else { this.loaded = true; - this.dispatchEvent({ - type: 'loaded', - content: this - }); + this.emit('loaded', { content: this }); } }; @@ -183,8 +180,5 @@ * @private */ PIXI.AtlasLoader.prototype.onError = function () { - this.dispatchEvent({ - type: 'error', - content: this - }); + this.emit('error', { content: this }); }; diff --git a/src/pixi/loaders/BitmapFontLoader.js b/src/pixi/loaders/BitmapFontLoader.js index f87fbdd..bbd1fed 100644 --- a/src/pixi/loaders/BitmapFontLoader.js +++ b/src/pixi/loaders/BitmapFontLoader.js @@ -21,7 +21,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -59,6 +58,7 @@ // constructor PIXI.BitmapFontLoader.prototype.constructor = PIXI.BitmapFontLoader; +PIXI.EventTarget.mixin(PIXI.BitmapFontLoader.prototype); /** * Loads the XML font data @@ -163,5 +163,5 @@ */ PIXI.BitmapFontLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/loaders/ImageLoader.js b/src/pixi/loaders/ImageLoader.js index ad4c61c..f1606f1 100644 --- a/src/pixi/loaders/ImageLoader.js +++ b/src/pixi/loaders/ImageLoader.js @@ -15,8 +15,6 @@ */ PIXI.ImageLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The texture being loaded * @@ -35,6 +33,7 @@ // constructor PIXI.ImageLoader.prototype.constructor = PIXI.ImageLoader; +PIXI.EventTarget.mixin(PIXI.ImageLoader.prototype); /** * Loads image or takes it from cache @@ -45,7 +44,7 @@ { if(!this.texture.baseTexture.hasLoaded) { - this.texture.baseTexture.addEventListener('loaded', this.onLoaded.bind(this)); + this.texture.baseTexture.on('loaded', this.onLoaded.bind(this)); } else { @@ -61,7 +60,7 @@ */ PIXI.ImageLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; /** diff --git a/src/pixi/loaders/JsonLoader.js b/src/pixi/loaders/JsonLoader.js index bdfd6e0..17a2a95 100644 --- a/src/pixi/loaders/JsonLoader.js +++ b/src/pixi/loaders/JsonLoader.js @@ -14,8 +14,6 @@ * @param crossorigin {Boolean} Whether requests should be treated as crossorigin */ PIXI.JsonLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The url of the bitmap font data * @@ -54,6 +52,7 @@ // constructor PIXI.JsonLoader.prototype.constructor = PIXI.JsonLoader; +PIXI.EventTarget.mixin(PIXI.JsonLoader.prototype); /** * Loads the JSON data diff --git a/src/pixi/loaders/SpineLoader.js b/src/pixi/loaders/SpineLoader.js index 23072f1..027a980 100644 --- a/src/pixi/loaders/SpineLoader.js +++ b/src/pixi/loaders/SpineLoader.js @@ -23,8 +23,6 @@ */ PIXI.SpineLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The url of the bitmap font data * @@ -53,6 +51,8 @@ PIXI.SpineLoader.prototype.constructor = PIXI.SpineLoader; +PIXI.EventTarget.mixin(PIXI.SpineLoader.prototype); + /** * Loads the JSON data * @@ -62,8 +62,8 @@ var scope = this; var jsonLoader = new PIXI.JsonLoader(this.url, this.crossorigin); - jsonLoader.addEventListener("loaded", function (event) { - scope.json = event.content.json; + jsonLoader.on('loaded', function (event) { + scope.json = event.data.content.json; scope.onLoaded(); }); jsonLoader.load(); @@ -77,6 +77,6 @@ */ PIXI.SpineLoader.prototype.onLoaded = function () { this.loaded = true; - this.dispatchEvent({type: "loaded", content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/loaders/SpriteSheetLoader.js b/src/pixi/loaders/SpriteSheetLoader.js index bfb827a..971724f 100644 --- a/src/pixi/loaders/SpriteSheetLoader.js +++ b/src/pixi/loaders/SpriteSheetLoader.js @@ -23,7 +23,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -70,6 +69,8 @@ // constructor PIXI.SpriteSheetLoader.prototype.constructor = PIXI.SpriteSheetLoader; +PIXI.EventTarget.mixin(PIXI.SpriteSheetLoader.prototype); + /** * This will begin loading the JSON file * @@ -78,8 +79,8 @@ PIXI.SpriteSheetLoader.prototype.load = function () { var scope = this; var jsonLoader = new PIXI.JsonLoader(this.url, this.crossorigin); - jsonLoader.addEventListener('loaded', function (event) { - scope.json = event.content.json; + jsonLoader.on('loaded', function (event) { + scope.json = event.data.content.json; scope.onLoaded(); }); jsonLoader.load(); @@ -92,8 +93,7 @@ * @private */ PIXI.SpriteSheetLoader.prototype.onLoaded = function () { - this.dispatchEvent({ - type: 'loaded', + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/renderers/webgl/WebGLRenderer.js b/src/pixi/renderers/webgl/WebGLRenderer.js index e333d78..20c7b26 100644 --- a/src/pixi/renderers/webgl/WebGLRenderer.js +++ b/src/pixi/renderers/webgl/WebGLRenderer.js @@ -21,7 +21,7 @@ * @param [options.antialias=false] {Boolean} sets antialias (only applicable in chrome at the moment) * @param [options.preserveDrawingBuffer=false] {Boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context * @param [options.resolution=1] {Number} the resolution of the renderer retina would be 2 - * + * */ PIXI.WebGLRenderer = function(width, height, options) { @@ -37,7 +37,7 @@ options = PIXI.defaultRenderOptions; } - + if(!PIXI.defaultRenderer) { PIXI.sayHello('webGL'); @@ -581,8 +581,8 @@ // deal with losing context.. // remove listeners - this.view.removeEventListener('webglcontextlost', this.contextLostFunction); - this.view.removeEventListener('webglcontextrestored', this.contextRestoredFunction); + this.view.off('webglcontextlost', this.contextLostFunction); + this.view.off('webglcontextrestored', this.contextRestoredFunction); PIXI.glContexts[this.glContextId] = null; diff --git a/src/pixi/textures/BaseTexture.js b/src/pixi/textures/BaseTexture.js index a41b86f..5f954e2 100644 --- a/src/pixi/textures/BaseTexture.js +++ b/src/pixi/textures/BaseTexture.js @@ -19,8 +19,6 @@ */ PIXI.BaseTexture = function(source, scaleMode) { - PIXI.EventTarget.call( this ); - /** * The Resolution of the texture. * @@ -134,6 +132,7 @@ }; PIXI.BaseTexture.prototype.constructor = PIXI.BaseTexture; +PIXI.EventTarget.mixin(PIXI.BaseTexture.prototype); /** * Destroys this base texture diff --git a/src/pixi/textures/RenderTexture.js b/src/pixi/textures/RenderTexture.js index f1e4ad1..021e97a 100644 --- a/src/pixi/textures/RenderTexture.js +++ b/src/pixi/textures/RenderTexture.js @@ -34,9 +34,6 @@ */ PIXI.RenderTexture = function(width, height, renderer, scaleMode, resolution) { - PIXI.EventTarget.call( this ); - - /** * The with of the render texture * @@ -44,6 +41,7 @@ * @type Number */ this.width = width || 100; + /** * The height of the render texture * @@ -51,9 +49,9 @@ * @type Number */ this.height = height || 100; - + /** - * The Resolution of the texture. + * The Resolution of the texture. * * @property resolution * @type Number @@ -76,7 +74,6 @@ * @type Rectangle */ this.crop = new PIXI.Rectangle(0, 0, this.width * this.resolution, this.height * this.resolution); - /** * The base texture object that this texture uses @@ -95,6 +92,11 @@ this.baseTexture.hasLoaded = true; + PIXI.Texture.call(this, + this.baseTexture, + new PIXI.Rectangle(0, 0, this.width, this.height) + ); + // each render texture can only belong to one renderer at the moment if its webGL this.renderer = renderer || PIXI.defaultRenderer; @@ -156,10 +158,10 @@ this.projection.x = this.width / 2; this.projection.y = -this.height / 2; } - + if(!this.valid)return; + this.textureBuffer.resize(this.width * this.resolution, this.height * this.resolution); - }; /** @@ -175,7 +177,7 @@ { this.renderer.gl.bindFramebuffer(this.renderer.gl.FRAMEBUFFER, this.textureBuffer.frameBuffer); } - + this.textureBuffer.clear(); }; @@ -228,7 +230,7 @@ PIXI.WebGLRenderer.updateTextures(); this.renderer.spriteBatch.dirty = true; - + this.renderer.renderDisplayObject(displayObject, this.projection, this.textureBuffer.frameBuffer); displayObject.worldTransform = originalWorldTransform; @@ -254,7 +256,7 @@ var originalWorldTransform = displayObject.worldTransform; displayObject.worldTransform = PIXI.RenderTexture.tempMatrix; - + if(position) { displayObject.worldTransform.tx = position.x; @@ -284,9 +286,9 @@ //this.baseTexture.resolution = 2; this.renderer.renderDisplayObject(displayObject, context); - + this.renderer.resolution = realResolution; - + context.setTransform(1,0,0,1,0,0); displayObject.worldTransform = originalWorldTransform; @@ -342,7 +344,7 @@ } tempCanvas.context.putImageData(canvasData, 0, 0); - + return tempCanvas.canvas; } else diff --git a/src/pixi/textures/Texture.js b/src/pixi/textures/Texture.js index f2e5a0f..112875b 100644 --- a/src/pixi/textures/Texture.js +++ b/src/pixi/textures/Texture.js @@ -19,8 +19,6 @@ */ PIXI.Texture = function(baseTexture, frame) { - PIXI.EventTarget.call( this ); - /** * Does this Texture have any frame data assigned to it? * @@ -118,6 +116,7 @@ }; PIXI.Texture.prototype.constructor = PIXI.Texture; +PIXI.EventTarget.mixin(PIXI.Texture.prototype); /** * Called when the base texture is loaded diff --git a/src/pixi/utils/EventTarget.js b/src/pixi/utils/EventTarget.js index ada6f59..39c04a7 100644 --- a/src/pixi/utils/EventTarget.js +++ b/src/pixi/utils/EventTarget.js @@ -1,112 +1,284 @@ /** * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * https://github.com/mrdoob/eventtarget.js/ - * THankS mr DOob! + * @author Chad Engler https://github.com/englercj @Rolnaaba */ /** - * Adds event emitter functionality to a class + * Originally based on https://github.com/mrdoob/eventtarget.js/ from mr DOob. + * Currently takes inspiration from the nodejs EventEmitter, EventEmitter3, and smokesignals + */ + +/** + * Mixins event emitter functionality to a class * * @class EventTarget * @example - * function MyEmitter() { - * PIXI.EventTarget.call(this); //mixes in event target stuff - * } + * function MyEmitter() {} + * + * PIXI.EventTarget.mixin(MyEmitter.prototype); * * var em = new MyEmitter(); - * em.emit({ type: 'eventName', data: 'some data' }); + * em.emit('eventName', 'some data', 'some moar data', {}, null, ...); */ -PIXI.EventTarget = function () { +PIXI.EventTarget = { + /** + * Backward compat from when this used to be a function + */ + call: function callCompat(obj) { + if(obj) { + obj = obj.prototype || obj; + PIXI.EventTarget.mixin(obj); + } + }, /** - * Holds all the listeners + * Mixes in the properties of the EventTarget prototype onto another object * - * @property listeners + * @method mixin + * @param object {Object} The obj to mix into + */ + mixin: function mixin(obj) { + /** + * Return a list of assigned event listeners. + * + * @method listeners + * @param eventName {String} The events that should be listed. + * @returns {Array} An array of listener functions + */ + obj.listeners = function listeners(eventName) { + this._listeners = this._listeners || {}; + + return this._listeners[eventName] ? this._listeners[eventName].slice() : []; + }; + + /** + * Emit an event to all registered event listeners. + * + * @method emit + * @alias dispatchEvent + * @param eventName {String} The name of the event. + * @returns {Boolean} Indication if we've emitted an event. + */ + obj.emit = obj.dispatchEvent = function emit(eventName, data) { + this._listeners = this._listeners || {}; + + //backwards compat with old method ".emit({ type: 'something' })" + if(typeof eventName === 'object') { + data = eventName; + eventName = eventName.type; + } + + //ensure we are using a real pixi event + if(!data || data.__isEventObject !== true) { + data = new PIXI.Event(this, eventName, data); + } + + //iterate the listeners + if(this._listeners && this._listeners[eventName]) { + var listeners = this._listeners[eventName], + length = listeners.length, + fn = listeners[0], + i; + + for(i = 0; i < length; fn = listeners[++i]) { + //call the event listener + fn.call(this, data); + + //if "stopImmediatePropagation" is called, stop calling sibling events + if(data.stoppedImmediate) { + return this; + } + } + + //if "stopPropagation" is called then don't bubble the event + if(data.stopped) { + return this; + } + } + + //bubble this event up the scene graph + if(this.parent && this.parent.emit) { + this.parent.emit.call(this.parent, eventName, data); + } + + return this; + }; + + /** + * Register a new EventListener for the given event. + * + * @method on + * @alias addEventListener + * @param eventName {String} Name of the event. + * @param callback {Functon} fn Callback function. + */ + obj.on = obj.addEventListener = function on(eventName, fn) { + this._listeners = this._listeners || {}; + + (this._listeners[eventName] = this._listeners[eventName] || []) + .push(fn); + + return this; + }; + + /** + * Add an EventListener that's only called once. + * + * @method once + * @param eventName {String} Name of the event. + * @param callback {Function} Callback function. + */ + obj.once = function once(eventName, fn) { + this._listeners = this._listeners || {}; + + var self = this; + function onceHandlerWrapper() { + fn.apply(self.off(eventName, onceHandlerWrapper), arguments); + } + onceHandlerWrapper._originalHandler = fn; + + return this.on(eventName, onceHandlerWrapper); + }; + + /** + * Remove event listeners. + * + * @method off + * @alias removeEventListener + * @param eventName {String} The event we want to remove. + * @param callback {Function} The listener that we need to find. + */ + obj.off = obj.removeEventListener = function off(eventName, fn) { + this._listeners = this._listeners || {}; + + if(!this._listeners[eventName]) + return this; + + var list = this._listeners[eventName], + i = list.length = fn ? list.length : 0; + + while(i-- > 0) { + if(list[i] === fn || list[i]._originalHandler === fn) { + list.splice(i, 1); + } + } + + if(list.length === 0) { + delete this._listeners[eventName]; + } + + return this; + }; + + /** + * Remove all listeners or only the listeners for the specified event. + * + * @method removeAllListeners + * @param eventName {String} The event you want to remove all listeners for. + */ + obj.removeAllListeners = function removeAllListeners(eventName) { + this._listeners = this._listeners || {}; + + if(!this._listeners[eventName]) + return this; + + delete this._listeners[eventName]; + + return this; + }; + } +}; + +/** + * Creates an homogenous object for tracking events so users can know what to expect. + * + * @class Event + * @extends Object + * @constructor + * @param target {Object} The target object that the event is called on + * @param name {String} The string name of the event that was triggered + * @param data {Object} Arbitrary event data to pass along + */ +PIXI.Event = function(target, name, data) { + //for duck typing in the ".on()" function + this.__isEventObject = true; + + /** + * Tracks the state of bubbling propagation. Do not + * set this directly, instead use `event.stopPropagation()` + * + * @property stopped + * @type Boolean + * @private + * @readOnly + */ + this.stopped = false; + + /** + * Tracks the state of sibling listener propagation. Do not + * set this directly, instead use `event.stopImmediatePropagation()` + * + * @property stoppedImmediate + * @type Boolean + * @private + * @readOnly + */ + this.stoppedImmediate = false; + + /** + * The original target the event triggered on. + * + * @property target * @type Object + * @readOnly */ - var listeners = {}; + this.target = target; /** - * Adds a listener for a specific event + * The string name of the event that this represents. * - * @method addEventListener - * @param type {string} A string representing the event type to listen for. - * @param listener {function} The callback function that will be fired when the event occurs + * @property type + * @type String + * @readOnly */ - this.addEventListener = this.on = function ( type, listener ) { - - - if ( listeners[ type ] === undefined ) { - - listeners[ type ] = []; - - } - - if ( listeners[ type ].indexOf( listener ) === - 1 ) { - - listeners[ type ].unshift( listener ); - } - - }; + this.type = name; /** - * Fires the event, ie pretends that the event has happened + * The data that was passed in with this event. * - * @method dispatchEvent - * @param event {Event} the event object + * @property data + * @type Object + * @readOnly */ - this.dispatchEvent = this.emit = function ( event ) { + this.data = data; - if ( !listeners[ event.type ] || !listeners[ event.type ].length ) { - - return; - - } - - - for(var i = listeners[ event.type ].length-1; i >= 0; i--) { -// for(var i = 0, l=listeners[ event.type ].length; i < l; i++) { - - - listeners[ event.type ][ i ]( event ); - - } - - }; + //backwards compat with older version of events + this.content = data; /** - * Removes the specified listener that was assigned to the specified event type + * The timestamp when the event occurred. * - * @method removeEventListener - * @param type {string} A string representing the event type which will have its listener removed - * @param listener {function} The callback function that was be fired when the event occured + * @property timeStamp + * @type Number + * @readOnly */ - this.removeEventListener = this.off = function ( type, listener ) { + this.timeStamp = Date.now(); +}; - if ( listeners[ type ] === undefined ) return; +/** + * Stops the propagation of events up the scene graph (prevents bubbling). + * + * @method stopPropagation + */ +PIXI.Event.prototype.stopPropagation = function stopPropagation() { + this.stopped = true; +}; - var index = listeners[ type ].indexOf( listener ); - - if ( index !== - 1 ) { - - listeners[ type ].splice( index, 1 ); - - } - - }; - - /** - * Removes all the listeners that were active for the specified event type - * - * @method removeAllEventListeners - * @param type {string} A string representing the event type which will have all its listeners removed - */ - this.removeAllEventListeners = function( type ) { - var a = listeners[type]; - if (a) - a.length = 0; - }; +/** + * Stops the propagation of events to sibling listeners (no longer calls any listeners). + * + * @method stopImmediatePropagation + */ +PIXI.Event.prototype.stopImmediatePropagation = function stopImmediatePropagation() { + this.stoppedImmediate = true; }; diff --git a/test/lib/pixi/textures/RenderTexture.js b/test/lib/pixi/textures/RenderTexture.js new file mode 100644 index 0000000..8556501 --- /dev/null +++ b/test/lib/pixi/textures/RenderTexture.js @@ -0,0 +1,14 @@ + +function pixi_textures_RenderTexture_confirmNew(obj, done) { + var expect = chai.expect; + + expect(obj).to.have.property('width'); + expect(obj).to.have.property('height'); + + expect(obj).to.have.property('render'); + expect(obj).to.have.property('renderer'); + // expect(obj).to.have.property('projection'); + expect(obj).to.have.property('textureBuffer'); + + pixi_textures_Texture_confirmNew(obj, done); +} diff --git a/test/lib/pixi/textures/Texture.js b/test/lib/pixi/textures/Texture.js index d8eeeb3..2493385 100644 --- a/test/lib/pixi/textures/Texture.js +++ b/test/lib/pixi/textures/Texture.js @@ -11,7 +11,7 @@ } expect(obj).to.be.an.instanceof(PIXI.Texture); - pixi_utils_EventTarget_like(obj); + pixi_utils_EventTarget_confirm(obj); expect(obj).to.have.property('baseTexture') .and.to.be.an.instanceof(PIXI.BaseTexture); @@ -20,7 +20,7 @@ if (obj.baseTexture.hasLoaded) { confirmFrameDone(); } else { - obj.addEventListener('update', confirmFrameDone); + obj.on('update', confirmFrameDone); pixi_core_Rectangle_confirm(obj.frame, 0, 0, 1, 1); } } diff --git a/test/lib/pixi/utils/EventTarget.js b/test/lib/pixi/utils/EventTarget.js index 7b1dcfa..a0c3924 100644 --- a/test/lib/pixi/utils/EventTarget.js +++ b/test/lib/pixi/utils/EventTarget.js @@ -1,8 +1,34 @@ -function pixi_utils_EventTarget_like(obj) { +function pixi_utils_EventTarget_confirm(obj) { var expect = chai.expect; + //public API + expect(obj).to.respondTo('listeners'); + expect(obj).to.respondTo('emit'); + expect(obj).to.respondTo('on'); + expect(obj).to.respondTo('once'); + expect(obj).to.respondTo('off'); + expect(obj).to.respondTo('removeAllListeners'); + + //Aliased names + expect(obj).to.respondTo('removeEventListener'); expect(obj).to.respondTo('addEventListener'); expect(obj).to.respondTo('dispatchEvent'); - expect(obj).to.respondTo('removeEventListener'); } + +function pixi_utils_EventTarget_Event_confirm(event, obj, data) { + var expect = chai.expect; + + expect(event).to.be.an.instanceOf(PIXI.Event); + + expect(event).to.have.property('stopped', false); + expect(event).to.have.property('stoppedImmediate', false); + + expect(event).to.have.property('target', obj); + expect(event).to.have.property('type', data.type || 'myevent'); + expect(event).to.have.property('data', data); + expect(event).to.have.property('content', data); + + expect(event).to.respondTo('stopPropagation'); + expect(event).to.respondTo('stopImmediatePropagation'); +} \ No newline at end of file diff --git a/src/pixi/display/Sprite.js b/src/pixi/display/Sprite.js index 5cf3105..cffecce 100644 --- a/src/pixi/display/Sprite.js +++ b/src/pixi/display/Sprite.js @@ -82,7 +82,7 @@ else { this.onTextureUpdateBind = this.onTextureUpdate.bind(this); - this.texture.addEventListener( 'update', this.onTextureUpdateBind ); + this.texture.on( 'update', this.onTextureUpdateBind ); } this.renderable = true; diff --git a/src/pixi/loaders/AssetLoader.js b/src/pixi/loaders/AssetLoader.js index feab211..cf6d3d2 100644 --- a/src/pixi/loaders/AssetLoader.js +++ b/src/pixi/loaders/AssetLoader.js @@ -20,8 +20,6 @@ */ PIXI.AssetLoader = function(assetURLs, crossorigin) { - PIXI.EventTarget.call(this); - /** * The array of asset URLs that are going to be loaded * @@ -58,6 +56,8 @@ }; }; +PIXI.EventTarget.mixin(PIXI.AssetLoader.prototype); + /** * Fired when an item has loaded * @event onProgress @@ -114,7 +114,7 @@ var scope = this; function onLoad(evt) { - scope.onAssetLoaded(evt.content); + scope.onAssetLoaded(evt.data.content); } this.loadCount = this.assetURLs.length; @@ -135,7 +135,7 @@ var loader = new Constructor(fileName, this.crossorigin); - loader.addEventListener('loaded', onLoad); + loader.on('loaded', onLoad); loader.load(); } }; @@ -149,12 +149,12 @@ PIXI.AssetLoader.prototype.onAssetLoaded = function(loader) { this.loadCount--; - this.dispatchEvent({ type: 'onProgress', content: this, loader: loader }); + this.emit('onProgress', { content: this, loader: loader }); if (this.onProgress) this.onProgress(loader); if (!this.loadCount) { - this.dispatchEvent({type: 'onComplete', content: this}); + this.emit('onComplete', { content: this }); if(this.onComplete) this.onComplete(); } }; diff --git a/src/pixi/loaders/AtlasLoader.js b/src/pixi/loaders/AtlasLoader.js index 98e32c9..aa6c381 100644 --- a/src/pixi/loaders/AtlasLoader.js +++ b/src/pixi/loaders/AtlasLoader.js @@ -7,14 +7,13 @@ * When loaded this class will dispatch a 'loaded' event * If loading fails this class will dispatch an 'error' event * @class AtlasLoader - * @extends EventTarget + * @uses EventTarget * @constructor * @param {String} url the url of the JSON file * @param {Boolean} crossorigin */ PIXI.AtlasLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); this.url = url; this.baseUrl = url.replace(/[^\/]*$/, ''); this.crossorigin = crossorigin; @@ -25,6 +24,7 @@ // constructor PIXI.AtlasLoader.constructor = PIXI.AtlasLoader; +PIXI.EventTarget.mixin(PIXI.AtlasLoader.prototype); /** * Starts loading the JSON file @@ -145,7 +145,7 @@ this.currentImageId = 0; for (j = 0; j < this.images.length; j++) { - this.images[j].addEventListener('loaded', selfOnLoaded); + this.images[j].on('loaded', selfOnLoaded); } this.images[this.currentImageId].load(); @@ -170,10 +170,7 @@ this.images[this.currentImageId].load(); } else { this.loaded = true; - this.dispatchEvent({ - type: 'loaded', - content: this - }); + this.emit('loaded', { content: this }); } }; @@ -183,8 +180,5 @@ * @private */ PIXI.AtlasLoader.prototype.onError = function () { - this.dispatchEvent({ - type: 'error', - content: this - }); + this.emit('error', { content: this }); }; diff --git a/src/pixi/loaders/BitmapFontLoader.js b/src/pixi/loaders/BitmapFontLoader.js index f87fbdd..bbd1fed 100644 --- a/src/pixi/loaders/BitmapFontLoader.js +++ b/src/pixi/loaders/BitmapFontLoader.js @@ -21,7 +21,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -59,6 +58,7 @@ // constructor PIXI.BitmapFontLoader.prototype.constructor = PIXI.BitmapFontLoader; +PIXI.EventTarget.mixin(PIXI.BitmapFontLoader.prototype); /** * Loads the XML font data @@ -163,5 +163,5 @@ */ PIXI.BitmapFontLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/loaders/ImageLoader.js b/src/pixi/loaders/ImageLoader.js index ad4c61c..f1606f1 100644 --- a/src/pixi/loaders/ImageLoader.js +++ b/src/pixi/loaders/ImageLoader.js @@ -15,8 +15,6 @@ */ PIXI.ImageLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The texture being loaded * @@ -35,6 +33,7 @@ // constructor PIXI.ImageLoader.prototype.constructor = PIXI.ImageLoader; +PIXI.EventTarget.mixin(PIXI.ImageLoader.prototype); /** * Loads image or takes it from cache @@ -45,7 +44,7 @@ { if(!this.texture.baseTexture.hasLoaded) { - this.texture.baseTexture.addEventListener('loaded', this.onLoaded.bind(this)); + this.texture.baseTexture.on('loaded', this.onLoaded.bind(this)); } else { @@ -61,7 +60,7 @@ */ PIXI.ImageLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; /** diff --git a/src/pixi/loaders/JsonLoader.js b/src/pixi/loaders/JsonLoader.js index bdfd6e0..17a2a95 100644 --- a/src/pixi/loaders/JsonLoader.js +++ b/src/pixi/loaders/JsonLoader.js @@ -14,8 +14,6 @@ * @param crossorigin {Boolean} Whether requests should be treated as crossorigin */ PIXI.JsonLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The url of the bitmap font data * @@ -54,6 +52,7 @@ // constructor PIXI.JsonLoader.prototype.constructor = PIXI.JsonLoader; +PIXI.EventTarget.mixin(PIXI.JsonLoader.prototype); /** * Loads the JSON data diff --git a/src/pixi/loaders/SpineLoader.js b/src/pixi/loaders/SpineLoader.js index 23072f1..027a980 100644 --- a/src/pixi/loaders/SpineLoader.js +++ b/src/pixi/loaders/SpineLoader.js @@ -23,8 +23,6 @@ */ PIXI.SpineLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The url of the bitmap font data * @@ -53,6 +51,8 @@ PIXI.SpineLoader.prototype.constructor = PIXI.SpineLoader; +PIXI.EventTarget.mixin(PIXI.SpineLoader.prototype); + /** * Loads the JSON data * @@ -62,8 +62,8 @@ var scope = this; var jsonLoader = new PIXI.JsonLoader(this.url, this.crossorigin); - jsonLoader.addEventListener("loaded", function (event) { - scope.json = event.content.json; + jsonLoader.on('loaded', function (event) { + scope.json = event.data.content.json; scope.onLoaded(); }); jsonLoader.load(); @@ -77,6 +77,6 @@ */ PIXI.SpineLoader.prototype.onLoaded = function () { this.loaded = true; - this.dispatchEvent({type: "loaded", content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/loaders/SpriteSheetLoader.js b/src/pixi/loaders/SpriteSheetLoader.js index bfb827a..971724f 100644 --- a/src/pixi/loaders/SpriteSheetLoader.js +++ b/src/pixi/loaders/SpriteSheetLoader.js @@ -23,7 +23,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -70,6 +69,8 @@ // constructor PIXI.SpriteSheetLoader.prototype.constructor = PIXI.SpriteSheetLoader; +PIXI.EventTarget.mixin(PIXI.SpriteSheetLoader.prototype); + /** * This will begin loading the JSON file * @@ -78,8 +79,8 @@ PIXI.SpriteSheetLoader.prototype.load = function () { var scope = this; var jsonLoader = new PIXI.JsonLoader(this.url, this.crossorigin); - jsonLoader.addEventListener('loaded', function (event) { - scope.json = event.content.json; + jsonLoader.on('loaded', function (event) { + scope.json = event.data.content.json; scope.onLoaded(); }); jsonLoader.load(); @@ -92,8 +93,7 @@ * @private */ PIXI.SpriteSheetLoader.prototype.onLoaded = function () { - this.dispatchEvent({ - type: 'loaded', + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/renderers/webgl/WebGLRenderer.js b/src/pixi/renderers/webgl/WebGLRenderer.js index e333d78..20c7b26 100644 --- a/src/pixi/renderers/webgl/WebGLRenderer.js +++ b/src/pixi/renderers/webgl/WebGLRenderer.js @@ -21,7 +21,7 @@ * @param [options.antialias=false] {Boolean} sets antialias (only applicable in chrome at the moment) * @param [options.preserveDrawingBuffer=false] {Boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context * @param [options.resolution=1] {Number} the resolution of the renderer retina would be 2 - * + * */ PIXI.WebGLRenderer = function(width, height, options) { @@ -37,7 +37,7 @@ options = PIXI.defaultRenderOptions; } - + if(!PIXI.defaultRenderer) { PIXI.sayHello('webGL'); @@ -581,8 +581,8 @@ // deal with losing context.. // remove listeners - this.view.removeEventListener('webglcontextlost', this.contextLostFunction); - this.view.removeEventListener('webglcontextrestored', this.contextRestoredFunction); + this.view.off('webglcontextlost', this.contextLostFunction); + this.view.off('webglcontextrestored', this.contextRestoredFunction); PIXI.glContexts[this.glContextId] = null; diff --git a/src/pixi/textures/BaseTexture.js b/src/pixi/textures/BaseTexture.js index a41b86f..5f954e2 100644 --- a/src/pixi/textures/BaseTexture.js +++ b/src/pixi/textures/BaseTexture.js @@ -19,8 +19,6 @@ */ PIXI.BaseTexture = function(source, scaleMode) { - PIXI.EventTarget.call( this ); - /** * The Resolution of the texture. * @@ -134,6 +132,7 @@ }; PIXI.BaseTexture.prototype.constructor = PIXI.BaseTexture; +PIXI.EventTarget.mixin(PIXI.BaseTexture.prototype); /** * Destroys this base texture diff --git a/src/pixi/textures/RenderTexture.js b/src/pixi/textures/RenderTexture.js index f1e4ad1..021e97a 100644 --- a/src/pixi/textures/RenderTexture.js +++ b/src/pixi/textures/RenderTexture.js @@ -34,9 +34,6 @@ */ PIXI.RenderTexture = function(width, height, renderer, scaleMode, resolution) { - PIXI.EventTarget.call( this ); - - /** * The with of the render texture * @@ -44,6 +41,7 @@ * @type Number */ this.width = width || 100; + /** * The height of the render texture * @@ -51,9 +49,9 @@ * @type Number */ this.height = height || 100; - + /** - * The Resolution of the texture. + * The Resolution of the texture. * * @property resolution * @type Number @@ -76,7 +74,6 @@ * @type Rectangle */ this.crop = new PIXI.Rectangle(0, 0, this.width * this.resolution, this.height * this.resolution); - /** * The base texture object that this texture uses @@ -95,6 +92,11 @@ this.baseTexture.hasLoaded = true; + PIXI.Texture.call(this, + this.baseTexture, + new PIXI.Rectangle(0, 0, this.width, this.height) + ); + // each render texture can only belong to one renderer at the moment if its webGL this.renderer = renderer || PIXI.defaultRenderer; @@ -156,10 +158,10 @@ this.projection.x = this.width / 2; this.projection.y = -this.height / 2; } - + if(!this.valid)return; + this.textureBuffer.resize(this.width * this.resolution, this.height * this.resolution); - }; /** @@ -175,7 +177,7 @@ { this.renderer.gl.bindFramebuffer(this.renderer.gl.FRAMEBUFFER, this.textureBuffer.frameBuffer); } - + this.textureBuffer.clear(); }; @@ -228,7 +230,7 @@ PIXI.WebGLRenderer.updateTextures(); this.renderer.spriteBatch.dirty = true; - + this.renderer.renderDisplayObject(displayObject, this.projection, this.textureBuffer.frameBuffer); displayObject.worldTransform = originalWorldTransform; @@ -254,7 +256,7 @@ var originalWorldTransform = displayObject.worldTransform; displayObject.worldTransform = PIXI.RenderTexture.tempMatrix; - + if(position) { displayObject.worldTransform.tx = position.x; @@ -284,9 +286,9 @@ //this.baseTexture.resolution = 2; this.renderer.renderDisplayObject(displayObject, context); - + this.renderer.resolution = realResolution; - + context.setTransform(1,0,0,1,0,0); displayObject.worldTransform = originalWorldTransform; @@ -342,7 +344,7 @@ } tempCanvas.context.putImageData(canvasData, 0, 0); - + return tempCanvas.canvas; } else diff --git a/src/pixi/textures/Texture.js b/src/pixi/textures/Texture.js index f2e5a0f..112875b 100644 --- a/src/pixi/textures/Texture.js +++ b/src/pixi/textures/Texture.js @@ -19,8 +19,6 @@ */ PIXI.Texture = function(baseTexture, frame) { - PIXI.EventTarget.call( this ); - /** * Does this Texture have any frame data assigned to it? * @@ -118,6 +116,7 @@ }; PIXI.Texture.prototype.constructor = PIXI.Texture; +PIXI.EventTarget.mixin(PIXI.Texture.prototype); /** * Called when the base texture is loaded diff --git a/src/pixi/utils/EventTarget.js b/src/pixi/utils/EventTarget.js index ada6f59..39c04a7 100644 --- a/src/pixi/utils/EventTarget.js +++ b/src/pixi/utils/EventTarget.js @@ -1,112 +1,284 @@ /** * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * https://github.com/mrdoob/eventtarget.js/ - * THankS mr DOob! + * @author Chad Engler https://github.com/englercj @Rolnaaba */ /** - * Adds event emitter functionality to a class + * Originally based on https://github.com/mrdoob/eventtarget.js/ from mr DOob. + * Currently takes inspiration from the nodejs EventEmitter, EventEmitter3, and smokesignals + */ + +/** + * Mixins event emitter functionality to a class * * @class EventTarget * @example - * function MyEmitter() { - * PIXI.EventTarget.call(this); //mixes in event target stuff - * } + * function MyEmitter() {} + * + * PIXI.EventTarget.mixin(MyEmitter.prototype); * * var em = new MyEmitter(); - * em.emit({ type: 'eventName', data: 'some data' }); + * em.emit('eventName', 'some data', 'some moar data', {}, null, ...); */ -PIXI.EventTarget = function () { +PIXI.EventTarget = { + /** + * Backward compat from when this used to be a function + */ + call: function callCompat(obj) { + if(obj) { + obj = obj.prototype || obj; + PIXI.EventTarget.mixin(obj); + } + }, /** - * Holds all the listeners + * Mixes in the properties of the EventTarget prototype onto another object * - * @property listeners + * @method mixin + * @param object {Object} The obj to mix into + */ + mixin: function mixin(obj) { + /** + * Return a list of assigned event listeners. + * + * @method listeners + * @param eventName {String} The events that should be listed. + * @returns {Array} An array of listener functions + */ + obj.listeners = function listeners(eventName) { + this._listeners = this._listeners || {}; + + return this._listeners[eventName] ? this._listeners[eventName].slice() : []; + }; + + /** + * Emit an event to all registered event listeners. + * + * @method emit + * @alias dispatchEvent + * @param eventName {String} The name of the event. + * @returns {Boolean} Indication if we've emitted an event. + */ + obj.emit = obj.dispatchEvent = function emit(eventName, data) { + this._listeners = this._listeners || {}; + + //backwards compat with old method ".emit({ type: 'something' })" + if(typeof eventName === 'object') { + data = eventName; + eventName = eventName.type; + } + + //ensure we are using a real pixi event + if(!data || data.__isEventObject !== true) { + data = new PIXI.Event(this, eventName, data); + } + + //iterate the listeners + if(this._listeners && this._listeners[eventName]) { + var listeners = this._listeners[eventName], + length = listeners.length, + fn = listeners[0], + i; + + for(i = 0; i < length; fn = listeners[++i]) { + //call the event listener + fn.call(this, data); + + //if "stopImmediatePropagation" is called, stop calling sibling events + if(data.stoppedImmediate) { + return this; + } + } + + //if "stopPropagation" is called then don't bubble the event + if(data.stopped) { + return this; + } + } + + //bubble this event up the scene graph + if(this.parent && this.parent.emit) { + this.parent.emit.call(this.parent, eventName, data); + } + + return this; + }; + + /** + * Register a new EventListener for the given event. + * + * @method on + * @alias addEventListener + * @param eventName {String} Name of the event. + * @param callback {Functon} fn Callback function. + */ + obj.on = obj.addEventListener = function on(eventName, fn) { + this._listeners = this._listeners || {}; + + (this._listeners[eventName] = this._listeners[eventName] || []) + .push(fn); + + return this; + }; + + /** + * Add an EventListener that's only called once. + * + * @method once + * @param eventName {String} Name of the event. + * @param callback {Function} Callback function. + */ + obj.once = function once(eventName, fn) { + this._listeners = this._listeners || {}; + + var self = this; + function onceHandlerWrapper() { + fn.apply(self.off(eventName, onceHandlerWrapper), arguments); + } + onceHandlerWrapper._originalHandler = fn; + + return this.on(eventName, onceHandlerWrapper); + }; + + /** + * Remove event listeners. + * + * @method off + * @alias removeEventListener + * @param eventName {String} The event we want to remove. + * @param callback {Function} The listener that we need to find. + */ + obj.off = obj.removeEventListener = function off(eventName, fn) { + this._listeners = this._listeners || {}; + + if(!this._listeners[eventName]) + return this; + + var list = this._listeners[eventName], + i = list.length = fn ? list.length : 0; + + while(i-- > 0) { + if(list[i] === fn || list[i]._originalHandler === fn) { + list.splice(i, 1); + } + } + + if(list.length === 0) { + delete this._listeners[eventName]; + } + + return this; + }; + + /** + * Remove all listeners or only the listeners for the specified event. + * + * @method removeAllListeners + * @param eventName {String} The event you want to remove all listeners for. + */ + obj.removeAllListeners = function removeAllListeners(eventName) { + this._listeners = this._listeners || {}; + + if(!this._listeners[eventName]) + return this; + + delete this._listeners[eventName]; + + return this; + }; + } +}; + +/** + * Creates an homogenous object for tracking events so users can know what to expect. + * + * @class Event + * @extends Object + * @constructor + * @param target {Object} The target object that the event is called on + * @param name {String} The string name of the event that was triggered + * @param data {Object} Arbitrary event data to pass along + */ +PIXI.Event = function(target, name, data) { + //for duck typing in the ".on()" function + this.__isEventObject = true; + + /** + * Tracks the state of bubbling propagation. Do not + * set this directly, instead use `event.stopPropagation()` + * + * @property stopped + * @type Boolean + * @private + * @readOnly + */ + this.stopped = false; + + /** + * Tracks the state of sibling listener propagation. Do not + * set this directly, instead use `event.stopImmediatePropagation()` + * + * @property stoppedImmediate + * @type Boolean + * @private + * @readOnly + */ + this.stoppedImmediate = false; + + /** + * The original target the event triggered on. + * + * @property target * @type Object + * @readOnly */ - var listeners = {}; + this.target = target; /** - * Adds a listener for a specific event + * The string name of the event that this represents. * - * @method addEventListener - * @param type {string} A string representing the event type to listen for. - * @param listener {function} The callback function that will be fired when the event occurs + * @property type + * @type String + * @readOnly */ - this.addEventListener = this.on = function ( type, listener ) { - - - if ( listeners[ type ] === undefined ) { - - listeners[ type ] = []; - - } - - if ( listeners[ type ].indexOf( listener ) === - 1 ) { - - listeners[ type ].unshift( listener ); - } - - }; + this.type = name; /** - * Fires the event, ie pretends that the event has happened + * The data that was passed in with this event. * - * @method dispatchEvent - * @param event {Event} the event object + * @property data + * @type Object + * @readOnly */ - this.dispatchEvent = this.emit = function ( event ) { + this.data = data; - if ( !listeners[ event.type ] || !listeners[ event.type ].length ) { - - return; - - } - - - for(var i = listeners[ event.type ].length-1; i >= 0; i--) { -// for(var i = 0, l=listeners[ event.type ].length; i < l; i++) { - - - listeners[ event.type ][ i ]( event ); - - } - - }; + //backwards compat with older version of events + this.content = data; /** - * Removes the specified listener that was assigned to the specified event type + * The timestamp when the event occurred. * - * @method removeEventListener - * @param type {string} A string representing the event type which will have its listener removed - * @param listener {function} The callback function that was be fired when the event occured + * @property timeStamp + * @type Number + * @readOnly */ - this.removeEventListener = this.off = function ( type, listener ) { + this.timeStamp = Date.now(); +}; - if ( listeners[ type ] === undefined ) return; +/** + * Stops the propagation of events up the scene graph (prevents bubbling). + * + * @method stopPropagation + */ +PIXI.Event.prototype.stopPropagation = function stopPropagation() { + this.stopped = true; +}; - var index = listeners[ type ].indexOf( listener ); - - if ( index !== - 1 ) { - - listeners[ type ].splice( index, 1 ); - - } - - }; - - /** - * Removes all the listeners that were active for the specified event type - * - * @method removeAllEventListeners - * @param type {string} A string representing the event type which will have all its listeners removed - */ - this.removeAllEventListeners = function( type ) { - var a = listeners[type]; - if (a) - a.length = 0; - }; +/** + * Stops the propagation of events to sibling listeners (no longer calls any listeners). + * + * @method stopImmediatePropagation + */ +PIXI.Event.prototype.stopImmediatePropagation = function stopImmediatePropagation() { + this.stoppedImmediate = true; }; diff --git a/test/lib/pixi/textures/RenderTexture.js b/test/lib/pixi/textures/RenderTexture.js new file mode 100644 index 0000000..8556501 --- /dev/null +++ b/test/lib/pixi/textures/RenderTexture.js @@ -0,0 +1,14 @@ + +function pixi_textures_RenderTexture_confirmNew(obj, done) { + var expect = chai.expect; + + expect(obj).to.have.property('width'); + expect(obj).to.have.property('height'); + + expect(obj).to.have.property('render'); + expect(obj).to.have.property('renderer'); + // expect(obj).to.have.property('projection'); + expect(obj).to.have.property('textureBuffer'); + + pixi_textures_Texture_confirmNew(obj, done); +} diff --git a/test/lib/pixi/textures/Texture.js b/test/lib/pixi/textures/Texture.js index d8eeeb3..2493385 100644 --- a/test/lib/pixi/textures/Texture.js +++ b/test/lib/pixi/textures/Texture.js @@ -11,7 +11,7 @@ } expect(obj).to.be.an.instanceof(PIXI.Texture); - pixi_utils_EventTarget_like(obj); + pixi_utils_EventTarget_confirm(obj); expect(obj).to.have.property('baseTexture') .and.to.be.an.instanceof(PIXI.BaseTexture); @@ -20,7 +20,7 @@ if (obj.baseTexture.hasLoaded) { confirmFrameDone(); } else { - obj.addEventListener('update', confirmFrameDone); + obj.on('update', confirmFrameDone); pixi_core_Rectangle_confirm(obj.frame, 0, 0, 1, 1); } } diff --git a/test/lib/pixi/utils/EventTarget.js b/test/lib/pixi/utils/EventTarget.js index 7b1dcfa..a0c3924 100644 --- a/test/lib/pixi/utils/EventTarget.js +++ b/test/lib/pixi/utils/EventTarget.js @@ -1,8 +1,34 @@ -function pixi_utils_EventTarget_like(obj) { +function pixi_utils_EventTarget_confirm(obj) { var expect = chai.expect; + //public API + expect(obj).to.respondTo('listeners'); + expect(obj).to.respondTo('emit'); + expect(obj).to.respondTo('on'); + expect(obj).to.respondTo('once'); + expect(obj).to.respondTo('off'); + expect(obj).to.respondTo('removeAllListeners'); + + //Aliased names + expect(obj).to.respondTo('removeEventListener'); expect(obj).to.respondTo('addEventListener'); expect(obj).to.respondTo('dispatchEvent'); - expect(obj).to.respondTo('removeEventListener'); } + +function pixi_utils_EventTarget_Event_confirm(event, obj, data) { + var expect = chai.expect; + + expect(event).to.be.an.instanceOf(PIXI.Event); + + expect(event).to.have.property('stopped', false); + expect(event).to.have.property('stoppedImmediate', false); + + expect(event).to.have.property('target', obj); + expect(event).to.have.property('type', data.type || 'myevent'); + expect(event).to.have.property('data', data); + expect(event).to.have.property('content', data); + + expect(event).to.respondTo('stopPropagation'); + expect(event).to.respondTo('stopImmediatePropagation'); +} \ No newline at end of file diff --git a/test/unit/pixi/textures/RenderTexture.js b/test/unit/pixi/textures/RenderTexture.js index 4a69b44..96acdb4 100644 --- a/test/unit/pixi/textures/RenderTexture.js +++ b/test/unit/pixi/textures/RenderTexture.js @@ -7,4 +7,9 @@ it('Module exists', function () { expect(RenderTexture).to.be.a('function'); }); + + it('Confirm new instance', function (done) { + var texture = new RenderTexture(100, 100, new PIXI.CanvasRenderer()); + pixi_textures_RenderTexture_confirmNew(texture, done); + }); }); diff --git a/src/pixi/display/Sprite.js b/src/pixi/display/Sprite.js index 5cf3105..cffecce 100644 --- a/src/pixi/display/Sprite.js +++ b/src/pixi/display/Sprite.js @@ -82,7 +82,7 @@ else { this.onTextureUpdateBind = this.onTextureUpdate.bind(this); - this.texture.addEventListener( 'update', this.onTextureUpdateBind ); + this.texture.on( 'update', this.onTextureUpdateBind ); } this.renderable = true; diff --git a/src/pixi/loaders/AssetLoader.js b/src/pixi/loaders/AssetLoader.js index feab211..cf6d3d2 100644 --- a/src/pixi/loaders/AssetLoader.js +++ b/src/pixi/loaders/AssetLoader.js @@ -20,8 +20,6 @@ */ PIXI.AssetLoader = function(assetURLs, crossorigin) { - PIXI.EventTarget.call(this); - /** * The array of asset URLs that are going to be loaded * @@ -58,6 +56,8 @@ }; }; +PIXI.EventTarget.mixin(PIXI.AssetLoader.prototype); + /** * Fired when an item has loaded * @event onProgress @@ -114,7 +114,7 @@ var scope = this; function onLoad(evt) { - scope.onAssetLoaded(evt.content); + scope.onAssetLoaded(evt.data.content); } this.loadCount = this.assetURLs.length; @@ -135,7 +135,7 @@ var loader = new Constructor(fileName, this.crossorigin); - loader.addEventListener('loaded', onLoad); + loader.on('loaded', onLoad); loader.load(); } }; @@ -149,12 +149,12 @@ PIXI.AssetLoader.prototype.onAssetLoaded = function(loader) { this.loadCount--; - this.dispatchEvent({ type: 'onProgress', content: this, loader: loader }); + this.emit('onProgress', { content: this, loader: loader }); if (this.onProgress) this.onProgress(loader); if (!this.loadCount) { - this.dispatchEvent({type: 'onComplete', content: this}); + this.emit('onComplete', { content: this }); if(this.onComplete) this.onComplete(); } }; diff --git a/src/pixi/loaders/AtlasLoader.js b/src/pixi/loaders/AtlasLoader.js index 98e32c9..aa6c381 100644 --- a/src/pixi/loaders/AtlasLoader.js +++ b/src/pixi/loaders/AtlasLoader.js @@ -7,14 +7,13 @@ * When loaded this class will dispatch a 'loaded' event * If loading fails this class will dispatch an 'error' event * @class AtlasLoader - * @extends EventTarget + * @uses EventTarget * @constructor * @param {String} url the url of the JSON file * @param {Boolean} crossorigin */ PIXI.AtlasLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); this.url = url; this.baseUrl = url.replace(/[^\/]*$/, ''); this.crossorigin = crossorigin; @@ -25,6 +24,7 @@ // constructor PIXI.AtlasLoader.constructor = PIXI.AtlasLoader; +PIXI.EventTarget.mixin(PIXI.AtlasLoader.prototype); /** * Starts loading the JSON file @@ -145,7 +145,7 @@ this.currentImageId = 0; for (j = 0; j < this.images.length; j++) { - this.images[j].addEventListener('loaded', selfOnLoaded); + this.images[j].on('loaded', selfOnLoaded); } this.images[this.currentImageId].load(); @@ -170,10 +170,7 @@ this.images[this.currentImageId].load(); } else { this.loaded = true; - this.dispatchEvent({ - type: 'loaded', - content: this - }); + this.emit('loaded', { content: this }); } }; @@ -183,8 +180,5 @@ * @private */ PIXI.AtlasLoader.prototype.onError = function () { - this.dispatchEvent({ - type: 'error', - content: this - }); + this.emit('error', { content: this }); }; diff --git a/src/pixi/loaders/BitmapFontLoader.js b/src/pixi/loaders/BitmapFontLoader.js index f87fbdd..bbd1fed 100644 --- a/src/pixi/loaders/BitmapFontLoader.js +++ b/src/pixi/loaders/BitmapFontLoader.js @@ -21,7 +21,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -59,6 +58,7 @@ // constructor PIXI.BitmapFontLoader.prototype.constructor = PIXI.BitmapFontLoader; +PIXI.EventTarget.mixin(PIXI.BitmapFontLoader.prototype); /** * Loads the XML font data @@ -163,5 +163,5 @@ */ PIXI.BitmapFontLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/loaders/ImageLoader.js b/src/pixi/loaders/ImageLoader.js index ad4c61c..f1606f1 100644 --- a/src/pixi/loaders/ImageLoader.js +++ b/src/pixi/loaders/ImageLoader.js @@ -15,8 +15,6 @@ */ PIXI.ImageLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The texture being loaded * @@ -35,6 +33,7 @@ // constructor PIXI.ImageLoader.prototype.constructor = PIXI.ImageLoader; +PIXI.EventTarget.mixin(PIXI.ImageLoader.prototype); /** * Loads image or takes it from cache @@ -45,7 +44,7 @@ { if(!this.texture.baseTexture.hasLoaded) { - this.texture.baseTexture.addEventListener('loaded', this.onLoaded.bind(this)); + this.texture.baseTexture.on('loaded', this.onLoaded.bind(this)); } else { @@ -61,7 +60,7 @@ */ PIXI.ImageLoader.prototype.onLoaded = function() { - this.dispatchEvent({type: 'loaded', content: this}); + this.emit('loaded', { content: this }); }; /** diff --git a/src/pixi/loaders/JsonLoader.js b/src/pixi/loaders/JsonLoader.js index bdfd6e0..17a2a95 100644 --- a/src/pixi/loaders/JsonLoader.js +++ b/src/pixi/loaders/JsonLoader.js @@ -14,8 +14,6 @@ * @param crossorigin {Boolean} Whether requests should be treated as crossorigin */ PIXI.JsonLoader = function (url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The url of the bitmap font data * @@ -54,6 +52,7 @@ // constructor PIXI.JsonLoader.prototype.constructor = PIXI.JsonLoader; +PIXI.EventTarget.mixin(PIXI.JsonLoader.prototype); /** * Loads the JSON data diff --git a/src/pixi/loaders/SpineLoader.js b/src/pixi/loaders/SpineLoader.js index 23072f1..027a980 100644 --- a/src/pixi/loaders/SpineLoader.js +++ b/src/pixi/loaders/SpineLoader.js @@ -23,8 +23,6 @@ */ PIXI.SpineLoader = function(url, crossorigin) { - PIXI.EventTarget.call(this); - /** * The url of the bitmap font data * @@ -53,6 +51,8 @@ PIXI.SpineLoader.prototype.constructor = PIXI.SpineLoader; +PIXI.EventTarget.mixin(PIXI.SpineLoader.prototype); + /** * Loads the JSON data * @@ -62,8 +62,8 @@ var scope = this; var jsonLoader = new PIXI.JsonLoader(this.url, this.crossorigin); - jsonLoader.addEventListener("loaded", function (event) { - scope.json = event.content.json; + jsonLoader.on('loaded', function (event) { + scope.json = event.data.content.json; scope.onLoaded(); }); jsonLoader.load(); @@ -77,6 +77,6 @@ */ PIXI.SpineLoader.prototype.onLoaded = function () { this.loaded = true; - this.dispatchEvent({type: "loaded", content: this}); + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/loaders/SpriteSheetLoader.js b/src/pixi/loaders/SpriteSheetLoader.js index bfb827a..971724f 100644 --- a/src/pixi/loaders/SpriteSheetLoader.js +++ b/src/pixi/loaders/SpriteSheetLoader.js @@ -23,7 +23,6 @@ * http://www.codeandweb.com/texturepacker * make sure to set the format as 'JSON' */ - PIXI.EventTarget.call(this); /** * The url of the bitmap font data @@ -70,6 +69,8 @@ // constructor PIXI.SpriteSheetLoader.prototype.constructor = PIXI.SpriteSheetLoader; +PIXI.EventTarget.mixin(PIXI.SpriteSheetLoader.prototype); + /** * This will begin loading the JSON file * @@ -78,8 +79,8 @@ PIXI.SpriteSheetLoader.prototype.load = function () { var scope = this; var jsonLoader = new PIXI.JsonLoader(this.url, this.crossorigin); - jsonLoader.addEventListener('loaded', function (event) { - scope.json = event.content.json; + jsonLoader.on('loaded', function (event) { + scope.json = event.data.content.json; scope.onLoaded(); }); jsonLoader.load(); @@ -92,8 +93,7 @@ * @private */ PIXI.SpriteSheetLoader.prototype.onLoaded = function () { - this.dispatchEvent({ - type: 'loaded', + this.emit('loaded', { content: this }); }; diff --git a/src/pixi/renderers/webgl/WebGLRenderer.js b/src/pixi/renderers/webgl/WebGLRenderer.js index e333d78..20c7b26 100644 --- a/src/pixi/renderers/webgl/WebGLRenderer.js +++ b/src/pixi/renderers/webgl/WebGLRenderer.js @@ -21,7 +21,7 @@ * @param [options.antialias=false] {Boolean} sets antialias (only applicable in chrome at the moment) * @param [options.preserveDrawingBuffer=false] {Boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context * @param [options.resolution=1] {Number} the resolution of the renderer retina would be 2 - * + * */ PIXI.WebGLRenderer = function(width, height, options) { @@ -37,7 +37,7 @@ options = PIXI.defaultRenderOptions; } - + if(!PIXI.defaultRenderer) { PIXI.sayHello('webGL'); @@ -581,8 +581,8 @@ // deal with losing context.. // remove listeners - this.view.removeEventListener('webglcontextlost', this.contextLostFunction); - this.view.removeEventListener('webglcontextrestored', this.contextRestoredFunction); + this.view.off('webglcontextlost', this.contextLostFunction); + this.view.off('webglcontextrestored', this.contextRestoredFunction); PIXI.glContexts[this.glContextId] = null; diff --git a/src/pixi/textures/BaseTexture.js b/src/pixi/textures/BaseTexture.js index a41b86f..5f954e2 100644 --- a/src/pixi/textures/BaseTexture.js +++ b/src/pixi/textures/BaseTexture.js @@ -19,8 +19,6 @@ */ PIXI.BaseTexture = function(source, scaleMode) { - PIXI.EventTarget.call( this ); - /** * The Resolution of the texture. * @@ -134,6 +132,7 @@ }; PIXI.BaseTexture.prototype.constructor = PIXI.BaseTexture; +PIXI.EventTarget.mixin(PIXI.BaseTexture.prototype); /** * Destroys this base texture diff --git a/src/pixi/textures/RenderTexture.js b/src/pixi/textures/RenderTexture.js index f1e4ad1..021e97a 100644 --- a/src/pixi/textures/RenderTexture.js +++ b/src/pixi/textures/RenderTexture.js @@ -34,9 +34,6 @@ */ PIXI.RenderTexture = function(width, height, renderer, scaleMode, resolution) { - PIXI.EventTarget.call( this ); - - /** * The with of the render texture * @@ -44,6 +41,7 @@ * @type Number */ this.width = width || 100; + /** * The height of the render texture * @@ -51,9 +49,9 @@ * @type Number */ this.height = height || 100; - + /** - * The Resolution of the texture. + * The Resolution of the texture. * * @property resolution * @type Number @@ -76,7 +74,6 @@ * @type Rectangle */ this.crop = new PIXI.Rectangle(0, 0, this.width * this.resolution, this.height * this.resolution); - /** * The base texture object that this texture uses @@ -95,6 +92,11 @@ this.baseTexture.hasLoaded = true; + PIXI.Texture.call(this, + this.baseTexture, + new PIXI.Rectangle(0, 0, this.width, this.height) + ); + // each render texture can only belong to one renderer at the moment if its webGL this.renderer = renderer || PIXI.defaultRenderer; @@ -156,10 +158,10 @@ this.projection.x = this.width / 2; this.projection.y = -this.height / 2; } - + if(!this.valid)return; + this.textureBuffer.resize(this.width * this.resolution, this.height * this.resolution); - }; /** @@ -175,7 +177,7 @@ { this.renderer.gl.bindFramebuffer(this.renderer.gl.FRAMEBUFFER, this.textureBuffer.frameBuffer); } - + this.textureBuffer.clear(); }; @@ -228,7 +230,7 @@ PIXI.WebGLRenderer.updateTextures(); this.renderer.spriteBatch.dirty = true; - + this.renderer.renderDisplayObject(displayObject, this.projection, this.textureBuffer.frameBuffer); displayObject.worldTransform = originalWorldTransform; @@ -254,7 +256,7 @@ var originalWorldTransform = displayObject.worldTransform; displayObject.worldTransform = PIXI.RenderTexture.tempMatrix; - + if(position) { displayObject.worldTransform.tx = position.x; @@ -284,9 +286,9 @@ //this.baseTexture.resolution = 2; this.renderer.renderDisplayObject(displayObject, context); - + this.renderer.resolution = realResolution; - + context.setTransform(1,0,0,1,0,0); displayObject.worldTransform = originalWorldTransform; @@ -342,7 +344,7 @@ } tempCanvas.context.putImageData(canvasData, 0, 0); - + return tempCanvas.canvas; } else diff --git a/src/pixi/textures/Texture.js b/src/pixi/textures/Texture.js index f2e5a0f..112875b 100644 --- a/src/pixi/textures/Texture.js +++ b/src/pixi/textures/Texture.js @@ -19,8 +19,6 @@ */ PIXI.Texture = function(baseTexture, frame) { - PIXI.EventTarget.call( this ); - /** * Does this Texture have any frame data assigned to it? * @@ -118,6 +116,7 @@ }; PIXI.Texture.prototype.constructor = PIXI.Texture; +PIXI.EventTarget.mixin(PIXI.Texture.prototype); /** * Called when the base texture is loaded diff --git a/src/pixi/utils/EventTarget.js b/src/pixi/utils/EventTarget.js index ada6f59..39c04a7 100644 --- a/src/pixi/utils/EventTarget.js +++ b/src/pixi/utils/EventTarget.js @@ -1,112 +1,284 @@ /** * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** - * https://github.com/mrdoob/eventtarget.js/ - * THankS mr DOob! + * @author Chad Engler https://github.com/englercj @Rolnaaba */ /** - * Adds event emitter functionality to a class + * Originally based on https://github.com/mrdoob/eventtarget.js/ from mr DOob. + * Currently takes inspiration from the nodejs EventEmitter, EventEmitter3, and smokesignals + */ + +/** + * Mixins event emitter functionality to a class * * @class EventTarget * @example - * function MyEmitter() { - * PIXI.EventTarget.call(this); //mixes in event target stuff - * } + * function MyEmitter() {} + * + * PIXI.EventTarget.mixin(MyEmitter.prototype); * * var em = new MyEmitter(); - * em.emit({ type: 'eventName', data: 'some data' }); + * em.emit('eventName', 'some data', 'some moar data', {}, null, ...); */ -PIXI.EventTarget = function () { +PIXI.EventTarget = { + /** + * Backward compat from when this used to be a function + */ + call: function callCompat(obj) { + if(obj) { + obj = obj.prototype || obj; + PIXI.EventTarget.mixin(obj); + } + }, /** - * Holds all the listeners + * Mixes in the properties of the EventTarget prototype onto another object * - * @property listeners + * @method mixin + * @param object {Object} The obj to mix into + */ + mixin: function mixin(obj) { + /** + * Return a list of assigned event listeners. + * + * @method listeners + * @param eventName {String} The events that should be listed. + * @returns {Array} An array of listener functions + */ + obj.listeners = function listeners(eventName) { + this._listeners = this._listeners || {}; + + return this._listeners[eventName] ? this._listeners[eventName].slice() : []; + }; + + /** + * Emit an event to all registered event listeners. + * + * @method emit + * @alias dispatchEvent + * @param eventName {String} The name of the event. + * @returns {Boolean} Indication if we've emitted an event. + */ + obj.emit = obj.dispatchEvent = function emit(eventName, data) { + this._listeners = this._listeners || {}; + + //backwards compat with old method ".emit({ type: 'something' })" + if(typeof eventName === 'object') { + data = eventName; + eventName = eventName.type; + } + + //ensure we are using a real pixi event + if(!data || data.__isEventObject !== true) { + data = new PIXI.Event(this, eventName, data); + } + + //iterate the listeners + if(this._listeners && this._listeners[eventName]) { + var listeners = this._listeners[eventName], + length = listeners.length, + fn = listeners[0], + i; + + for(i = 0; i < length; fn = listeners[++i]) { + //call the event listener + fn.call(this, data); + + //if "stopImmediatePropagation" is called, stop calling sibling events + if(data.stoppedImmediate) { + return this; + } + } + + //if "stopPropagation" is called then don't bubble the event + if(data.stopped) { + return this; + } + } + + //bubble this event up the scene graph + if(this.parent && this.parent.emit) { + this.parent.emit.call(this.parent, eventName, data); + } + + return this; + }; + + /** + * Register a new EventListener for the given event. + * + * @method on + * @alias addEventListener + * @param eventName {String} Name of the event. + * @param callback {Functon} fn Callback function. + */ + obj.on = obj.addEventListener = function on(eventName, fn) { + this._listeners = this._listeners || {}; + + (this._listeners[eventName] = this._listeners[eventName] || []) + .push(fn); + + return this; + }; + + /** + * Add an EventListener that's only called once. + * + * @method once + * @param eventName {String} Name of the event. + * @param callback {Function} Callback function. + */ + obj.once = function once(eventName, fn) { + this._listeners = this._listeners || {}; + + var self = this; + function onceHandlerWrapper() { + fn.apply(self.off(eventName, onceHandlerWrapper), arguments); + } + onceHandlerWrapper._originalHandler = fn; + + return this.on(eventName, onceHandlerWrapper); + }; + + /** + * Remove event listeners. + * + * @method off + * @alias removeEventListener + * @param eventName {String} The event we want to remove. + * @param callback {Function} The listener that we need to find. + */ + obj.off = obj.removeEventListener = function off(eventName, fn) { + this._listeners = this._listeners || {}; + + if(!this._listeners[eventName]) + return this; + + var list = this._listeners[eventName], + i = list.length = fn ? list.length : 0; + + while(i-- > 0) { + if(list[i] === fn || list[i]._originalHandler === fn) { + list.splice(i, 1); + } + } + + if(list.length === 0) { + delete this._listeners[eventName]; + } + + return this; + }; + + /** + * Remove all listeners or only the listeners for the specified event. + * + * @method removeAllListeners + * @param eventName {String} The event you want to remove all listeners for. + */ + obj.removeAllListeners = function removeAllListeners(eventName) { + this._listeners = this._listeners || {}; + + if(!this._listeners[eventName]) + return this; + + delete this._listeners[eventName]; + + return this; + }; + } +}; + +/** + * Creates an homogenous object for tracking events so users can know what to expect. + * + * @class Event + * @extends Object + * @constructor + * @param target {Object} The target object that the event is called on + * @param name {String} The string name of the event that was triggered + * @param data {Object} Arbitrary event data to pass along + */ +PIXI.Event = function(target, name, data) { + //for duck typing in the ".on()" function + this.__isEventObject = true; + + /** + * Tracks the state of bubbling propagation. Do not + * set this directly, instead use `event.stopPropagation()` + * + * @property stopped + * @type Boolean + * @private + * @readOnly + */ + this.stopped = false; + + /** + * Tracks the state of sibling listener propagation. Do not + * set this directly, instead use `event.stopImmediatePropagation()` + * + * @property stoppedImmediate + * @type Boolean + * @private + * @readOnly + */ + this.stoppedImmediate = false; + + /** + * The original target the event triggered on. + * + * @property target * @type Object + * @readOnly */ - var listeners = {}; + this.target = target; /** - * Adds a listener for a specific event + * The string name of the event that this represents. * - * @method addEventListener - * @param type {string} A string representing the event type to listen for. - * @param listener {function} The callback function that will be fired when the event occurs + * @property type + * @type String + * @readOnly */ - this.addEventListener = this.on = function ( type, listener ) { - - - if ( listeners[ type ] === undefined ) { - - listeners[ type ] = []; - - } - - if ( listeners[ type ].indexOf( listener ) === - 1 ) { - - listeners[ type ].unshift( listener ); - } - - }; + this.type = name; /** - * Fires the event, ie pretends that the event has happened + * The data that was passed in with this event. * - * @method dispatchEvent - * @param event {Event} the event object + * @property data + * @type Object + * @readOnly */ - this.dispatchEvent = this.emit = function ( event ) { + this.data = data; - if ( !listeners[ event.type ] || !listeners[ event.type ].length ) { - - return; - - } - - - for(var i = listeners[ event.type ].length-1; i >= 0; i--) { -// for(var i = 0, l=listeners[ event.type ].length; i < l; i++) { - - - listeners[ event.type ][ i ]( event ); - - } - - }; + //backwards compat with older version of events + this.content = data; /** - * Removes the specified listener that was assigned to the specified event type + * The timestamp when the event occurred. * - * @method removeEventListener - * @param type {string} A string representing the event type which will have its listener removed - * @param listener {function} The callback function that was be fired when the event occured + * @property timeStamp + * @type Number + * @readOnly */ - this.removeEventListener = this.off = function ( type, listener ) { + this.timeStamp = Date.now(); +}; - if ( listeners[ type ] === undefined ) return; +/** + * Stops the propagation of events up the scene graph (prevents bubbling). + * + * @method stopPropagation + */ +PIXI.Event.prototype.stopPropagation = function stopPropagation() { + this.stopped = true; +}; - var index = listeners[ type ].indexOf( listener ); - - if ( index !== - 1 ) { - - listeners[ type ].splice( index, 1 ); - - } - - }; - - /** - * Removes all the listeners that were active for the specified event type - * - * @method removeAllEventListeners - * @param type {string} A string representing the event type which will have all its listeners removed - */ - this.removeAllEventListeners = function( type ) { - var a = listeners[type]; - if (a) - a.length = 0; - }; +/** + * Stops the propagation of events to sibling listeners (no longer calls any listeners). + * + * @method stopImmediatePropagation + */ +PIXI.Event.prototype.stopImmediatePropagation = function stopImmediatePropagation() { + this.stoppedImmediate = true; }; diff --git a/test/lib/pixi/textures/RenderTexture.js b/test/lib/pixi/textures/RenderTexture.js new file mode 100644 index 0000000..8556501 --- /dev/null +++ b/test/lib/pixi/textures/RenderTexture.js @@ -0,0 +1,14 @@ + +function pixi_textures_RenderTexture_confirmNew(obj, done) { + var expect = chai.expect; + + expect(obj).to.have.property('width'); + expect(obj).to.have.property('height'); + + expect(obj).to.have.property('render'); + expect(obj).to.have.property('renderer'); + // expect(obj).to.have.property('projection'); + expect(obj).to.have.property('textureBuffer'); + + pixi_textures_Texture_confirmNew(obj, done); +} diff --git a/test/lib/pixi/textures/Texture.js b/test/lib/pixi/textures/Texture.js index d8eeeb3..2493385 100644 --- a/test/lib/pixi/textures/Texture.js +++ b/test/lib/pixi/textures/Texture.js @@ -11,7 +11,7 @@ } expect(obj).to.be.an.instanceof(PIXI.Texture); - pixi_utils_EventTarget_like(obj); + pixi_utils_EventTarget_confirm(obj); expect(obj).to.have.property('baseTexture') .and.to.be.an.instanceof(PIXI.BaseTexture); @@ -20,7 +20,7 @@ if (obj.baseTexture.hasLoaded) { confirmFrameDone(); } else { - obj.addEventListener('update', confirmFrameDone); + obj.on('update', confirmFrameDone); pixi_core_Rectangle_confirm(obj.frame, 0, 0, 1, 1); } } diff --git a/test/lib/pixi/utils/EventTarget.js b/test/lib/pixi/utils/EventTarget.js index 7b1dcfa..a0c3924 100644 --- a/test/lib/pixi/utils/EventTarget.js +++ b/test/lib/pixi/utils/EventTarget.js @@ -1,8 +1,34 @@ -function pixi_utils_EventTarget_like(obj) { +function pixi_utils_EventTarget_confirm(obj) { var expect = chai.expect; + //public API + expect(obj).to.respondTo('listeners'); + expect(obj).to.respondTo('emit'); + expect(obj).to.respondTo('on'); + expect(obj).to.respondTo('once'); + expect(obj).to.respondTo('off'); + expect(obj).to.respondTo('removeAllListeners'); + + //Aliased names + expect(obj).to.respondTo('removeEventListener'); expect(obj).to.respondTo('addEventListener'); expect(obj).to.respondTo('dispatchEvent'); - expect(obj).to.respondTo('removeEventListener'); } + +function pixi_utils_EventTarget_Event_confirm(event, obj, data) { + var expect = chai.expect; + + expect(event).to.be.an.instanceOf(PIXI.Event); + + expect(event).to.have.property('stopped', false); + expect(event).to.have.property('stoppedImmediate', false); + + expect(event).to.have.property('target', obj); + expect(event).to.have.property('type', data.type || 'myevent'); + expect(event).to.have.property('data', data); + expect(event).to.have.property('content', data); + + expect(event).to.respondTo('stopPropagation'); + expect(event).to.respondTo('stopImmediatePropagation'); +} \ No newline at end of file diff --git a/test/unit/pixi/textures/RenderTexture.js b/test/unit/pixi/textures/RenderTexture.js index 4a69b44..96acdb4 100644 --- a/test/unit/pixi/textures/RenderTexture.js +++ b/test/unit/pixi/textures/RenderTexture.js @@ -7,4 +7,9 @@ it('Module exists', function () { expect(RenderTexture).to.be.a('function'); }); + + it('Confirm new instance', function (done) { + var texture = new RenderTexture(100, 100, new PIXI.CanvasRenderer()); + pixi_textures_RenderTexture_confirmNew(texture, done); + }); }); diff --git a/test/unit/pixi/utils/EventTarget.js b/test/unit/pixi/utils/EventTarget.js index 67c6ad1..1524ff5 100644 --- a/test/unit/pixi/utils/EventTarget.js +++ b/test/unit/pixi/utils/EventTarget.js @@ -2,107 +2,281 @@ 'use strict'; var expect = chai.expect; - var EventTarget = PIXI.EventTarget; + + var Clazz, PClazz, obj, pobj, obj2; + beforeEach(function () { + Clazz = function () {}; + PClazz = function () {}; + + PIXI.EventTarget.mixin(Clazz.prototype); + PIXI.EventTarget.mixin(PClazz.prototype); + + obj = new Clazz(); + obj2 = new Clazz(); + pobj = new PClazz(); + + obj.parent = pobj; + obj2.parent = obj; + }); it('Module exists', function () { - expect(EventTarget).to.be.a('function'); + expect(PIXI.EventTarget).to.be.an('object'); }); it('Confirm new instance', function () { - var obj = {}; - - EventTarget.call(obj); - pixi_utils_EventTarget_like(obj); + pixi_utils_EventTarget_confirm(obj); }); - it('addEventListener and dispatchEvent works', function (done) { - var myData = {}, - obj = {}; + it('simple on/emit case works', function () { + var myData = {}; - EventTarget.call(obj); - - obj.addEventListener('myevent', function (event) { - expect(event).to.be.an('object'); - expect(event).to.have.property('type', 'myevent'); - expect(event).to.have.property('data', myData); - done(); + obj.on('myevent', function (event) { + pixi_utils_EventTarget_Event_confirm(event, obj, myData); }); - obj.dispatchEvent({type: 'myevent', data: myData}); + obj.emit('myevent', myData); }); - it('removeEventListener works', function (done) { - var obj = {}; + it('simple once case works', function () { + var called = 0; - EventTarget.call(obj); + obj.once('myevent', function() { called++; }); + obj.emit('myevent'); + obj.emit('myevent'); + obj.emit('myevent'); + obj.emit('myevent'); + obj.emit('myevent'); + + expect(called).to.equal(1); + }); + + it('simple off case works', function (done) { function onMyEvent() { - done(new Error('addEventListener should not have been called')); + done(new Error('Event listener should not have been called')); } - obj.addEventListener('myevent', onMyEvent); - obj.removeEventListener('myevent', onMyEvent); - obj.dispatchEvent({type: 'myevent'}); + obj.on('myevent', onMyEvent); + obj.off('myevent', onMyEvent); + obj.emit('myevent'); + done(); }); - it('multiple dispatches', function () { - var called = 0, - obj = {}; + it('simple propagation case works', function (done) { + var myData = {}; - EventTarget.call(obj); + pobj.on('myevent', function () { + done(); + }); + + obj.emit('myevent'); + }); + + it('simple stopPropagation case works', function (done) { + var myData = {}; + + pobj.on('myevent', function () { + done(new Error('Event listener should not have been called on the parent element')); + }); + + obj.on('myevent', function (evt) { + evt.stopPropagation(); + }); + + obj.emit('myevent'); + + done(); + }); + + it('simple stopImmediatePropagation case works', function (done) { + var myData = {}; + + pobj.on('myevent', function () { + done(new Error('Event listener should not have been called on the parent')); + }); + + obj.on('myevent', function (evt) { + evt.stopImmediatePropagation(); + }); + + obj.on('myevent', function () { + done(new Error('Event listener should not have been called on the second')); + }); + + obj.emit('myevent'); + + done(); + }); + + it('multiple dispatches work properly', function () { + var called = 0; function onMyEvent() { called++; } - obj.addEventListener('myevent', onMyEvent); - obj.dispatchEvent({type: 'myevent'}); - obj.dispatchEvent({type: 'myevent'}); - obj.dispatchEvent({type: 'myevent'}); - obj.dispatchEvent({type: 'myevent'}); + obj.on('myevent', onMyEvent); + obj.emit('myevent'); + obj.emit('myevent'); + obj.emit('myevent'); + obj.emit('myevent'); + expect(called).to.equal(4); }); - it('multiple events', function () { - var called = 0, - obj = {}; - - EventTarget.call(obj); + it('multiple events work properly', function () { + var called = 0; function onMyEvent() { called++; } - obj.addEventListener('myevent1', onMyEvent); - obj.addEventListener('myevent2', onMyEvent); - obj.addEventListener('myevent3', onMyEvent); - obj.dispatchEvent({type: 'myevent1'}); - obj.dispatchEvent({type: 'myevent2'}); - obj.dispatchEvent({type: 'myevent3'}); + obj.on('myevent1', onMyEvent); + obj.on('myevent2', onMyEvent); + obj.on('myevent3', onMyEvent); + obj.emit('myevent1'); + obj.emit('myevent2'); + obj.emit('myevent3'); + expect(called).to.equal(3); }); - it('multiple events one removed', function () { - var called = 0, - obj = {}; - - EventTarget.call(obj); + it('multiple events one removed works properly', function () { + var called = 0; function onMyEvent() { called++; } + obj.on('myevent1', onMyEvent); + obj.on('myevent2', onMyEvent); + obj.on('myevent3', onMyEvent); + + obj.emit('myevent1'); + obj.emit('myevent2'); + obj.emit('myevent3'); + + obj.off('myevent2', onMyEvent); + + obj.emit('myevent1'); + obj.emit('myevent2'); + obj.emit('myevent3'); + + expect(called).to.equal(5); + }); + + it('handles multiple instances with the same prototype', function () { + var called = 0; + + function onMyEvent(e) { + called++; + } + + obj.on('myevent1', onMyEvent); + obj.on('myevent2', onMyEvent); + + obj2.istwo = true; + obj2.on('myevent1', onMyEvent); + obj2.on('myevent2', onMyEvent); + + obj.emit('myevent1'); + obj.emit('myevent2'); + obj2.emit('myevent1'); + obj2.emit('myevent2'); + + //we emit 4 times, but since obj2 is a child of obj the event should bubble + //up to obj and show up there as well. So the obj2.emit() calls each increment + //the counter twice. + expect(called).to.equal(6); + }); + + it('is backwards compatible with older .dispatchEvent({})', function () { + var called = 0, + data = { + some: 'thing', + hello: true + }; + + function onMyEvent(event) { + pixi_utils_EventTarget_Event_confirm(event, obj, data); + + called++; + } + + obj.on('myevent1', onMyEvent); + obj.on('myevent2', onMyEvent); + obj.on('myevent3', onMyEvent); + + data.type = 'myevent1'; + obj.emit(data); + + data.type = 'myevent2'; + obj.emit(data); + + data.type = 'myevent3'; + obj.emit(data); + + obj.off('myevent2', onMyEvent); + + data.type = 'myevent1'; + obj.emit(data); + + data.type = 'myevent2'; + obj.emit(data); + + data.type = 'myevent3'; + obj.emit(data); + + expect(called).to.equal(5); + }); + + it('is backwards compatible with older .call(this)', function () { + var Fn = function() { + PIXI.EventTarget.call(this); + }, + o = new Fn(); + + pixi_utils_EventTarget_confirm(o); + }); + + it('is backwards compatible with older .addEventListener("")', function () { + var called = 0, + data = { + some: 'thing', + hello: true + }; + + function onMyEvent(event) { + pixi_utils_EventTarget_Event_confirm(event, obj, data); + + called++; + } + obj.addEventListener('myevent1', onMyEvent); obj.addEventListener('myevent2', onMyEvent); obj.addEventListener('myevent3', onMyEvent); - obj.dispatchEvent({type: 'myevent1'}); - obj.dispatchEvent({type: 'myevent2'}); - obj.dispatchEvent({type: 'myevent3'}); - obj.removeEventListener('myevent2', onMyEvent); - obj.dispatchEvent({type: 'myevent1'}); - obj.dispatchEvent({type: 'myevent2'}); - obj.dispatchEvent({type: 'myevent3'}); + + data.type = 'myevent1'; + obj.emit(data); + + data.type = 'myevent2'; + obj.emit(data); + + data.type = 'myevent3'; + obj.emit(data); + + obj.off('myevent2', onMyEvent); + + data.type = 'myevent1'; + obj.emit(data); + + data.type = 'myevent2'; + obj.emit(data); + + data.type = 'myevent3'; + obj.emit(data); + expect(called).to.equal(5); }); });