Fixed Connector

This commit is contained in:
Chromium 2025-05-06 10:10:15 +01:00
parent fa5cba5ded
commit 2e32a83484
6 changed files with 109 additions and 28 deletions

View File

@ -1,5 +1,8 @@
using System; using System;
using System.Linq;
using System.Reflection; using System.Reflection;
using ImageProcessingGraph.Editor.Nodes.Types.Utils.Connector;
using ImageProcessingGraph.Editor.Unity_Image_Processing.Scripts.Editor.Windows;
using UnityEditor.Experimental.GraphView; using UnityEditor.Experimental.GraphView;
using UnityEngine; using UnityEngine;
using UnityEngine.UIElements; using UnityEngine.UIElements;
@ -36,12 +39,51 @@ namespace ImageProcessingGraph.Editor
public override void Connect(Edge edge) public override void Connect(Edge edge)
{ {
ImageProcessingGraphNodeVisual IPTVis = this.node as ImageProcessingGraphNodeVisual;
IPTPort extenalPort = null;
if (IPTVis.GraphNode is GenericConnection bleh)
{
foreach (var port in IPTVis.AllPorts)
{
port.portType = edge.output.portType;
}
}
base.Connect(edge); base.Connect(edge);
OnPortConnected?.Invoke(); OnPortConnected?.Invoke();
} }
public override void Disconnect(Edge edge) public override void Disconnect(Edge edge)
{ {
ImageProcessingGraphNodeVisual IPTVis = this.node as ImageProcessingGraphNodeVisual;
if (IPTVis.GraphNode is GenericConnection bleh)
{
bool allPortsDisconnected = true;
foreach (var port in IPTVis.AllPorts)
{
if (port.connections != null && port.connections.Count() > 0)
{
allPortsDisconnected = false;
break;
}
}
if (allPortsDisconnected)
{
foreach (var port in IPTVis.AllPorts)
{
port.portType = typeof(object);
}
bleh.InternalType = null;
}
}
base.Disconnect(edge); base.Disconnect(edge);
OnPortDisconnected?.Invoke(); OnPortDisconnected?.Invoke();
} }
@ -62,5 +104,6 @@ namespace ImageProcessingGraph.Editor
port.AddManipulator(port.m_EdgeConnector); port.AddManipulator(port.m_EdgeConnector);
return port; return port;
} }
} }
} }

View File

@ -5,5 +5,6 @@ namespace ImageProcessingGraph.Editor
{ {
public class IPT_Edge : Edge public class IPT_Edge : Edge
{ {
} }
} }

View File

