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,8 +129,32 @@ namespace XNodeEditor {
public void Rename(string newName) {
if (newName == null || newName.Trim() == "") newName = NodeEditorUtilities.NodeDefaultName(target.GetType());
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)]
public class CustomNodeEditorAttribute : Attribute,

View File

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

View File

@ -254,6 +254,9 @@ namespace XNode {
/// <summary> Called after a connection is removed from this port </summary>
/// <param name="port">Output or Input</param>
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>
public void ClearConnections() {