Browse Source

Replaced all editor.refresh from prefabs with editor.queueReload

Clément Espeute 1 year ago
parent
commit
c1c0220b26

+ 53 - 36
hide/comp/SceneEditor.hx

@@ -363,10 +363,7 @@ class SceneEditorContext extends hide.prefab.EditContext {
 	}
 
 	override function rebuildPrefab( p : hrt.prefab.Prefab, ?sceneOnly : Bool) {
-		if(sceneOnly)
-			editor.refreshScene();
-		else
-			editor.refresh();
+		editor.queueRebuild(p);
 	}
 
 	public function cleanup() {
@@ -812,7 +809,8 @@ class CustomEditor {
 	}
 
 	function refresh( ?callb: Void->Void ) {
-		editor.refresh(Full, callb);
+		editor.queueRebuild(editor.sceneData);
+		//editor.refresh(Full, callb);
 	}
 
 	function show( elt : hide.Element ) {
@@ -2830,14 +2828,10 @@ class SceneEditor {
 	}
 
 	function makePrefab(elt: PrefabElement) {
-		if (elt == sceneData) {
-			refreshScene();
-			return;
-		}
 		scene.setCurrent();
 
-		elt.shared.current3d = elt.parent.findFirstLocal3d();
-		elt.shared.current2d = elt.parent.findFirstLocal2d();
+		elt.shared.current3d = elt.parent?.findFirstLocal3d() ?? root3d;
+		elt.shared.current2d = elt.parent?.findFirstLocal2d() ?? root2d;
 		elt.setEditor(this, this.scene);
 		elt.make();
 
@@ -3928,7 +3922,9 @@ class SceneEditor {
 			var index = elt.parent.children.indexOf(elt);
 			removeInstance(elt);
 			parent.children.remove(elt);
-			refreshTree(() -> selectElements(selectedPrefabs, NoHistory));
+			if (doRefresh) {
+				refreshTree(() -> selectElements(selectedPrefabs, NoHistory));
+			}
 
 			undoes.unshift(function(undo) {
 				if(undo) elt.parent.children.insert(index, elt);
@@ -3948,6 +3944,9 @@ class SceneEditor {
 				if(undo)
 					for(e in elts) rebuild(e);
 				endRebuild();
+				if (doRefresh) {
+					refreshTree(() -> selectElements(selectedPrefabs, NoHistory));
+				}
 			}));
 		}
 	}
@@ -4126,6 +4125,8 @@ class SceneEditor {
 			}
 
 			endRebuild();
+
+			refreshTree(() -> selectElements(selectedPrefabs, NoHistory));
 		}
 
 		return exec;
@@ -4133,15 +4134,20 @@ class SceneEditor {
 
 	function checkWantRebuild(target: PrefabElement, original: PrefabElement) {
 		if (target == null) return;
-		if (target.onEditorChildRebuild(original)) {
-			queueRebuild(target);
-			return;
+		var wantRebuild = target.onEditorTreeChanged(original);
+		switch(wantRebuild) {
+			case Skip:
+				checkWantRebuild(target.parent, original);
+			case Rebuild:
+				queueRebuild(target);
+			case Notify(callback):
+				rebuildQueue.set(target, wantRebuild);
+				checkWantRebuild(target.parent, original);
 		}
-		checkWantRebuild(target.parent, original);
 	}
 
-	var rebuildQueue : Map<PrefabElement, Bool> = [];
-	function queueRebuild(prefab: PrefabElement) {
+	var rebuildQueue : Map<PrefabElement, hrt.prefab.Prefab.TreeChangedResult> = [];
+	public function queueRebuild(prefab: PrefabElement) {
 		if (rebuildQueue != null && rebuildQueue.exists(prefab))
 			return;
 
@@ -4154,7 +4160,7 @@ class SceneEditor {
 		var parent = prefab.parent;
 		checkWantRebuild(parent, prefab);
 
-		rebuildQueue.set(prefab, true);
+		rebuildQueue.set(prefab, Rebuild);
 		if (instant) {
 			endRebuild();
 		}
@@ -4165,27 +4171,38 @@ class SceneEditor {
 	}
 
 	function endRebuild() {
-		for (prefab => _ in rebuildQueue) {
-			var parent = prefab.parent;
-			var skip = false;
-
-			// don't rebuild this prefab if it's parent will get rebuild anyways
-			while(parent != null) {
-				if (rebuildQueue.exists(parent)) {
-					skip = true;
-					break;
-				}
-				parent = parent.parent;
-			}
+		var notifyList : Array<Void -> Void> = [];
 
-			if (skip == true)
-				continue;
+		for (prefab => want in rebuildQueue) {
+			switch (want) {
+				case Skip:
+					continue;
+				case Notify(callback):
+					notifyList.push(callback);
+				case Rebuild:
+					var parent = prefab.parent;
+					var skip = false;
+
+					// don't rebuild this prefab if it's parent will get rebuild anyways
+					while(parent != null) {
+						if (rebuildQueue.get(parent) == Rebuild) {
+							skip = true;
+							break;
+						}
+						parent = parent.parent;
+					}
 
-			rebuild(prefab);
+					if (skip == true)
+						continue;
+
+					rebuild(prefab);
+			}
 		}
-		refreshTree(() -> selectElements(selectedPrefabs, NoHistory));
 
-		rebuildQueue = [];
+		for (callback in notifyList) {
+			callback();
+		}
+		rebuildQueue = null;
 	}
 
 	function rebuild(prefab: PrefabElement) {

+ 5 - 4
hide/prefab/PolygonEditor.hx

@@ -720,12 +720,12 @@ class PolygonEditor {
 
 				refreshPolygon();
 				refreshInteractive();
-				ctx.scene.editor.refresh();
+				ctx.scene.editor.queueRebuild(polygonPrefab);
 			}));
 
 			refreshPolygon();
 			refreshInteractive();
-			ctx.scene.editor.refresh();
+			ctx.scene.editor.queueRebuild(polygonPrefab);
 		});
 
 		props.find(".snap").click(function(_) {
@@ -775,7 +775,7 @@ class PolygonEditor {
 
 			refreshPolygon();
 			refreshInteractive();
-			ctx.scene.editor.refresh();
+			ctx.scene.editor.queueRebuild(polygonPrefab);
 
 			undo.change(Custom(function(undo) {
 				var undoDelta = deltaChildSpace.clone();
@@ -826,7 +826,8 @@ class PolygonEditor {
 
 				refreshPolygon();
 				refreshInteractive();
-				ctx.scene.editor.refresh();
+							ctx.scene.editor.queueRebuild(polygonPrefab);
+
 			}));
 		});
 

+ 5 - 6
hide/view/FXEditor.hx

@@ -746,12 +746,12 @@ class FXEditor extends hide.view.FileView {
 				}
 			}
 
-			sceneEditor.refresh();
+			sceneEditor.queueRebuild(@:privateAccess sceneEditor.sceneData);
 			rebuildAnimPanel();
 		}
 
 		if (pname == "blendParam") {
-			sceneEditor.refresh();
+			sceneEditor.queueRebuild(@:privateAccess sceneEditor.sceneData);
 			rebuildAnimPanel();
 		}
 
@@ -1379,11 +1379,10 @@ class FXEditor extends hide.view.FileView {
 				else
 					element.children.push(c);
 			}
-			sceneEditor.refresh();
+			sceneEditor.queueRebuild(@:privateAccess sceneEditor.sceneData);
 		}));
-		sceneEditor.refresh(function() {
-			sceneEditor.selectElements([element]);
-		});
+		sceneEditor.queueRebuild(@:privateAccess sceneEditor.sceneData);
+		sceneEditor.selectElements([element]);
 		return added;
 	}
 

