Prechádzať zdrojové kódy

[shgraph] Local variables editor support

Clément Espeute 7 mesiacov pred
rodič
commit
fa6634a0fc

+ 56 - 20
hide/view/shadereditor/ShaderEditor.hx

@@ -207,25 +207,56 @@ class ShaderEditor extends hide.view.FileView implements GraphInterface.IGraphEd
 			graphEditor.centerView();
 		}, 50);
 
-		graphEditor.element.on("drop" ,function(e) {
-			var posCursor = new Point(graphEditor.lX(ide.mouseX - 25), graphEditor.lY(ide.mouseY - 10));
+		graphEditor.element.get(0).ondrop = (e:js.html.DragEvent) -> {
+			var posCursor = new Point(graphEditor.lX(e.clientX - 25), graphEditor.lY(e.clientY-25));
+
+			function addNode(inst: ShaderNode) : Void {
+				@:privateAccess var id = currentGraph.current_node_id++;
+				inst.id = id;
+				inst.setPos(posCursor);
+
+				graphEditor.opBox(inst, true, graphEditor.currentUndoBuffer);
+				graphEditor.commitUndo();
+			}
+			if (e.dataTransfer.types.contains(variableList.getDragKeyName())) {
+				var index = Std.parseInt(e.dataTransfer.getData(variableList.getDragKeyName()));
+				var hasAnyWrite = false;
+				currentGraph.mapShaderVar((v) -> {
+					if (v.varId == index && Std.downcast(v, hrt.shgraph.nodes.VarWrite) != null) {
+						hasAnyWrite = true;
+						return false;
+					}
+					return true;
+				});
+
+				if (hasAnyWrite) {
+					var read = new hrt.shgraph.nodes.VarRead();
+					read.varId = index;
+					addNode(read);
+				} else {
+					hide.comp.ContextMenu.createFromPoint(e.clientX, e.clientY, [{
+						label: "Write", click: () -> {
+							var write = new hrt.shgraph.nodes.VarWrite();
+							write.varId = index;
+							addNode(write);
+						}
+					},
+					{
+						label: "Read", click: () -> {
+							var read = new hrt.shgraph.nodes.VarRead();
+							read.varId = index;
+							addNode(read);
+						}
+					}]);
+				}
+				return;
+			}
+
 			var inst = new ShaderParam();
-			@:privateAccess var id = currentGraph.current_node_id++;
-			inst.id = id;
 			inst.parameterId = draggedParamId;
 			inst.shaderGraph = shaderGraph;
-			inst.setPos(posCursor);
-
-			graphEditor.opBox(inst, true, graphEditor.currentUndoBuffer);
-			graphEditor.commitUndo();
-			// var node = Std.downcast(currentGraph.addNode(posCursor.x, posCursor.y, ShaderParam, []), ShaderParam);
-			// node.parameterId = draggedParamId;
-			// var paramShader = shaderGraph.getParameter(draggedParamId);
-			// node.variable = paramShader.variable;
-			// node.setName(paramShader.name);
-			//setDisplayValue(node, paramShader.type, paramShader.defaultValue);
-			//addBox(posCursor, ShaderParam, node);
-		});
+			addNode(inst);
+		};
 
 		var rightPannel = new Element(
 			'<div id="rightPanel">
@@ -438,6 +469,7 @@ class ShaderEditor extends hide.view.FileView implements GraphInterface.IGraphEd
 		}
 	}
 
