Przeglądaj źródła

added key events

ncannasse 10 lat temu
rodzic
commit
2bde101ef8
2 zmienionych plików z 98 dodań i 57 usunięć
  1. 36 10
      hxd/net/PropInspector.hx
  2. 62 47
      hxd/net/SceneInspector.hx

+ 36 - 10
hxd/net/PropInspector.hx

@@ -44,6 +44,20 @@ class PropInspector extends cdb.jq.Client {
 		connect();
 		connect();
 	}
 	}
 
 
+	override function onKey(e:cdb.jq.Event) {
+		switch( e.keyCode ) {
+		case 'Z'.code if( e.ctrlKey ):
+			undo();
+		case 'Y'.code if( e.ctrlKey ):
+			redo();
+		default:
+			handleKey(e);
+		}
+	}
+
+	public dynamic function handleKey( e : cdb.jq.Event ) {
+	}
+
 	function connect() {
 	function connect() {
 		sock.close();
 		sock.close();
 		sock.connect(host, port, function() {
 		sock.connect(host, port, function() {
@@ -152,6 +166,8 @@ class PropInspector extends cdb.jq.Client {
 		case PTexture(_, _, set):
 		case PTexture(_, _, set):
 			if( !Std.is(v, String) ) throw "Invalid texture value " + v;
 			if( !Std.is(v, String) ) throw "Invalid texture value " + v;
 			var path : String = v;
 			var path : String = v;
+			if( path.charCodeAt(0) != '/'.code && path.charCodeAt(1) != ':'.code )
+				path = hxd.File.applicationPath() + path;
 			hxd.File.load(path, function(data) set( hxd.res.Any.fromBytes(path, data).toTexture() ));
 			hxd.File.load(path, function(data) set( hxd.res.Any.fromBytes(path, data).toTexture() ));
 		case PGroup(_), PPopup(_):
 		case PGroup(_), PPopup(_):
 			throw "Cannot set property " + p.getName();
 			throw "Cannot set property " + p.getName();
@@ -393,7 +409,7 @@ class PropInspector extends cdb.jq.Client {
 			});
 			});
 			init();
 			init();
 		case PTexture(_, get, set):
 		case PTexture(_, get, set):
-			var path = null;
+			var filePath = null;
 			var isLoaded = false;
 			var isLoaded = false;
 			function init() {
 			function init() {
 				var t = get();
 				var t = get();
@@ -407,16 +423,16 @@ class PropInspector extends cdb.jq.Client {
 					// resolve path
 					// resolve path
 					var lfs = Std.instance(hxd.res.Loader.currentInstance.fs, hxd.fs.LocalFileSystem);
 					var lfs = Std.instance(hxd.res.Loader.currentInstance.fs, hxd.fs.LocalFileSystem);
 					if( lfs != null )
 					if( lfs != null )
-						path = lfs.baseDir + res.entry.path;
+						filePath = lfs.baseDir + res.entry.path;
 					else {
 					else {
 						var resPath = haxe.macro.Compiler.getDefine("resPath");
 						var resPath = haxe.macro.Compiler.getDefine("resPath");
 						if( resPath == null ) resPath = "res";
 						if( resPath == null ) resPath = "res";
-						path = hxd.File.applicationPath() + resPath + "/" + res.entry.path;
+						filePath = hxd.File.applicationPath() + resPath + "/" + res.entry.path;
 					}
 					}
 				} else if( t != null && t.name != null && (t.name.charCodeAt(0) == '/'.code || t.name.charCodeAt(1) == ':'.code) )
 				} else if( t != null && t.name != null && (t.name.charCodeAt(0) == '/'.code || t.name.charCodeAt(1) == ':'.code) )
-					path = t.name;
+					filePath = t.name;
 
 
-				if( path == null ) {
+				if( filePath == null ) {
 					if( t == null )
 					if( t == null )
 						jprop.text("");
 						jprop.text("");
 					else {
 					else {
@@ -434,19 +450,29 @@ class PropInspector extends cdb.jq.Client {
 						});
 						});
 					}
 					}
 				} else
 				} else
-					jprop.html('<img src="file://$path"/>');
+					jprop.html('<img src="file://$filePath"/>');
 			}
 			}
 			init();
 			init();
