Browse Source

added snapping

Aleksandar Rodic 12 years ago
parent
commit
3c50a85ad5
3 changed files with 24 additions and 0 deletions
  1. 1 0
      editor/index.html
  2. 9 0
      editor/js/ui/Toolbar.js
  3. 14 0
      editor/js/ui/Viewport.js

+ 1 - 0
editor/index.html

@@ -139,6 +139,7 @@
 				// notifications
 
 				modifierAxisChanged: new SIGNALS.Signal(),
+				snapChanged: new SIGNALS.Signal(),
 				rendererChanged: new SIGNALS.Signal(),
 				sceneChanged: new SIGNALS.Signal(),
 				objectAdded: new SIGNALS.Signal(),

+ 9 - 0
editor/js/ui/Toolbar.js

@@ -8,6 +8,13 @@ var Toolbar = function ( signals ) {
 	buttons.setPadding( '7px' );
 	container.add( buttons );
 
+	var snap = new UI.Checkbox( true ).onChange( update );
+	buttons.add( snap );
+	buttons.add( new UI.Text( 'snap: ' ) );
+	var snapDist = new UI.Number( 25 ).onChange( update );
+	snapDist.dom.style.width = '42px';
+	buttons.add( snapDist );
+
 	var x = new UI.Checkbox( true ).onChange( update );
 	buttons.add( x );
 	buttons.add( new UI.Text( 'x' ) );
@@ -29,6 +36,8 @@ var Toolbar = function ( signals ) {
 
 		signals.modifierAxisChanged.dispatch( axis );
 
+		signals.snapChanged.dispatch( snap.getValue() === true ? snapDist.getValue() : null );
+
 	}
 
 	return container;

+ 14 - 0
editor/js/ui/Viewport.js

@@ -26,6 +26,7 @@ var Viewport = function ( signals ) {
 	sceneHelpers.add( grid );
 
 	var modifierAxis = new THREE.Vector3( 1, 1, 1 );
+	var snapDist = null;
 
 	var selectionBox = new THREE.BoxHelper();
 	selectionBox.material.color.setHex( 0xffff00 );
@@ -146,10 +147,17 @@ var Viewport = function ( signals ) {
 
 			var point = intersects[ 0 ].point.sub( offset );
 
+			if (snapDist) {
+				point.x = point.x - point.x % snapDist;
+				point.y = point.y - point.y % snapDist;
+				point.z = point.z - point.z % snapDist;
+			}
+
 			selected.position.x = modifierAxis.x === 1 ? point.x : intersectionPlane.position.x;
 			selected.position.y = modifierAxis.y === 1 ? point.y : intersectionPlane.position.y;
 			selected.position.z = modifierAxis.z === 1 ? point.z : intersectionPlane.position.z;
 
+
 			signals.objectChanged.dispatch( selected );
 
 			render();
@@ -227,6 +235,12 @@ var Viewport = function ( signals ) {
 
 	} );
 
+	signals.snapChanged.add( function ( dist ) {
+
+		snapDist = dist;
+
+	} );
+
 	signals.rendererChanged.add( function ( object ) {
 
 		container.dom.removeChild( renderer.domElement );