Skip to content
This repository has been archived by the owner on Oct 13, 2022. It is now read-only.

Commit

Permalink
Lay the groundwork for - drag port to connect to new node
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Rockman committed Jan 3, 2021
1 parent 0f48259 commit 14a26a9
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 52 deletions.
6 changes: 2 additions & 4 deletions Editor/UIXGraphAssetEditor.cs → Editor/GraphAssetEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

namespace RedOwl.Sleipnir.Editor
{
#if !ODIN_INSPECTOR
[CustomEditor(typeof(GraphAsset))]
public class UIXGraphAssetEditor : UnityEditor.Editor
public class GraphAssetEditor : UnityEditor.Editor
{
public override void OnInspectorGUI ()
{
if (GUILayout.Button("Open Editor", GUILayout.Height(40))) UIXGraphWindow.Open((GraphAsset) serializedObject.targetObject);
if (GUILayout.Button("Open Editor", GUILayout.Height(40))) SleipnirWindow.Open((GraphAsset) serializedObject.targetObject);
}
}
#endif
}
File renamed without changes.
22 changes: 22 additions & 0 deletions Editor/GraphNodePropertyProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#if ODIN_INSPECTOR
using System.Collections.Generic;
using RedOwl.Sleipnir.Engine;
using Sirenix.OdinInspector.Editor;

namespace RedOwl.Sleipnir.Editor
{
public class GraphNodePropertyProcessor<T> : OdinPropertyProcessor<T> where T : INode
{
public override void ProcessMemberProperties(List<InspectorPropertyInfo> propertyInfos)
{
var match = typeof(Port);
for (int i = propertyInfos.Count - 1; i >= 0; i--)
{
var info = propertyInfos[i];
if (match.IsAssignableFrom(info.TypeOfValue)) propertyInfos.RemoveAt(i);
}
}
}
}

#endif
53 changes: 53 additions & 0 deletions Editor/SleipnirGraphEdgeConnectorListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using PortView = UnityEditor.Experimental.GraphView.Port;

namespace RedOwl.Sleipnir.Editor
{
/// <summary>
/// Custom connector listener so that we can link up nodes and
/// open a search box when the user drops an edge into the canvas
/// </summary>
public class SleipnirGraphEdgeConnectorListener : IEdgeConnectorListener
{
private SleipnirGraphView view;

public SleipnirGraphEdgeConnectorListener(SleipnirGraphView view)
{
this.view = view;
}

/// <summary>
/// Handle connecting nodes when an edge is dropped between two ports
/// </summary>
public void OnDrop(GraphView graphView, Edge edge)
{
view.AddElement(edge);
}

/// <summary>
/// Activate the search dialog when an edge is dropped on an arbitrary location
/// </summary>
public void OnDropOutsidePort(Edge edge, Vector2 position)
{
var screenPosition = GUIUtility.GUIToScreenPoint(
Event.current.mousePosition
);

if (edge.output != null)
{
view.OpenSearch(
screenPosition,
edge.output.edgeConnector.edgeDragHelper.draggedPort as PortView
);
}
else if (edge.input != null)
{
view.OpenSearch(
screenPosition,
edge.input.edgeConnector.edgeDragHelper.draggedPort as PortView
);
}
}
}
}
3 changes: 3 additions & 0 deletions Editor/SleipnirGraphEdgeConnectorListener.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using RedOwl.Sleipnir.Engine;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
Expand Down Expand Up @@ -39,7 +38,7 @@ public void Add(SleipnirNodeReflection data)
}
}

