Added the functionality to add RelayNode directly to edge between two Prot;

This commit is contained in:
Misaki
2024-11-07 00:42:52 +09:00
parent 02ae77f17a
commit 5ac1081d32
16 changed files with 243 additions and 150 deletions

View File

@@ -70,7 +70,7 @@ namespace Misaki.GraphView.Editor
exposedProperty.propertyName = newValue;
_owner.Query<PropertyInputNodeView>().ForEach(n =>
{
if (n.Data.Property.Equals(exposedProperty))
if (n.DataNode.Property.Equals(exposedProperty))
{
n.title = newValue;
}

View File

@@ -33,15 +33,15 @@ namespace Misaki.GraphView.Editor
Add(gridBackground);
gridBackground.SendToBack();
var minimapConfig = _graphViewConfig.miniMapConfig;
if (minimapConfig is { enable: true })
var miniMapConfig = _graphViewConfig.miniMapConfig;
if (miniMapConfig is { enable: true })
{
var minimap = new MiniMap()
var miniMap = new MiniMap()
{
anchored = true
};
minimap.SetPosition(minimapConfig.position);
Add(minimap);
miniMap.SetPosition(miniMapConfig.position);
Add(miniMap);
}
_blackboardView = new GraphBlackboardView(_graphObject, this, _graphViewConfig.serializedObject, _graphViewConfig.exposedPropertyTypeManager);
@@ -135,9 +135,9 @@ namespace Misaki.GraphView.Editor
private void OnDragPerform(DragPerformEvent evt)
{
var data = DragAndDrop.GetGenericData("DragSelection");
if (data is List<ISelectable> selectables)
if (data is List<ISelectable> selectable)
{
var propertyViews = selectables.OfType<BlackboardPropertyView>().ToArray();
var propertyViews = selectable.OfType<BlackboardPropertyView>().ToArray();
if (propertyViews.Length <= 0)
{
return;

View File

@@ -28,7 +28,7 @@ namespace Misaki.GraphView.Editor
}, DropdownMenuAction.AlwaysEnabled);
}
if (evt.target is Edge)
if (evt.target is Edge edge)
{
evt.menu.AppendAction("Create Relay Node", e =>
{
@@ -37,7 +37,7 @@ namespace Misaki.GraphView.Editor
position = new Rect(mousePosition, Vector2.zero)
};
AddRelayNode(relayNode);
AddRelayNode(relayNode, edge);
}, DropdownMenuAction.AlwaysEnabled);
}
}

View File

@@ -46,16 +46,19 @@ namespace Misaki.GraphView.Editor
}
}
// if (graphViewChange.movedElements != null)
// {
// var movedElements = graphViewChange.movedElements;
// Undo.RecordObject(_graphObject, $"Move {movedElements.FirstOrDefault()?.GetType().Name}");
//
// foreach (var element in graphViewChange.movedElements)
// {
// element.SetPosition(element.GetPosition());
// }
// }
if (graphViewChange.movedElements != null)
{
var movedElements = graphViewChange.movedElements;
Undo.RecordObject(_graphObject, $"Move {movedElements.FirstOrDefault()?.GetType().Name}");
foreach (var element in movedElements)
{
if (element is IDataNodeView dataNodeView)
{
dataNodeView.GetDataNode().position = element.GetPosition();
}
}
}
if (graphViewChange.edgesToCreate != null)
{
@@ -63,14 +66,15 @@ namespace Misaki.GraphView.Editor
Undo.RecordObject(_graphObject, $"Connect {createdEdges.FirstOrDefault()?.GetType().Name}");
foreach (var edge in createdEdges)
{
if (edge.input.userData is Slot inputSlot && edge.output.userData is Slot outputSlot)
{
var connection = new SlotConnection(inputSlot.slotData, outputSlot.slotData);
outputSlot.Link(inputSlot, out var connection);
_slotConnections.Add(edge, connection);
outputSlot.Link(inputSlot);
AddConnection(connection);
}
}
}
_graphObject.SetGraphTransform(viewTransform);

View File

