1
0
mirror of https://github.com/Siccity/xNode.git synced 2026-03-26 22:49:02 +08:00

Fixed code review issues

This commit is contained in:
Thomas Morgner 2018-11-20 10:22:24 +00:00
parent 956d8b1002
commit 1076fda3be
2 changed files with 45 additions and 47 deletions

View File

@ -62,8 +62,8 @@ namespace XNode {
protected void OnEnable() { protected void OnEnable() {
UpdateStaticPorts(); UpdateStaticPorts();
Init();
if (graph != null && !graph.CopyInProgress) { if (graph != null && !graph.copyInProgress) {
OnEnableOverride(); OnEnableOverride();
} }
} }
@ -80,11 +80,12 @@ namespace XNode {
/// <summary> /// <summary>
/// Initialize node. Called on creation and after clone in the correct order. /// Initialize node. Called on creation and after clone in the correct order.
/// </summary> /// </summary>
public virtual void OnEnableOverride() {
// Implementation note: This method is called after a node has been instantiated, and also // Implementation note: This method is called after a node has been instantiated, and also
// called directly when cloning a full graph. This simply calls Init(), which cannot be // called directly when cloning a full graph. This simply calls Init(), which cannot be
// called from outside, as it is protected, not public, and making it public would break // called from outside, as it is protected, not public, and making it public would break
// existing code. // existing code.
public virtual void OnEnableOverride() {
Init(); Init();
} }

View File

@ -11,10 +11,12 @@ namespace XNode {
/// See: <see cref="AddNode{T}"/> </summary> /// See: <see cref="AddNode{T}"/> </summary>
[SerializeField] public List<Node> nodes = new List<Node>(); [SerializeField] public List<Node> nodes = new List<Node>();
// Non serialized flag to indicate a copy operation is currently underway. /// <summary>
// This stops nodes from calling OnEnableOverride before we updated the /// Non serialized flag to indicate a copy operation is currently underway.
// graph reference. /// This stops nodes from calling OnEnableOverride before we updated the
public bool CopyInProgress { get; protected set; } /// graph reference.
/// </summary>
public bool copyInProgress { get; protected set; }
/// <summary> Add a node to the graph by type </summary> /// <summary> Add a node to the graph by type </summary>
public T AddNode<T>() where T : Node { public T AddNode<T>() where T : Node {
@ -32,19 +34,15 @@ namespace XNode {
/// <summary> Creates a copy of the original node in the graph </summary> /// <summary> Creates a copy of the original node in the graph </summary>
public virtual Node CopyNode(Node original) { public virtual Node CopyNode(Node original) {
this.CopyInProgress = true; this.copyInProgress = true;
try {
Node node = ScriptableObject.Instantiate(original); Node node = ScriptableObject.Instantiate(original);
node.ClearConnections(); node.ClearConnections();
nodes.Add(node); nodes.Add(node);
node.graph = this; node.graph = this;
node.OnEnableOverride(); node.OnEnableOverride();
this.copyInProgress = false;
return node; return node;
} }
finally {
this.CopyInProgress = false;
}
}
/// <summary> Safely remove a node and all its connections </summary> /// <summary> Safely remove a node and all its connections </summary>
/// <param name="node"> The node to remove </param> /// <param name="node"> The node to remove </param>
@ -68,8 +66,8 @@ namespace XNode {
public XNode.NodeGraph Copy() { public XNode.NodeGraph Copy() {
// Instantiate a new nodegraph instance // Instantiate a new nodegraph instance
NodeGraph graph = Instantiate(this); NodeGraph graph = Instantiate(this);
graph.CopyInProgress = true; graph.copyInProgress = true;
try {
// Instantiate all nodes inside the graph // Instantiate all nodes inside the graph
for (int i = 0; i < nodes.Count; i++) { for (int i = 0; i < nodes.Count; i++) {
if (nodes[i] == null) continue; if (nodes[i] == null) continue;
@ -93,10 +91,9 @@ namespace XNode {
if (node == null) continue; if (node == null) continue;
node.OnEnableOverride(); node.OnEnableOverride();
} }
}
finally { graph.copyInProgress = false;
graph.CopyInProgress = false;
}
return graph; return graph;
} }