RenamePopup.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using UnityEditor;
  2. using UnityEngine;
  3. namespace XNodeEditor {
  4. /// <summary> Utility for renaming assets </summary>
  5. public class RenamePopup : EditorWindow {
  6. private const string inputControlName = "nameInput";
  7. public static RenamePopup current { get; private set; }
  8. public Object target;
  9. public string input;
  10. private bool firstFrame = true;
  11. /// <summary> Show a rename popup for an asset at mouse position. Will trigger reimport of the asset on apply.
  12. public static RenamePopup Show(Object target, float width = 200) {
  13. RenamePopup window = EditorWindow.GetWindow<RenamePopup>(true, "Rename " + target.name, true);
  14. if (current != null) current.Close();
  15. current = window;
  16. window.target = target;
  17. window.input = target.name;
  18. window.minSize = new Vector2(100, 44);
  19. window.position = new Rect(0, 0, width, 44);
  20. window.UpdatePositionToMouse();
  21. return window;
  22. }
  23. private void UpdatePositionToMouse() {
  24. if (Event.current == null) return;
  25. Vector3 mousePoint = GUIUtility.GUIToScreenPoint(Event.current.mousePosition);
  26. Rect pos = position;
  27. pos.x = mousePoint.x - position.width * 0.5f;
  28. pos.y = mousePoint.y - 10;
  29. position = pos;
  30. }
  31. private void OnLostFocus() {
  32. // Make the popup close on lose focus
  33. Close();
  34. }
  35. private void OnGUI() {
  36. if (firstFrame) {
  37. UpdatePositionToMouse();
  38. firstFrame = false;
  39. }
  40. GUI.SetNextControlName(inputControlName);
  41. input = EditorGUILayout.TextField(input);
  42. EditorGUI.FocusTextInControl(inputControlName);
  43. Event e = Event.current;
  44. // If input is empty, revert name to default instead
  45. if (input == null || input.Trim() == "") {
  46. if (GUILayout.Button("Revert to default") || (e.isKey && e.keyCode == KeyCode.Return)) {
  47. target.name = NodeEditorUtilities.NodeDefaultName(target.GetType());
  48. NodeEditor.GetEditor((XNode.Node)target, NodeEditorWindow.current).OnRename();
  49. AssetDatabase.SetMainObject((target as XNode.Node).graph, AssetDatabase.GetAssetPath(target));
  50. AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(target));
  51. Close();
  52. target.TriggerOnValidate();
  53. }
  54. }
  55. // Rename asset to input text
  56. else {
  57. if (GUILayout.Button("Apply") || (e.isKey && e.keyCode == KeyCode.Return)) {
  58. target.name = input;
  59. NodeEditor.GetEditor((XNode.Node)target, NodeEditorWindow.current).OnRename();
  60. AssetDatabase.SetMainObject((target as XNode.Node).graph, AssetDatabase.GetAssetPath(target));
  61. AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(target));
  62. Close();
  63. target.TriggerOnValidate();
  64. }
  65. }
  66. if (e.isKey && e.keyCode == KeyCode.Escape) {
  67. Close();
  68. }
  69. }
  70. private void OnDestroy() {
  71. EditorGUIUtility.editingTextField = false;
  72. }
  73. }
  74. }