1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 17:26:02 +08:00

Added public overrides for custom NodePort CanConnect conditions in GraphEditor

This commit is contained in:
Thor Brigsted 2022-09-21 13:53:28 +02:00
parent fa62765daa
commit 0e7e4aa254
3 changed files with 22 additions and 15 deletions

View File

@ -221,7 +221,7 @@ namespace XNodeEditor {
//Port drag release
if (IsDraggingPort) {
// If connection is valid, save it
if (draggedOutputTarget != null && draggedOutput.CanConnectTo(draggedOutputTarget)) {
if (draggedOutputTarget != null && graphEditor.CanConnect(draggedOutput, draggedOutputTarget)) {
XNode.Node node = draggedOutputTarget.node;
if (graph.nodes.Count != 0) draggedOutput.Connect(draggedOutputTarget);
@ -553,12 +553,9 @@ namespace XNodeEditor {
public void AutoConnect(XNode.Node node) {
if (autoConnectOutput == null) return;
// Find input port of same type
XNode.NodePort inputPort = node.Ports.FirstOrDefault(x => x.IsInput && x.ValueType == autoConnectOutput.ValueType);
// Fallback to input port
if (inputPort == null) inputPort = node.Ports.FirstOrDefault(x => x.IsInput);
// Autoconnect if connection is compatible
if (inputPort != null && inputPort.CanConnectTo(autoConnectOutput)) autoConnectOutput.Connect(inputPort);
// Find compatible input port
XNode.NodePort inputPort = node.Ports.FirstOrDefault(x => x.IsInput && graphEditor.CanConnect(autoConnectOutput, x));
if (inputPort != null) autoConnectOutput.Connect(inputPort);
// Save changes
EditorUtility.SetDirty(graph);

View File

@ -59,6 +59,13 @@ namespace XNodeEditor {
return 0;
}
/// <summary>
/// Called before connecting two ports in the graph view to see if the output port is compatible with the input port
/// </summary>
public virtual bool CanConnect(XNode.NodePort output, XNode.NodePort input) {
return output.CanConnectTo(input);
}
/// <summary>
/// Add items for the context menu when right-clicking this node.
/// Override to add custom menu items.

View File

@ -70,6 +70,9 @@ namespace XNode {
for (int i = 0; i < reconnectConnections.Count; i++) {
NodePort connection = reconnectConnections[i];
if (connection == null) continue;
// CAVEAT: Ports connected under special conditions defined in graphEditor.CanConnect overrides will not auto-connect.
// To fix this, this code would need to be moved to an editor script and call graphEditor.CanConnect instead of port.CanConnectTo.
// This is only a problem in the rare edge case where user is using non-standard CanConnect overrides and changes port type of an already connected port
if (port.CanConnectTo(connection)) port.Connect(connection);
}
}
@ -196,7 +199,7 @@ namespace XNode {
portDataCache[nodeType].Add(new NodePort(fieldInfo[i]));
}
if(formerlySerializedAsAttribute != null) {
if (formerlySerializedAsAttribute != null) {
if (formerlySerializedAsCache == null) formerlySerializedAsCache = new Dictionary<System.Type, Dictionary<string, string>>();
if (!formerlySerializedAsCache.ContainsKey(nodeType)) formerlySerializedAsCache.Add(nodeType, new Dictionary<string, string>());