+
+			function relPath(path) {
+				var base = hxd.File.applicationPath();
+				if( StringTools.startsWith(path, base) )
+					return path.substr(base.length);
+				return path;
+			}
+
 			jprop.dblclick(function(_) {
 			jprop.dblclick(function(_) {
-				jprop.special("fileSelect", [path, "png,jpg,jpeg,gif"], function(path) {
+				jprop.special("fileSelect", [filePath, "png,jpg,jpeg,gif"], function(newPath) {
 
 
-					if( path == null ) return;
+					if( newPath == null ) return;
 
 
-					hxd.File.load(path, function(data) {
+					hxd.File.load(newPath, function(data) {
 						if( isLoaded ) get().dispose();
 						if( isLoaded ) get().dispose();
 						isLoaded = true;
 						isLoaded = true;
-						set( hxd.res.Any.fromBytes(path, data).toTexture() );
+						set( hxd.res.Any.fromBytes(newPath, data).toTexture() );
+						addHistory(path, relPath(filePath), relPath(newPath));
 						init();
 						init();
+						filePath = newPath;
 					});
 					});
 
 
 				});
 				});

+ 62 - 47
hxd/net/SceneInspector.hx

@@ -61,12 +61,23 @@ class SceneInspector {
 		inspect.resolveProps = resolveProps;
 		inspect.resolveProps = resolveProps;
 		inspect.onRefresh = refresh;
 		inspect.onRefresh = refresh;
 		inspect.onChange = onChange;
 		inspect.onChange = onChange;
+		inspect.handleKey = onKey;
 	}
 	}
 
 
 	inline function J( ?elt : cdb.jq.Dom, ?query : String ) {
 	inline function J( ?elt : cdb.jq.Dom, ?query : String ) {
 		return inspect.J(elt,query);
 		return inspect.J(elt,query);
 	}
 	}
 
 
+	function onKey( e : cdb.jq.Event ) {
+		switch( e.keyCode ) {
+		case 'S'.code if( e.ctrlKey ):
+			save();
+		case hxd.Key.F1:
+			load();
+		default:
+		}
+	}
+
 	function onTrace( v : Dynamic, ?pos : haxe.PosInfos ) {
 	function onTrace( v : Dynamic, ?pos : haxe.PosInfos ) {
 		if( !inspect.connected )
 		if( !inspect.connected )
 			oldLog(v, pos);
 			oldLog(v, pos);
@@ -133,57 +144,61 @@ class SceneInspector {
 		});
 		});
 		j.find("#history-undo").click(function(_) inspect.undo());
 		j.find("#history-undo").click(function(_) inspect.undo());
 		j.find("#history-redo").click(function(_) inspect.redo());
 		j.find("#history-redo").click(function(_) inspect.redo());
-		j.find("#state-save").click(function(_) {
-			var o : Dynamic = { };
-			for( s in state.keys() ) {
-				var path = s.split(".");
-				var o = o;
-				while( path.length > 1 ) {
-					var name = path.shift();
-					var s = Reflect.field(o, name);
-					if( s == null ) {
-						s = { };
-						Reflect.setField(o, name, s);
-					}
-					o = s;
-				}
-				Reflect.setField(o, path[0], state.get(s).current);
-			}
-			var js = haxe.Json.stringify(o, null, "\t");
-			hxd.File.saveAs(haxe.io.Bytes.ofString(js), { defaultPath : savedFile, saveFileName : function(name) savedFile = name } );
-		});
-		j.find("#state-load").click(function(_) {
-			hxd.File.browse(function(b) {
-				savedFile = b.fileName;
-				b.load(function(bytes) {
-					var o : Dynamic = haxe.Json.parse(bytes.toString());
-					state = new Map();
-					function browseRec( path : Array<String>, v : Dynamic ) {
-						switch( Type.typeof(v) ) {
-						case TNull, TInt, TFloat, TBool, TClass(_):
-							var path = path.join(".");
-							state.set(path, { original : null, current : v });
-						case TUnknown, TFunction, TEnum(_):
-							throw "Invalid value " + v;
-						case TObject:
-							for( f in Reflect.fields(v) ) {
-								var fv = Reflect.field(v, f);
-								path.push(f);
-								browseRec(path, fv);
-								path.pop();
-							}
+		j.find("#state-save").click(function(_) save());
+		j.find("#state-load").click(function(_) load());
+
+		J("#log").dock(j.get(), Down, 0.3);
+		J("#props").dock(scene.get(), Down, 0.5);
+	}
+
+	function load() {
+		hxd.File.browse(function(b) {
+			savedFile = b.fileName;
+			b.load(function(bytes) {
+				var o : Dynamic = haxe.Json.parse(bytes.toString());
+				state = new Map();
+				function browseRec( path : Array<String>, v : Dynamic ) {
+					switch( Type.typeof(v) ) {
+					case TNull, TInt, TFloat, TBool, TClass(_):
+						var path = path.join(".");
+						state.set(path, { original : null, current : v });
+					case TUnknown, TFunction, TEnum(_):
+						throw "Invalid value " + v;
+					case TObject:
+						for( f in Reflect.fields(v) ) {
+							var fv = Reflect.field(v, f);
+							path.push(f);
+							browseRec(path, fv);
+							path.pop();
 						}
 						}
 					}
 					}
-					browseRec([], o);
-					for( s in state.keys() )
-						inspect.setPathPropValue(s, state.get(s).current);
-				});
+				}
+				browseRec([], o);
+				for( s in state.keys() )
+					inspect.setPathPropValue(s, state.get(s).current);
+			});
 
 
-			},{ defaultPath : savedFile, fileTypes : [ { name:"Scene Props", extensions:["js"] } ] } );
-		});
+		},{ defaultPath : savedFile, fileTypes : [ { name:"Scene Props", extensions:["js"] } ] } );
+	}
 
 
-		J("#log").dock(j.get(), Down, 0.3);
-		J("#props").dock(scene.get(), Down, 0.5);
+	function save() {
+		var o : Dynamic = { };
+		for( s in state.keys() ) {
+			var path = s.split(".");
+			var o = o;
+			while( path.length > 1 ) {
+				var name = path.shift();
+				var s = Reflect.field(o, name);
+				if( s == null ) {
+					s = { };
+					Reflect.setField(o, name, s);
+				}
+				o = s;
+			}
+			Reflect.setField(o, path[0], state.get(s).current);
+		}
+		var js = haxe.Json.stringify(o, null, "\t");
+		hxd.File.saveAs(haxe.io.Bytes.ofString(js), { defaultPath : savedFile, saveFileName : function(name) savedFile = name } );
 	}
 	}
 
 
 	function addElement( name : String, icon : String, getProps : Void -> Array<Property> ) {
 	function addElement( name : String, icon : String, getProps : Void -> Array<Property> ) {