Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GridPaletteAddWhiteboxPopup.cs 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using UnityEditor.PackageManager.UI;
  2. using UnityEngine;
  3. using UnityEngine.UIElements;
  4. namespace UnityEditor.Tilemaps
  5. {
  6. internal class GridPaletteAddWhiteboxPopup : EditorWindow
  7. {
  8. static class Styles
  9. {
  10. public static readonly GUIContent nameLabel = EditorGUIUtility.TrTextContent("Name");
  11. public static readonly GUIContent ok = EditorGUIUtility.TrTextContent("Create");
  12. public static readonly GUIContent cancel = EditorGUIUtility.TrTextContent("Cancel");
  13. public static readonly GUIContent header = EditorGUIUtility.TrTextContent("Create New Palette");
  14. public static readonly GUIContent gridLabel = EditorGUIUtility.TrTextContent("Grid");
  15. public static readonly GUIContent sizeLabel = EditorGUIUtility.TrTextContent("Cell Size");
  16. public static readonly GUIContent hexagonLabel = EditorGUIUtility.TrTextContent("Hexagon Type");
  17. }
  18. private static long s_LastClosedTime;
  19. private static GridPaletteAddWhiteboxPopup s_Instance;
  20. private DropdownField m_DropdownField;
  21. void Init(Rect buttonRect)
  22. {
  23. buttonRect = GUIUtility.GUIToScreenRect(buttonRect);
  24. ShowAsDropDown(buttonRect, new Vector2(380, 45));
  25. }
  26. internal void CreateGUI()
  27. {
  28. var root = new Box();
  29. root.style.borderBottomWidth = 1f;
  30. root.style.borderTopWidth = 1f;
  31. root.style.borderLeftWidth = 1f;
  32. root.style.borderRightWidth = 1f;
  33. root.style.borderBottomColor = Color.gray;
  34. root.style.borderTopColor = Color.gray;
  35. root.style.borderLeftColor = Color.gray;
  36. root.style.borderRightColor = Color.gray;
  37. rootVisualElement.Add(root);
  38. m_DropdownField = new DropdownField("White Box Type", TilePaletteWhiteboxSamplesUtility.whiteboxSampleNames, 0);
  39. root.Add(m_DropdownField);
  40. var buttons = new VisualElement();
  41. buttons.style.flexDirection = FlexDirection.Row;
  42. var importButton = new Button(Import);
  43. importButton.text = "Import";
  44. var space = new VisualElement();
  45. space.style.flexGrow = 1f;
  46. var cancelButton = new Button(Cancel);
  47. cancelButton.text = "Cancel";
  48. buttons.Add(space);
  49. buttons.Add(importButton);
  50. buttons.Add(cancelButton);
  51. root.Add(buttons);
  52. }
  53. private void Import()
  54. {
  55. TilePaletteWhiteboxSamplesUtility.ImportWhiteboxSample(m_DropdownField.index);
  56. if (GridPaintPaletteClipboard.instances is { Count: > 0 })
  57. {
  58. var clipboard = GridPaintPaletteClipboard.instances[0];
  59. clipboard.PickFirstFromPalette();
  60. }
  61. Close();
  62. }
  63. private void Cancel()
  64. {
  65. Close();
  66. }
  67. internal static bool ShowAtPosition(Rect buttonRect)
  68. {
  69. // We could not use realtimeSinceStartUp since it is set to 0 when entering/exitting playmode, we assume an increasing time when comparing time.
  70. long nowMilliSeconds = System.DateTime.Now.Ticks / System.TimeSpan.TicksPerMillisecond;
  71. bool justClosed = nowMilliSeconds < s_LastClosedTime + 50;
  72. if (!justClosed)
  73. {
  74. Event.current.Use();
  75. if (s_Instance == null)
  76. s_Instance = ScriptableObject.CreateInstance<GridPaletteAddWhiteboxPopup>();
  77. s_Instance.Init(buttonRect);
  78. return true;
  79. }
  80. return false;
  81. }
  82. }
  83. }
  84. // namespace