浏览代码

Updated to prototype based EventDispatcher.

Mr.doob 12 年之前
父节点
当前提交
6c35dc23cf

+ 12 - 25
docs/api/core/EventDispatcher.html

@@ -9,36 +9,14 @@
 	<body>
 		<h1>[name]</h1>
 
-		<div class="desc">JavaScript events for custom objects.</div>
-
-		<h2>Example</h2>
-
-		<code>
-		var Car = function () {
-
-			EventDispatcher.call( this );
-			this.start = function () {
-
-				this.dispatchEvent( { type: 'start', message: 'vroom vroom!' } );
-
-			};
-
-		};
-
-		var car = new Car();
-		car.addEventListener( 'start', function ( event ) {
-
-			alert( event.message );
-
-		} );
-		car.start();
-		</code>
+		<div class="desc">JavaScript events for custom objects.<br />
+		<a href="https://github.com/mrdoob/eventdispatcher.js">https://github.com/mrdoob/eventdispatcher.js</a></div>
 
 		<h2>Constructor</h2>
 
 		<h3>[name]()</h3>
 		<div>
-		Creates eventDispatcher object. It needs to be called with '.call' to add its functionality to an object.
+		Creates EventDispatcher object.
 		</div>
 
 
@@ -53,6 +31,15 @@
 		Adds a listener to an event type.
 		</div>
 
+		<h3>.hasEventListener( [page:String type], [page:Function listener] )</h3>
+		<div>
+		type - The type of event to listen to.<br />
+		listener - The function that gets called when the event is fired.
+		</div>
+		<div>
+		Checks if listener is added to an event type.
+		</div>
+
 		<h3>.removeEventListener( [page:String type], [page:Function listener] )</h3>
 		<div>
 		type - The type of the listener that gets removed.<br />

+ 2 - 2
examples/js/controls/OrbitControls.js

