Fixed the bug that RealyNode can not connect and disconnect correctly.

This commit is contained in:
Misaki
2024-11-07 21:13:01 +09:00
parent 5ac1081d32
commit e15570459c
6 changed files with 121 additions and 112 deletions

View File

@@ -3,6 +3,7 @@ using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
namespace Misaki.GraphView.Editor
{
@@ -64,8 +65,8 @@ namespace Misaki.GraphView.Editor
{
Undo.RecordObject(_graphObject, $"Remove {dataNode.GetType().Name}");
_graphObject.RemoveNode(dataNode);
RemoveNodeView(dataNode);
_graphObject.RemoveNode(dataNode);
EditorUtility.SetDirty(_graphObject);
}
@@ -74,12 +75,25 @@ namespace Misaki.GraphView.Editor
{
if (_nodeViewsMap.Remove(dataNode.Id, out var nodeView))
{
RemoveElement(nodeView);
if (nodeView is IInspectable inspectable)
{
inspectable.OnItemSelected -= ChangeInspectorView;
}
if (nodeView is RelayNodeView relayNodeView)
{
relayNodeView.Disconnect(out var newConnections, out var newEdges);
_graphObject.AddConnections(newConnections);
var count = Mathf.Min(newConnections.Count, newEdges.Count);
for (var i = 0; i < count; i++)
{
_slotConnections.Add(newEdges[i], newConnections[i]);
AddElement(newEdges[i]);
}
}
RemoveElement(nodeView);
}
}
@@ -104,9 +118,8 @@ namespace Misaki.GraphView.Editor
{
Undo.RecordObject(_graphObject, $"Remove {stickyNote.title}");
_graphObject.RemoveStickyNote(stickyNote);
RemoveStickyNoteView(stickyNote);
_graphObject.RemoveStickyNote(stickyNote);
}
private void RemoveStickyNoteView(StickyNoteData stickyNote)
@@ -174,7 +187,7 @@ namespace Misaki.GraphView.Editor
}
}
public void AddRelayNode(RelayNode relayNode, Edge edge)
private void AddRelayNode(RelayNode relayNode, Edge edge)
{
Undo.RecordObject(_graphObject, $"Add {relayNode.GetType().Name}");
@@ -205,6 +218,8 @@ namespace Misaki.GraphView.Editor
AddElement(relayNodeView);
AddElement(inputEdge);
AddElement(outputEdge);
_nodeViewsMap.Add(relayNode.Id, relayNodeView);
}
}
}

View File

@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.Experimental.GraphView;
using UnityEngine.UIElements;
@@ -24,6 +26,9 @@ namespace Misaki.GraphView.Editor
_inputPort = Port.Create<Edge>(Orientation.Horizontal, Direction.Input, Port.Capacity.Single, typeof(object));
_outputPort = Port.Create<Edge>(Orientation.Horizontal, Direction.Output, Port.Capacity.Multi, typeof(object));
_inputPort.userData = _dataNode.GetSlot(0, SlotDirection.Input);
_outputPort.userData = _dataNode.GetSlot(0, SlotDirection.Output);
this.Q<VisualElement>("title").style.height = 0;
var divider = this.Q<VisualElement>("divider");
divider.style.height = 0;
@@ -64,6 +69,38 @@ namespace Misaki.GraphView.Editor
}
}
public void Disconnect(out List<SlotConnection> newConnections, out List<Edge> newEdges)
{
newConnections = new List<SlotConnection>();
newEdges = new List<Edge>();
if (_inputPort.userData is not Slot inputSlot || _outputPort.userData is not Slot outputSlot)
{
return;
}
var linkedOutputPort = _inputPort.connections.FirstOrDefault().output;
foreach (var edge in _outputPort.connections.ToList())
{
var linkedInputPort = edge.input;
if (linkedOutputPort.userData is Slot linkedOutputSlot && linkedInputPort.userData is Slot linkedInputSlot)
{
linkedOutputSlot.Link(linkedInputSlot, out var inputConnection);
newConnections.Add(inputConnection);
newEdges.Add(linkedOutputPort.ConnectTo(linkedInputPort));
}
}
inputSlot.UnlinkAll();
outputSlot.UnlinkAll();
_inputPort.DisconnectAll();
_outputPort.DisconnectAll();
return;
}
private void SetPortsTypeAndColor(Type portType)
{
_inputPort.portType = portType;