1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 01:06:01 +08:00
xNode/Scripts/NodeGraph.cs
Thor Brigsted aeeeb74290 WIP
2019-07-24 14:37:51 +02:00

103 lines
4.0 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
namespace XNode {
/// <summary> Base class for all node graphs </summary>
[Serializable]
public abstract class NodeGraph : ScriptableObject, INodeGraph {
/// <summary> All nodes in the graph. <para/>
/// See: <see cref="AddNode{T}"/> </summary>
[SerializeField] public List<Node> nodes = new List<Node>();
#region Interface implementation
IEnumerable<INode> INodeGraph.Nodes { get { foreach (Node node in nodes) yield return node; } }
UnityEngine.Object INodeGraph.Object { get { return this; } }
INode INodeGraph.AddNode(Type type) { return AddNode(type); }
void INodeGraph.MoveNodeToTop(INode node) { MoveNodeToTop(node as Node); }
INode INodeGraph.CopyNode(INode original) { return CopyNode(original as Node); }
void INodeGraph.RemoveNode(INode node) { RemoveNode(node as Node); }
#endregion
/// <summary> Add a node to the graph by type (convenience method - will call the System.Type version) </summary>
public T AddNode<T>() where T : Node {
return AddNode(typeof(T)) as T;
}
/// <summary> Draw this node on top of other nodes by placing it last in the graph.nodes list </summary>
public void MoveNodeToTop(XNode.Node node) {
int index;
while ((index = nodes.IndexOf(node as Node)) != nodes.Count - 1) {
nodes[index] = nodes[index + 1];
nodes[index + 1] = node as Node;
}
}
/// <summary> Add a node to the graph by type </summary>
public virtual Node AddNode(Type type) {
Node.graphHotfix = this;
Node node = ScriptableObject.CreateInstance(type) as Node;
node.graph = this;
nodes.Add(node);
return node;
}
/// <summary> Creates a copy of the original node in the graph </summary>
public virtual Node CopyNode(Node original) {
Node.graphHotfix = this;
Node node = ScriptableObject.Instantiate(original);
node.graph = this;
node.ClearConnections();
nodes.Add(node);
return node;
}
/// <summary> Safely remove a node and all its connections </summary>
/// <param name="node"> The node to remove </param>
public virtual void RemoveNode(Node node) {
node.ClearConnections();
nodes.Remove(node);
if (Application.isPlaying) Destroy(node);
}
/// <summary> Remove all nodes and connections from the graph </summary>
public virtual void Clear() {
if (Application.isPlaying) {
for (int i = 0; i < nodes.Count; i++) {
Destroy(nodes[i]);
}
}
nodes.Clear();
}
/// <summary> Create a new deep copy of this graph </summary>
public virtual XNode.NodeGraph Copy() {
// Instantiate a new nodegraph instance
NodeGraph graph = Instantiate(this);
// Instantiate all nodes inside the graph
for (int i = 0; i < nodes.Count; i++) {
if (nodes[i] == null) continue;
Node.graphHotfix = graph;
Node node = Instantiate(nodes[i]) as Node;
node.graph = graph;
graph.nodes[i] = node;
}
// Redirect all connections
for (int i = 0; i < graph.nodes.Count; i++) {
if (graph.nodes[i] == null) continue;
foreach (NodePort port in graph.nodes[i].Ports) {
port.Redirect(nodes, graph.nodes);
}
}
return graph;
}
protected virtual void OnDestroy() {
// Remove all nodes prior to graph destruction
Clear();
}
}
}