+ 4 - 2
hrt/prefab/Material.hx

@@ -254,6 +254,8 @@ class Material extends Prefab {
 			previewSphere.remove();
 			return true;
 		}
+		// temporary untill we find a proper way to remove a material
+		shared.editor.queueRebuild(parent);
 		return false;
 	}
 
@@ -604,11 +606,11 @@ class Material extends Prefab {
 				materialName = undo ? previous : actual;
 				ctx.onChange(this, null);
 				ctx.rebuildProperties();
-				ctx.scene.editor.refresh();
+				ctx.scene.editor.queueRebuild(this);
 			}));
 			ctx.onChange(this, null);
 			ctx.rebuildProperties();
-			ctx.scene.editor.refresh();
+			ctx.scene.editor.queueRebuild(this);
 
 			var fx = findParent(hrt.prefab.fx.FX);
 			if(fx != null)

+ 11 - 4
hrt/prefab/Prefab.hx

@@ -35,6 +35,14 @@ abstract ContextMake(ContextShared) from ContextShared to ContextShared {
 	}
 }
 
+#if editor
+enum TreeChangedResult {
+	Skip; /**Don't rebuild this prefab**/
+	Rebuild; /** Force rebuild this prefab **/
+	Notify(callback: Void -> Void); /**Call the callback once all the prefab that wanted rebuild have been rebuild. Call order betwteen multiple Notify are not guaranteed. Only one callback will be called by prefab in the tree**/
+}
+#end
+
 @:allow(hide)
 @:keepSub
 @:autoBuild(hrt.prefab.Macros.buildPrefab())
@@ -584,11 +592,10 @@ class Prefab {
 	}
 
 	/**
-		Called by the editor when a child of this object gets build or rebuilded.
-		If this function returns true, this prefab will get rebuild as well.
+		Called by the editor when a child of this object gets added, rebuild or removed.
 	**/
