Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improving extendability by making accessors more open #287

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Scripts/Editor/NodeEditor.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class NodeEditor : XNodeEditor.Internal.NodeEditorBase<NodeEditor, NodeEd

/// <summary> Fires every whenever a node was modified through the editor </summary>
public static Action<XNode.Node> onUpdateNode;
public readonly static Dictionary<XNode.NodePort, Vector2> portPositions = new Dictionary<XNode.NodePort, Vector2>();
public static readonly Dictionary<XNode.NodePort, Vector2> portPositions = new Dictionary<XNode.NodePort, Vector2>();

#if ODIN_INSPECTOR
protected internal static bool inNodeEditor = false;
Expand Down
16 changes: 8 additions & 8 deletions Scripts/Editor/NodeEditorAction.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ public enum NodeActivity { Idle, HoldNode, DragNode, HoldGrid, DragGrid }
private bool IsHoveringPort { get { return hoveredPort != null; } }
private bool IsHoveringNode { get { return hoveredNode != null; } }
private bool IsHoveringReroute { get { return hoveredReroute.port != null; } }
private XNode.Node hoveredNode = null;
protected XNode.Node hoveredNode = null;
[NonSerialized] public XNode.NodePort hoveredPort = null;
[NonSerialized] private XNode.NodePort draggedOutput = null;
[NonSerialized] private XNode.NodePort draggedOutputTarget = null;
[NonSerialized] private XNode.NodePort autoConnectOutput = null;
[NonSerialized] private List<Vector2> draggedOutputReroutes = new List<Vector2>();
private RerouteReference hoveredReroute = new RerouteReference();
protected RerouteReference hoveredReroute = new RerouteReference();
public List<RerouteReference> selectedReroutes = new List<RerouteReference>();
private Vector2 dragBoxStart;
private UnityEngine.Object[] preBoxSelection;
private RerouteReference[] preBoxSelectionReroute;
private Rect selectionBox;
protected Vector2 dragBoxStart;
protected UnityEngine.Object[] preBoxSelection;
protected RerouteReference[] preBoxSelectionReroute;
protected Rect selectionBox;
private bool isDoubleClick = false;
private Vector2 lastMousePosition;
private float dragThreshold = 1f;