public class UIXGraphSearchProvider : ScriptableObject, ISearchWindowProvider
public class SleipnirGraphSearchProvider : ScriptableObject, ISearchWindowProvider
{
private SleipnirGraphView _view;

Expand Down
13 changes: 10 additions & 3 deletions Editor/SleipnirGraphView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public class SleipnirGraphView : GraphView
public IGraph Graph => _graph;

private MiniMap _map;
private UIXGraphSearchProvider _search;
private SleipnirGraphSearchProvider _search;
private IEdgeConnectorListener _edgeConnectorListener;

public SleipnirGraphView(GraphAsset asset)
{
Expand Down Expand Up @@ -47,6 +48,7 @@ public SleipnirGraphView(GraphAsset asset)
this.AddManipulator(new RectangleSelector());
graphViewChanged = OnGraphViewChanged;

_edgeConnectorListener = new SleipnirGraphEdgeConnectorListener(this);
CreateGridBackground();
CreateMiniMap();
CreateSearch();
Expand All @@ -70,7 +72,7 @@ private void CreateMiniMap()

private void CreateSearch()
{
_search = ScriptableObject.CreateInstance<UIXGraphSearchProvider>();
_search = ScriptableObject.CreateInstance<SleipnirGraphSearchProvider>();
_search.Initialize(this);
nodeCreationRequest = ctx => SearchWindow.Open(new SearchWindowContext(ctx.screenMousePosition), _search);
}
Expand Down Expand Up @@ -221,7 +223,7 @@ internal void CreateNode(SleipnirNodeReflection data, Vector2 position)
private Dictionary<string, SleipnirNodeView> _nodeViewCache;
private void CreateNodeView<T>(T node) where T : INode
{
var element = new SleipnirNodeView(node);
var element = new SleipnirNodeView(node, _edgeConnectorListener);
_nodeViewCache.Add(node.NodeId, element);
AddElement(element);
}
Expand All @@ -233,6 +235,11 @@ internal void SaveAsset()
AssetDatabase.SaveAssets();
}

public void OpenSearch(Vector2 screenPosition, PortView port = null)
{
//_search.SourcePort = connectedPort;
SearchWindow.Open(new SearchWindowContext(screenPosition), _search);
}

/*
public void Clean()
Expand Down
29 changes: 13 additions & 16 deletions Editor/SleipnirNodeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ public class SleipnirNodeView : NodeView
public VisualElement FlowOutPortContainer { get; private set; }
public INode Model => (INode) userData;

public SleipnirNodeView(INode node)
public SleipnirNodeView(INode node, IEdgeConnectorListener listener)
{
userData = node;
Initialize(node);
Initialize(node, listener);
}

private void Initialize(INode node)
private void Initialize(INode node, IEdgeConnectorListener listener)
{
name = node.NodeId;
title = node.NodeTitle;
Expand All @@ -35,9 +35,9 @@ private void Initialize(INode node)

CreateBody(node);
CreateFlowPortContainers();
if (node is IFlowNode flowNode) CreateFlowPorts(flowNode);
if (node is IFlowNode flowNode) CreateFlowPorts(flowNode, listener);
AttachFlowPortContainers();
CreateValuePorts(node);
CreateValuePorts(node, listener);
RefreshExpandedState();
RefreshPorts();
}
Expand All @@ -53,25 +53,25 @@ private void CreateBody(INode node)
#endif
}

private PortView CreatePortView(IPort valuePort, Orientation orientation)
private PortView CreatePortView(IPort valuePort, Orientation orientation, IEdgeConnectorListener listener)
{
var view = PortView.Create<Edge>(orientation, ConvertDirection(valuePort.Direction), ConvertCapacity(valuePort.Capacity), valuePort.ValueType);
var view = new SleipnirPortView(orientation, valuePort.Direction, valuePort.Capacity, valuePort.ValueType, listener);
view.name = valuePort.Id.Port;
view.userData = valuePort;
view.portName = valuePort.Name;
return view;
}

private void CreateValuePorts(INode node)
private void CreateValuePorts(INode node, IEdgeConnectorListener listener)
{
foreach (var valuePort in node.ValueInPorts.Values)
{
inputContainer.Add(CreatePortView(valuePort, Orientation.Horizontal));
inputContainer.Add(CreatePortView(valuePort, Orientation.Horizontal, listener));
}

foreach (var valuePort in node.ValueOutPorts.Values)
{
outputContainer.Add(CreatePortView(valuePort, Orientation.Horizontal));
outputContainer.Add(CreatePortView(valuePort, Orientation.Horizontal, listener));
}
}

Expand All @@ -83,16 +83,16 @@ private void CreateFlowPortContainers()
FlowOutPortContainer.AddToClassList("FlowOutPorts");
}

private void CreateFlowPorts(IFlowNode node)
private void CreateFlowPorts(IFlowNode node, IEdgeConnectorListener listener)
{
foreach (var flowPort in node.FlowInPorts.Values)
{
FlowInPortContainer.Add(CreatePortView(flowPort, Orientation.Vertical));
FlowInPortContainer.Add(CreatePortView(flowPort, Orientation.Vertical, listener));
}

foreach (var flowPort in node.FlowOutPorts.Values)
{
FlowOutPortContainer.Add(CreatePortView(flowPort, Orientation.Vertical));
FlowOutPortContainer.Add(CreatePortView(flowPort, Orientation.Vertical, listener));
}
}

Expand All @@ -101,9 +101,6 @@ private void AttachFlowPortContainers()
if (FlowInPortContainer.childCount > 0) mainContainer.parent.Insert(0, FlowInPortContainer);
if (FlowOutPortContainer.childCount > 0) mainContainer.parent.Add(FlowOutPortContainer);
}

private Direction ConvertDirection(PortDirection value) => value == PortDirection.Input ? Direction.Input : Direction.Output;
private PortView.Capacity ConvertCapacity(PortCapacity value) => value == PortCapacity.Single ? PortView.Capacity.Single : PortView.Capacity.Multi;
}

public static class NodeViewExtensions
Expand Down
20 changes: 20 additions & 0 deletions Editor/SleipnirPortView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using RedOwl.Sleipnir.Engine;
using UnityEditor.Experimental.GraphView;
using UnityEngine.UIElements;
using PortView = UnityEditor.Experimental.GraphView.Port;

namespace RedOwl.Sleipnir.Editor
{
public class SleipnirPortView : PortView
{
public SleipnirPortView(Orientation orientation, PortDirection direction, PortCapacity capacity, Type type, IEdgeConnectorListener listener) : base(orientation, ConvertDirection(direction), ConvertCapacity(capacity), type)
{
m_EdgeConnector = new EdgeConnector<Edge>(listener);
this.AddManipulator(m_EdgeConnector);
}

private static Direction ConvertDirection(PortDirection value) => value == PortDirection.Input ? Direction.Input : Direction.Output;
private static Capacity ConvertCapacity(PortCapacity value) => value == PortCapacity.Single ? Capacity.Single : Capacity.Multi;
}
}
3 changes: 3 additions & 0 deletions Editor/SleipnirPortView.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Editor/SleipnirWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public static bool OnOpenAsset(int instanceID, int line)

public static SleipnirWindow GetOrCreate()
{
// TODO: Support Multi Window
// TODO: Is there a way to "find" the window with the current asset and just focus it?
// var window = CreateInstance<SleipnirWindow>();
// window.Show();
var window = GetWindow<SleipnirWindow>();
window.titleContent = new GUIContent("Graph Editor");
return window;
Expand Down
27 changes: 0 additions & 27 deletions Editor/UIXGraphNodePropertyProcessor.cs

This file was deleted.

0 comments on commit 14a26a9

Please sign in to comment.