From 4f348fe66a78603fcbbb32b940514d51da7ade36 Mon Sep 17 00:00:00 2001 From: Chromium <62724067+Chromum@users.noreply.github.com> Date: Sun, 27 Apr 2025 23:39:47 +0100 Subject: [PATCH] Various Fixes --- Editor/Scripts/Editor/Data Types/IPTPort.cs | 15 ++ .../Editor/Nodes/Base/BaseImageNode.cs | 2 +- .../Editor/Nodes/Base/PortConnection.cs | 5 + ...ageProcessingGraphEdgeConnectorListener.cs | 4 + .../Windows/ImageProcessingGraphNodeVisual.cs | 223 +++++++++--------- .../Windows/ImageProcessingGraphViewWindow.cs | 30 ++- 6 files changed, 166 insertions(+), 113 deletions(-) diff --git a/Editor/Scripts/Editor/Data Types/IPTPort.cs b/Editor/Scripts/Editor/Data Types/IPTPort.cs index 2cfdb0c..f88fa34 100644 --- a/Editor/Scripts/Editor/Data Types/IPTPort.cs +++ b/Editor/Scripts/Editor/Data Types/IPTPort.cs @@ -1,6 +1,7 @@ using System; using System.Reflection; using UnityEditor.Experimental.GraphView; +using UnityEngine; using UnityEngine.UIElements; namespace ImageProcessingGraph.Editor @@ -8,6 +9,20 @@ namespace ImageProcessingGraph.Editor public class IPTPort : Port { public FieldInfo fieldInfo; + private VisualElement _exposedPropertyContainer; + + public VisualElement ExposedPropertyContainer + { + set + { + Debug.Log($"🔥 Someone is SETTING ExposedPropertyContainer to {value} 🔥"); + _exposedPropertyContainer = value; + } + get + { + return _exposedPropertyContainer; + } + } protected IPTPort(Orientation portOrientation, Direction portDirection, Capacity portCapacity, Type type) : base(portOrientation, portDirection, portCapacity, type) { diff --git a/Editor/Scripts/Editor/Nodes/Base/BaseImageNode.cs b/Editor/Scripts/Editor/Nodes/Base/BaseImageNode.cs index 7271ceb..16306bb 100644 --- a/Editor/Scripts/Editor/Nodes/Base/BaseImageNode.cs +++ b/Editor/Scripts/Editor/Nodes/Base/BaseImageNode.cs @@ -115,7 +115,7 @@ namespace ImageProcessingGraph.Editor catch (Exception e) { onFailed?.Invoke(); - Debug.LogError(e); + //Debug.LogError(e); return false; } } diff --git a/Editor/Scripts/Editor/Nodes/Base/PortConnection.cs b/Editor/Scripts/Editor/Nodes/Base/PortConnection.cs index c996ade..c4ca97f 100644 --- a/Editor/Scripts/Editor/Nodes/Base/PortConnection.cs +++ b/Editor/Scripts/Editor/Nodes/Base/PortConnection.cs @@ -24,6 +24,11 @@ namespace ImageProcessingGraph.Editor this.outputPort = new GraphPort(outputNodeGuid, outputPortID, outputNodeType); this.internalEdge = internalEdge; } + + public void SetInternalEdge(Edge edge) + { + this.internalEdge = edge; + } } [System.Serializable] diff --git a/Editor/Scripts/Editor/Windows/ImageProcessingGraphEdgeConnectorListener.cs b/Editor/Scripts/Editor/Windows/ImageProcessingGraphEdgeConnectorListener.cs index 612a607..1951bc8 100644 --- a/Editor/Scripts/Editor/Windows/ImageProcessingGraphEdgeConnectorListener.cs +++ b/Editor/Scripts/Editor/Windows/ImageProcessingGraphEdgeConnectorListener.cs @@ -42,6 +42,8 @@ namespace ImageProcessingGraph.Editor.Unity_Image_Processing.Scripts.Editor.Wind { window.asset.Connections.Remove(VARIABLE); } + + ((ImageProcessingGraphNodeVisual)edge.input.node).ToggleExposedVariable((IPTPort)edge.input, true); } @@ -63,6 +65,8 @@ namespace ImageProcessingGraph.Editor.Unity_Image_Processing.Scripts.Editor.Wind window.asset.Connections.Remove(VARIABLE); } + ((ImageProcessingGraphNodeVisual)edge.input.node).ToggleExposedVariable((IPTPort)edge.input, true); + this.m_EdgesToCreate.Clear(); this.m_EdgesToCreate.Add(edge); diff --git a/Editor/Scripts/Editor/Windows/ImageProcessingGraphNodeVisual.cs b/Editor/Scripts/Editor/Windows/ImageProcessingGraphNodeVisual.cs index 027c9c2..d0bf546 100644 --- a/Editor/Scripts/Editor/Windows/ImageProcessingGraphNodeVisual.cs +++ b/Editor/Scripts/Editor/Windows/ImageProcessingGraphNodeVisual.cs @@ -23,7 +23,7 @@ namespace ImageProcessingGraph.Editor.Unity_Image_Processing.Scripts.Editor.Wind private ImageProcessingGraphViewWindow window; private StyleSheet errorStyleSheet; - + public ImageProcessingGraphNodeVisual(BaseImageNode node, ImageProcessingGraphViewWindow window) { this.AddToClassList("image-node-visual"); @@ -68,6 +68,16 @@ namespace ImageProcessingGraph.Editor.Unity_Image_Processing.Scripts.Editor.Wind CreateInputPorts(inputFieldInfo); CreateOutputPorts(outputFieldInfo); + + foreach (Port input in InputPorts) + { + + } + + foreach (Port input in OutputPorts) + { + + } errorStyleSheet = AssetDatabase.LoadAssetAtPath("Assets/Unity Image Processing/Node.uss"); if (errorStyleSheet == null) @@ -129,118 +139,119 @@ namespace ImageProcessingGraph.Editor.Unity_Image_Processing.Scripts.Editor.Wind // Exposes a variable on the port for editing when it's not connected public void ExposeVariableToPort(Port port, FieldInfo field) { - // Only expose when the port is not connected - if (port.connections.Count() == 0) + var ExposedPropertyContainer = ((IPTPort)port).ExposedPropertyContainer; + Type containerType = null; + + if (ExposedPropertyContainer == null) { - var propertyFieldContainer = new VisualElement(); - propertyFieldContainer.name = "property-field-container"; - var propertyField = CreatePropertyFieldForType(field.FieldType, field.GetValue(graphNode)); - - if (propertyField != null) - { - // Register a callback for when the value changes - if (propertyField.GetType() == typeof(IntegerField)) - { - var intField = propertyField as IntegerField; - intField.RegisterValueChangedCallback(evt => - { - field.SetValue(graphNode, evt.newValue); // Update the field with the new value - }); - } - else if (propertyField is FloatField floatField) - { - floatField.RegisterValueChangedCallback(evt => - { - field.SetValue(graphNode, evt.newValue); // Update the field with the new value - }); - } - else if (propertyField is Toggle boolField) - { - boolField.RegisterValueChangedCallback(evt => - { - field.SetValue(graphNode, evt.newValue); // Update the field with the new value - }); - } - else if (propertyField is TextField stringField) - { - stringField.RegisterValueChangedCallback(evt => - { - field.SetValue(graphNode, evt.newValue); // Update the field with the new value - }); - } - else if (propertyField is ColorField colorField) - { - colorField.RegisterValueChangedCallback(evt => - { - field.SetValue(graphNode, evt.newValue); // Update the field with the new value - }); - } - else if (propertyField is Vector3Field vector3Field) - { - vector3Field.RegisterValueChangedCallback(evt => - { - field.SetValue(graphNode, evt.newValue); // Update the field with the new value - }); - } - else if (propertyField is Vector2Field vector2Field) - { - vector2Field.RegisterValueChangedCallback(evt => - { - field.SetValue(graphNode, evt.newValue); // Update the field with the new value - }); - } - else if (propertyField is ObjectField objectField) - { - objectField.RegisterValueChangedCallback(evt => - { - field.SetValue(graphNode, evt.newValue); // Update the field with the new value - }); - } - else if (propertyField is EnumField enumField) - { - enumField.RegisterValueChangedCallback(evt => - { - field.SetValue(graphNode, evt.newValue); // 🎯 Update the field with the new enum value - }); - } - else if (propertyField.GetType() == typeof(GreyscaleField)) - { - var greyscaleField = propertyField as GreyscaleField; - greyscaleField.RegisterValueChangedCallback(evt => - { - var value = (GreyscaleValue)field.GetValue(graphNode); - - if(evt.newValue > greyscaleField.minMax.Item2) - value.value = greyscaleField.minMax.Item2; - else if(evt.newValue < greyscaleField.minMax.Item1) - value.value = greyscaleField.minMax.Item1; - - value.value = (int)evt.newValue; - }); - } - - propertyFieldContainer.Add(propertyField); - port.Add(propertyFieldContainer); - } + var NewElement = new VisualElement(); + NewElement.name = "property-field-container"; + VisualElement the = CreatePropertyFieldForType(field.FieldType, field.GetValue(graphNode)); + + if(the != null) + containerType = the.GetType(); + + NewElement.Add(the); + ((IPTPort)port).ExposedPropertyContainer = the; + ExposedPropertyContainer = ((IPTPort)port).ExposedPropertyContainer; } else { - // If the port is connected, remove the exposed UI element - List children = new List(); - - foreach (var child in port.Children()) - { - children.Add(child); - } - - var existingPropertyFieldContainer = port.Q("property-field-container"); - if (existingPropertyFieldContainer != null) - { - port.Remove(existingPropertyFieldContainer); - } + containerType = ExposedPropertyContainer.GetType(); } + + if (containerType == null) + return; + + if (ExposedPropertyContainer.GetType() == typeof(IntegerField)) + { + var intField = ExposedPropertyContainer as IntegerField; + intField.RegisterValueChangedCallback(evt => + { + field.SetValue(graphNode, evt.newValue); // Update the field with the new value + }); + } + else if (ExposedPropertyContainer is FloatField floatField) + { + floatField.RegisterValueChangedCallback(evt => + { + field.SetValue(graphNode, evt.newValue); // Update the field with the new value + }); + } + else if (ExposedPropertyContainer is Toggle boolField) + { + boolField.RegisterValueChangedCallback(evt => + { + field.SetValue(graphNode, evt.newValue); // Update the field with the new value + }); + } + else if (ExposedPropertyContainer is TextField stringField) + { + stringField.RegisterValueChangedCallback(evt => + { + field.SetValue(graphNode, evt.newValue); // Update the field with the new value + }); + } + else if (ExposedPropertyContainer is ColorField colorField) + { + colorField.RegisterValueChangedCallback(evt => + { + field.SetValue(graphNode, evt.newValue); // Update the field with the new value + }); + } + else if (ExposedPropertyContainer is Vector3Field vector3Field) + { + vector3Field.RegisterValueChangedCallback(evt => + { + field.SetValue(graphNode, evt.newValue); // Update the field with the new value + }); + } + else if (ExposedPropertyContainer is Vector2Field vector2Field) + { + vector2Field.RegisterValueChangedCallback(evt => + { + field.SetValue(graphNode, evt.newValue); // Update the field with the new value + }); + } + else if (ExposedPropertyContainer is ObjectField objectField) + { + objectField.RegisterValueChangedCallback(evt => + { + field.SetValue(graphNode, evt.newValue); // Update the field with the new value + }); + } + else if (ExposedPropertyContainer is EnumField enumField) + { + enumField.RegisterValueChangedCallback(evt => + { + field.SetValue(graphNode, evt.newValue); // 🎯 Update the field with the new enum value + }); + } + else if (ExposedPropertyContainer.GetType() == typeof(GreyscaleField)) + { + var greyscaleField = ExposedPropertyContainer as GreyscaleField; + greyscaleField.RegisterValueChangedCallback(evt => + { + var value = (GreyscaleValue)field.GetValue(graphNode); + + if (evt.newValue > greyscaleField.minMax.Item2) + value.value = greyscaleField.minMax.Item2; + else if (evt.newValue < greyscaleField.minMax.Item1) value.value = greyscaleField.minMax.Item1; + + value.value = evt.newValue; + }); + } + + port.Add(ExposedPropertyContainer); } + public void ToggleExposedVariable(Port port, bool value) + { + IPTPort iptPort = port as IPTPort; + if(iptPort.ExposedPropertyContainer != null) + iptPort.ExposedPropertyContainer.visible = value; + } + private VisualElement CreatePropertyFieldForType(Type type, object value) { if (type == typeof(int)) diff --git a/Editor/Scripts/Editor/Windows/ImageProcessingGraphViewWindow.cs b/Editor/Scripts/Editor/Windows/ImageProcessingGraphViewWindow.cs index 9fc4930..cd4a3bf 100644 --- a/Editor/Scripts/Editor/Windows/ImageProcessingGraphViewWindow.cs +++ b/Editor/Scripts/Editor/Windows/ImageProcessingGraphViewWindow.cs @@ -70,6 +70,17 @@ namespace ImageProcessingGraph.Editor DrawNodes(); DrawConnections(); + foreach (var conn in asset.Connections) + { + if (conn.internalEdge == null) + { + + } + + //GetNode(conn.inputPort.nodeID).ToggleExposedVariable(conn.internalEdge.input, true); + + } + graphViewChanged += OnGraphViewChanged; Undo.undoRedoEvent += UndoEvent; } @@ -226,7 +237,7 @@ namespace ImageProcessingGraph.Editor private void UndoEvent(in UndoRedoInfo undo) { DrawNodes(); - + DrawConnections(); } private GraphViewChange OnGraphViewChanged(GraphViewChange graphviewchange) @@ -297,7 +308,7 @@ namespace ImageProcessingGraph.Editor IPTPort portOut = (IPTPort)edge.output; if (portIn.fieldInfo != null) - inputNode.ExposeVariableToPort(edge.input, portOut.fieldInfo); + inputNode.ToggleExposedVariable(edge.input, false); asset.Connections.Add(connection); } @@ -317,12 +328,18 @@ namespace ImageProcessingGraph.Editor IPTPort portIn = (IPTPort)edge.input; if(portIn.fieldInfo != null) - inputNode.ExposeVariableToPort(edge.input, portIn.fieldInfo); + inputNode.ToggleExposedVariable(edge.input, true); } } private void DrawConnections() { + foreach (KeyValuePair node in connectionDictionary) + { + RemoveElement(node.Key); + } + connectionDictionary.Clear(); + if (asset.Connections != null) { foreach (GraphConnection conn in asset.Connections) @@ -332,14 +349,15 @@ namespace ImageProcessingGraph.Editor if (inputNode != null && outputNode != null) { - Port inPort = inputNode.InputPorts[conn.inputPort.portID]; - Port outPort = outputNode.OutputPorts[conn.outputPort.portID]; + IPTPort inPort = inputNode.InputPorts[conn.inputPort.portID] as IPTPort; + IPTPort outPort = outputNode.OutputPorts[conn.outputPort.portID] as IPTPort; Edge edge = inPort.ConnectTo(outPort); AddElement(edge); connectionDictionary.Add(edge, conn); + conn.SetInternalEdge(edge); - + ((ImageProcessingGraphNodeVisual)inPort.node).ToggleExposedVariable(inPort, false); } }