public void Controls() {
protected virtual void Controls() {
wantsMouseMove = true;
Event e = Event.current;
switch (e.type) {
Expand Down Expand Up @@ -483,7 +483,7 @@ private void InsertDuplicateNodes(XNode.Node[] nodes, Vector2 topLeft) {
}

/// <summary> Draw a connection as we are dragging it </summary>
public void DrawDraggedConnection() {
protected virtual void DrawDraggedConnection() {
if (IsDraggingPort) {
Gradient gradient = graphEditor.GetNoodleGradient(draggedOutput, null);
float thickness = graphEditor.GetNoodleThickness(draggedOutput, null);
Expand Down
402 changes: 231 additions & 171 deletions Scripts/Editor/NodeEditorGUI.cs

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion Scripts/Editor/NodeEditorResources.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ public static class NodeEditorResources {
public static Styles _styles = null;
public static GUIStyle OutputPort { get { return new GUIStyle(EditorStyles.label) { alignment = TextAnchor.UpperRight }; } }
public class Styles {
public GUIStyle inputPort, nodeHeader, nodeBody, tooltip, nodeHighlight;
public GUIStyle inputPort, nodeHeader, nodeBody, tooltip, nodeHighlight, connectionLabel;

public Styles() {
GUIStyle baseStyle = new GUIStyle("Label");
baseStyle.fixedHeight = 18;

connectionLabel = new GUIStyle(EditorStyles.boldLabel);
connectionLabel.richText = true;
connectionLabel.alignment = TextAnchor.MiddleCenter;

inputPort = new GUIStyle(baseStyle);
inputPort.alignment = TextAnchor.UpperLeft;
inputPort.padding.left = 10;
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Editor/NodeEditorWindow.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private static void OnSelectionChanged() {
}

/// <summary> Make sure the graph editor is assigned and to the right object </summary>
private void ValidateGraphEditor() {
protected void ValidateGraphEditor() {
NodeGraphEditor graphEditor = NodeGraphEditor.GetEditor(graph, this);
if (this.graphEditor != graphEditor && graphEditor != null) {
this.graphEditor = graphEditor;
Expand Down
8 changes: 4 additions & 4 deletions Scripts/Node.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void ClearInstancePorts() {
/// <summary> Position on the <see cref="NodeGraph"/> </summary>
[SerializeField] public Vector2 position;
/// <summary> It is recommended not to modify these at hand. Instead, see <see cref="InputAttribute"/> and <see cref="OutputAttribute"/> </summary>
[SerializeField] private NodePortDictionary ports = new NodePortDictionary();
[SerializeField] protected NodePortDictionary ports = new NodePortDictionary();

/// <summary> Used during node instantiation to fix null/misconfigured graph during OnEnable/Init. Set it before instantiating a node. Will automatically be unset during OnEnable </summary>
public static NodeGraph graphHotfix;
Expand Down Expand Up @@ -140,14 +140,14 @@ public void VerifyConnections() {
/// <summary> Convenience function. </summary>
/// <seealso cref="AddInstancePort"/>
/// <seealso cref="AddInstanceOutput"/>
public NodePort AddDynamicInput(Type type, Node.ConnectionType connectionType = Node.ConnectionType.Multiple, Node.TypeConstraint typeConstraint = TypeConstraint.None, string fieldName = null) {
public virtual NodePort AddDynamicInput(Type type, Node.ConnectionType connectionType = Node.ConnectionType.Multiple, Node.TypeConstraint typeConstraint = TypeConstraint.None, string fieldName = null) {
return AddDynamicPort(type, NodePort.IO.Input, connectionType, typeConstraint, fieldName);
}

/// <summary> Convenience function. </summary>
/// <seealso cref="AddInstancePort"/>
/// <seealso cref="AddInstanceInput"/>
public NodePort AddDynamicOutput(Type type, Node.ConnectionType connectionType = Node.ConnectionType.Multiple, Node.TypeConstraint typeConstraint = TypeConstraint.None, string fieldName = null) {
public virtual NodePort AddDynamicOutput(Type type, Node.ConnectionType connectionType = Node.ConnectionType.Multiple, Node.TypeConstraint typeConstraint = TypeConstraint.None, string fieldName = null) {
return AddDynamicPort(type, NodePort.IO.Output, connectionType, typeConstraint, fieldName);
}

Expand Down Expand Up @@ -387,7 +387,7 @@ public NodeWidthAttribute(int width) {
}
#endregion

[Serializable] private class NodePortDictionary : Dictionary<string, NodePort>, ISerializationCallbackReceiver {
[Serializable] protected class NodePortDictionary : Dictionary<string, NodePort>, ISerializationCallbackReceiver {
[SerializeField] private List<string> keys = new List<string>();
[SerializeField] private List<NodePort> values = new List<NodePort>();

Expand Down
37 changes: 10 additions & 27 deletions Scripts/NodePort.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public NodePort Connection {
}
}

public IO direction {
public IO direction {
get { return _direction; }
internal set { _direction = value; }
}
Expand Down Expand Up @@ -202,7 +202,8 @@ public int GetInputSum(int fallback) {

/// <summary> Connect this <see cref="NodePort"/> to another </summary>
/// <param name="port">The <see cref="NodePort"/> to connect to</param>
public void Connect(NodePort port) {
/// <param name="connectionLabel">The optional label of the connection</param>
public void Connect(NodePort port, string connectionLabel = null) {
if (connections == null) connections = new List<PortConnection>();
if (port == null) { Debug.LogWarning("Cannot connect to null port"); return; }
if (port == this) { Debug.LogWarning("Cannot connect port to self."); return; }
Expand All @@ -214,9 +215,9 @@ public void Connect(NodePort port) {
#endif
if (port.connectionType == Node.ConnectionType.Override && port.ConnectionCount != 0) { port.ClearConnections(); }
if (connectionType == Node.ConnectionType.Override && ConnectionCount != 0) { ClearConnections(); }
connections.Add(new PortConnection(port));
connections.Add(new PortConnection(port, connectionLabel));
if (port.connections == null) port.connections = new List<PortConnection>();
if (!port.IsConnectedTo(this)) port.connections.Add(new PortConnection(this));
if (!port.IsConnectedTo(this)) port.connections.Add(new PortConnection(this, connectionLabel));
node.OnCreateConnection(this, port);
port.node.OnCreateConnection(this, port);
}
Expand All @@ -230,6 +231,11 @@ public List<NodePort> GetConnections() {
return result;
}

public PortConnection GetPortConnection(int index)
{
return connections[index];
}

public NodePort GetConnection(int i) {
//If the connection is broken for some reason, remove it.
if (connections[i].node == null || string.IsNullOrEmpty(connections[i].fieldName)) {
Expand Down Expand Up @@ -391,28 +397,5 @@ public void Redirect(List<Node> oldNodes, List<Node> newNodes) {
if (index >= 0) connection.node = newNodes[index];
}
}

[Serializable]
private class PortConnection {
[SerializeField] public string fieldName;
[SerializeField] public Node node;
public NodePort Port { get { return port != null ? port : port = GetPort(); } }

[NonSerialized] private NodePort port;
/// <summary> Extra connection path points for organization </summary>
[SerializeField] public List<Vector2> reroutePoints = new List<Vector2>();

public PortConnection(NodePort port) {
this.port = port;
node = port.node;
fieldName = port.fieldName;
}

/// <summary> Returns the port that this <see cref="PortConnection"/> points to </summary>
private NodePort GetPort() {
if (node == null || string.IsNullOrEmpty(fieldName)) return null;
return node.GetPort(fieldName);
}
}
}
}
31 changes: 31 additions & 0 deletions Scripts/PortConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using UnityEngine;

namespace XNode
{
[Serializable]
public class PortConnection {
[SerializeField] public string fieldName;
[SerializeField] public string connectionLabel;
[SerializeField] public Node node;
public NodePort Port { get { return port != null ? port : port = GetPort(); } }

[NonSerialized] protected NodePort port;
/// <summary> Extra connection path points for organization </summary>
[SerializeField] public List<Vector2> reroutePoints = new List<Vector2>();

public PortConnection(NodePort port, string connectionLabel = null) {
this.port = port;
node = port.node;
fieldName = port.fieldName;
this.connectionLabel = connectionLabel;
}

/// <summary> Returns the port that this <see cref="PortConnection"/> points to </summary>
private NodePort GetPort() {
if (node == null || string.IsNullOrEmpty(fieldName)) return null;
return node.GetPort(fieldName);
}
}
}
3 changes: 3 additions & 0 deletions Scripts/PortConnection.cs.meta

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