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:
parent
956d8b1002
commit
1076fda3be
@ -62,8 +62,8 @@ namespace XNode {
|
||||
|
||||
protected void OnEnable() {
|
||||
UpdateStaticPorts();
|
||||
Init();
|
||||
if (graph != null && !graph.CopyInProgress) {
|
||||
|
||||
if (graph != null && !graph.copyInProgress) {
|
||||
OnEnableOverride();
|
||||
}
|
||||
}
|
||||
@ -80,11 +80,12 @@ namespace XNode {
|
||||
/// <summary>
|
||||
/// Initialize node. Called on creation and after clone in the correct order.
|
||||
/// </summary>
|
||||
// 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 from outside, as it is protected, not public, and making it public would break
|
||||
// existing code.
|
||||
public virtual void OnEnableOverride() {
|
||||
// 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 from outside, as it is protected, not public, and making it public would break
|
||||
// existing code.
|
||||
|
||||
Init();
|
||||
}
|
||||
|
||||
|
||||
@ -11,10 +11,12 @@ namespace XNode {
|
||||
/// See: <see cref="AddNode{T}"/> </summary>
|
||||
[SerializeField] public List<Node> nodes = new List<Node>();
|
||||
|
||||
// Non serialized flag to indicate a copy operation is currently underway.
|
||||
// This stops nodes from calling OnEnableOverride before we updated the
|
||||
// graph reference.
|
||||
public bool CopyInProgress { get; protected set; }
|
||||
/// <summary>
|
||||
/// Non serialized flag to indicate a copy operation is currently underway.
|
||||
/// This stops nodes from calling OnEnableOverride before we updated the
|
||||
/// graph reference.
|
||||
/// </summary>
|
||||
public bool copyInProgress { get; protected set; }
|
||||
|
||||
/// <summary> Add a node to the graph by type </summary>
|
||||
public T AddNode<T>() where T : Node {
|
||||
@ -32,18 +34,14 @@ namespace XNode {
|
||||
|
||||
/// <summary> Creates a copy of the original node in the graph </summary>
|
||||
public virtual Node CopyNode(Node original) {
|
||||
this.CopyInProgress = true;
|
||||
try {
|
||||
Node node = ScriptableObject.Instantiate(original);
|
||||
node.ClearConnections();
|
||||
nodes.Add(node);
|
||||
node.graph = this;
|
||||
node.OnEnableOverride();
|
||||
return node;
|
||||
}
|
||||
finally {
|
||||
this.CopyInProgress = false;
|
||||
}
|
||||
this.copyInProgress = true;
|
||||
Node node = ScriptableObject.Instantiate(original);
|
||||
node.ClearConnections();
|
||||
nodes.Add(node);
|
||||
node.graph = this;
|
||||
node.OnEnableOverride();
|
||||
this.copyInProgress = false;
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <summary> Safely remove a node and all its connections </summary>
|
||||
@ -68,36 +66,35 @@ namespace XNode {
|
||||
public XNode.NodeGraph Copy() {
|
||||
// Instantiate a new nodegraph instance
|
||||
NodeGraph graph = Instantiate(this);
|
||||
graph.CopyInProgress = true;
|
||||
try {
|
||||
// Instantiate all nodes inside the graph
|
||||
for (int i = 0; i < nodes.Count; i++) {
|
||||
if (nodes[i] == null) continue;
|
||||
Node node = Instantiate(nodes[i]) as Node;
|
||||
node.graph = graph;
|
||||
graph.nodes[i] = node;
|
||||
}
|
||||
graph.copyInProgress = true;
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
// Call the Enable notifier, which was not fired during the
|
||||
// normal instantiation.
|
||||
for (int i = 0; i < graph.nodes.Count; i++) {
|
||||
var node = graph.nodes[i];
|
||||
if (node == null) continue;
|
||||
node.OnEnableOverride();
|
||||
// Instantiate all nodes inside the graph
|
||||
for (int i = 0; i < nodes.Count; i++) {
|
||||
if (nodes[i] == null) continue;
|
||||
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);
|
||||
}
|
||||
}
|
||||
finally {
|
||||
graph.CopyInProgress = false;
|
||||
|
||||
// Call the Enable notifier, which was not fired during the
|
||||
// normal instantiation.
|
||||
for (int i = 0; i < graph.nodes.Count; i++) {
|
||||
var node = graph.nodes[i];
|
||||
if (node == null) continue;
|
||||
node.OnEnableOverride();
|
||||
}
|
||||
|
||||
graph.copyInProgress = false;
|
||||
|
||||
|
||||
return graph;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user