75 lines
1.4 KiB
JavaScript
75 lines
1.4 KiB
JavaScript
/**
|
|
* @author Mat Groves http://matgroves.com/ @Doormat23
|
|
*/
|
|
|
|
/**
|
|
* https://github.com/mrdoob/eventtarget.js/
|
|
* THankS mr DOob!
|
|
*/
|
|
|
|
/**
|
|
* Adds event emitter functionality to a class
|
|
*
|
|
* @class EventTarget
|
|
* @example
|
|
* function MyEmitter() {
|
|
* PIXI.EventTarget.call(this); //mixes in event target stuff
|
|
* }
|
|
*
|
|
* var em = new MyEmitter();
|
|
* em.emit({ type: 'eventName', data: 'some data' });
|
|
*/
|
|
PIXI.EventTarget = function () {
|
|
|
|
var listeners = {};
|
|
|
|
this.addEventListener = this.on = function ( type, listener ) {
|
|
|
|
|
|
if ( listeners[ type ] === undefined ) {
|
|
|
|
listeners[ type ] = [];
|
|
|
|
}
|
|
|
|
if ( listeners[ type ].indexOf( listener ) === - 1 ) {
|
|
|
|
listeners[ type ].push( listener );
|
|
}
|
|
|
|
};
|
|
|
|
this.dispatchEvent = this.emit = function ( event ) {
|
|
|
|
if ( !listeners[ event.type ] || !listeners[ event.type ].length ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for(var i = 0, l = listeners[ event.type ].length; i < l; i++) {
|
|
|
|
listeners[ event.type ][ i ]( event );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.removeEventListener = this.off = function ( type, listener ) {
|
|
|
|
var index = listeners[ type ].indexOf( listener );
|
|
|
|
if ( index !== - 1 ) {
|
|
|
|
listeners[ type ].splice( index, 1 );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.removeAllEventListeners = function( type ) {
|
|
var a = listeners[type];
|
|
if (a)
|
|
a.length = 0;
|
|
};
|
|
};
|