@ -48,7 +48,6 @@ namespace ImageProcessingGraph.Editor
public void RunGraph() public void RunGraph()
{ {
var bleh = GetAllVariableNodesWithTypes(); var bleh = GetAllVariableNodesWithTypes();
OnRun?.Invoke(); OnRun?.Invoke();
@ -58,8 +57,6 @@ namespace ImageProcessingGraph.Editor
runOrder = GetExecutionOrder(this.nodes, this.connections); runOrder = GetExecutionOrder(this.nodes, this.connections);
foreach (var VARIABLE in runOrder) foreach (var VARIABLE in runOrder)
{ {
if (!VARIABLE.RunNode()) if (!VARIABLE.RunNode())

View File

@ -1,6 +1,7 @@
using ImageProcessingGraph.Editor.Nodes.NodeAttributes; using ImageProcessingGraph.Editor.Nodes.NodeAttributes;
using ImageProcessingGraph.Editor.Unity_Image_Processing.Scripts.Editor.Windows; using ImageProcessingGraph.Editor.Unity_Image_Processing.Scripts.Editor.Windows;
using UnityEditor; using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements; using UnityEngine.UIElements;
namespace ImageProcessingGraph.Editor.Nodes.Types.Utils.Connector namespace ImageProcessingGraph.Editor.Nodes.Types.Utils.Connector
@ -8,6 +9,9 @@ namespace ImageProcessingGraph.Editor.Nodes.Types.Utils.Connector
[NodeInfo("", "Utility/Connection", false, null , editorType: typeof(GenericConnectionEditor))] [NodeInfo("", "Utility/Connection", false, null , editorType: typeof(GenericConnectionEditor))]
public class GenericConnection : BaseImageNode public class GenericConnection : BaseImageNode
{ {
private System.Type internalType;
public System.Type InternalType { get { return internalType; } set { internalType = value; } }
[NodeAttributes.Input(" ")] public object input; [NodeAttributes.Input(" ")] public object input;
[NodeAttributes.Output(" ")] public object output; [NodeAttributes.Output(" ")] public object output;
@ -21,13 +25,6 @@ namespace ImageProcessingGraph.Editor.Nodes.Types.Utils.Connector
{ {
public GenericConnectionEditor(BaseImageNode node, ImageProcessingGraphViewWindow window) : base(node, window) public GenericConnectionEditor(BaseImageNode node, ImageProcessingGraphViewWindow window) : base(node, window)
{ {
// StyleSheet styleSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/Unity Image Processing/GenericNode.uss");
// if (styleSheet == null)
// {
// styleSheet = EditorGUIUtility.Load("Packages/com.chromium.imageprocessingrah/GenericNode.uss") as StyleSheet;
// }
// styleSheets.Add(styleSheet);
this.Q("title").style.display = DisplayStyle.None; this.Q("title").style.display = DisplayStyle.None;
} }
} }

View File

@ -17,6 +17,7 @@ namespace ImageProcessingGraph.Editor.Unity_Image_Processing.Scripts.Editor.Wind
private BaseImageNode graphNode; private BaseImageNode graphNode;
public BaseImageNode GraphNode => graphNode; public BaseImageNode GraphNode => graphNode;
public List<Port> AllPorts = new List<Port>();
public List<Port> InputPorts { get; } public List<Port> InputPorts { get; }
public List<Port> OutputPorts { get; } public List<Port> OutputPorts { get; }
@ -107,6 +108,7 @@ namespace ImageProcessingGraph.Editor.Unity_Image_Processing.Scripts.Editor.Wind
if (label != "") if (label != "")
port.portName = label; port.portName = label;
InputPorts.Add(port); InputPorts.Add(port);
AllPorts.Add(port);
inputContainer.Add(port); inputContainer.Add(port);
@ -127,6 +129,7 @@ namespace ImageProcessingGraph.Editor.Unity_Image_Processing.Scripts.Editor.Wind
if (label != "") if (label != "")
port.portName = label; port.portName = label;
OutputPorts.Add(port); OutputPorts.Add(port);
AllPorts.Add(port);
outputContainer.Add(port); outputContainer.Add(port);
port.fieldInfo = field; port.fieldInfo = field;

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using ImageProcessingGraph.Editor.Nodes.NodeAttributes; using ImageProcessingGraph.Editor.Nodes.NodeAttributes;
using ImageProcessingGraph.Editor.Nodes.Types.Utils.Connector;
using ImageProcessingGraph.Editor.Unity_Image_Processing.Scripts.Editor.Windows; using ImageProcessingGraph.Editor.Unity_Image_Processing.Scripts.Editor.Windows;
using ImageProcessingGraph.Editor.Windows; using ImageProcessingGraph.Editor.Windows;
using UnityEditor; using UnityEditor;
@ -233,33 +234,53 @@ namespace ImageProcessingGraph.Editor
{ {
List<Port> compatiblePorts = new List<Port>(); List<Port> compatiblePorts = new List<Port>();
foreach (var node in graphNodes) foreach (var node in graphNodes)
{ {
// Prevent connections to self
if (node == startPort.node) if (node == startPort.node)
continue; continue;
foreach (var port in node.inputContainer.Children().Concat(node.outputContainer.Children()).OfType<Port>()) var ports = node.inputContainer.Children()
.Concat(node.outputContainer.Children())
.OfType<Port>();
foreach (var port in ports)
{ {
// Prevent connecting input to input or output to output if (startPort.direction == port.direction)
if (port.direction == startPort.direction)
continue; continue;
if (!port.portType.IsAssignableFrom(startPort.portType) || startPort.portType.IsAssignableFrom(port.portType)) Type typeA = GetPortEffectiveType(startPort);
{ Type typeB = GetPortEffectiveType(port);
if (DoesConversionNodeExist())
{
}
else // if (typeA == typeof(object))
// {
// var BaseGenPort = startPort.node as ImageProcessingGraphNodeVisual;
// var GenPort = BaseGenPort.GraphNode as GenericConnection;
//
// if (GenPort.InternalType != null)
// {
// if(!GenPort.InternalType.IsAssignableFrom(typeB))
// continue;
// }
// }
//
// if (typeB == typeof(object))
// {
// var BaseGenPort = port.node as ImageProcessingGraphNodeVisual;
// var GenPort = BaseGenPort.GraphNode as GenericConnection;
//
// if (GenPort.InternalType != null)
// {
// if(!GenPort.InternalType.IsAssignableFrom(typeA))
// continue;
// }
// }
if (!typeA.IsAssignableFrom(typeB) && !typeB.IsAssignableFrom(typeA))
continue; continue;
}
// Prevent connection if it creates a cycle
if (startPort.direction == Direction.Output && CreatesCycle(startPort, port)) if (startPort.direction == Direction.Output && CreatesCycle(startPort, port))
continue; continue;
if (startPort.direction == Direction.Input && CreatesCycle(port, startPort)) if (startPort.direction == Direction.Input && CreatesCycle(port, startPort))
continue; continue;
@ -270,6 +291,25 @@ namespace ImageProcessingGraph.Editor
return compatiblePorts; return compatiblePorts;
} }
private Type GetPortEffectiveType(Port port)
{
if (port is IPTPort iptPort)
{
if (iptPort.node is ImageProcessingGraphNodeVisual vis &&
vis.GraphNode is GenericConnection conn &&
conn.InternalType != null)
{
return conn.InternalType;
}
return port.portType ?? typeof(object);
}
return port.portType ?? typeof(object);
}
private GraphViewChange OnGraphViewChanged(GraphViewChange graphviewchange) private GraphViewChange OnGraphViewChanged(GraphViewChange graphviewchange)
{ {