Various Fixes

This commit is contained in:
Chromium 2025-04-27 23:39:47 +01:00
parent 499d187c96
commit 4f348fe66a
6 changed files with 166 additions and 113 deletions

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Reflection; using System.Reflection;
using UnityEditor.Experimental.GraphView; using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.UIElements; using UnityEngine.UIElements;
namespace ImageProcessingGraph.Editor namespace ImageProcessingGraph.Editor
@ -8,6 +9,20 @@ namespace ImageProcessingGraph.Editor
public class IPTPort : Port public class IPTPort : Port
{ {
public FieldInfo fieldInfo; 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) protected IPTPort(Orientation portOrientation, Direction portDirection, Capacity portCapacity, Type type) : base(portOrientation, portDirection, portCapacity, type)
{ {

View File

@ -115,7 +115,7 @@ namespace ImageProcessingGraph.Editor
catch (Exception e) catch (Exception e)
{ {
onFailed?.Invoke(); onFailed?.Invoke();
Debug.LogError(e); //Debug.LogError(e);
return false; return false;
} }
} }

View File

@ -24,6 +24,11 @@ namespace ImageProcessingGraph.Editor
this.outputPort = new GraphPort(outputNodeGuid, outputPortID, outputNodeType); this.outputPort = new GraphPort(outputNodeGuid, outputPortID, outputNodeType);
this.internalEdge = internalEdge; this.internalEdge = internalEdge;
} }
public void SetInternalEdge(Edge edge)
{
this.internalEdge = edge;
}
} }
[System.Serializable] [System.Serializable]

View File

@ -42,6 +42,8 @@ namespace ImageProcessingGraph.Editor.Unity_Image_Processing.Scripts.Editor.Wind
{ {
window.asset.Connections.Remove(VARIABLE); 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); window.asset.Connections.Remove(VARIABLE);
} }
((ImageProcessingGraphNodeVisual)edge.input.node).ToggleExposedVariable((IPTPort)edge.input, true);
this.m_EdgesToCreate.Clear(); this.m_EdgesToCreate.Clear();
this.m_EdgesToCreate.Add(edge); this.m_EdgesToCreate.Add(edge);

View File

