فهرست منبع

material save / undo / reset ok

Nicolas Cannasse 8 سال پیش
والد
کامیت
5bd08bc5ee
4فایلهای تغییر یافته به همراه51 افزوده شده و 12 حذف شده
  1. 4 1
      hide/comp/IconTree.hx
  2. 20 6
      hide/comp/PropsEditor.hx
  3. 1 0
      hide/comp/SceneTree.hx
  4. 26 5
      hide/view/Model.hx

+ 4 - 1
hide/comp/IconTree.hx

@@ -43,7 +43,10 @@ class IconTree extends Component {
 					for( c in content )
 						if( c.state == null ) {
 							var s = getDisplayState((parent == null ? "" : parent + "/") + c.id);
-							if( s ) c.state = { opened : true };
+							if( s != null ) {
+								if( c.state == null ) c.state = {};
+								c.state.opened = s;
+							}
 						}
 					callb.call(this,content);
 				}

+ 20 - 6
hide/comp/PropsEditor.hx

@@ -20,16 +20,18 @@ class PropsEditor extends Component {
 	public function addMaterial( m : h3d.mat.Material, ?parent : Element ) {
 		var props = m.props;
 		var def = h3d.mat.MaterialSetup.current.editMaterial(props);
-		def = add(def, props);
-		def.find("input,select").change(function(_) {
+		def = add(def, props, function(undo) {
+			if( m.model != null )
+				h3d.mat.MaterialSetup.current.saveModelMaterial(m);
 			m.refreshProps();
 			def.remove();
 			addMaterial(m, parent);
 		});
-		if( parent != null && parent.length != 0 ) def.appendTo(parent);
+		if( parent != null && parent.length != 0 )
+			def.appendTo(parent);
 	}
 
-	public function add( e : Element, ?context : Dynamic ) {
+	public function add( e : Element, ?context : Dynamic, ?onChange ) {
 
 		e.appendTo(root);
 		e = e.wrap("<div></div>").parent(); // necessary to have find working on top level element
@@ -80,8 +82,11 @@ class PropsEditor extends Component {
 		}).find("input").mousedown(function(e) e.stopPropagation());
 
 		// init input reflection
-		for( f in e.find("[field]").elements() )
-			fields.push(new PropsField(this,f,context));
+		for( f in e.find("[field]").elements() ) {
+			var f = new PropsField(this, f, context);
+			if( onChange != null ) f.onChange = onChange;
+			fields.push(f);
+		}
 
 		return e;
 	}
@@ -117,9 +122,11 @@ class PropsField extends Component {
 					var f = resolveField();
 					f.current = Reflect.field(f.context, fname);
 					f.root.prop("checked", f.current);
+					f.onChange(true);
 				});
 				current = f.prop("checked");
 				Reflect.setProperty(context, fname, current);
+				onChange(false);
 			});
 			return;
 		case "texture":
@@ -130,9 +137,11 @@ class PropsField extends Component {
 					var f = resolveField();
 					f.current = Reflect.field(f.context, fname);
 					f.tselect.value = f.current;
+					f.onChange(true);
 				});
 				current = tselect.value;
 				Reflect.setProperty(context, fname, current);
+				onChange(false);
 			}
 			return;
 		default:
@@ -190,13 +199,18 @@ class PropsField extends Component {
 					var f = resolveField();
 					f.current = Reflect.field(f.context, fname);
 					f.root.val(f.current);
+					f.onChange(true);
 				});
 			}
 			current = newVal;
 			Reflect.setProperty(context, fname, newVal);
+			onChange(false);
 		});
 	}
 
+	public dynamic function onChange( wasUndo : Bool ) {
+	}
+
 	function resolveField() {
 		/*
 			If our panel has been removed but another bound to the same object has replaced it (a refresh for instance)

+ 1 - 0
hide/comp/SceneTree.hx

@@ -75,6 +75,7 @@ class SceneTree extends IconTree {
 					text : c.name == null ? c.toString()+"@"+i : c.name,
 					icon : "fa fa-" + getIcon(c),
 					children : c.isMesh() || c.numChildren > 0,
+					state : { opened : c.numChildren > 0 }
 				}
 			}
 		];

+ 26 - 5
hide/view/Model.hx

@@ -29,13 +29,37 @@ class Model extends FileView {
 		');
 		tools = new hide.comp.Toolbar(root.find(".toolbar"));
 		overlay = root.find(".hide-scene-layer .tree");
-		properties = new hide.comp.PropsEditor(root.find(".props"));
+		properties = new hide.comp.PropsEditor(root.find(".props"), undo);
 		properties.saveDisplayKey = "Model";
 		scene = new hide.comp.Scene(root.find(".scene"));
 		scene.onReady = init;
 	}
 
+	function selectMaterial( m : h3d.mat.Material ) {
+		properties.clear();
+		var e = properties.add(new Element('
+			<div class="group" name="${m.name}">
+			</div>
+			<dl>
+				<dt></dt><dd><input type="button" value="Reset Defaults" class="reset"/></dd>
+			</dl>
+		'));
+		properties.addMaterial(m, e.find(".group > .content"));
+		e.find(".reset").click(function(_) {
+			var cur = h3d.mat.MaterialSetup.current;
+			var old = m.props;
+			m.props = null;
+			cur.saveModelMaterial(m);
+			cur.initModelMaterial(m);
+			selectMaterial(m);
+			undo.change(Field(m, "props", old), selectMaterial.bind(m));
+		});
+	}
+
 	function init() {
+
+		undo.onChange = function() {};
+
 		obj = scene.loadModel(state.path);
 		new h3d.scene.Object(scene.s3d).addChild(obj);
 
@@ -47,10 +71,7 @@ class Model extends FileView {
 
 		control = new h3d.scene.CameraController(scene.s3d);
 		tree = new hide.comp.SceneTree(obj, overlay, obj.name != null);
-		tree.onSelectMaterial = function(m) {
-			properties.clear();
-			properties.addMaterial(m);
-		}
+		tree.onSelectMaterial = selectMaterial;
 
 		this.saveDisplayKey = "Model:"+state.path;
 		var cam = getDisplayState("Camera");