Browse Source

some work to deal with structure changes directly done within prefabs

Nicolas Cannasse 4 years ago
parent
commit
8f8e25ea64
4 changed files with 53 additions and 15 deletions
  1. 2 2
      hide/Ide.hx
  2. 1 0
      hide/comp/SceneEditor.hx
  3. 8 6
      hide/comp/cdb/Editor.hx
  4. 42 7
      hide/comp/cdb/ObjEditor.hx

+ 2 - 2
hide/Ide.hx

@@ -714,7 +714,7 @@ class Ide {
 		}
 		}
 	}
 	}
 
 
-	public function saveDatabase() {
+	public function saveDatabase( ?forcePrefabs ) {
 		hide.comp.cdb.DataFiles.save(function() {
 		hide.comp.cdb.DataFiles.save(function() {
 			if( databaseDiff != null ) {
 			if( databaseDiff != null ) {
 				sys.io.File.saveContent(getPath(databaseDiff), toJSON(new cdb.DiffFile().make(originDataBase,database)));
 				sys.io.File.saveContent(getPath(databaseDiff), toJSON(new cdb.DiffFile().make(originDataBase,database)));
@@ -729,7 +729,7 @@ class Ide {
 				sys.io.File.saveContent(getPath(databaseFile), database.save());
 				sys.io.File.saveContent(getPath(databaseFile), database.save());
 				fileWatcher.ignorePrevChange(dbWatcher);
 				fileWatcher.ignorePrevChange(dbWatcher);
 			}
 			}
-		});
+		}, forcePrefabs);
 	}
 	}
 
 
 	public function createDBSheet( ?index : Int ) {
 	public function createDBSheet( ?index : Int ) {

+ 1 - 0
hide/comp/SceneEditor.hx

@@ -1464,6 +1464,7 @@ class SceneEditor {
 			var props = new hide.Element('<div></div>').appendTo(group.find(".content"));
 			var props = new hide.Element('<div></div>').appendTo(group.find(".content"));
 			var editor = new hide.comp.cdb.ObjEditor(curType, view.config, e.props, props);
 			var editor = new hide.comp.cdb.ObjEditor(curType, view.config, e.props, props);
 			editor.undo = properties.undo;
 			editor.undo = properties.undo;
+			editor.fileView = view;
 			editor.onChange = function(pname) {
 			editor.onChange = function(pname) {
 				edit.onChange(e, 'props.$pname');
 				edit.onChange(e, 'props.$pname');
 				var e = Std.instance(e, hrt.prefab.l3d.Instance);
 				var e = Std.instance(e, hrt.prefab.l3d.Instance);

+ 8 - 6
hide/comp/cdb/Editor.hx

@@ -396,7 +396,7 @@ class Editor extends Component {
 		Call before modifying the database, allow to group several changes together.
 		Call before modifying the database, allow to group several changes together.
 		Allow recursion, only last endChanges() will trigger db save and undo point creation.
 		Allow recursion, only last endChanges() will trigger db save and undo point creation.
 	**/
 	**/
-	public function beginChanges() {
+	public function beginChanges( ?structure : Bool ) {
 		if( changesDepth == 0 )
 		if( changesDepth == 0 )
 			undoState.unshift(getState());
 			undoState.unshift(getState());
 		changesDepth++;
 		changesDepth++;
@@ -645,7 +645,7 @@ class Editor extends Component {
 			var c = modal.getColumn(col);
 			var c = modal.getColumn(col);
 			if (c == null)
 			if (c == null)
 				return;
 				return;
-			beginChanges();
+			beginChanges(true);
 			var err;
 			var err;
 			if( col != null )
 			if( col != null )
 				err = base.updateColumn(sheet, col, c);
 				err = base.updateColumn(sheet, col, c);
@@ -734,11 +734,13 @@ class Editor extends Component {
 			}},
 			}},
 			{ label: "", isSeparator: true },
 			{ label: "", isSeparator: true },
 			{ label : "Delete", click : function () {
 			{ label : "Delete", click : function () {
-				beginChanges();
-				if( table.displayMode == Properties )
+				if( table.displayMode == Properties ) {
+					beginChanges();
 					changeObject(cell.line, col, base.getDefault(col,sheet));
 					changeObject(cell.line, col, base.getDefault(col,sheet));
-				else
+				} else {
+					beginChanges(true);
 					sheet.deleteColumn(col.name);
 					sheet.deleteColumn(col.name);
+				}
 				endChanges();
 				endChanges();
 				refresh();
 				refresh();
 			}}
 			}}
@@ -769,7 +771,7 @@ class Editor extends Component {
 			menu.insert(1,{ label : "Edit all", click : function() editScripts(table,col) });
 			menu.insert(1,{ label : "Edit all", click : function() editScripts(table,col) });
 		if( table.displayMode == Properties ) {
 		if( table.displayMode == Properties ) {
 			menu.push({ label : "Delete All", click : function() {
 			menu.push({ label : "Delete All", click : function() {
-				beginChanges();
+				beginChanges(true);
 				table.sheet.deleteColumn(col.name);
 				table.sheet.deleteColumn(col.name);
 				endChanges();
 				endChanges();
 				refresh();
 				refresh();

+ 42 - 7
hide/comp/cdb/ObjEditor.hx

@@ -2,29 +2,65 @@ package hide.comp.cdb;
 
 
 class ObjEditor extends Editor {
 class ObjEditor extends Editor {
 
 
-	public dynamic function onChange(propName : String) {}
+	public var fileView : hide.view.FileView;
 
 
 	var obj : {};
 	var obj : {};
+	var structureWasChanged = false;
 
 
 	public function new( sheet : cdb.Sheet, props, obj : {}, ?parent : Element ) {
 	public function new( sheet : cdb.Sheet, props, obj : {}, ?parent : Element ) {
 		this.displayMode = AllProperties;
 		this.displayMode = AllProperties;
 		this.obj = obj;
 		this.obj = obj;
+
+		// track changes in columns and props (structure changes made within local editor)
+		function makeStructSign() {
+			var sheets = [for( s in sheet.base.sheets ) Reflect.copy(@:privateAccess s.sheet)];
+			for( s in sheets ) {
+				s.separators = null;
+				s.lines = null;
+				s.linesData = null;
+			}
+			return haxe.crypto.Md5.encode(haxe.Serializer.run(sheets)).substr(0,16);
+		}
+
 		var api = {
 		var api = {
 			load : function(v:Any) {
 			load : function(v:Any) {
-				var obj2 = haxe.Json.parse((v:String));
+				var obj2 = haxe.Json.parse((v:String).substr(16));
 				for( f in Reflect.fields(obj) )
 				for( f in Reflect.fields(obj) )
 					Reflect.deleteField(obj,f);
 					Reflect.deleteField(obj,f);
 				for( f in Reflect.fields(obj2) )
 				for( f in Reflect.fields(obj2) )
 					Reflect.setField(obj, f, Reflect.field(obj2,f));
 					Reflect.setField(obj, f, Reflect.field(obj2,f));
 			},
 			},
-			copy : function() return (haxe.Json.stringify(obj) : Any),
-			save : function() throw "assert",
+			copy : function() return ((makeStructSign() + haxe.Json.stringify(obj)) : Any),
+			save : function() {
+				// allow save in case structure was changed
+				ide.saveDatabase(true);
+				if( structureWasChanged ) {
+					structureWasChanged = false;
+					haxe.Timer.delay(function() {
+						fileView.modified = false; // prevent message prompt
+						@:privateAccess fileView.onFileChanged(false);
+					},0);
+				}
+			}
 		};
 		};
 		super(props, api);
 		super(props, api);
 		sheet = makePseudoSheet(sheet);
 		sheet = makePseudoSheet(sheet);
 		show(sheet, parent);
 		show(sheet, parent);
 	}
 	}
 
 
+	override function beginChanges( ?structure ) {
+		if( structure && fileView != null ) {
+			/*
+				We are about to change structure, but our prefab will not see its data changed...
+				Let's save first our file and reload it in DataFiles so the changes gets applied to it
+			*/
+			fileView.save();
+			DataFiles.load();
+			structureWasChanged = true;
+		}
+		super.beginChanges(structure);
+	}
+
 	override function show(sheet:cdb.Sheet, ?parent:Element) {
 	override function show(sheet:cdb.Sheet, ?parent:Element) {
 		super.show(sheet, parent);
 		super.show(sheet, parent);
 		element.addClass("cdb-obj-editor");
 		element.addClass("cdb-obj-editor");
@@ -50,12 +86,11 @@ class ObjEditor extends Editor {
 		return s;
 		return s;
 	}
 	}
 
 
-	override function save() {
-	}
-
 	override function changeObject(line:Line, column:cdb.Data.Column, value:Dynamic) {
 	override function changeObject(line:Line, column:cdb.Data.Column, value:Dynamic) {
 		super.changeObject(line, column, value);
 		super.changeObject(line, column, value);
 		onChange(column.name);
 		onChange(column.name);
 	}
 	}
 
 
+	public dynamic function onChange(propName : String) {}
+
 }
 }