+
 	function createVariable(type: SgType) {
 		var name = "New Variable";
 		var i = 0;
@@ -480,11 +512,15 @@ class ShaderEditor extends hide.view.FileView implements GraphInterface.IGraphEd
 			variableList.refresh();
 
 			var index = graph.variables.indexOf(variable);
-			for (node in currentGraph.nodes) {
-				if (Std.downcast(node, hrt.shgraph.nodes.VarRead)?.varId == index ||
-					Std.downcast(node, hrt.shgraph.nodes.VarWrite)?.varId == index) {
-					graphEditor.refreshBox(node.id);
+			currentGraph.mapShaderVar((variable: hrt.shgraph.nodes.ShaderVar) -> {
+				if (variable.varId == index) {
+					graphEditor.refreshBox(variable.id);
 				}
+				return true;
+			});
+
+			for (node in currentGraph.nodes) {
+
 			}
 		}
 

+ 1 - 1
hrt/shgraph/NodeGenContext.hx

@@ -122,7 +122,7 @@ class NodeGenContext {
 	}
 
 	public function getLocalTVar(name: String, type: Type) : TVar {
-		return localVars.getOrPut(name, {id: hxsl.Ast.Tools.allocVarId(), name: name, type: type, kind: Local});
+		return MapUtils.getOrPut(localVars, name, {id: hxsl.Ast.Tools.allocVarId(), name: name, type: type, kind: Local});
 	}
 
 	function getOrAllocateFromTVar(tvar: TVar) : TVar {

+ 14 - 0
hrt/shgraph/ShaderGraph.hx

@@ -927,6 +927,20 @@ class Graph {
 		return parent.getParameter(id);
 	}
 
+	/**
+		Iterate on all the shaderVars in the graph, breaking if the cb return false
+	**/
+	public function mapShaderVar(cb: (v: hrt.shgraph.nodes.ShaderVar) -> Bool) {
+		for (node in nodes) {
+			var asVar = Std.downcast(node, hrt.shgraph.nodes.ShaderVar);
+			if (asVar != null) {
+				if (cb(asVar) == false) {
+					break;
+				}
+			}
+		}
+	}
+
 	public function hasCycle() : Bool {
 		var ctx = new ShaderGraphGenContext(this, false);
 		@:privateAccess ctx.initNodes();

+ 8 - 4
hrt/shgraph/nodes/VarRead.hx

@@ -3,10 +3,12 @@ package hrt.shgraph.nodes;
 
 @name("Read Var")
 @description("Read a value from a local variable")
-@width(80)
+@width(130)
 @group("Variables")
-class VarRead extends ShaderNode {
-	@prop() public var varId : Int = 0;
+class VarRead extends ShaderVar {
+
+	public function new() {
+	}
 
 	var outputs: Array<ShaderNode.OutputInfo>;
 	override public function getOutputs() : Array<ShaderNode.OutputInfo> {
@@ -27,7 +29,9 @@ class VarRead extends ShaderNode {
 	#if editor
 	override function getInfo():hide.view.GraphInterface.GraphNodeInfo {
 		var info = super.getInfo();
-		info.name = "Read: " + @:privateAccess (cast editor.editor: hide.view.shadereditor.ShaderEditor).currentGraph.variables[varId].name;
+		if (editor != null) {
+			info.name = "Read: " + @:privateAccess (cast editor.editor: hide.view.shadereditor.ShaderEditor).currentGraph.variables[varId].name;
+		}
 		return info;
 	}
 	#end

+ 8 - 4
hrt/shgraph/nodes/VarWrite.hx

@@ -2,10 +2,12 @@ package hrt.shgraph.nodes;
 
 @name("Write Var")
 @description("Write a value to a local variable")
-@width(80)
+@width(130)
 @group("Variables")
-class VarWrite extends ShaderNode {
-	@prop() public var varId : Int = 0;
+class VarWrite extends ShaderVar {
+
+	public function new() {
+	}
 
 	var inputs: Array<ShaderNode.InputInfo>;
 	override public function getInputs() : Array<ShaderNode.InputInfo> {
@@ -24,7 +26,9 @@ class VarWrite extends ShaderNode {
 	#if editor
 	override function getInfo():hide.view.GraphInterface.GraphNodeInfo {
 		var info = super.getInfo();
-		info.name = "Write: " + @:privateAccess (cast editor.editor: hide.view.shadereditor.ShaderEditor).currentGraph.variables[varId].name;
+		if (editor != null) {
+			info.name = "Write: " + @:privateAccess (cast editor.editor: hide.view.shadereditor.ShaderEditor).currentGraph.variables[varId].name;
+		}
 		return info;
 	}
 	#end