-	public function onEditorChildRebuild(child: Prefab) : Bool {
-		return false;
+	public function onEditorTreeChanged(child: Prefab) : TreeChangedResult {
+		return Skip;
 	}
 
 	/**

+ 5 - 0
hrt/prefab/Shader.hx

@@ -158,6 +158,11 @@ class Shader extends Prefab {
 
 	#if editor
 
+	override function editorRemoveInstance() : Bool {
+		shared.editor.queueRebuild(parent);
+		return true;
+	}
+
 	function getEditProps(shaderDef: hxsl.SharedShader) : Array<hrt.prefab.Props.PropDef> {
 		var props = [];
 		for(v in shaderDef.data.vars) {

+ 2 - 2
hrt/prefab/fx/Emitter.hx

@@ -1768,8 +1768,8 @@ class Emitter extends Object3D {
 		return false; // Emitter removal is buggy
 	}*/
 
-	override function onEditorChildRebuild(child: Prefab) {
-		return true;
+	override function onEditorTreeChanged(child: Prefab) : hrt.prefab.Prefab.TreeChangedResult {
+		return Rebuild;
 	}
 
 	override function edit( ctx : hide.prefab.EditContext ) {

+ 2 - 5
hrt/prefab/fx/FX.hx

@@ -581,11 +581,8 @@ class FX extends Object3D implements BaseFX {
 
 	#if editor
 
-	override function onEditorChildRebuild(prefab: Prefab) {
-		if (Std.is(prefab, Emitter)) {
-			return true;
-		}
-		return false;
+	override function onEditorTreeChanged(child: Prefab) : hrt.prefab.Prefab.TreeChangedResult {
+		return Rebuild;
 	}
 
 	public function refreshObjectAnims() : Void {

+ 3 - 3
hrt/prefab/l3d/MeshSpray.hx

@@ -676,7 +676,7 @@ class MeshSpray extends Spray {
 						}
 						elt.val(newPath);
 						elt.html(extractItemName(newPath));
-						sceneEditor.refresh();
+						sceneEditor.queueRebuild(this);
 						undo.change(Custom(function(undo) {
 							if(undo) {
 								removeSourcePath(newPath);
@@ -689,7 +689,7 @@ class MeshSpray extends Spray {
 								}
 								elt.val(path);
 								elt.html(extractItemName(path));
-								sceneEditor.refresh();
+								sceneEditor.queueRebuild(this);
 							}
 							else {
 								removeSourcePath(elt.val());
@@ -702,7 +702,7 @@ class MeshSpray extends Spray {
 								}
 								elt.val(newPath);
 								elt.html(extractItemName(newPath));
-								sceneEditor.refresh();
+								sceneEditor.queueRebuild(this);
 							}
 						}));
 					}) },

+ 3 - 3
hrt/prefab/l3d/PrefabSpray.hx

@@ -76,7 +76,7 @@ class PrefabSpray extends Spray {
 						}
 						elt.val(newPath);
 						elt.html(extractItemName(newPath));
-						sceneEditor.refresh();
+						sceneEditor.queueRebuild(this);
 						undo.change(Custom(function(undo) {
 							if(undo) {
 								removeSourcePath(newPath);
@@ -89,7 +89,7 @@ class PrefabSpray extends Spray {
 								}
 								elt.val(path);
 								elt.html(extractItemName(path));
-								sceneEditor.refresh();
+								sceneEditor.queueRebuild(this);
 							}
 							else {
 								removeSourcePath(elt.val());
@@ -102,7 +102,7 @@ class PrefabSpray extends Spray {
 								}
 								elt.val(newPath);
 								elt.html(extractItemName(newPath));
-								sceneEditor.refresh();
+								sceneEditor.queueRebuild(this);
 							}
 						}));
 					}) },

+ 3 - 2
hrt/prefab/l3d/Spray.hx

@@ -338,8 +338,9 @@ class Spray extends Object3D {
 	function removeInteractiveBrush() {
 		if( interactive != null ) interactive.remove();
 		clearPreview();
-		if (wasEdited)
-			sceneEditor.refresh(Partial, () -> { });
+		if (wasEdited) {
+			//sceneEditor.queueRebuild(this);
+		}
 		wasEdited = false;
 		clearBrushes();
 	}

+ 1 - 1
hrt/prefab/terrain/Terrain.hx

@@ -603,7 +603,7 @@ class Terrain extends Object3D {
 			terrain.refreshAllGrids();
 			terrain.refreshAllTex();
 			if( editor != null ) {
-				editor.refresh();
+				shared.editor.queueRebuild(this);
 				@:privateAccess editor.blendEdges(terrain.tiles);
 			}
 			modified = true;