Explorar el Código

Level3D: Support undo. Fix Range scaling

trethaller hace 7 años
padre
commit
3f54533eaf
Se han modificado 3 ficheros con 58 adiciones y 21 borrados
  1. 12 11
      hide/comp/Range.hx
  2. 1 1
      hide/prefab/Object3D.hx
  3. 45 9
      hide/view/Level3D.hx

+ 12 - 11
hide/comp/Range.hx

@@ -50,30 +50,31 @@ class Range extends Component {
 
 		f.on("input", function(_) {
 			var v = Math.round(Std.parseFloat(f.val()) * 100 * scale) / 100;
-			inputView.val(v);
-			current = v * scale;
+			setInner(v);
+			inputView.val(current / scale);
+			f.val(current / scale);
 			onChange(true);
 		});
 		inputView.keyup(function(e) {
 			if( e.keyCode == 13 || e.keyCode == 27 ) {
 				inputView.blur();
-				inputView.val(current * scale);
+				inputView.val(current / scale);
 				return;
 			}
-			var v = Std.parseFloat(inputView.val()) / scale;
+			var v = Std.parseFloat(inputView.val()) * scale;
 			if( Math.isNaN(v) ) return;
 			setInner(v);
-			f.val(v);
+			f.val(v / scale);
 			onChange(false);
 		});
 
-		f.val(current);
-		inputView.val(current);
+		f.val(current / scale);
+		inputView.val(current / scale);
 
 		f.change(function(e) {
 			var v = Math.round(Std.parseFloat(f.val()) * 100 * scale) / 100;
-			setInner(v/scale);
-			inputView.val(v);
+			setInner(v);
+			inputView.val(current / scale);
 			onChange(false);
 		});
 	}
@@ -82,8 +83,8 @@ class Range extends Component {
 		if( original == null ) original = v;
 		setInner(v);
 		current = v;
-		inputView.val(current * scale);
-		f.val(current);
+		inputView.val(current / scale);
+		f.val(current / scale);
 		return v;
 	}
 

+ 1 - 1
hide/prefab/Object3D.hx

@@ -44,7 +44,7 @@ class Object3D extends Prefab {
 		return o;
 	}
 
-	function applyPos( o : h3d.scene.Object ) {
+	public function applyPos( o : h3d.scene.Object ) {
 		o.x = x;
 		o.y = y;
 		o.z = z;

+ 45 - 9
hide/view/Level3D.hx

@@ -301,6 +301,13 @@ class Level3D extends FileView {
 		setupGizmo();
 	}
 
+	function refreshProps() {
+		properties.clear();
+		if(curEdit != null && curEdit.elements != null && curEdit.elements.length > 0) {
+			curEdit.elements[0].edit(curEdit);
+		}
+	}
+
 	function setupGizmo() {
 		if(curEdit == null) return;
 		gizmo.startMove = function() {
@@ -317,6 +324,9 @@ class Level3D extends FileView {
 				m.multiply(m, invPivot);
 				m;
 			}];
+
+			var objects3d = [for(e in curEdit.elements) Std.instance(e, hide.prefab.Object3D)];
+			var prevState = [for(o in objects3d) o.save()];			
 			
 			gizmo.onMove = function(translate: h3d.Vector, rot: h3d.Quat) {
 				var transf = new h3d.Matrix();
@@ -326,19 +336,45 @@ class Level3D extends FileView {
 					var newMat = localMats[i].clone();
 					newMat.multiply(newMat, transf);
 					newMat.multiply(newMat, pivot);
-					var obj = objects[i];
-					obj.x = newMat.tx;
-					obj.y = newMat.ty;
-					obj.z = newMat.tz;
-					var q = new h3d.Quat();
-					q.initRotateMatrix(newMat);
-					q.normalize();
-					obj.setRotationQuat(q);
+					// var obj = objects[i];
+					// obj.x = newMat.tx;
+					// obj.y = newMat.ty;
+					// obj.z = newMat.tz;
+					// var q = new h3d.Quat();
+					// q.initRotateMatrix(newMat);
+					// q.normalize();
+
+					var rot = newMat.getEulerAngles();
+					var obj3d = objects3d[i];
+					obj3d.x = newMat.tx;
+					obj3d.y = newMat.ty;
+					obj3d.z = newMat.tz;
+					obj3d.rotationX = rot.x;
+					obj3d.rotationY = rot.y;
+					obj3d.rotationZ = rot.z;
+					obj3d.applyPos(objects[i]);
 				}
 			}
 
 			gizmo.finishMove = function() {
-
+				var newState = [for(o in objects3d) o.save()];
+				refreshProps();
+				undo.change(Custom(function(undo) {
+					if( undo ) {
+						for(i in 0...objects3d.length) {
+							objects3d[i].load(prevState[i]);
+							objects3d[i].applyPos(objects[i]);
+						}
+						refreshProps();
+					}
+					else {
+						for(i in 0...objects3d.length) {
+							objects3d[i].load(newState[i]);
+							objects3d[i].applyPos(objects[i]);
+						}
+						refreshProps();
+					}
+				}));
 			}
 		}
 	}