@ -69,6 +69,16 @@ namespace ImageProcessingGraph.Editor.Unity_Image_Processing.Scripts.Editor.Wind
CreateInputPorts(inputFieldInfo); CreateInputPorts(inputFieldInfo);
CreateOutputPorts(outputFieldInfo); CreateOutputPorts(outputFieldInfo);
foreach (Port input in InputPorts)
{
}
foreach (Port input in OutputPorts)
{
}
errorStyleSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/Unity Image Processing/Node.uss"); errorStyleSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/Unity Image Processing/Node.uss");
if (errorStyleSheet == null) if (errorStyleSheet == null)
{ {
@ -129,116 +139,117 @@ namespace ImageProcessingGraph.Editor.Unity_Image_Processing.Scripts.Editor.Wind
// Exposes a variable on the port for editing when it's not connected // Exposes a variable on the port for editing when it's not connected
public void ExposeVariableToPort(Port port, FieldInfo field) public void ExposeVariableToPort(Port port, FieldInfo field)
{ {
// Only expose when the port is not connected var ExposedPropertyContainer = ((IPTPort)port).ExposedPropertyContainer;
if (port.connections.Count() == 0) Type containerType = null;
{
var propertyFieldContainer = new VisualElement();
propertyFieldContainer.name = "property-field-container";
var propertyField = CreatePropertyFieldForType(field.FieldType, field.GetValue(graphNode));
if (propertyField != null) if (ExposedPropertyContainer == null)
{ {
// Register a callback for when the value changes var NewElement = new VisualElement();
if (propertyField.GetType() == typeof(IntegerField)) 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
{ {
var intField = propertyField as IntegerField; containerType = ExposedPropertyContainer.GetType();
}
if (containerType == null)
return;
if (ExposedPropertyContainer.GetType() == typeof(IntegerField))
{
var intField = ExposedPropertyContainer as IntegerField;
intField.RegisterValueChangedCallback(evt => intField.RegisterValueChangedCallback(evt =>
{ {
field.SetValue(graphNode, evt.newValue); // Update the field with the new value field.SetValue(graphNode, evt.newValue); // Update the field with the new value
}); });
} }
else if (propertyField is FloatField floatField) else if (ExposedPropertyContainer is FloatField floatField)
{ {
floatField.RegisterValueChangedCallback(evt => floatField.RegisterValueChangedCallback(evt =>
{ {
field.SetValue(graphNode, evt.newValue); // Update the field with the new value field.SetValue(graphNode, evt.newValue); // Update the field with the new value
}); });
} }
else if (propertyField is Toggle boolField) else if (ExposedPropertyContainer is Toggle boolField)
{ {
boolField.RegisterValueChangedCallback(evt => boolField.RegisterValueChangedCallback(evt =>
{ {
field.SetValue(graphNode, evt.newValue); // Update the field with the new value field.SetValue(graphNode, evt.newValue); // Update the field with the new value
}); });
} }
else if (propertyField is TextField stringField) else if (ExposedPropertyContainer is TextField stringField)
{ {
stringField.RegisterValueChangedCallback(evt => stringField.RegisterValueChangedCallback(evt =>
{ {
field.SetValue(graphNode, evt.newValue); // Update the field with the new value field.SetValue(graphNode, evt.newValue); // Update the field with the new value
}); });
} }
else if (propertyField is ColorField colorField) else if (ExposedPropertyContainer is ColorField colorField)
{ {
colorField.RegisterValueChangedCallback(evt => colorField.RegisterValueChangedCallback(evt =>
{ {
field.SetValue(graphNode, evt.newValue); // Update the field with the new value field.SetValue(graphNode, evt.newValue); // Update the field with the new value
}); });
} }
else if (propertyField is Vector3Field vector3Field) else if (ExposedPropertyContainer is Vector3Field vector3Field)
{ {
vector3Field.RegisterValueChangedCallback(evt => vector3Field.RegisterValueChangedCallback(evt =>
{ {
field.SetValue(graphNode, evt.newValue); // Update the field with the new value field.SetValue(graphNode, evt.newValue); // Update the field with the new value
}); });
} }
else if (propertyField is Vector2Field vector2Field) else if (ExposedPropertyContainer is Vector2Field vector2Field)
{ {
vector2Field.RegisterValueChangedCallback(evt => vector2Field.RegisterValueChangedCallback(evt =>
{ {
field.SetValue(graphNode, evt.newValue); // Update the field with the new value field.SetValue(graphNode, evt.newValue); // Update the field with the new value
}); });
} }
else if (propertyField is ObjectField objectField) else if (ExposedPropertyContainer is ObjectField objectField)
{ {
objectField.RegisterValueChangedCallback(evt => objectField.RegisterValueChangedCallback(evt =>
{ {
field.SetValue(graphNode, evt.newValue); // Update the field with the new value field.SetValue(graphNode, evt.newValue); // Update the field with the new value
}); });
} }
else if (propertyField is EnumField enumField) else if (ExposedPropertyContainer is EnumField enumField)
{ {
enumField.RegisterValueChangedCallback(evt => enumField.RegisterValueChangedCallback(evt =>
{ {
field.SetValue(graphNode, evt.newValue); // 🎯 Update the field with the new enum value field.SetValue(graphNode, evt.newValue); // 🎯 Update the field with the new enum value
}); });
} }
else if (propertyField.GetType() == typeof(GreyscaleField)) else if (ExposedPropertyContainer.GetType() == typeof(GreyscaleField))
{ {
var greyscaleField = propertyField as GreyscaleField; var greyscaleField = ExposedPropertyContainer as GreyscaleField;
greyscaleField.RegisterValueChangedCallback(evt => greyscaleField.RegisterValueChangedCallback(evt =>
{ {
var value = (GreyscaleValue)field.GetValue(graphNode); var value = (GreyscaleValue)field.GetValue(graphNode);
if(evt.newValue > greyscaleField.minMax.Item2) if (evt.newValue > greyscaleField.minMax.Item2)
value.value = greyscaleField.minMax.Item2; value.value = greyscaleField.minMax.Item2;
else if(evt.newValue < greyscaleField.minMax.Item1) else if (evt.newValue < greyscaleField.minMax.Item1) value.value = greyscaleField.minMax.Item1;
value.value = greyscaleField.minMax.Item1;
value.value = (int)evt.newValue; value.value = evt.newValue;
}); });
} }
propertyFieldContainer.Add(propertyField); port.Add(ExposedPropertyContainer);
port.Add(propertyFieldContainer);
}
}
else
{
// If the port is connected, remove the exposed UI element
List<VisualElement> children = new List<VisualElement>();
foreach (var child in port.Children())
{
children.Add(child);
} }
var existingPropertyFieldContainer = port.Q<VisualElement>("property-field-container"); public void ToggleExposedVariable(Port port, bool value)
if (existingPropertyFieldContainer != null)
{ {
port.Remove(existingPropertyFieldContainer); IPTPort iptPort = port as IPTPort;
} if(iptPort.ExposedPropertyContainer != null)
} iptPort.ExposedPropertyContainer.visible = value;
} }
private VisualElement CreatePropertyFieldForType(Type type, object value) private VisualElement CreatePropertyFieldForType(Type type, object value)

View File

@ -70,6 +70,17 @@ namespace ImageProcessingGraph.Editor
DrawNodes(); DrawNodes();
DrawConnections(); DrawConnections();
foreach (var conn in asset.Connections)
{
if (conn.internalEdge == null)
{
}
//GetNode(conn.inputPort.nodeID).ToggleExposedVariable(conn.internalEdge.input, true);
}
graphViewChanged += OnGraphViewChanged; graphViewChanged += OnGraphViewChanged;
Undo.undoRedoEvent += UndoEvent; Undo.undoRedoEvent += UndoEvent;
} }
@ -226,7 +237,7 @@ namespace ImageProcessingGraph.Editor
private void UndoEvent(in UndoRedoInfo undo) private void UndoEvent(in UndoRedoInfo undo)
{ {
DrawNodes(); DrawNodes();
DrawConnections();
} }
private GraphViewChange OnGraphViewChanged(GraphViewChange graphviewchange) private GraphViewChange OnGraphViewChanged(GraphViewChange graphviewchange)
@ -297,7 +308,7 @@ namespace ImageProcessingGraph.Editor
IPTPort portOut = (IPTPort)edge.output; IPTPort portOut = (IPTPort)edge.output;
if (portIn.fieldInfo != null) if (portIn.fieldInfo != null)
inputNode.ExposeVariableToPort(edge.input, portOut.fieldInfo); inputNode.ToggleExposedVariable(edge.input, false);
asset.Connections.Add(connection); asset.Connections.Add(connection);
} }
@ -317,12 +328,18 @@ namespace ImageProcessingGraph.Editor
IPTPort portIn = (IPTPort)edge.input; IPTPort portIn = (IPTPort)edge.input;
if(portIn.fieldInfo != null) if(portIn.fieldInfo != null)
inputNode.ExposeVariableToPort(edge.input, portIn.fieldInfo); inputNode.ToggleExposedVariable(edge.input, true);
} }
} }
private void DrawConnections() private void DrawConnections()
{ {
foreach (KeyValuePair<Edge, GraphConnection> node in connectionDictionary)
{
RemoveElement(node.Key);
}
connectionDictionary.Clear();
if (asset.Connections != null) if (asset.Connections != null)
{ {
foreach (GraphConnection conn in asset.Connections) foreach (GraphConnection conn in asset.Connections)
@ -332,14 +349,15 @@ namespace ImageProcessingGraph.Editor
if (inputNode != null && outputNode != null) if (inputNode != null && outputNode != null)
{ {
Port inPort = inputNode.InputPorts[conn.inputPort.portID]; IPTPort inPort = inputNode.InputPorts[conn.inputPort.portID] as IPTPort;
Port outPort = outputNode.OutputPorts[conn.outputPort.portID]; IPTPort outPort = outputNode.OutputPorts[conn.outputPort.portID] as IPTPort;
Edge edge = inPort.ConnectTo(outPort); Edge edge = inPort.ConnectTo(outPort);
AddElement(edge); AddElement(edge);
connectionDictionary.Add(edge, conn); connectionDictionary.Add(edge, conn);
conn.SetInternalEdge(edge);
((ImageProcessingGraphNodeVisual)inPort.node).ToggleExposedVariable(inPort, false);
} }
} }