@@ -7,8 +7,6 @@
 
 THREE.OrbitControls = function ( object, domElement ) {
 
-	THREE.EventDispatcher.call( this );
-
 	this.object = object;
 	this.domElement = ( domElement !== undefined ) ? domElement : document;
 
@@ -354,3 +352,5 @@ THREE.OrbitControls = function ( object, domElement ) {
 	this.domElement.addEventListener( 'keydown', onKeyDown, false );
 
 };
+
+THREE.extend( THREE.OrbitControls.prototype, THREE.EventDispatcher.prototype );

+ 2 - 2
examples/js/controls/TrackballControls.js

@@ -4,8 +4,6 @@
 
 THREE.TrackballControls = function ( object, domElement ) {
 
-	THREE.EventDispatcher.call( this );
-
 	var _this = this;
 	var STATE = { NONE: -1, ROTATE: 0, ZOOM: 1, PAN: 2, TOUCH_ROTATE: 3, TOUCH_ZOOM: 4, TOUCH_PAN: 5 };
 
@@ -535,3 +533,5 @@ THREE.TrackballControls = function ( object, domElement ) {
 	this.handleResize();
 
 };
+
+THREE.extend( THREE.TrackballControls.prototype, THREE.EventDispatcher.prototype );

+ 6 - 4
examples/js/loaders/MTLLoader.js

@@ -6,8 +6,6 @@
 
 THREE.MTLLoader = function( baseUrl, options ) {
 
-	THREE.EventDispatcher.call( this );
-	
 	this.baseUrl = baseUrl;
 	this.options = options;
 
@@ -15,6 +13,8 @@ THREE.MTLLoader = function( baseUrl, options ) {
 
 THREE.MTLLoader.prototype = {
 
+	constructor: THREE.MTLLoader,
+
 	/**
 	 * Loads a MTL file
 	 *
@@ -131,6 +131,8 @@ THREE.MTLLoader.prototype = {
 
 };
 
+THREE.extend( THREE.MTLLoader.prototype, THREE.EventDispatcher.prototype );
+
 /**
  * Create a new THREE-MTLLoader.MaterialCreator
  * @param baseUrl - Url relative to which textures are loaded
@@ -150,8 +152,6 @@ THREE.MTLLoader.prototype = {
 
 THREE.MTLLoader.MaterialCreator = function( baseUrl, options ) {
 
-	THREE.EventDispatcher.call( this );
-
 	this.baseUrl = baseUrl;
 	this.options = options;
 	this.materialsInfo = {};
@@ -166,6 +166,8 @@ THREE.MTLLoader.MaterialCreator = function( baseUrl, options ) {
 
 THREE.MTLLoader.MaterialCreator.prototype = {
 
+	constructor: THREE.MTLLoader.MaterialCreator,
+
 	setMaterials: function( materialsInfo ) {
 
 		this.materialsInfo = this.convert( materialsInfo );

+ 4 - 6
examples/js/loaders/OBJLoader.js

@@ -2,11 +2,7 @@
  * @author mrdoob / http://mrdoob.com/
  */
 
-THREE.OBJLoader = function () {
-
-	THREE.EventDispatcher.call( this );
-
-};
+THREE.OBJLoader = function () {};
 
 THREE.OBJLoader.prototype = {
 
@@ -425,4 +421,6 @@ THREE.OBJLoader.prototype = {
 
 	}
 
-}
+};
+
+THREE.extend( THREE.OBJLoader.prototype, THREE.EventDispatcher.prototype );

+ 5 - 7
examples/js/loaders/OBJMTLLoader.js

@@ -5,11 +5,7 @@
  * @author angelxuanchang
  */
 
-THREE.OBJMTLLoader = function () {
-
-	THREE.EventDispatcher.call( this );
-
-};
+THREE.OBJMTLLoader = function () {};
 
 THREE.OBJMTLLoader.prototype = {
 
@@ -561,12 +557,14 @@ THREE.OBJMTLLoader.prototype = {
 			}
 
 		}
-		
+
 		//Add last object
 		meshN(undefined, undefined);
-		
+
 		return group;
 
 	}
 
 };
+
+THREE.extend( THREE.OBJMTLLoader.prototype, THREE.EventDispatcher.prototype );

+ 3 - 5
examples/js/loaders/PLYLoader.js

@@ -19,11 +19,7 @@
  */
 
 
-THREE.PLYLoader = function () {
-
-	THREE.EventDispatcher.call( this );
-
-};
+THREE.PLYLoader = function () {};
 
 THREE.PLYLoader.prototype = {
 
@@ -174,3 +170,5 @@ THREE.PLYLoader.prototype = {
 
 
 };
+
+THREE.extend( THREE.PLYLoader.prototype, THREE.EventDispatcher.prototype );

+ 3 - 5
examples/js/loaders/STLLoader.js

@@ -22,11 +22,7 @@
  */
 
 
-THREE.STLLoader = function () {
-
-	THREE.EventDispatcher.call( this );
-
-};
+THREE.STLLoader = function () {};
 
 THREE.STLLoader.prototype = {
 
@@ -193,3 +189,5 @@ THREE.STLLoader.prototype = {
 	}
 
 };
+
+THREE.extend( THREE.STLLoader.prototype, THREE.EventDispatcher.prototype );

+ 4 - 6
examples/js/loaders/SceneLoader2.js

@@ -2,11 +2,7 @@
  * @author mrdoob / http://mrdoob.com/
  */
 
-THREE.SceneLoader2 = function () {
-
-	THREE.EventDispatcher.call( this );
-
-};
+THREE.SceneLoader2 = function () {};
 
 THREE.SceneLoader2.prototype = {
 
@@ -275,4 +271,6 @@ THREE.SceneLoader2.prototype = {
 
 	}
 
-}
+};
+
+THREE.extend( THREE.SceneLoader2.prototype, THREE.EventDispatcher.prototype );

+ 3 - 5
examples/js/loaders/VTKLoader.js

@@ -2,11 +2,7 @@
  * @author mrdoob / http://mrdoob.com/
  */
 
-THREE.VTKLoader = function () {
-
-	THREE.EventDispatcher.call( this );
-
-};
+THREE.VTKLoader = function () {};
 
 THREE.VTKLoader.prototype = {
 
@@ -114,3 +110,5 @@ THREE.VTKLoader.prototype = {
 	}
 
 }
+
+THREE.extend( THREE.VTKLoader.prototype, THREE.EventDispatcher.prototype );

+ 3 - 4
src/core/BufferGeometry.js

@@ -4,8 +4,6 @@
 
 THREE.BufferGeometry = function () {
 
-	THREE.EventDispatcher.call( this );
-
 	this.id = THREE.GeometryIdCount ++;
 
 	// attributes
@@ -35,7 +33,7 @@ THREE.BufferGeometry = function () {
 
 THREE.BufferGeometry.prototype = {
 
-	constructor : THREE.BufferGeometry,
+	constructor: THREE.BufferGeometry,
 
 	applyMatrix: function ( matrix ) {
 
@@ -511,7 +509,7 @@ THREE.BufferGeometry.prototype = {
 			test = tmp2.dot( tan2[ v ] );
 			w = ( test < 0.0 ) ? -1.0 : 1.0;
 
-			tangents[ v * 4 ] 	  = tmp.x;
+			tangents[ v * 4 ]     = tmp.x;
 			tangents[ v * 4 + 1 ] = tmp.y;
 			tangents[ v * 4 + 2 ] = tmp.z;
 			tangents[ v * 4 + 3 ] = w;
@@ -551,3 +549,4 @@ THREE.BufferGeometry.prototype = {
 
 };
 
+THREE.extend( THREE.BufferGeometry.prototype, THREE.EventDispatcher.prototype );

+ 36 - 8
src/core/EventDispatcher.js

@@ -2,11 +2,17 @@
  * https://github.com/mrdoob/eventdispatcher.js/
  */
 
-THREE.EventDispatcher = function () {
+THREE.EventDispatcher = function () {}
 
-	var listeners = {};
+THREE.EventDispatcher.prototype = {
 
-	this.addEventListener = function ( type, listener ) {
+	constructor: THREE.EventDispatcher,
+
+	addEventListener: function ( type, listener ) {
+
+		if ( this._listeners === undefined ) this._listeners = {};
+
+		var listeners = this._listeners;
 
 		if ( listeners[ type ] === undefined ) {
 
@@ -20,10 +26,29 @@ THREE.EventDispatcher = function () {
 
 		}
 
-	};
+	},
+
+	hasEventListener: function ( type, listener ) {
+
+		if ( this._listeners === undefined ) return false;
+
+		var listeners = this._listeners;
+
+		if ( listeners[ type ] !== undefined && listeners[ type ].indexOf( listener ) !== - 1 ) {
+
+			return true;
+
+		}
+
+		return false;
 
-	this.removeEventListener = function ( type, listener ) {
+	},
 
+	removeEventListener: function ( type, listener ) {
+
+		if ( this._listeners === undefined ) return;
+
+		var listeners = this._listeners;
 		var index = listeners[ type ].indexOf( listener );
 
 		if ( index !== - 1 ) {
@@ -32,10 +57,13 @@ THREE.EventDispatcher = function () {
 
 		}
 
-	};
+	},
+
+	dispatchEvent: function ( event ) {
 
-	this.dispatchEvent = function ( event ) {
+		if ( this._listeners === undefined ) return;
 
+		var listeners = this._listeners;
 		var listenerArray = listeners[ event.type ];
 
 		if ( listenerArray !== undefined ) {
@@ -50,6 +78,6 @@ THREE.EventDispatcher = function () {
 
 		}
 
-	};
+	}
 
 };

+ 2 - 2
src/core/Geometry.js

@@ -9,8 +9,6 @@
 
 THREE.Geometry = function () {
 
-	THREE.EventDispatcher.call( this );
-
 	this.id = THREE.GeometryIdCount ++;
 
 	this.name = '';
@@ -800,4 +798,6 @@ THREE.Geometry.prototype = {
 
 };
 
+THREE.extend( THREE.Geometry.prototype, THREE.EventDispatcher.prototype );
+
 THREE.GeometryIdCount = 0;

+ 2 - 5
src/loaders/GeometryLoader.js

@@ -2,8 +2,5 @@
  * @author mrdoob / http://mrdoob.com/
  */
 
-THREE.GeometryLoader = function () {
-
-	THREE.EventDispatcher.call( this );
-
-};
+THREE.GeometryLoader = function () {};
+THREE.extend( THREE.GeometryLoader.prototype, THREE.EventDispatcher.prototype );

+ 13 - 19
src/loaders/ImageLoader.js

@@ -4,38 +4,32 @@
 
 THREE.ImageLoader = function () {
 
-	THREE.EventDispatcher.call( this );
-
 	this.crossOrigin = null;
 
 };
 
-THREE.ImageLoader.prototype = {
-
-	constructor: THREE.ImageLoader,
+THREE.ImageLoader.prototype.load = function ( url, image ) {
 
-	load: function ( url, image ) {
+	var scope = this;
 
-		var scope = this;
+	if ( image === undefined ) image = new Image();
 
-		if ( image === undefined ) image = new Image();
+	image.addEventListener( 'load', function () {
 
-		image.addEventListener( 'load', function () {
+		scope.dispatchEvent( { type: 'load', content: image } );
 
-			scope.dispatchEvent( { type: 'load', content: image } );
+	}, false );
 
-		}, false );
+	image.addEventListener( 'error', function () {
 
-		image.addEventListener( 'error', function () {
+		scope.dispatchEvent( { type: 'error', message: 'Couldn\'t load URL [' + url + ']' } );
 
-			scope.dispatchEvent( { type: 'error', message: 'Couldn\'t load URL [' + url + ']' } );
+	}, false );
 
-		}, false );
+	if ( scope.crossOrigin ) image.crossOrigin = scope.crossOrigin;
 
-		if ( scope.crossOrigin ) image.crossOrigin = scope.crossOrigin;
+	image.src = url;
 
-		image.src = url;
-
-	}
+};
 
-}
+THREE.extend( THREE.ImageLoader.prototype, THREE.EventDispatcher.prototype );

+ 2 - 2
src/loaders/LoadingMonitor.js

@@ -4,8 +4,6 @@
 
 THREE.LoadingMonitor = function () {
 
-	THREE.EventDispatcher.call( this );
-
 	var scope = this;
 
 	var loaded = 0;
@@ -34,3 +32,5 @@ THREE.LoadingMonitor = function () {
 	};
 
 };
+
+THREE.extend( THREE.LoadingMonitor.prototype, THREE.EventDispatcher.prototype );

+ 3 - 5
src/loaders/MaterialLoader.js

@@ -2,11 +2,7 @@
  * @author mrdoob / http://mrdoob.com/
  */
 
-THREE.MaterialLoader = function () {
-
-	THREE.EventDispatcher.call( this );
-
-};
+THREE.MaterialLoader = function () {};
 
 THREE.MaterialLoader.prototype = {
 
@@ -124,3 +120,5 @@ THREE.MaterialLoader.prototype = {
 	}
 
 };
+
+THREE.extend( THREE.MaterialLoader.prototype, THREE.EventDispatcher.prototype );

+ 15 - 21
src/loaders/TextureLoader.js

@@ -4,41 +4,35 @@
 
 THREE.TextureLoader = function () {
 
-	THREE.EventDispatcher.call( this );
-
 	this.crossOrigin = null;
 
 };
 
-THREE.TextureLoader.prototype = {
-
-	constructor: THREE.TextureLoader,
+THREE.TextureLoader.prototype.load = function ( url ) {
 
-	load: function ( url ) {
+	var scope = this;
 
-		var scope = this;
+	var image = new Image();
 
-		var image = new Image();
+	image.addEventListener( 'load', function () {
 
-		image.addEventListener( 'load', function () {
+		var texture = new THREE.Texture( image );
+		texture.needsUpdate = true;
 
-			var texture = new THREE.Texture( image );
-			texture.needsUpdate = true;
+		scope.dispatchEvent( { type: 'load', content: texture } );
 
-			scope.dispatchEvent( { type: 'load', content: texture } );
+	}, false );
 
-		}, false );
+	image.addEventListener( 'error', function () {
 
-		image.addEventListener( 'error', function () {
+		scope.dispatchEvent( { type: 'error', message: 'Couldn\'t load URL [' + url + ']' } );
 
-			scope.dispatchEvent( { type: 'error', message: 'Couldn\'t load URL [' + url + ']' } );
+	}, false );
 
-		}, false );
+	if ( scope.crossOrigin ) image.crossOrigin = scope.crossOrigin;
 
-		if ( scope.crossOrigin ) image.crossOrigin = scope.crossOrigin;
+	image.src = url;
 
-		image.src = url;
-
-	}
+};
 
-}
+THREE.extend( THREE.TextureLoader.prototype, THREE.EventDispatcher.prototype );

+ 50 - 44
src/materials/Material.js

@@ -5,8 +5,6 @@
 
 THREE.Material = function () {
 
-	THREE.EventDispatcher.call( this );
-
 	this.id = THREE.MaterialIdCount ++;
 
 	this.name = '';
@@ -39,87 +37,95 @@ THREE.Material = function () {
 
 };
 
-THREE.Material.prototype.setValues = function ( values ) {
+THREE.Material.prototype = {
 
-	if ( values === undefined ) return;
+	constructor: THREE.Material,
 
-	for ( var key in values ) {
+	setValues: function ( values ) {
 
-		var newValue = values[ key ];
+		if ( values === undefined ) return;
 
-		if ( newValue === undefined ) {
+		for ( var key in values ) {
 
-			console.warn( 'THREE.Material: \'' + key + '\' parameter is undefined.' );
-			continue;
+			var newValue = values[ key ];
 
-		}
+			if ( newValue === undefined ) {
+
+				console.warn( 'THREE.Material: \'' + key + '\' parameter is undefined.' );
+				continue;
+
+			}
+
+			if ( key in this ) {
 
-		if ( key in this ) {
+				var currentValue = this[ key ];
 
-			var currentValue = this[ key ];
+				if ( currentValue instanceof THREE.Color && newValue instanceof THREE.Color ) {
 
-			if ( currentValue instanceof THREE.Color && newValue instanceof THREE.Color ) {
+					currentValue.copy( newValue );
 
-				currentValue.copy( newValue );
+				} else if ( currentValue instanceof THREE.Color ) {
 
-			} else if ( currentValue instanceof THREE.Color ) {
+					currentValue.set( newValue );
 
-				currentValue.set( newValue );
+				} else if ( currentValue instanceof THREE.Vector3 && newValue instanceof THREE.Vector3 ) {
 
-			} else if ( currentValue instanceof THREE.Vector3 && newValue instanceof THREE.Vector3 ) {
+					currentValue.copy( newValue );
 
-				currentValue.copy( newValue );
+				} else {
 
-			} else {
+					this[ key ] = newValue;
 
-				this[ key ] = newValue;
+				}
 
 			}
 
 		}
 
-	}
+	},
 
-};
+	clone: function ( material ) {
 
-THREE.Material.prototype.clone = function ( material ) {
+		if ( material === undefined ) material = new THREE.Material();
 
-	if ( material === undefined ) material = new THREE.Material();
+		material.name = this.name;
 
-	material.name = this.name;
+		material.side = this.side;
 
-	material.side = this.side;
+		material.opacity = this.opacity;
+		material.transparent = this.transparent;
 
-	material.opacity = this.opacity;
-	material.transparent = this.transparent;
+		material.blending = this.blending;
 
-	material.blending = this.blending;
+		material.blendSrc = this.blendSrc;
+		material.blendDst = this.blendDst;
+		material.blendEquation = this.blendEquation;
 
-	material.blendSrc = this.blendSrc;
-	material.blendDst = this.blendDst;
-	material.blendEquation = this.blendEquation;
+		material.depthTest = this.depthTest;
+		material.depthWrite = this.depthWrite;
 
-	material.depthTest = this.depthTest;
-	material.depthWrite = this.depthWrite;
+		material.polygonOffset = this.polygonOffset;
+		material.polygonOffsetFactor = this.polygonOffsetFactor;
+		material.polygonOffsetUnits = this.polygonOffsetUnits;
 
-	material.polygonOffset = this.polygonOffset;
-	material.polygonOffsetFactor = this.polygonOffsetFactor;
-	material.polygonOffsetUnits = this.polygonOffsetUnits;
+		material.alphaTest = this.alphaTest;
 
-	material.alphaTest = this.alphaTest;
+		material.overdraw = this.overdraw;
 
-	material.overdraw = this.overdraw;
+		material.visible = this.visible;
 
-	material.visible = this.visible;
+		return material;
 
-	return material;
+	},
 
-};
+	dispose: function () {
 
-THREE.Material.prototype.dispose = function () {
+		this.dispatchEvent( { type: 'dispose' } );
 
-	this.dispatchEvent( { type: 'dispose' } );
+	}
 
 };
 
+THREE.extend( THREE.Material.prototype, THREE.EventDispatcher.prototype );
+
 THREE.MaterialIdCount = 0;

+ 27 - 21
src/renderers/WebGLRenderTarget.js

@@ -5,8 +5,6 @@
 
 THREE.WebGLRenderTarget = function ( width, height, options ) {
 
-	THREE.EventDispatcher.call( this );
-
 	this.width = width;
 	this.height = height;
 
@@ -35,37 +33,45 @@ THREE.WebGLRenderTarget = function ( width, height, options ) {
 
 };
 
-THREE.WebGLRenderTarget.prototype.clone = function() {
+THREE.WebGLRenderTarget.prototype = {
 
-	var tmp = new THREE.WebGLRenderTarget( this.width, this.height );
+	constructor: THREE.WebGLRenderTarget,
 
-	tmp.wrapS = this.wrapS;
-	tmp.wrapT = this.wrapT;
+	clone: function () {
 
-	tmp.magFilter = this.magFilter;
-	tmp.minFilter = this.minFilter;
+		var tmp = new THREE.WebGLRenderTarget( this.width, this.height );
 
-	tmp.anisotropy = this.anisotropy;
+		tmp.wrapS = this.wrapS;
+		tmp.wrapT = this.wrapT;
 
-	tmp.offset.copy( this.offset );
-	tmp.repeat.copy( this.repeat );
+		tmp.magFilter = this.magFilter;
+		tmp.minFilter = this.minFilter;
 
-	tmp.format = this.format;
-	tmp.type = this.type;
+		tmp.anisotropy = this.anisotropy;
 
-	tmp.depthBuffer = this.depthBuffer;
-	tmp.stencilBuffer = this.stencilBuffer;
+		tmp.offset.copy( this.offset );
+		tmp.repeat.copy( this.repeat );
 
-	tmp.generateMipmaps = this.generateMipmaps;
+		tmp.format = this.format;
+		tmp.type = this.type;
 
-	tmp.shareDepthFrom = this.shareDepthFrom;
+		tmp.depthBuffer = this.depthBuffer;
+		tmp.stencilBuffer = this.stencilBuffer;
 
-	return tmp;
+		tmp.generateMipmaps = this.generateMipmaps;
 
-};
+		tmp.shareDepthFrom = this.shareDepthFrom;
+
+		return tmp;
 
-THREE.WebGLRenderTarget.prototype.dispose = function () {
+	},
 
-	this.dispatchEvent( { type: 'dispose' } );
+	dispose: function () {
+
+		this.dispatchEvent( { type: 'dispose' } );
+
+	}
 
 };
+
+THREE.extend( THREE.WebGLRenderTarget.prototype, THREE.EventDispatcher.prototype );

+ 2 - 2
src/textures/Texture.js

@@ -6,8 +6,6 @@
 
 THREE.Texture = function ( image, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ) {
 
-	THREE.EventDispatcher.call( this );
-
 	this.id = THREE.TextureIdCount ++;
 
 	this.name = '';
@@ -85,4 +83,6 @@ THREE.Texture.prototype = {
 
 };
 
+THREE.extend( THREE.Texture.prototype, THREE.EventDispatcher.prototype );
+
 THREE.TextureIdCount = 0;