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

Nodes can now receive notifications after a rename has taken place.

This commit is contained in:
Lumos 2019-12-31 12:53:34 +01:00
parent e98bd85531
commit a68ec65e6c
3 changed files with 30 additions and 3 deletions

View File

@ -129,9 +129,33 @@ namespace XNodeEditor {
public void Rename(string newName) { public void Rename(string newName) {
if (newName == null || newName.Trim() == "") newName = NodeEditorUtilities.NodeDefaultName(target.GetType()); if (newName == null || newName.Trim() == "") newName = NodeEditorUtilities.NodeDefaultName(target.GetType());
target.name = newName; target.name = newName;
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(target)); TriggerRenameNotifications(target);
} }
/// <summary>
/// Triggers OnRename on all nodes in the graph after a node's name changes,
/// reimporting asset files for all subsequently renamed nodes.
/// </summary>
public static void TriggerRenameNotifications(XNode.Node renamedNode) {
List<XNode.Node> graphNodes = renamedNode.graph.nodes;
string[] oldNodeNames = new string[graphNodes.Count];
// Notify all nodes, allow them to further change names accordingly
for (int i = 0; i < graphNodes.Count; i++) {
XNode.Node node = graphNodes[i];
oldNodeNames[i] = node.name;
node.OnRename(renamedNode);
}
// After all user-driven modifications have been resolved, update all assets whose node names have changed
// (And always reimport the node which got renamed in the first place)
for (int i = 0; i < graphNodes.Count; i++) {
if (graphNodes[i] == renamedNode || graphNodes[i].name != oldNodeNames[i]) {
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(graphNodes[i]));
}
}
}
[AttributeUsage(AttributeTargets.Class)] [AttributeUsage(AttributeTargets.Class)]
public class CustomNodeEditorAttribute : Attribute, public class CustomNodeEditorAttribute : Attribute,
XNodeEditor.Internal.NodeEditorBase<NodeEditor, NodeEditor.CustomNodeEditorAttribute, XNode.Node>.INodeEditorAttrib { XNodeEditor.Internal.NodeEditorBase<NodeEditor, NodeEditor.CustomNodeEditorAttribute, XNode.Node>.INodeEditorAttrib {

View File

@ -49,7 +49,7 @@ namespace XNodeEditor {
if (input == null || input.Trim() == "") { if (input == null || input.Trim() == "") {
if (GUILayout.Button("Revert to default") || (e.isKey && e.keyCode == KeyCode.Return)) { if (GUILayout.Button("Revert to default") || (e.isKey && e.keyCode == KeyCode.Return)) {
target.name = NodeEditorUtilities.NodeDefaultName(target.GetType()); target.name = NodeEditorUtilities.NodeDefaultName(target.GetType());
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(target)); NodeEditor.TriggerRenameNotifications((XNode.Node)target);
Close(); Close();
target.TriggerOnValidate(); target.TriggerOnValidate();
} }
@ -58,7 +58,7 @@ namespace XNodeEditor {
else { else {
if (GUILayout.Button("Apply") || (e.isKey && e.keyCode == KeyCode.Return)) { if (GUILayout.Button("Apply") || (e.isKey && e.keyCode == KeyCode.Return)) {
target.name = input; target.name = input;
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(target)); NodeEditor.TriggerRenameNotifications((XNode.Node)target);
Close(); Close();
target.TriggerOnValidate(); target.TriggerOnValidate();
} }

View File

@ -255,6 +255,9 @@ namespace XNode {
/// <param name="port">Output or Input</param> /// <param name="port">Output or Input</param>
public virtual void OnRemoveConnection(NodePort port) { } public virtual void OnRemoveConnection(NodePort port) { }
/// <summary> Called after a node's name has changed. </summary>
public virtual void OnRename(Node node) { }
/// <summary> Disconnect everything from this node </summary> /// <summary> Disconnect everything from this node </summary>
public void ClearConnections() { public void ClearConnections() {
foreach (NodePort port in Ports) port.ClearConnections(); foreach (NodePort port in Ports) port.ClearConnections();