@@ -52,6 +52,10 @@ namespace Misaki.GraphView.Editor
type ??= typeof(ExecutableNodeView);
return Activator.CreateInstance(type, executableNode, _graphViewConfig.serializedObject, _graphViewConfig.portColorManager, _graphObject.Logger) as ExecutableNodeView;
}
else if (node is RelayNode relayNode)
{
return new RelayNodeView(relayNode, _graphViewConfig.portColorManager);
}
return null;
}
@@ -123,10 +127,14 @@ namespace Misaki.GraphView.Editor
EditorUtility.SetDirty(_graphObject);
}
private void RemoveConnection(SlotConnection connection)
private void RemoveConnection(SlotConnection connection, bool notify = true)
{
Undo.RecordObject(_graphObject, $"Remove {connection.GetType().Name}");
if (notify)
{
Undo.RecordObject(_graphObject, $"Remove {connection.GetType().Name}");
}
RemoveConnectionView(connection);
_graphObject.RemoveConnection(connection);
EditorUtility.SetDirty(_graphObject);
@@ -148,29 +156,55 @@ namespace Misaki.GraphView.Editor
return;
}
var inputPort = inputPortContainer.GetPort(inputSlotData.slotIndex, Direction.Input);
var outputPort = outputPortContainer.GetPort(outputSlotData.slotIndex, Direction.Output);
var portA = inputPortContainer.GetPort(inputSlotData.slotIndex, (Direction)inputSlotData.direction);
var portB = outputPortContainer.GetPort(outputSlotData.slotIndex, (Direction)outputSlotData.direction);
var edge = inputPort.ConnectTo(outputPort);
var edge = portA.ConnectTo(portB);
AddElement(edge);
_slotConnections.Add(edge, connection);
}
public void AddRelayNode(RelayNode relayNode)
private void RemoveConnectionView(SlotConnection connection)
{
var edge = _slotConnections.FirstOrDefault(x => x.Value == connection).Key;
if (edge != null)
{
RemoveElement(edge);
_slotConnections.Remove(edge);
}
}
public void AddRelayNode(RelayNode relayNode, Edge edge)
{
Undo.RecordObject(_graphObject, $"Add {relayNode.GetType().Name}");
_graphObject.AddNode(relayNode);
AddRelayNodeView(relayNode);
AddRelayNodeView(relayNode, edge);
var connection = _slotConnections[edge];
RemoveConnection(connection, false);
EditorUtility.SetDirty(_graphObject);
}
private void AddRelayNodeView(RelayNode relayNode)
private void AddRelayNodeView(RelayNode relayNode, Edge edge)
{
var relayNodeView = new RelayNodeView(relayNode, _graphViewConfig.portColorManager);
relayNodeView.SetPosition(relayNode.position);
relayNodeView.Connect(edge,
out var inputConnection, out var outputConnection,
out var inputEdge, out var outputEdge);
_graphObject.AddConnection(inputConnection);
_graphObject.AddConnection(outputConnection);
_slotConnections.Add(inputEdge, inputConnection);
_slotConnections.Add(outputEdge, outputConnection);
AddElement(relayNodeView);
AddElement(inputEdge);
AddElement(outputEdge);
}
}
}

View File

