Explorar el Código

DragControls: Refactoring and API changes.

Mr.doob hace 8 años
padre
commit
7b529ccc32

+ 34 - 29
examples/js/controls/DragControls.js

@@ -4,59 +4,50 @@
  * Running this will allow you to drag three.js objects around the screen.
  */
 
-THREE.DragControls = function ( _camera, _objects, _domElement ) {
+THREE.DragControls = function ( _objects, _camera, _domElement ) {
+
+	if ( _objects instanceof THREE.Camera ) {
+
+		console.warn( 'THREE.DragControls: Constructor now expects ( objects, camera, domElement )' );
+		var temp = _objects; _objects = _camera; _camera = temp;
+
+	}
 
 	var _raycaster = new THREE.Raycaster();
 
-	var _mouse = new THREE.Vector3(),
-		_offset = new THREE.Vector3();
+	var _mouse = new THREE.Vector3(), _offset = new THREE.Vector3();
 	var _selected, _hovered = null;
 
 	var p3subp1 = new THREE.Vector3();
 	var targetposition = new THREE.Vector3();
 
-	this.enabled = false;
-
-	var scope = this;
-
-	this.setObjects = function ( objects ) {
-
-		if ( Array.isArray( objects ) === false ) {
+	this.enabled = true;
 
-			console.error( 'THREE.DragControls.setObjects() expects an Array.' );
-			return;
-
-		}
-
-		_objects = objects;
+	//
 
-	};
-
-	this.setObjects( _objects );
+	var scope = this;
 
-	this.activate = function () {
+	function activate() {
 
 		_domElement.addEventListener( 'mousemove', onDocumentMouseMove, false );
 		_domElement.addEventListener( 'mousedown', onDocumentMouseDown, false );
 		_domElement.addEventListener( 'mouseup', onDocumentMouseUp, false );
 
-	};
+	}
 
-	this.deactivate = function () {
+	function deactivate() {
 
 		_domElement.removeEventListener( 'mousemove', onDocumentMouseMove, false );
 		_domElement.removeEventListener( 'mousedown', onDocumentMouseDown, false );
 		_domElement.removeEventListener( 'mouseup', onDocumentMouseUp, false );
 
-	};
-
-	this.dispose = function () {
+	}
 
-		scope.deactivate();
+	function dispose() {
 
-	};
+		deactivate();
 
-	this.activate();
+	}
 
 	function onDocumentMouseMove( event ) {
 
@@ -77,6 +68,7 @@ THREE.DragControls = function ( _camera, _objects, _domElement ) {
 			// http://paulbourke.net/geometry/planeline/
 
 			var denom = normal.dot( ray.direction );
+
 			if ( denom == 0 ) {
 
 				// bail
@@ -153,7 +145,6 @@ THREE.DragControls = function ( _camera, _objects, _domElement ) {
 
 			}
 
-
 		}
 
 	}
@@ -201,8 +192,22 @@ THREE.DragControls = function ( _camera, _objects, _domElement ) {
 
 	}
 
+	activate();
+
+	// API
+
+	this.activate = activate;
+	this.deactivate = deactivate;
+	this.dispose = dispose;
+
 	// Backward compatibility
 
+	this.setObjects = function () {
+
+		console.error( 'THREE.DragControls: setObjects() has been removed.' );
+
+	};
+
 	this.on = function ( type, listener ) {
 
 		console.warn( 'THREE.DragControls: on() has been deprecated. Use addEventListener() instead.' );

+ 1 - 1
examples/webgl_geometry_spline_editor.html

@@ -166,7 +166,7 @@
 
 				} );
 
-				var dragcontrols = new THREE.DragControls( camera, splineHelperObjects, renderer.domElement ); //
+				var dragcontrols = new THREE.DragControls( splineHelperObjects, camera, renderer.domElement ); //
 
 				dragcontrols.addEventListener( 'hoveron', function ( event ) {
 

+ 3 - 4
examples/webgl_interactive_draggablecubes.html

@@ -102,10 +102,9 @@
 
 				container.appendChild( renderer.domElement );
 
-				var dragControls = new THREE.DragControls( camera, objects, renderer.domElement );
-				dragControls.enabled = true;
-				dragControls.addEventListener( 'dragstart', function ( e ) { controls.enabled = false; } );
-				dragControls.addEventListener( 'dragend', function ( e ) { controls.enabled = true; } );
+				var dragControls = new THREE.DragControls( objects, camera, renderer.domElement );
+				dragControls.addEventListener( 'dragstart', function ( event ) { controls.enabled = false; } );
+				dragControls.addEventListener( 'dragend', function ( event ) { controls.enabled = true; } );
 
 				var info = document.createElement( 'div' );
 				info.style.position = 'absolute';