1
0
mirror of https://github.com/Siccity/xNode.git synced 2026-02-06 15:24:55 +08:00

Visual representation in DrawConnections

This commit is contained in:
Thor Brigsted 2018-04-01 02:15:47 +02:00
parent 0132c16448
commit 34e195e33d
3 changed files with 29 additions and 9 deletions

View File

@ -217,6 +217,8 @@ namespace XNodeEditor {
/// <summary> Draws all connections </summary>
public void DrawConnections() {
List<int> drawnReroutes = new List<int>();
foreach (XNode.Node node in graph.nodes) {
//If a null node is found, return. This can happen if the nodes associated script is deleted. It is currently not possible in Unity to delete a null asset.
if (node == null) continue;
@ -224,16 +226,29 @@ namespace XNodeEditor {
foreach (XNode.NodePort output in node.Outputs) {
//Needs cleanup. Null checks are ugly
if (!portConnectionPoints.ContainsKey(output)) continue;
Color connectionColor = graphEditor.GetTypeColor(output.ValueType);
Vector2 from = _portConnectionPoints[output].center;
for (int k = 0; k < output.ConnectionCount; k++) {
XNode.NodePort input = output.GetConnection(k);
// Error handling
if (input == null) continue; //If a script has been updated and the port doesn't exist, it is removed and null is returned. If this happens, return.
if (!input.IsConnectedTo(output)) input.Connect(output);
if (!_portConnectionPoints.ContainsKey(input)) continue;
Vector2 to = _portConnectionPoints[input].center;
Color connectionColor = graphEditor.GetTypeColor(output.ValueType);
DrawConnection(from, to, connectionColor);
Vector2 to = Vector2.zero;
int[] rerouteIndices = output.GetReroutes(k);
for (int i = 0; i < rerouteIndices.Length + 1; i++) {
if (i != rerouteIndices.Length) to = graph.reroutes[rerouteIndices[i]];
else to = _portConnectionPoints[input].center;
DrawConnection(from, to, connectionColor);
from = to;
if (drawnReroutes.Contains(i)) break;
else drawnReroutes.Add(i);
}
}
}
}

View File

@ -10,11 +10,8 @@ namespace XNode {
/// <summary> All nodes in the graph. <para/>
/// See: <see cref="AddNode{T}"/> </summary>
[SerializeField] public List<Node> nodes = new List<Node>();
#if UNITY_EDITOR
/// <summary> Nodes used primarily for organization (Editor only) </summary>
public List<Vector2> reroutes = new List<Vector2>();
#endif
/// <summary> Nodes used primarily for organization </summary>
[SerializeField] public List<Vector2> reroutes = new List<Vector2>();
/// <summary> Add a node to the graph by type </summary>
public T AddNode<T>() where T : Node {

View File

@ -250,6 +250,12 @@ namespace XNode {
}
}
/// <summary> Get reroute indices. This is used for graph organization purposes </summary>
/// <param name="i"> Connection index </param>
public int[] GetReroutes(int i) {
return connections[i].reroutes;
}
/// <summary> Swap connected nodes from the old list with nodes from the new list </summary>
public void Redirect(List<Node> oldNodes, List<Node> newNodes) {
foreach (PortConnection connection in connections) {
@ -263,6 +269,8 @@ namespace XNode {
[SerializeField] public string fieldName;
[SerializeField] public Node node;
public NodePort Port { get { return port != null ? port : port = GetPort(); } }
/// <summary> Used for organization</summary>
[SerializeField] public int[] reroutes = new int[0];
[NonSerialized] private NodePort port;