@@ -10,7 +10,7 @@ using UnityEngine.UIElements;
namespace Misaki.GraphView.Editor
{
public class ExecutableNodeView : Node, IInspectable, IPortContainer
public class ExecutableNodeView : Node, IInspectable, IPortContainer, IDataNodeView<ExecutableNode>
{
private readonly ExecutableNode _dataNode;
private readonly Type _nodeType;
@@ -24,6 +24,9 @@ namespace Misaki.GraphView.Editor
private readonly VisualElement _logContainer = new();
public ExecutableNode DataNode => _dataNode;
public DataNode GetDataNode() => _dataNode;
public Action<IInspectable> OnItemSelected
{
get; set;
@@ -194,12 +197,6 @@ namespace Misaki.GraphView.Editor
_outputPorts.Add(outputPort);
}
public override void SetPosition(Rect newPos)
{
base.SetPosition(newPos);
_dataNode.position = newPos;
}
public override void OnSelected()
{
base.OnSelected();

View File

@@ -1,21 +1,21 @@
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.UIElements;
namespace Misaki.GraphView.Editor
{
public class PropertyInputNodeView : TokenNode, IPortContainer
public class PropertyInputNodeView : TokenNode, IPortContainer, IDataNodeView<PropertyInput>
{
private readonly Port _outputPort;
private readonly PropertyInput _data;
private readonly PropertyInput _dataNode;
//private readonly ExposedPropertyEditor _editor;
public PropertyInput Data => _data;
public PropertyInput DataNode => _dataNode;
public DataNode GetDataNode() => _dataNode;
public PropertyInputNodeView(PropertyInput data, Port output) : base(null, output)
{
_data = data;
_dataNode = data;
_outputPort = output;
name = data.Property.propertyName;
@@ -60,13 +60,7 @@ namespace Misaki.GraphView.Editor
return port;
}
public string InspectorName => _data.Property.propertyName;
public override void SetPosition(Rect newPos)
{
base.SetPosition(newPos);
_data.position = newPos;
}
public string InspectorName => _dataNode.Property.propertyName;
public Port GetPort(int index, Direction direction)
{

View File

@@ -1,9 +1,10 @@
using System;
using UnityEditor.Experimental.GraphView;
using UnityEngine.UIElements;
namespace Misaki.GraphView.Editor
{
public class RelayNodeView : Node, IPortContainer
public class RelayNodeView : Node, IPortContainer, IDataNodeView<RelayNode>
{
private RelayNode _dataNode;
private IPortColorManager _portColorManager;
@@ -11,21 +12,56 @@ namespace Misaki.GraphView.Editor
private readonly Port _inputPort;
private readonly Port _outputPort;
public RelayNode DataNode => _dataNode;
public DataNode GetDataNode() => _dataNode;
public RelayNodeView(RelayNode dataNode, IPortColorManager portColorManager)
{
_dataNode = dataNode;
_portColorManager = portColorManager;
userData = dataNode;
_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.portName = string.Empty;
_outputPort.portName = string.Empty;
this.Q<VisualElement>("title").style.height = 0;
var divider = this.Q<VisualElement>("divider");
divider.style.height = 0;
divider.style.borderBottomWidth = 0;
SetPortsTypeAndColor(typeof(object));
_inputPort.style.height = 16;
_outputPort.style.height = 16;
inputContainer.Add(_inputPort);
outputContainer.Add(_outputPort);
var portType = Type.GetType(_dataNode.portValueType) ?? typeof(object);
SetPortsTypeAndColor(portType);
}
public void Connect(Edge edge, out SlotConnection inputConnection, out SlotConnection outputConnection, out Edge inputEdge, out Edge outputEdge)
{
inputConnection = default;
outputConnection = default;
inputEdge = null;
outputEdge = null;
var input = edge.input;
var output = edge.output;
if (input.userData is Slot inputSlot && output.userData is Slot outputSlot)
{
input.Disconnect(edge);
output.Disconnect(edge);
inputSlot.Link(_dataNode.GetSlot(0, SlotDirection.Output), out inputConnection);
outputSlot.Link(_dataNode.GetSlot(0, SlotDirection.Input), out outputConnection);
inputSlot.Unlink(outputSlot);
inputEdge = output.ConnectTo(_inputPort);
outputEdge = _outputPort.ConnectTo(input);
SetPortsTypeAndColor(input.portType);
}
}
private void SetPortsTypeAndColor(Type portType)
@@ -33,6 +69,9 @@ namespace Misaki.GraphView.Editor
_inputPort.portType = portType;
_outputPort.portType = portType;
_inputPort.portName = string.Empty;
_outputPort.portName = string.Empty;
if (_portColorManager != null)
{
if (_portColorManager.TryGetColor(portType, out var portColor))
@@ -41,6 +80,8 @@ namespace Misaki.GraphView.Editor
_outputPort.portColor = portColor;
}
}
_dataNode.portValueType = portType.FullName;
}
public Port GetPort(int index, Direction direction)