1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 09:16:01 +08:00

Rework node renaming

Renaming now happens on a separate popup.
This commit is contained in:
Thor Brigsted 2019-03-08 19:50:31 +01:00
parent 01d7f782e4
commit 4263e45b24
4 changed files with 88 additions and 32 deletions

View File

@ -13,34 +13,9 @@ namespace XNodeEditor {
/// <summary> Fires every whenever a node was modified through the editor </summary> /// <summary> Fires every whenever a node was modified through the editor </summary>
public static Action<XNode.Node> onUpdateNode; public static Action<XNode.Node> onUpdateNode;
public static Dictionary<XNode.NodePort, Vector2> portPositions; public static Dictionary<XNode.NodePort, Vector2> portPositions;
public int renaming;
public virtual void OnHeaderGUI() { public virtual void OnHeaderGUI() {
string title = target.name; GUILayout.Label(target.name, NodeEditorResources.styles.nodeHeader, GUILayout.Height(30));
if (renaming != 0) {
if (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) {
Debug.Log("Finish renaming");
Rename(target.name);
renaming = 0;
}
}
else {
// Selection changed, so stop renaming.
GUILayout.Label(title, NodeEditorResources.styles.nodeHeader, GUILayout.Height(30));
Rename(target.name);
renaming = 0;
}
} else {
GUILayout.Label(title, NodeEditorResources.styles.nodeHeader, GUILayout.Height(30));
}
} }
/// <summary> Draws standard field editors for all public fields </summary> /// <summary> Draws standard field editors for all public fields </summary>
@ -63,7 +38,7 @@ namespace XNodeEditor {
} }
// Iterate through instance ports and draw them in the order in which they are serialized // Iterate through instance ports and draw them in the order in which they are serialized
foreach(XNode.NodePort instancePort in target.InstancePorts) { foreach (XNode.NodePort instancePort in target.InstancePorts) {
if (NodeEditorGUILayout.IsInstancePortListPort(instancePort)) continue; if (NodeEditorGUILayout.IsInstancePortListPort(instancePort)) continue;
NodeEditorGUILayout.PortField(instancePort); NodeEditorGUILayout.PortField(instancePort);
} }
@ -109,10 +84,7 @@ namespace XNodeEditor {
} }
} }
public void InitiateRename() { /// <summary> Rename the node asset. This will trigger a reimport of the node. </summary>
renaming = 1;
}
public void Rename(string newName) { public void Rename(string newName) {
if (newName == null || newName.Trim() == "") newName = UnityEditor.ObjectNames.NicifyVariableName(target.GetType().Name); if (newName == null || newName.Trim() == "") newName = UnityEditor.ObjectNames.NicifyVariableName(target.GetType().Name);
target.name = newName; target.name = newName;

View File

@ -366,7 +366,12 @@ namespace XNodeEditor {
public void RenameSelectedNode() { public void RenameSelectedNode() {
if (Selection.objects.Length == 1 && Selection.activeObject is XNode.Node) { if (Selection.objects.Length == 1 && Selection.activeObject is XNode.Node) {
XNode.Node node = Selection.activeObject as XNode.Node; XNode.Node node = Selection.activeObject as XNode.Node;
NodeEditor.GetEditor(node).InitiateRename(); Vector2 size;
if (nodeSizes.TryGetValue(node, out size)) {
RenamePopup.Show(Selection.activeObject, size.x);
} else {
RenamePopup.Show(Selection.activeObject);
}
} }
} }

View File

@ -0,0 +1,66 @@
using UnityEditor;
using UnityEngine;
namespace XNodeEditor {
/// <summary> Utility for renaming assets </summary>
public class RenamePopup : EditorWindow {
public static RenamePopup current { get; private set; }
public Object target;
public string input;
private bool firstFrame = true;
/// <summary> Show a rename popup for an asset at mouse position. Will trigger reimport of the asset on apply.
public static RenamePopup Show(Object target, float width = 200) {
RenamePopup window = EditorWindow.GetWindow<RenamePopup>(true, "Rename " + target.name, true);
if (current != null) current.Close();
current = window;
window.target = target;
window.input = target.name;
window.minSize = new Vector2(100, 44);
window.position = new Rect(0, 0, width, 44);
GUI.FocusControl("ClearAllFocus");
window.UpdatePositionToMouse();
return window;
}
private void UpdatePositionToMouse() {
if (Event.current == null) return;
Vector3 mousePoint = GUIUtility.GUIToScreenPoint(Event.current.mousePosition);
Rect pos = position;
pos.x = mousePoint.x - position.width * 0.5f;
pos.y = mousePoint.y - 10;
position = pos;
}
private void OnLostFocus() {
// Make the popup close on lose focus
Close();
}
private void OnGUI() {
if (firstFrame) {
UpdatePositionToMouse();
firstFrame = false;
}
input = EditorGUILayout.TextField(input);
Event e = Event.current;
// If input is empty, revert name to default instead
if (input == null || input.Trim() == "") {
if (GUILayout.Button("Revert to default") || (e.isKey && e.keyCode == KeyCode.Return)) {
target.name = UnityEditor.ObjectNames.NicifyVariableName(target.GetType().Name);
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(target));
Close();
}
}
// Rename asset to input text
else {
if (GUILayout.Button("Apply") || (e.isKey && e.keyCode == KeyCode.Return)) {
target.name = input;
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(target));
Close();
}
}
}
}
}

View File

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 4ef3ddc25518318469bce838980c64be
timeCreated: 1552067957
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: