diff --git a/src/utils/Detector.js b/src/utils/Detector.js index fe38f04..99f4e39 100644 --- a/src/utils/Detector.js +++ b/src/utils/Detector.js @@ -1,30 +1,25 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ +//TODO: Replace this code with external modules! /** * This helper function will automatically detect which renderer you should be using. * WebGL is the preferred renderer as it is a lot faster. If webGL is not supported by * the browser then this function will return a canvas renderer * - * @method autoDetectRenderer - * @for PIXI * @static - * @param width=800 {Number} the width of the renderers view - * @param height=600 {Number} the height of the renderers view - * - * @param [options] {Object} The optional renderer parameters + * @namespace PIXI + * @param width=800 {number} the width of the renderers view + * @param height=600 {number} the height of the renderers view + * @param [options] {object} The optional renderer parameters * @param [options.view] {HTMLCanvasElement} the canvas to use as a view, optional - * @param [options.transparent=false] {Boolean} If the render view is transparent, default false - * @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 - * + * @param [options.transparent=false] {boolean} If the render view is transparent, default false + * @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.autoDetectRenderer = function(width, height, options) -{ - if(!width)width = 800; - if(!height)height = 600; +function autoDetectRenderer(width, height, options) { + width = width || 800; + height = height || 600; // BORROWED from Mr Doob (mrdoob.com) var webgl = ( function () { try { @@ -35,38 +30,34 @@ } } )(); - if( webgl ) - { - return new PIXI.WebGLRenderer(width, height, options); + if (webgl) { + return new WebGLRenderer(width, height, options); } - return new PIXI.CanvasRenderer(width, height, options); -}; + return new CanvasRenderer(width, height, options); +} /** * This helper function will automatically detect which renderer you should be using. * This function is very similar to the autoDetectRenderer function except that is will return a canvas renderer for android. - * Even thought both android chrome supports webGL the canvas implementation perform better at the time of writing. + * Even thought both android chrome supports webGL the canvas implementation perform better at the time of writing. * This function will likely change and update as webGL performance improves on these devices. - * - * @method autoDetectRecommendedRenderer - * @for PIXI + * * @static - * @param width=800 {Number} the width of the renderers view - * @param height=600 {Number} the height of the renderers view - * - * @param [options] {Object} The optional renderer parameters + * @namespace PIXI + * @param width=800 {number} the width of the renderers view + * @param height=600 {number} the height of the renderers view + * @param [options] {object} The optional renderer parameters * @param [options.view] {HTMLCanvasElement} the canvas to use as a view, optional - * @param [options.transparent=false] {Boolean} If the render view is transparent, default false - * @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 - * + * @param [options.transparent=false] {boolean} If the render view is transparent, default false + * @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.autoDetectRecommendedRenderer = function(width, height, options) -{ - if(!width)width = 800; - if(!height)height = 600; +function autoDetectRecommendedRenderer(width, height, options) { + width = width || 800; + height = height || 600; // BORROWED from Mr Doob (mrdoob.com) var webgl = ( function () { try { @@ -79,10 +70,9 @@ var isAndroid = /Android/i.test(navigator.userAgent); - if( webgl && !isAndroid) - { - return new PIXI.WebGLRenderer(width, height, options); + if (webgl && !isAndroid) { + return new WebGLRenderer(width, height, options); } - return new PIXI.CanvasRenderer(width, height, options); -}; + return new CanvasRenderer(width, height, options); +} diff --git a/src/utils/Detector.js b/src/utils/Detector.js index fe38f04..99f4e39 100644 --- a/src/utils/Detector.js +++ b/src/utils/Detector.js @@ -1,30 +1,25 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ +//TODO: Replace this code with external modules! /** * This helper function will automatically detect which renderer you should be using. * WebGL is the preferred renderer as it is a lot faster. If webGL is not supported by * the browser then this function will return a canvas renderer * - * @method autoDetectRenderer - * @for PIXI * @static - * @param width=800 {Number} the width of the renderers view - * @param height=600 {Number} the height of the renderers view - * - * @param [options] {Object} The optional renderer parameters + * @namespace PIXI + * @param width=800 {number} the width of the renderers view + * @param height=600 {number} the height of the renderers view + * @param [options] {object} The optional renderer parameters * @param [options.view] {HTMLCanvasElement} the canvas to use as a view, optional - * @param [options.transparent=false] {Boolean} If the render view is transparent, default false - * @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 - * + * @param [options.transparent=false] {boolean} If the render view is transparent, default false + * @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.autoDetectRenderer = function(width, height, options) -{ - if(!width)width = 800; - if(!height)height = 600; +function autoDetectRenderer(width, height, options) { + width = width || 800; + height = height || 600; // BORROWED from Mr Doob (mrdoob.com) var webgl = ( function () { try { @@ -35,38 +30,34 @@ } } )(); - if( webgl ) - { - return new PIXI.WebGLRenderer(width, height, options); + if (webgl) { + return new WebGLRenderer(width, height, options); } - return new PIXI.CanvasRenderer(width, height, options); -}; + return new CanvasRenderer(width, height, options); +} /** * This helper function will automatically detect which renderer you should be using. * This function is very similar to the autoDetectRenderer function except that is will return a canvas renderer for android. - * Even thought both android chrome supports webGL the canvas implementation perform better at the time of writing. + * Even thought both android chrome supports webGL the canvas implementation perform better at the time of writing. * This function will likely change and update as webGL performance improves on these devices. - * - * @method autoDetectRecommendedRenderer - * @for PIXI + * * @static - * @param width=800 {Number} the width of the renderers view - * @param height=600 {Number} the height of the renderers view - * - * @param [options] {Object} The optional renderer parameters + * @namespace PIXI + * @param width=800 {number} the width of the renderers view + * @param height=600 {number} the height of the renderers view + * @param [options] {object} The optional renderer parameters * @param [options.view] {HTMLCanvasElement} the canvas to use as a view, optional - * @param [options.transparent=false] {Boolean} If the render view is transparent, default false - * @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 - * + * @param [options.transparent=false] {boolean} If the render view is transparent, default false + * @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.autoDetectRecommendedRenderer = function(width, height, options) -{ - if(!width)width = 800; - if(!height)height = 600; +function autoDetectRecommendedRenderer(width, height, options) { + width = width || 800; + height = height || 600; // BORROWED from Mr Doob (mrdoob.com) var webgl = ( function () { try { @@ -79,10 +70,9 @@ var isAndroid = /Android/i.test(navigator.userAgent); - if( webgl && !isAndroid) - { - return new PIXI.WebGLRenderer(width, height, options); + if (webgl && !isAndroid) { + return new WebGLRenderer(width, height, options); } - return new PIXI.CanvasRenderer(width, height, options); -}; + return new CanvasRenderer(width, height, options); +} diff --git a/src/utils/EventTarget.js b/src/utils/EventTarget.js index 35aa31b..e01b7d6 100644 --- a/src/utils/EventTarget.js +++ b/src/utils/EventTarget.js @@ -1,9 +1,4 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 - * @author Chad Engler https://github.com/englercj @Rolnaaba - */ - -/** * Originally based on https://github.com/mrdoob/eventtarget.js/ from mr Doob. * Currently takes inspiration from the nodejs EventEmitter, EventEmitter3, and smokesignals */ @@ -11,206 +6,202 @@ /** * Mixins event emitter functionality to a class * - * @class EventTarget + * @mixin + * @namespace PIXI * @example * function MyEmitter() {} * - * PIXI.EventTarget.mixin(MyEmitter.prototype); + * EventTarget.mixin(MyEmitter.prototype); * * var em = new MyEmitter(); * em.emit('eventName', 'some data', 'some more data', {}, null, ...); */ -PIXI.EventTarget = { +function EventTarget(obj) { + /** + * Return a list of assigned event listeners. + * + * @param eventName {string} The events that should be listed. + * @return {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. + * + * @alias dispatchEvent + * @param eventName {string} The name of the event. + * @return {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 Event(this, eventName, data); + } + + //iterate the listeners + if (this._listeners && this._listeners[eventName]) { + var listeners = this._listeners[eventName].slice(0), + 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. + * + * @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. + * + * @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. + * + * @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 = 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. + * + * @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; + }; +}; + +module.exports = { /** * Backward compat from when this used to be a function */ call: function callCompat(obj) { - if(obj) { + if (obj) { obj = obj.prototype || obj; - PIXI.EventTarget.mixin(obj); + EventTarget(obj); } }, /** * Mixes in the properties of the EventTarget prototype onto another object * - * @method mixin - * @param object {Object} The obj to mix into + * @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. - * @return {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. - * @return {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].slice(0), - 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 = 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; - }; + EventTarget(obj); } -}; +} /** * 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 + * @class + * @namespace PIXI + * @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 +function Event(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 + * @member {boolean} * @private - * @readOnly + * @readonly */ this.stopped = false; @@ -218,37 +209,33 @@ * Tracks the state of sibling listener propagation. Do not * set this directly, instead use `event.stopImmediatePropagation()` * - * @property stoppedImmediate - * @type Boolean + * @member {boolean} * @private - * @readOnly + * @readonly */ this.stoppedImmediate = false; /** * The original target the event triggered on. * - * @property target - * @type Object - * @readOnly + * @member {object} + * @readonly */ this.target = target; /** * The string name of the event that this represents. * - * @property type - * @type String - * @readOnly + * @member {string} + * @readonly */ this.type = name; /** * The data that was passed in with this event. * - * @property data - * @type Object - * @readOnly + * @member {object} + * @readonly */ this.data = data; @@ -258,9 +245,8 @@ /** * The timestamp when the event occurred. * - * @property timeStamp - * @type Number - * @readOnly + * @member {number} + * @readonly */ this.timeStamp = Date.now(); }; @@ -268,17 +254,15 @@ /** * Stops the propagation of events up the scene graph (prevents bubbling). * - * @method stopPropagation */ -PIXI.Event.prototype.stopPropagation = function stopPropagation() { +Event.prototype.stopPropagation = function stopPropagation() { this.stopped = true; }; /** * Stops the propagation of events to sibling listeners (no longer calls any listeners). * - * @method stopImmediatePropagation */ -PIXI.Event.prototype.stopImmediatePropagation = function stopImmediatePropagation() { +Event.prototype.stopImmediatePropagation = function stopImmediatePropagation() { this.stoppedImmediate = true; }; diff --git a/src/utils/Detector.js b/src/utils/Detector.js index fe38f04..99f4e39 100644 --- a/src/utils/Detector.js +++ b/src/utils/Detector.js @@ -1,30 +1,25 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ +//TODO: Replace this code with external modules! /** * This helper function will automatically detect which renderer you should be using. * WebGL is the preferred renderer as it is a lot faster. If webGL is not supported by * the browser then this function will return a canvas renderer * - * @method autoDetectRenderer - * @for PIXI * @static - * @param width=800 {Number} the width of the renderers view - * @param height=600 {Number} the height of the renderers view - * - * @param [options] {Object} The optional renderer parameters + * @namespace PIXI + * @param width=800 {number} the width of the renderers view + * @param height=600 {number} the height of the renderers view + * @param [options] {object} The optional renderer parameters * @param [options.view] {HTMLCanvasElement} the canvas to use as a view, optional - * @param [options.transparent=false] {Boolean} If the render view is transparent, default false - * @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 - * + * @param [options.transparent=false] {boolean} If the render view is transparent, default false + * @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.autoDetectRenderer = function(width, height, options) -{ - if(!width)width = 800; - if(!height)height = 600; +function autoDetectRenderer(width, height, options) { + width = width || 800; + height = height || 600; // BORROWED from Mr Doob (mrdoob.com) var webgl = ( function () { try { @@ -35,38 +30,34 @@ } } )(); - if( webgl ) - { - return new PIXI.WebGLRenderer(width, height, options); + if (webgl) { + return new WebGLRenderer(width, height, options); } - return new PIXI.CanvasRenderer(width, height, options); -}; + return new CanvasRenderer(width, height, options); +} /** * This helper function will automatically detect which renderer you should be using. * This function is very similar to the autoDetectRenderer function except that is will return a canvas renderer for android. - * Even thought both android chrome supports webGL the canvas implementation perform better at the time of writing. + * Even thought both android chrome supports webGL the canvas implementation perform better at the time of writing. * This function will likely change and update as webGL performance improves on these devices. - * - * @method autoDetectRecommendedRenderer - * @for PIXI + * * @static - * @param width=800 {Number} the width of the renderers view - * @param height=600 {Number} the height of the renderers view - * - * @param [options] {Object} The optional renderer parameters + * @namespace PIXI + * @param width=800 {number} the width of the renderers view + * @param height=600 {number} the height of the renderers view + * @param [options] {object} The optional renderer parameters * @param [options.view] {HTMLCanvasElement} the canvas to use as a view, optional - * @param [options.transparent=false] {Boolean} If the render view is transparent, default false - * @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 - * + * @param [options.transparent=false] {boolean} If the render view is transparent, default false + * @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.autoDetectRecommendedRenderer = function(width, height, options) -{ - if(!width)width = 800; - if(!height)height = 600; +function autoDetectRecommendedRenderer(width, height, options) { + width = width || 800; + height = height || 600; // BORROWED from Mr Doob (mrdoob.com) var webgl = ( function () { try { @@ -79,10 +70,9 @@ var isAndroid = /Android/i.test(navigator.userAgent); - if( webgl && !isAndroid) - { - return new PIXI.WebGLRenderer(width, height, options); + if (webgl && !isAndroid) { + return new WebGLRenderer(width, height, options); } - return new PIXI.CanvasRenderer(width, height, options); -}; + return new CanvasRenderer(width, height, options); +} diff --git a/src/utils/EventTarget.js b/src/utils/EventTarget.js index 35aa31b..e01b7d6 100644 --- a/src/utils/EventTarget.js +++ b/src/utils/EventTarget.js @@ -1,9 +1,4 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 - * @author Chad Engler https://github.com/englercj @Rolnaaba - */ - -/** * Originally based on https://github.com/mrdoob/eventtarget.js/ from mr Doob. * Currently takes inspiration from the nodejs EventEmitter, EventEmitter3, and smokesignals */ @@ -11,206 +6,202 @@ /** * Mixins event emitter functionality to a class * - * @class EventTarget + * @mixin + * @namespace PIXI * @example * function MyEmitter() {} * - * PIXI.EventTarget.mixin(MyEmitter.prototype); + * EventTarget.mixin(MyEmitter.prototype); * * var em = new MyEmitter(); * em.emit('eventName', 'some data', 'some more data', {}, null, ...); */ -PIXI.EventTarget = { +function EventTarget(obj) { + /** + * Return a list of assigned event listeners. + * + * @param eventName {string} The events that should be listed. + * @return {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. + * + * @alias dispatchEvent + * @param eventName {string} The name of the event. + * @return {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 Event(this, eventName, data); + } + + //iterate the listeners + if (this._listeners && this._listeners[eventName]) { + var listeners = this._listeners[eventName].slice(0), + 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. + * + * @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. + * + * @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. + * + * @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 = 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. + * + * @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; + }; +}; + +module.exports = { /** * Backward compat from when this used to be a function */ call: function callCompat(obj) { - if(obj) { + if (obj) { obj = obj.prototype || obj; - PIXI.EventTarget.mixin(obj); + EventTarget(obj); } }, /** * Mixes in the properties of the EventTarget prototype onto another object * - * @method mixin - * @param object {Object} The obj to mix into + * @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. - * @return {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. - * @return {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].slice(0), - 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 = 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; - }; + EventTarget(obj); } -}; +} /** * 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 + * @class + * @namespace PIXI + * @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 +function Event(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 + * @member {boolean} * @private - * @readOnly + * @readonly */ this.stopped = false; @@ -218,37 +209,33 @@ * Tracks the state of sibling listener propagation. Do not * set this directly, instead use `event.stopImmediatePropagation()` * - * @property stoppedImmediate - * @type Boolean + * @member {boolean} * @private - * @readOnly + * @readonly */ this.stoppedImmediate = false; /** * The original target the event triggered on. * - * @property target - * @type Object - * @readOnly + * @member {object} + * @readonly */ this.target = target; /** * The string name of the event that this represents. * - * @property type - * @type String - * @readOnly + * @member {string} + * @readonly */ this.type = name; /** * The data that was passed in with this event. * - * @property data - * @type Object - * @readOnly + * @member {object} + * @readonly */ this.data = data; @@ -258,9 +245,8 @@ /** * The timestamp when the event occurred. * - * @property timeStamp - * @type Number - * @readOnly + * @member {number} + * @readonly */ this.timeStamp = Date.now(); }; @@ -268,17 +254,15 @@ /** * Stops the propagation of events up the scene graph (prevents bubbling). * - * @method stopPropagation */ -PIXI.Event.prototype.stopPropagation = function stopPropagation() { +Event.prototype.stopPropagation = function stopPropagation() { this.stopped = true; }; /** * Stops the propagation of events to sibling listeners (no longer calls any listeners). * - * @method stopImmediatePropagation */ -PIXI.Event.prototype.stopImmediatePropagation = function stopImmediatePropagation() { +Event.prototype.stopImmediatePropagation = function stopImmediatePropagation() { this.stoppedImmediate = true; }; diff --git a/src/utils/Polyk.js b/src/utils/Polyk.js index 838e6a2..c53c473 100644 --- a/src/utils/Polyk.js +++ b/src/utils/Polyk.js @@ -35,30 +35,28 @@ * Based on the Polyk library http://polyk.ivank.net released under MIT licence. * This is an amazing lib! * Slightly modified by Mat Groves (matgroves.com); - * @class PolyK + * + * @namespace PIXI */ -PIXI.PolyK = {}; +var PolyK = module.exports = {}; /** * Triangulates shapes for webGL graphic fills. * - * @method Triangulate */ -PIXI.PolyK.Triangulate = function(p) -{ +PolyK.Triangulate = function (p) { var sign = true; var n = p.length >> 1; - if(n < 3) return []; + if (n < 3) return []; var tgs = []; var avl = []; - for(var i = 0; i < n; i++) avl.push(i); + for (var i = 0; i < n; i++) avl.push(i); i = 0; var al = n; - while(al > 3) - { + while(al > 3) { var i0 = avl[(i+0)%al]; var i1 = avl[(i+1)%al]; var i2 = avl[(i+2)%al]; @@ -68,45 +66,39 @@ var cx = p[2*i2], cy = p[2*i2+1]; var earFound = false; - if(PIXI.PolyK._convex(ax, ay, bx, by, cx, cy, sign)) - { + if (PolyK._convex(ax, ay, bx, by, cx, cy, sign)) { earFound = true; - for(var j = 0; j < al; j++) - { + for (var j = 0; j < al; j++) { var vi = avl[j]; - if(vi === i0 || vi === i1 || vi === i2) continue; + if (vi === i0 || vi === i1 || vi === i2) continue; - if(PIXI.PolyK._PointInTriangle(p[2*vi], p[2*vi+1], ax, ay, bx, by, cx, cy)) { + if (PolyK._PointInTriangle(p[2*vi], p[2*vi+1], ax, ay, bx, by, cx, cy)) { earFound = false; break; } } } - if(earFound) - { + if (earFound) { tgs.push(i0, i1, i2); avl.splice((i+1)%al, 1); al--; i = 0; } - else if(i++ > 3*al) - { + else if (i++ > 3*al) { // need to flip flip reverse it! // reset! - if(sign) - { + if (sign) { tgs = []; avl = []; - for(i = 0; i < n; i++) avl.push(i); + for (i = 0; i < n; i++) avl.push(i); i = 0; al = n; sign = false; } - else - { + else { // window.console.log("PIXI Warning: shape too complex to fill"); return null; } @@ -120,20 +112,18 @@ /** * Checks whether a point is within a triangle * - * @method _PointInTriangle - * @param px {Number} x coordinate of the point to test - * @param py {Number} y coordinate of the point to test - * @param ax {Number} x coordinate of the a point of the triangle - * @param ay {Number} y coordinate of the a point of the triangle - * @param bx {Number} x coordinate of the b point of the triangle - * @param by {Number} y coordinate of the b point of the triangle - * @param cx {Number} x coordinate of the c point of the triangle - * @param cy {Number} y coordinate of the c point of the triangle + * @param px {number} x coordinate of the point to test + * @param py {number} y coordinate of the point to test + * @param ax {number} x coordinate of the a point of the triangle + * @param ay {number} y coordinate of the a point of the triangle + * @param bx {number} x coordinate of the b point of the triangle + * @param by {number} y coordinate of the b point of the triangle + * @param cx {number} x coordinate of the c point of the triangle + * @param cy {number} y coordinate of the c point of the triangle * @private - * @return {Boolean} + * @return {boolean} */ -PIXI.PolyK._PointInTriangle = function(px, py, ax, ay, bx, by, cx, cy) -{ +PolyK._PointInTriangle = function (px, py, ax, ay, bx, by, cx, cy) { var v0x = cx-ax; var v0y = cy-ay; var v1x = bx-ax; @@ -158,11 +148,9 @@ /** * Checks whether a shape is convex * - * @method _convex * @private - * @return {Boolean} + * @return {boolean} */ -PIXI.PolyK._convex = function(ax, ay, bx, by, cx, cy, sign) -{ +PolyK._convex = function (ax, ay, bx, by, cx, cy, sign) { return ((ay-by)*(cx-bx) + (bx-ax)*(cy-by) >= 0) === sign; }; diff --git a/src/utils/Detector.js b/src/utils/Detector.js index fe38f04..99f4e39 100644 --- a/src/utils/Detector.js +++ b/src/utils/Detector.js @@ -1,30 +1,25 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ +//TODO: Replace this code with external modules! /** * This helper function will automatically detect which renderer you should be using. * WebGL is the preferred renderer as it is a lot faster. If webGL is not supported by * the browser then this function will return a canvas renderer * - * @method autoDetectRenderer - * @for PIXI * @static - * @param width=800 {Number} the width of the renderers view - * @param height=600 {Number} the height of the renderers view - * - * @param [options] {Object} The optional renderer parameters + * @namespace PIXI + * @param width=800 {number} the width of the renderers view + * @param height=600 {number} the height of the renderers view + * @param [options] {object} The optional renderer parameters * @param [options.view] {HTMLCanvasElement} the canvas to use as a view, optional - * @param [options.transparent=false] {Boolean} If the render view is transparent, default false - * @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 - * + * @param [options.transparent=false] {boolean} If the render view is transparent, default false + * @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.autoDetectRenderer = function(width, height, options) -{ - if(!width)width = 800; - if(!height)height = 600; +function autoDetectRenderer(width, height, options) { + width = width || 800; + height = height || 600; // BORROWED from Mr Doob (mrdoob.com) var webgl = ( function () { try { @@ -35,38 +30,34 @@ } } )(); - if( webgl ) - { - return new PIXI.WebGLRenderer(width, height, options); + if (webgl) { + return new WebGLRenderer(width, height, options); } - return new PIXI.CanvasRenderer(width, height, options); -}; + return new CanvasRenderer(width, height, options); +} /** * This helper function will automatically detect which renderer you should be using. * This function is very similar to the autoDetectRenderer function except that is will return a canvas renderer for android. - * Even thought both android chrome supports webGL the canvas implementation perform better at the time of writing. + * Even thought both android chrome supports webGL the canvas implementation perform better at the time of writing. * This function will likely change and update as webGL performance improves on these devices. - * - * @method autoDetectRecommendedRenderer - * @for PIXI + * * @static - * @param width=800 {Number} the width of the renderers view - * @param height=600 {Number} the height of the renderers view - * - * @param [options] {Object} The optional renderer parameters + * @namespace PIXI + * @param width=800 {number} the width of the renderers view + * @param height=600 {number} the height of the renderers view + * @param [options] {object} The optional renderer parameters * @param [options.view] {HTMLCanvasElement} the canvas to use as a view, optional - * @param [options.transparent=false] {Boolean} If the render view is transparent, default false - * @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 - * + * @param [options.transparent=false] {boolean} If the render view is transparent, default false + * @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.autoDetectRecommendedRenderer = function(width, height, options) -{ - if(!width)width = 800; - if(!height)height = 600; +function autoDetectRecommendedRenderer(width, height, options) { + width = width || 800; + height = height || 600; // BORROWED from Mr Doob (mrdoob.com) var webgl = ( function () { try { @@ -79,10 +70,9 @@ var isAndroid = /Android/i.test(navigator.userAgent); - if( webgl && !isAndroid) - { - return new PIXI.WebGLRenderer(width, height, options); + if (webgl && !isAndroid) { + return new WebGLRenderer(width, height, options); } - return new PIXI.CanvasRenderer(width, height, options); -}; + return new CanvasRenderer(width, height, options); +} diff --git a/src/utils/EventTarget.js b/src/utils/EventTarget.js index 35aa31b..e01b7d6 100644 --- a/src/utils/EventTarget.js +++ b/src/utils/EventTarget.js @@ -1,9 +1,4 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 - * @author Chad Engler https://github.com/englercj @Rolnaaba - */ - -/** * Originally based on https://github.com/mrdoob/eventtarget.js/ from mr Doob. * Currently takes inspiration from the nodejs EventEmitter, EventEmitter3, and smokesignals */ @@ -11,206 +6,202 @@ /** * Mixins event emitter functionality to a class * - * @class EventTarget + * @mixin + * @namespace PIXI * @example * function MyEmitter() {} * - * PIXI.EventTarget.mixin(MyEmitter.prototype); + * EventTarget.mixin(MyEmitter.prototype); * * var em = new MyEmitter(); * em.emit('eventName', 'some data', 'some more data', {}, null, ...); */ -PIXI.EventTarget = { +function EventTarget(obj) { + /** + * Return a list of assigned event listeners. + * + * @param eventName {string} The events that should be listed. + * @return {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. + * + * @alias dispatchEvent + * @param eventName {string} The name of the event. + * @return {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 Event(this, eventName, data); + } + + //iterate the listeners + if (this._listeners && this._listeners[eventName]) { + var listeners = this._listeners[eventName].slice(0), + 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. + * + * @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. + * + * @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. + * + * @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 = 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. + * + * @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; + }; +}; + +module.exports = { /** * Backward compat from when this used to be a function */ call: function callCompat(obj) { - if(obj) { + if (obj) { obj = obj.prototype || obj; - PIXI.EventTarget.mixin(obj); + EventTarget(obj); } }, /** * Mixes in the properties of the EventTarget prototype onto another object * - * @method mixin - * @param object {Object} The obj to mix into + * @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. - * @return {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. - * @return {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].slice(0), - 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 = 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; - }; + EventTarget(obj); } -}; +} /** * 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 + * @class + * @namespace PIXI + * @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 +function Event(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 + * @member {boolean} * @private - * @readOnly + * @readonly */ this.stopped = false; @@ -218,37 +209,33 @@ * Tracks the state of sibling listener propagation. Do not * set this directly, instead use `event.stopImmediatePropagation()` * - * @property stoppedImmediate - * @type Boolean + * @member {boolean} * @private - * @readOnly + * @readonly */ this.stoppedImmediate = false; /** * The original target the event triggered on. * - * @property target - * @type Object - * @readOnly + * @member {object} + * @readonly */ this.target = target; /** * The string name of the event that this represents. * - * @property type - * @type String - * @readOnly + * @member {string} + * @readonly */ this.type = name; /** * The data that was passed in with this event. * - * @property data - * @type Object - * @readOnly + * @member {object} + * @readonly */ this.data = data; @@ -258,9 +245,8 @@ /** * The timestamp when the event occurred. * - * @property timeStamp - * @type Number - * @readOnly + * @member {number} + * @readonly */ this.timeStamp = Date.now(); }; @@ -268,17 +254,15 @@ /** * Stops the propagation of events up the scene graph (prevents bubbling). * - * @method stopPropagation */ -PIXI.Event.prototype.stopPropagation = function stopPropagation() { +Event.prototype.stopPropagation = function stopPropagation() { this.stopped = true; }; /** * Stops the propagation of events to sibling listeners (no longer calls any listeners). * - * @method stopImmediatePropagation */ -PIXI.Event.prototype.stopImmediatePropagation = function stopImmediatePropagation() { +Event.prototype.stopImmediatePropagation = function stopImmediatePropagation() { this.stoppedImmediate = true; }; diff --git a/src/utils/Polyk.js b/src/utils/Polyk.js index 838e6a2..c53c473 100644 --- a/src/utils/Polyk.js +++ b/src/utils/Polyk.js @@ -35,30 +35,28 @@ * Based on the Polyk library http://polyk.ivank.net released under MIT licence. * This is an amazing lib! * Slightly modified by Mat Groves (matgroves.com); - * @class PolyK + * + * @namespace PIXI */ -PIXI.PolyK = {}; +var PolyK = module.exports = {}; /** * Triangulates shapes for webGL graphic fills. * - * @method Triangulate */ -PIXI.PolyK.Triangulate = function(p) -{ +PolyK.Triangulate = function (p) { var sign = true; var n = p.length >> 1; - if(n < 3) return []; + if (n < 3) return []; var tgs = []; var avl = []; - for(var i = 0; i < n; i++) avl.push(i); + for (var i = 0; i < n; i++) avl.push(i); i = 0; var al = n; - while(al > 3) - { + while(al > 3) { var i0 = avl[(i+0)%al]; var i1 = avl[(i+1)%al]; var i2 = avl[(i+2)%al]; @@ -68,45 +66,39 @@ var cx = p[2*i2], cy = p[2*i2+1]; var earFound = false; - if(PIXI.PolyK._convex(ax, ay, bx, by, cx, cy, sign)) - { + if (PolyK._convex(ax, ay, bx, by, cx, cy, sign)) { earFound = true; - for(var j = 0; j < al; j++) - { + for (var j = 0; j < al; j++) { var vi = avl[j]; - if(vi === i0 || vi === i1 || vi === i2) continue; + if (vi === i0 || vi === i1 || vi === i2) continue; - if(PIXI.PolyK._PointInTriangle(p[2*vi], p[2*vi+1], ax, ay, bx, by, cx, cy)) { + if (PolyK._PointInTriangle(p[2*vi], p[2*vi+1], ax, ay, bx, by, cx, cy)) { earFound = false; break; } } } - if(earFound) - { + if (earFound) { tgs.push(i0, i1, i2); avl.splice((i+1)%al, 1); al--; i = 0; } - else if(i++ > 3*al) - { + else if (i++ > 3*al) { // need to flip flip reverse it! // reset! - if(sign) - { + if (sign) { tgs = []; avl = []; - for(i = 0; i < n; i++) avl.push(i); + for (i = 0; i < n; i++) avl.push(i); i = 0; al = n; sign = false; } - else - { + else { // window.console.log("PIXI Warning: shape too complex to fill"); return null; } @@ -120,20 +112,18 @@ /** * Checks whether a point is within a triangle * - * @method _PointInTriangle - * @param px {Number} x coordinate of the point to test - * @param py {Number} y coordinate of the point to test - * @param ax {Number} x coordinate of the a point of the triangle - * @param ay {Number} y coordinate of the a point of the triangle - * @param bx {Number} x coordinate of the b point of the triangle - * @param by {Number} y coordinate of the b point of the triangle - * @param cx {Number} x coordinate of the c point of the triangle - * @param cy {Number} y coordinate of the c point of the triangle + * @param px {number} x coordinate of the point to test + * @param py {number} y coordinate of the point to test + * @param ax {number} x coordinate of the a point of the triangle + * @param ay {number} y coordinate of the a point of the triangle + * @param bx {number} x coordinate of the b point of the triangle + * @param by {number} y coordinate of the b point of the triangle + * @param cx {number} x coordinate of the c point of the triangle + * @param cy {number} y coordinate of the c point of the triangle * @private - * @return {Boolean} + * @return {boolean} */ -PIXI.PolyK._PointInTriangle = function(px, py, ax, ay, bx, by, cx, cy) -{ +PolyK._PointInTriangle = function (px, py, ax, ay, bx, by, cx, cy) { var v0x = cx-ax; var v0y = cy-ay; var v1x = bx-ax; @@ -158,11 +148,9 @@ /** * Checks whether a shape is convex * - * @method _convex * @private - * @return {Boolean} + * @return {boolean} */ -PIXI.PolyK._convex = function(ax, ay, bx, by, cx, cy, sign) -{ +PolyK._convex = function (ax, ay, bx, by, cx, cy, sign) { return ((ay-by)*(cx-bx) + (bx-ax)*(cy-by) >= 0) === sign; }; diff --git a/src/utils/Utils.js b/src/utils/Utils.js index 1a0127d..5c94d92 100644 --- a/src/utils/Utils.js +++ b/src/utils/Utils.js @@ -1,7 +1,3 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - // http://paulirish.com/2011/requestanimationframe-for-smart-animating/ // http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating @@ -11,31 +7,30 @@ /** * A polyfill for requestAnimationFrame - * You can actually use both requestAnimationFrame and requestAnimFrame, + * You can actually use both requestAnimationFrame and requestAnimFrame, * you will still benefit from the polyfill * - * @method requestAnimationFrame */ /** * A polyfill for cancelAnimationFrame * - * @method cancelAnimationFrame */ -(function(window) { +(function (window) { var lastTime = 0; var vendors = ['ms', 'moz', 'webkit', 'o']; - for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { + + for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame']; window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] || window[vendors[x] + 'CancelRequestAnimationFrame']; } if (!window.requestAnimationFrame) { - window.requestAnimationFrame = function(callback) { + window.requestAnimationFrame = function (callback) { var currTime = new Date().getTime(); var timeToCall = Math.max(0, 16 - (currTime - lastTime)); - var id = window.setTimeout(function() { callback(currTime + timeToCall); }, + var id = window.setTimeout(function () { callback(currTime + timeToCall); }, timeToCall); lastTime = currTime + timeToCall; return id; @@ -43,7 +38,7 @@ } if (!window.cancelAnimationFrame) { - window.cancelAnimationFrame = function(id) { + window.cancelAnimationFrame = function (id) { clearTimeout(id); }; } @@ -54,34 +49,30 @@ /** * Converts a hex color number to an [R, G, B] array * - * @method hex2rgb - * @param hex {Number} + * @param hex {number} */ -PIXI.hex2rgb = function(hex) { +function hex2rgb(hex) { return [(hex >> 16 & 0xFF) / 255, ( hex >> 8 & 0xFF) / 255, (hex & 0xFF)/ 255]; }; /** * Converts a color as an [R, G, B] array to a hex number * - * @method rgb2hex * @param rgb {Array} */ -PIXI.rgb2hex = function(rgb) { +function rgb2hex(rgb) { return ((rgb[0]*255 << 16) + (rgb[1]*255 << 8) + rgb[2]*255); }; /** * A polyfill for Function.prototype.bind * - * @method bind */ if (typeof Function.prototype.bind !== 'function') { Function.prototype.bind = (function () { return function (thisArg) { var target = this, i = arguments.length - 1, boundArgs = []; - if (i > 0) - { + if (i > 0) { boundArgs.length = i; while (i--) boundArgs[i] = arguments[i + 1]; } @@ -108,17 +99,14 @@ /** * A wrapper for ajax requests to be handled cross browser * - * @class AjaxRequest - * @constructor + * @class + * @namespace PIXI */ -PIXI.AjaxRequest = function() -{ +function AjaxRequest() { var activexmodes = ['Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.3.0', 'Microsoft.XMLHTTP']; //activeX versions to check for in IE - if (window.ActiveXObject) - { //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken) - for (var i=0; i 0 && (number & (number - 1)) === 0) // see: http://goo.gl/D9kPj return number; - else - { + else { var result = 1; while (result < number) result <<= 1; return result; @@ -208,13 +187,11 @@ /** * checks if the given width and height make a power of two texture - * @method isPowerOfTwo - * @param width {Number} - * @param height {Number} - * @return {Boolean} + * @param width {number} + * @param height {number} + * @return {boolean} */ -PIXI.isPowerOfTwo = function(width, height) -{ +function isPowerOfTwo(width, height) { return (width > 0 && (width & (width - 1)) === 0 && height > 0 && (height & (height - 1)) === 0); };