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:
parent
01d7f782e4
commit
4263e45b24
@ -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>
|
||||||
@ -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;
|
||||||
|
|||||||
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
66
Scripts/Editor/RenamePopup.cs
Normal file
66
Scripts/Editor/RenamePopup.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
13
Scripts/Editor/RenamePopup.cs.meta
Normal file
13
Scripts/Editor/RenamePopup.cs.meta
Normal 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:
|
||||||
Loading…
x
Reference in New Issue
Block a user