Browse Source

ShaderGraph: move edge from both sides

Tom SPIRA 6 years ago
parent
commit
f72bc06272
3 changed files with 42 additions and 19 deletions
  1. 41 15
      hide/view/Graph.hx
  2. 1 3
      hide/view/shadereditor/ShaderEditor.hx
  3. 0 1
      hrt/shgraph/ShaderGraph.hx

+ 41 - 15
hide/view/Graph.hx

@@ -169,9 +169,16 @@ class Graph extends FileView {
 		}
 		// Moving edge
 		if (currentEdge != null) {
-			startUpdateViewPosition();
-			// TODO: handle moving edge => disconnect closest node
-			// try to use the same code when user clicks on input node already connected and here
+			var distOutput = distanceToElement(currentEdge.nodeFrom, clientX, clientY);
+			var distInput = distanceToElement(currentEdge.nodeTo, clientX, clientY);
+
+			if (distOutput > distInput) {
+				replaceEdge(FromOutput, currentEdge.nodeTo, clientX, clientY);
+			} else {
+				replaceEdge(FromInput, currentEdge.nodeFrom, clientX, clientY);
+			}
+			currentEdge = null;
+			return;
 		}
 		if (isPanning) {
 			pan(new Point(clientX - lastClickPan.x, clientY - lastClickPan.y));
@@ -275,7 +282,6 @@ class Graph extends FileView {
 		});
 		listOfBoxes.push(box);
 
-
 		var fields = std.Type.getInstanceFields(nodeClass);
 
 		var metas = haxe.rtti.Meta.getFields(nodeClass);
@@ -316,17 +322,8 @@ class Graph extends FileView {
 					e.stopPropagation();
 					var node = grNode.find(".node");
 					if (node.attr("hasLink") != null) {
-						isCreatingLink = FromOutput;
-						for (edge in listOfEdges) {
-							if (edge.nodeTo.is(node)) {
-								startLinkGrNode = edge.nodeFrom.parent();
-								startLinkBox = edge.from;
-								setAvailableInputNodes(edge.from, edge.nodeFrom.attr("field"));
-								removeEdge(edge);
-								createLink(e.clientX, e.clientY);
-								return;
-							}
-						}
+						replaceEdge(FromOutput, node, e.clientX, e.clientY);
+						return;
 					}
 					isCreatingLink = FromInput;
 					startLinkGrNode = grNode;
@@ -370,6 +367,35 @@ class Graph extends FileView {
 		listOfEdges.remove(edge);
 	}
 
+	function replaceEdge(state : EdgeState, node : JQuery, x : Int, y : Int) {
+		switch (state) {
+			case FromOutput:
+				for (edge in listOfEdges) {
+					if (edge.nodeTo.is(node)) {
+						isCreatingLink = FromOutput;
+						startLinkGrNode = edge.nodeFrom.parent();
+						startLinkBox = edge.from;
+						setAvailableInputNodes(edge.from, edge.nodeFrom.attr("field"));
+						removeEdge(edge);
+						createLink(x, y);
+					}
+				}
+			case FromInput:
+				for (edge in listOfEdges) {
+					if (edge.nodeFrom.is(node)) {
+						isCreatingLink = FromInput;
+						startLinkGrNode = edge.nodeTo.parent();
+						startLinkBox = edge.to;
+						setAvailableOutputNodes(edge.to, edge.nodeTo.attr("field"));
+						removeEdge(edge);
+						createLink(x, y);
+					}
+				}
+			default:
+				return;
+		}
+	}
+
 	function setAvailableInputNodes(boxOutput : Box, field : String) {
 		var type = boxOutput.getInstance().getOutputType(field);
 		var sType : SType;

+ 1 - 3
hide/view/shadereditor/ShaderEditor.hx

@@ -2,14 +2,12 @@ package hide.view.shadereditor;
 
 import hxsl.DynamicShader;
 import h3d.Vector;
-import h3d.Engine;
 import hrt.shgraph.ShaderParam;
 import hrt.shgraph.ShaderException;
 import haxe.Timer;
 using hxsl.Ast.Type;
 
 import haxe.rtti.Meta;
-import hxsl.Shader;
 import hide.comp.SceneEditor;
 import js.jquery.JQuery;
 import h2d.col.Point;
@@ -172,7 +170,7 @@ class ShaderEditor extends hide.view.Graph {
 			});
 			elements.push(deleteNode);
 
-			customContextMenu(elements); // TODO: add menu right click
+			customContextMenu(elements);
 			e.preventDefault();
 			return false;
 		});

+ 0 - 1
hrt/shgraph/ShaderGraph.hx

@@ -266,7 +266,6 @@ class ShaderGraph {
 	}
 
 	#if editor
-
 	public function addNode(x : Float, y : Float, nameClass : Class<ShaderNode>) {
 		var node : Node = { x : x, y : y, comment: "", id : current_node_id, type: std.Type.getClassName(nameClass) };