Sfoglia il codice sorgente

some work to deal with structure changes directly done within prefabs

Nicolas Cannasse 4 anni fa
parent
commit
8f8e25ea64
4 ha cambiato i file con 53 aggiunte e 15 eliminazioni
  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() {
 			if( databaseDiff != null ) {
 				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());
 				fileWatcher.ignorePrevChange(dbWatcher);
 			}
-		});
+		}, forcePrefabs);
 	}
 
 	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 editor = new hide.comp.cdb.ObjEditor(curType, view.config, e.props, props);
 			editor.undo = properties.undo;
+			editor.fileView = view;
 			editor.onChange = function(pname) {
 				edit.onChange(e, 'props.$pname');
 				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.
 		Allow recursion, only last endChanges() will trigger db save and undo point creation.
 	**/
-	public function beginChanges() {
+	public function beginChanges( ?structure : Bool ) {
 		if( changesDepth == 0 )
 			undoState.unshift(getState());
 		changesDepth++;
@@ -645,7 +645,7 @@ class Editor extends Component {
 			var c = modal.getColumn(col);
 			if (c == null)
 				return;
-			beginChanges();
+			beginChanges(true);
 			var err;
 			if( col != null )
 				err = base.updateColumn(sheet, col, c);
@@ -734,11 +734,13 @@ class Editor extends Component {
 			}},
 			{ label: "", isSeparator: true },
 			{ label : "Delete", click : function () {
-				beginChanges();
-				if( table.displayMode == Properties )
+				if( table.displayMode == Properties ) {
+					beginChanges();
 					changeObject(cell.line, col, base.getDefault(col,sheet));
-				else
+				} else {
+					beginChanges(true);
 					sheet.deleteColumn(col.name);
+				}
 				endChanges();
 				refresh();
 			}}
@@ -769,7 +771,7 @@ class Editor extends Component {
 			menu.insert(1,{ label : "Edit all", click : function() editScripts(table,col) });
 		if( table.displayMode == Properties ) {
 			menu.push({ label : "Delete All", click : function() {
-				beginChanges();
+				beginChanges(true);
 				table.sheet.deleteColumn(col.name);
 				endChanges();
 				refresh();

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

@@ -2,29 +2,65 @@ package hide.comp.cdb;
 
 class ObjEditor extends Editor {
 
-	public dynamic function onChange(propName : String) {}
+	public var fileView : hide.view.FileView;
 
 	var obj : {};
+	var structureWasChanged = false;
 
 	public function new( sheet : cdb.Sheet, props, obj : {}, ?parent : Element ) {
 		this.displayMode = AllProperties;
 		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 = {
 			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) )
 					Reflect.deleteField(obj,f);
 				for( f in Reflect.fields(obj2) )
 					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);
 		sheet = makePseudoSheet(sheet);
 		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) {
 		super.show(sheet, parent);
 		element.addClass("cdb-obj-editor");
@@ -50,12 +86,11 @@ class ObjEditor extends Editor {
 		return s;
 	}
 
-	override function save() {
-	}
-
 	override function changeObject(line:Line, column:cdb.Data.Column, value:Dynamic) {
 		super.changeObject(line, column, value);
 		onChange(column.name);
 	}
 
+	public dynamic function onChange(propName : String) {}
+
 }