From 19e244212c50adca0270b0d045b3175817297f55 Mon Sep 17 00:00:00 2001 From: Thor Brigsted Date: Thu, 5 Apr 2018 20:59:50 +0200 Subject: [PATCH] Added NodeRename #11 --- Scripts/Editor/NodeEditor.cs | 26 +++++++++++++++++++++++++- Scripts/Editor/NodeEditorAction.cs | 12 ++++++++++++ Scripts/Editor/NodeEditorGUI.cs | 9 ++------- Scripts/Editor/NodeGraphEditor.cs | 1 + Scripts/Node.cs | 2 +- Scripts/NodeGraph.cs | 7 ------- 6 files changed, 41 insertions(+), 16 deletions(-) diff --git a/Scripts/Editor/NodeEditor.cs b/Scripts/Editor/NodeEditor.cs index 39febe9..8a64663 100644 --- a/Scripts/Editor/NodeEditor.cs +++ b/Scripts/Editor/NodeEditor.cs @@ -13,6 +13,7 @@ namespace XNodeEditor { /// Fires every whenever a node was modified through the editor public static Action onUpdateNode; public static Dictionary portPositions; + public static int renaming; /// Draws the node GUI. /// Port handle positions need to be returned to the NodeEditorWindow @@ -24,7 +25,21 @@ namespace XNodeEditor { public virtual void OnHeaderGUI() { GUI.color = Color.white; string title = target.name; - GUILayout.Label(title, NodeEditorResources.styles.nodeHeader, GUILayout.Height(30)); + if (renaming != 0 && Selection.Contains(target)) { + int controlID = EditorGUIUtility.GetControlID(FocusType.Keyboard) + 1; + if (renaming == 1) { + EditorGUIUtility.keyboardControl = controlID; + EditorGUIUtility.editingTextField = true; + renaming = 2; + } + target.name = EditorGUILayout.TextField(target.name, NodeEditorResources.styles.nodeHeader, GUILayout.Height(30)); + if (!EditorGUIUtility.editingTextField) { + Rename(target.name); + renaming = 0; + } + } else { + GUILayout.Label(title, NodeEditorResources.styles.nodeHeader, GUILayout.Height(30)); + } } /// Draws standard field editors for all public fields @@ -52,6 +67,15 @@ namespace XNodeEditor { else return Color.white; } + public void InitiateRename() { + renaming = 1; + } + + public void Rename(string newName) { + target.name = newName; + AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(target)); + } + [AttributeUsage(AttributeTargets.Class)] public class CustomNodeEditorAttribute : Attribute, XNodeEditor.Internal.NodeEditorBase.INodeEditorAttrib { diff --git a/Scripts/Editor/NodeEditorAction.cs b/Scripts/Editor/NodeEditorAction.cs index 4c20270..7ef961c 100644 --- a/Scripts/Editor/NodeEditorAction.cs +++ b/Scripts/Editor/NodeEditorAction.cs @@ -291,6 +291,9 @@ namespace XNodeEditor { public void CreateNode(Type type, Vector2 position) { XNode.Node node = graph.AddNode(type); node.position = position; + node.name = UnityEditor.ObjectNames.NicifyVariableName(type.ToString()); + AssetDatabase.AddObjectToAsset(node, graph); + AssetDatabase.SaveAssets(); Repaint(); } @@ -304,6 +307,15 @@ namespace XNodeEditor { } } + /// Draw this node on top of other nodes by placing it last in the graph.nodes list + public void MoveNodeToTop(XNode.Node node) { + int index; + while ((index = graph.nodes.IndexOf(node)) != graph.nodes.Count - 1) { + graph.nodes[index] = graph.nodes[index + 1]; + graph.nodes[index + 1] = node; + } + } + /// Dublicate selected nodes and select the dublicates public void DublicateSelectedNodes() { UnityEngine.Object[] newNodes = new UnityEngine.Object[Selection.objects.Length]; diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index 2ff98ec..9e70693 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -109,13 +109,8 @@ namespace XNodeEditor { // If only one node is selected if (Selection.objects.Length == 1 && Selection.activeObject is XNode.Node) { XNode.Node node = Selection.activeObject as XNode.Node; - contextMenu.AddItem(new GUIContent("Move To Top"), false, () => { - int index; - while ((index = graph.nodes.IndexOf(node)) != graph.nodes.Count - 1) { - graph.nodes[index] = graph.nodes[index + 1]; - graph.nodes[index + 1] = node; - } - }); + contextMenu.AddItem(new GUIContent("Move To Top"), false, () => MoveNodeToTop(node)); + contextMenu.AddItem(new GUIContent("Rename"), false, NodeEditor.GetEditor(node).InitiateRename); } contextMenu.AddItem(new GUIContent("Duplicate"), false, DublicateSelectedNodes); diff --git a/Scripts/Editor/NodeGraphEditor.cs b/Scripts/Editor/NodeGraphEditor.cs index 3fa9406..2a8f43c 100644 --- a/Scripts/Editor/NodeGraphEditor.cs +++ b/Scripts/Editor/NodeGraphEditor.cs @@ -10,6 +10,7 @@ namespace XNodeEditor { public class NodeGraphEditor : XNodeEditor.Internal.NodeEditorBase { /// Custom node editors defined with [CustomNodeGraphEditor] [NonSerialized] private static Dictionary editors; + protected bool isRenaming; public virtual Texture2D GetGridTexture() { return NodeEditorPreferences.GetSettings().gridTexture; diff --git a/Scripts/Node.cs b/Scripts/Node.cs index 099c56e..be3d5a7 100644 --- a/Scripts/Node.cs +++ b/Scripts/Node.cs @@ -71,7 +71,7 @@ namespace XNode { } /// Initialize node. Called on creation. - protected virtual void Init() { name = GetType().Name; } + protected virtual void Init() { } /// Checks all connections for invalid references, and removes them. public void VerifyConnections() { diff --git a/Scripts/NodeGraph.cs b/Scripts/NodeGraph.cs index c2092e0..05b865b 100644 --- a/Scripts/NodeGraph.cs +++ b/Scripts/NodeGraph.cs @@ -19,13 +19,6 @@ namespace XNode { /// Add a node to the graph by type public virtual Node AddNode(Type type) { Node node = ScriptableObject.CreateInstance(type) as Node; -#if UNITY_EDITOR - if (!Application.isPlaying) { - UnityEditor.AssetDatabase.AddObjectToAsset(node, this); - UnityEditor.AssetDatabase.SaveAssets(); - node.name = UnityEditor.ObjectNames.NicifyVariableName(node.name); - } -#endif nodes.Add(node); node.graph = this; return node;