Преглед изворни кода

[shgraph] Working comment function, undo/redo of commenting

Clément Espeute пре 1 година
родитељ
комит
492b3284c4
3 измењених фајлова са 32 додато и 17 уклоњено
  1. 28 7
      hide/view/GraphEditor.hx
  2. 2 10
      hide/view/shadereditor/Box.hx
  3. 2 0
      hide/view/shadereditor/ShaderEditor.hx

+ 28 - 7
hide/view/GraphEditor.hx

@@ -279,8 +279,7 @@ class GraphEditor extends hide.comp.Component {
 
 		var nodes = editor.getNodes();
 		for (node in nodes) {
-			node.getPos(tmpPoint);
-			addBox(tmpPoint, node);
+			addBox(node);
 		}
 
 		var edges = editor.getEdges();
@@ -767,6 +766,7 @@ class GraphEditor extends hide.comp.Component {
 			if (b.info.comment != null && !e.shiftKey) {
 				var bounds = inline b.getBounds();
 				var min = inline new Point(bounds.x, bounds.y);
+				trace(min);
 				var max = inline new Point(bounds.x + bounds.w, bounds.y + bounds.h);
 
 				for (bb in boxes) {
@@ -778,7 +778,6 @@ class GraphEditor extends hide.comp.Component {
 		}
 
 		undoSave = saveMovedBoxes();
-
 		trace(boxesToMove);
 	}
 
@@ -859,6 +858,7 @@ class GraphEditor extends hide.comp.Component {
 			var box = boxes[id];
 
 			box.node.getPos(Box.tmpPoint);
+			trace(Box.tmpPoint);
 			bounds.addPos(Box.tmpPoint.x, Box.tmpPoint.y);
 			var previewHeight = (box.info.preview?.getVisible() ?? false) ? box.width : 0;
 			bounds.addPos(Box.tmpPoint.x + box.width, Box.tmpPoint.y + box.getHeight() + previewHeight);
@@ -870,6 +870,8 @@ class GraphEditor extends hide.comp.Component {
 		bounds.xMax += border;
 		bounds.yMax += border;
 
+		trace(bounds.xMin, bounds.yMin);
+
 		Box.tmpPoint.set(bounds.xMin, bounds.yMin);
 		commentNode.setPos(Box.tmpPoint);
 		Box.tmpPoint.set(bounds.width, bounds.height);
@@ -914,13 +916,14 @@ class GraphEditor extends hide.comp.Component {
 
 	public function opBox(node: IGraphNode, doAdd: Bool, undoBuffer: UndoBuffer) : Void {
 		var data = editor.serializeNode(node);
+		trace(data);
 
 		var exec = function(isUndo : Bool) : Void {
 			if (!doAdd) isUndo = !isUndo;
 			if (!isUndo) {
 				var node = editor.unserializeNode(data, false);
-				node.getPos(Box.tmpPoint);
-				addBox(Box.tmpPoint, node);
+				trace(Box.tmpPoint);
+				addBox(node);
 				editor.addNode(node);
 			}
 			else {
@@ -953,6 +956,23 @@ class GraphEditor extends hide.comp.Component {
 		exec(false);
 	}
 
+	public function opComment(box: Box, newComment: String, undoBuffer: UndoBuffer) : Void {
+		var id = box.node.getId();
+		var prev = box.info.comment.getComment();
+		if (newComment == prev)
+			return;
+		function exec(isUndo : Bool) {
+			var box = boxes.get(id);
+			var v = !isUndo ? newComment : prev;
+			trace("opComment", v);
+
+			box.info.comment.setComment(v);
+			box.element.find(".comment-title").get(0).innerText = v;
+		}
+		exec(false);
+		undoBuffer.push(exec);
+	}
+
 	function opEdge(output: Int, input: Int, doAdd: Bool, undoBuffer: UndoBuffer) : Void {
 		var edge = edgeFromPack(output, input);
 		var previousFrom : Null<Int> = outputsToInputs.getLeft(input);
@@ -998,10 +1018,11 @@ class GraphEditor extends hide.comp.Component {
 	}
 
 	static var tmpPoint = new h2d.col.Point();
-	function addBox(point: h2d.col.Point, node : IGraphNode) : Box {
+	function addBox(node : IGraphNode) : Box {
 		node.editor = this;
 		var box = new Box(this, editorMatrix, node);
-		box.setPosition(point.x, point.y);
+		node.getPos(Box.tmpPoint);
+		box.setPosition(Box.tmpPoint.x, Box.tmpPoint.y);
 
 		var elt = box.getElement();
 		elt.get(0).onpointerdown = function(e: js.html.MouseEvent) {

+ 2 - 10
hide/view/shadereditor/Box.hx

@@ -217,16 +217,8 @@ class Box {
 				var editable = new hide.comp.ContentEditable(null, commentTitle);
 				editable.value = info.comment.getComment();
 				editable.onChange = function(v: String) {
-					var prev = info.comment.getComment();
-					function exec(undo : Bool) {
-						if(!undo) {
-							info.comment.setComment(v);
-						} else {
-							info.comment.setComment(prev);
-						}
-					}
-					editor.editor.getUndo().change(Custom(exec));
-					exec(false);
+					editor.opComment(this, v, editor.currentUndoBuffer);
+					editor.commitUndo();
 				};
 			}
 			else {

+ 2 - 0
hide/view/shadereditor/ShaderEditor.hx

@@ -1360,6 +1360,7 @@ class ShaderEditor extends hide.view.FileView implements GraphInterface.IGraphEd
 
 	public function createCommentNode() : Null<IGraphNode> {
 		var node = new hrt.shgraph.nodes.Comment();
+		node.comment = "Comment";
 		@:privateAccess var newId = currentGraph.current_node_id++;
 		node.setId(newId);
 		return node;
@@ -1413,6 +1414,7 @@ class ShaderEditor extends hide.view.FileView implements GraphInterface.IGraphEd
 		return entries;
 	}
 
+
 	public function addNode(node: IGraphNode) : Void {
 		currentGraph.addNode(cast node);
 		requestRecompile();