InputDialogWindow.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. namespace UnityVersionControl
  5. {
  6. // Enhanced Input Dialog Window
  7. public class InputDialogWindow : EditorWindow
  8. {
  9. private string inputText = "";
  10. private string dialogTitle = "";
  11. private string dialogMessage = "";
  12. private System.Action<string> onConfirm;
  13. private System.Action onCancel;
  14. private bool focusSet = false;
  15. public static void Show(string title, string message, System.Action<string> onConfirm, System.Action onCancel = null)
  16. {
  17. var window = GetWindow<InputDialogWindow>(true, title, true);
  18. window.dialogTitle = title;
  19. window.dialogMessage = message;
  20. window.onConfirm = onConfirm;
  21. window.onCancel = onCancel;
  22. window.inputText = "";
  23. window.focusSet = false;
  24. // Center the window
  25. window.position = new Rect(
  26. (Screen.currentResolution.width - 400) / 2,
  27. (Screen.currentResolution.height - 150) / 2,
  28. 400, 150
  29. );
  30. window.ShowModal();
  31. }
  32. private void OnGUI()
  33. {
  34. GUILayout.Space(10);
  35. EditorGUILayout.LabelField(dialogMessage, EditorStyles.wordWrappedLabel);
  36. GUILayout.Space(10);
  37. GUI.SetNextControlName("InputField");
  38. inputText = EditorGUILayout.TextField(inputText);
  39. // Auto-focus the input field
  40. if (!focusSet)
  41. {
  42. EditorGUI.FocusTextInControl("InputField");
  43. focusSet = true;
  44. }
  45. // Handle Enter key
  46. if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Return)
  47. {
  48. ConfirmInput();
  49. return;
  50. }
  51. // Handle Escape key
  52. if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Escape)
  53. {
  54. CancelInput();
  55. return;
  56. }
  57. GUILayout.Space(10);
  58. EditorGUILayout.BeginHorizontal();
  59. GUILayout.FlexibleSpace();
  60. GUI.enabled = !string.IsNullOrEmpty(inputText.Trim());
  61. if (GUILayout.Button("OK", GUILayout.Width(80)))
  62. {
  63. ConfirmInput();
  64. }
  65. GUI.enabled = true;
  66. if (GUILayout.Button("Cancel", GUILayout.Width(80)))
  67. {
  68. CancelInput();
  69. }
  70. EditorGUILayout.EndHorizontal();
  71. }
  72. private void ConfirmInput()
  73. {
  74. if (!string.IsNullOrEmpty(inputText.Trim()))
  75. {
  76. onConfirm?.Invoke(inputText.Trim());
  77. Close();
  78. }
  79. }
  80. private void CancelInput()
  81. {
  82. onCancel?.Invoke();
  83. Close();
  84. }
  85. }
  86. // Note: EditorInputDialog is now defined in VersionControlWindow.cs to avoid conflicts
  87. }