Geen omschrijving
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.

GridPaletteAddPopup.cs 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using UnityEngine;
  2. namespace UnityEditor.Tilemaps
  3. {
  4. internal class GridPaletteAddPopup : EditorWindow
  5. {
  6. static class Styles
  7. {
  8. public static readonly GUIContent nameLabel = EditorGUIUtility.TrTextContent("Name");
  9. public static readonly GUIContent ok = EditorGUIUtility.TrTextContent("Create");
  10. public static readonly GUIContent cancel = EditorGUIUtility.TrTextContent("Cancel");
  11. public static readonly GUIContent header = EditorGUIUtility.TrTextContent("Create New Palette");
  12. public static readonly GUIContent gridLabel = EditorGUIUtility.TrTextContent("Grid");
  13. public static readonly GUIContent sizeLabel = EditorGUIUtility.TrTextContent("Cell Size");
  14. public static readonly GUIContent hexagonLabel = EditorGUIUtility.TrTextContent("Hexagon Type");
  15. public static readonly GUIContent[] hexagonSwizzleTypeLabel =
  16. {
  17. EditorGUIUtility.TrTextContent("Point Top"),
  18. EditorGUIUtility.TrTextContent("Flat Top"),
  19. };
  20. public static readonly GridLayout.CellSwizzle[] hexagonSwizzleTypeValue =
  21. {
  22. GridLayout.CellSwizzle.XYZ,
  23. GridLayout.CellSwizzle.YXZ,
  24. };
  25. public static readonly GUIContent transparencySortModeLabel =
  26. EditorGUIUtility.TrTextContent("Sort Mode");
  27. public static readonly GUIContent transparencySortAxisLabel =
  28. EditorGUIUtility.TrTextContent("Sort Axis");
  29. }
  30. private static long s_LastClosedTime;
  31. private string m_Name = "New Palette";
  32. private static GridPaletteAddPopup s_Instance;
  33. private GridPaintPaletteWindow m_Owner;
  34. private GridLayout.CellLayout m_Layout;
  35. private int m_HexagonLayout;
  36. private GridPalette.CellSizing m_CellSizing;
  37. private Vector3 m_CellSize;
  38. private TransparencySortMode m_TransparencySortMode;
  39. private Vector3 m_TransparencySortAxis = new Vector3(0f, 0f, 1f);
  40. void Init(Rect buttonRect, GridPaintPaletteWindow owner)
  41. {
  42. m_Owner = owner;
  43. m_CellSize = new Vector3(1, 1, 0);
  44. buttonRect = GUIUtility.GUIToScreenRect(buttonRect);
  45. ShowAsDropDown(buttonRect, new Vector2(312, 185));
  46. }
  47. internal void OnGUI()
  48. {
  49. GUI.Label(new Rect(0, 0, position.width, position.height), GUIContent.none, "grey_border");
  50. GUILayout.Space(3);
  51. GUILayout.Label(Styles.header, EditorStyles.boldLabel);
  52. GUILayout.Space(4);
  53. GUILayout.BeginHorizontal();
  54. GUILayout.Label(Styles.nameLabel, GUILayout.Width(90f));
  55. m_Name = EditorGUILayout.TextField(m_Name);
  56. GUILayout.EndHorizontal();
  57. GUILayout.BeginHorizontal();
  58. GUILayout.Label(Styles.gridLabel, GUILayout.Width(90f));
  59. EditorGUI.BeginChangeCheck();
  60. var newLayout = (GridLayout.CellLayout)EditorGUILayout.EnumPopup(m_Layout);
  61. if (EditorGUI.EndChangeCheck())
  62. {
  63. // Set useful user settings for certain layouts
  64. switch (newLayout)
  65. {
  66. case GridLayout.CellLayout.Rectangle:
  67. case GridLayout.CellLayout.Hexagon:
  68. {
  69. m_CellSizing = GridPalette.CellSizing.Automatic;
  70. m_CellSize = new Vector3(1, 1, 0);
  71. break;
  72. }
  73. case GridLayout.CellLayout.Isometric:
  74. {
  75. m_CellSizing = GridPalette.CellSizing.Manual;
  76. m_CellSize = new Vector3(1, 0.5f, 1);
  77. break;
  78. }
  79. case GridLayout.CellLayout.IsometricZAsY:
  80. {
  81. m_CellSizing = GridPalette.CellSizing.Manual;
  82. m_CellSize = new Vector3(1, 0.5f, 1);
  83. m_TransparencySortMode = TransparencySortMode.CustomAxis;
  84. m_TransparencySortAxis = new Vector3(0f, 1f, -0.25f);
  85. break;
  86. }
  87. }
  88. m_Layout = newLayout;
  89. }
  90. GUILayout.EndHorizontal();
  91. if (m_Layout == GridLayout.CellLayout.Hexagon)
  92. {
  93. GUILayout.BeginHorizontal();
  94. float oldLabelWidth = UnityEditor.EditorGUIUtility.labelWidth;
  95. EditorGUIUtility.labelWidth = 94;
  96. m_HexagonLayout = EditorGUILayout.Popup(Styles.hexagonLabel, m_HexagonLayout, Styles.hexagonSwizzleTypeLabel);
  97. EditorGUIUtility.labelWidth = oldLabelWidth;
  98. GUILayout.EndHorizontal();
  99. }
  100. GUILayout.BeginHorizontal();
  101. GUILayout.Label(Styles.sizeLabel, GUILayout.Width(90f));
  102. m_CellSizing = (GridPalette.CellSizing)EditorGUILayout.EnumPopup(m_CellSizing);
  103. GUILayout.EndHorizontal();
  104. using (new EditorGUI.DisabledScope(m_CellSizing == GridPalette.CellSizing.Automatic))
  105. {
  106. GUILayout.BeginHorizontal();
  107. GUILayout.Label(GUIContent.none, GUILayout.Width(90f));
  108. m_CellSize = EditorGUILayout.Vector3Field(GUIContent.none, m_CellSize);
  109. GUILayout.EndHorizontal();
  110. }
  111. GUILayout.FlexibleSpace();
  112. GUILayout.BeginHorizontal();
  113. GUILayout.Label(Styles.transparencySortModeLabel, GUILayout.Width(90f));
  114. m_TransparencySortMode = (TransparencySortMode)EditorGUILayout.EnumPopup(m_TransparencySortMode);
  115. GUILayout.EndHorizontal();
  116. using (new EditorGUI.DisabledScope(m_TransparencySortMode != TransparencySortMode.CustomAxis))
  117. {
  118. GUILayout.BeginHorizontal();
  119. GUILayout.Label(Styles.transparencySortAxisLabel, GUILayout.Width(90f));
  120. m_TransparencySortAxis = EditorGUILayout.Vector3Field("", m_TransparencySortAxis);
  121. GUILayout.EndHorizontal();
  122. }
  123. GUILayout.FlexibleSpace();
  124. // Cancel, Ok
  125. GUILayout.BeginHorizontal();
  126. GUILayout.Space(10);
  127. if (GUILayout.Button(Styles.cancel))
  128. {
  129. Close();
  130. }
  131. using (new EditorGUI.DisabledScope(!Utils.Paths.IsValidAssetPath(m_Name)))
  132. {
  133. if (GUILayout.Button(Styles.ok))
  134. {
  135. s_LastClosedTime = System.DateTime.Now.Ticks / System.TimeSpan.TicksPerMillisecond;
  136. // case 1077362: Close window to prevent overlap with OS folder window when saving new palette asset
  137. Close();
  138. var swizzle = GridLayout.CellSwizzle.XYZ;
  139. if (m_Layout == GridLayout.CellLayout.Hexagon)
  140. swizzle = Styles.hexagonSwizzleTypeValue[m_HexagonLayout];
  141. GameObject go = GridPaletteUtility.CreateNewPaletteAtCurrentFolder(m_Name, m_Layout, m_CellSizing, m_CellSize
  142. , swizzle, m_TransparencySortMode, m_TransparencySortAxis);
  143. if (go != null)
  144. {
  145. m_Owner.palette = go;
  146. m_Owner.Repaint();
  147. }
  148. GUIUtility.ExitGUI();
  149. }
  150. }
  151. GUILayout.Space(10);
  152. GUILayout.EndHorizontal();
  153. }
  154. internal static bool ShowAtPosition(Rect buttonRect, GridPaintPaletteWindow owner)
  155. {
  156. // We could not use realtimeSinceStartUp since it is set to 0 when entering/exitting playmode, we assume an increasing time when comparing time.
  157. long nowMilliSeconds = System.DateTime.Now.Ticks / System.TimeSpan.TicksPerMillisecond;
  158. bool justClosed = nowMilliSeconds < s_LastClosedTime + 50;
  159. if (!justClosed)
  160. {
  161. Event.current.Use();
  162. if (s_Instance == null)
  163. s_Instance = ScriptableObject.CreateInstance<GridPaletteAddPopup>();
  164. s_Instance.Init(buttonRect, owner);
  165. return true;
  166. }
  167. return false;
  168. }
  169. }
  170. }
  171. // namespace