Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using System;
  2. using UnityEditor.Experimental;
  3. using UnityEngine;
  4. namespace UnityEditor.Tilemaps
  5. {
  6. internal class GridPalettesDropdown : PopupWindowContent
  7. {
  8. private class Styles
  9. {
  10. public GUIStyle menuItem = "MenuItem";
  11. public GUIContent backIcon = EditorGUIUtility.TrIconContent("tab_next");
  12. public static readonly GUIStyle sceneVisibilityStyle = "SceneVisibility";
  13. }
  14. private static Styles s_Styles;
  15. internal class MenuItemProvider : IFlexibleMenuItemProvider
  16. {
  17. public int Count()
  18. {
  19. var count = GridPaintingState.palettes.Count + 1;
  20. if (TilePaletteWhiteboxSamplesUtility.whiteboxSamples.Count > 0)
  21. count += 1;
  22. return count;
  23. }
  24. public object GetItem(int index)
  25. {
  26. if (index < GridPaintingState.palettes.Count)
  27. return GridPaintingState.palettes[index];
  28. return null;
  29. }
  30. public int Add(object obj)
  31. {
  32. throw new NotImplementedException();
  33. }
  34. public void Replace(int index, object newPresetObject)
  35. {
  36. throw new NotImplementedException();
  37. }
  38. public void Remove(int index)
  39. {
  40. throw new NotImplementedException();
  41. }
  42. public object Create()
  43. {
  44. throw new NotImplementedException();
  45. }
  46. public void Move(int index, int destIndex, bool insertAfterDestIndex)
  47. {
  48. throw new NotImplementedException();
  49. }
  50. public string GetName(int index)
  51. {
  52. if (index < GridPaintingState.palettes.Count)
  53. return GridPaintingState.palettes[index].name;
  54. else if (index == GridPaintingState.palettes.Count)
  55. return "Create New Tile Palette";
  56. else if (index == GridPaintingState.palettes.Count + 1)
  57. return "Create New Whitebox Tile Palette";
  58. else
  59. return "";
  60. }
  61. public bool IsModificationAllowed(int index)
  62. {
  63. return false;
  64. }
  65. public int[] GetSeperatorIndices()
  66. {
  67. return new int[] { GridPaintingState.palettes.Count - 1 };
  68. }
  69. }
  70. private IFlexibleMenuItemProvider m_ItemProvider;
  71. private FlexibleMenuModifyItemUI m_ModifyItemUI;
  72. private readonly Action<int, object> m_ItemClickedCallback;
  73. private readonly Action<int, Rect> m_ItemHoveredCallback;
  74. private Vector2 m_ScrollPosition = Vector2.zero;
  75. private bool m_ShowAddNewPresetItem;
  76. private int m_HoverIndex;
  77. private int[] m_SeperatorIndices;
  78. private float m_CachedWidth = -1f;
  79. private float m_MinTextWidth;
  80. private const float LineHeight = 18f;
  81. private const float SeperatorHeight = 8f;
  82. private int maxIndex { get { return m_ShowAddNewPresetItem ? m_ItemProvider.Count() : m_ItemProvider.Count() - 1; } }
  83. public int selectedIndex { get; set; }
  84. protected float minTextWidth { get { return m_MinTextWidth; } set { m_MinTextWidth = value; ClearCachedWidth(); } }
  85. public GridPalettesDropdown(IFlexibleMenuItemProvider itemProvider
  86. , int selectionIndex
  87. , FlexibleMenuModifyItemUI modifyItemUi
  88. , Action<int, object> itemClickedCallback
  89. , Action<int, Rect> itemHoveredCallback
  90. , float minWidth)
  91. {
  92. m_ItemProvider = itemProvider;
  93. m_ModifyItemUI = modifyItemUi;
  94. m_ItemClickedCallback = itemClickedCallback;
  95. m_ItemHoveredCallback = itemHoveredCallback;
  96. m_SeperatorIndices = m_ItemProvider.GetSeperatorIndices();
  97. selectedIndex = selectionIndex;
  98. m_ShowAddNewPresetItem = m_ModifyItemUI != null;
  99. m_MinTextWidth = minWidth;
  100. }
  101. public override Vector2 GetWindowSize()
  102. {
  103. return CalcSize();
  104. }
  105. protected Vector2 CalcSize()
  106. {
  107. float height = (maxIndex + 1) * LineHeight + m_SeperatorIndices.Length * SeperatorHeight;
  108. if (m_CachedWidth < 0)
  109. m_CachedWidth = Math.Max(m_MinTextWidth, CalcWidth());
  110. return new Vector2(m_CachedWidth, height);
  111. }
  112. private void ClearCachedWidth()
  113. {
  114. m_CachedWidth = -1f;
  115. }
  116. private float CalcWidth()
  117. {
  118. if (s_Styles == null)
  119. s_Styles = new Styles();
  120. float maxWidth = 0;
  121. for (int i = 0; i < m_ItemProvider.Count(); ++i)
  122. {
  123. float w = s_Styles.menuItem.CalcSize(GUIContent.Temp(m_ItemProvider.GetName(i))).x;
  124. maxWidth = Mathf.Max(w, maxWidth);
  125. }
  126. const float rightMargin = 6f;
  127. return maxWidth + rightMargin;
  128. }
  129. private void DrawRect(Rect rect, Color color)
  130. {
  131. if (Event.current.type != EventType.Repaint)
  132. return;
  133. Color orgColor = GUI.color;
  134. GUI.color = GUI.color * color;
  135. GUI.DrawTexture(rect, EditorGUIUtility.whiteTexture);
  136. GUI.color = orgColor;
  137. }
  138. private void Repaint()
  139. {
  140. HandleUtility.Repaint(); // repaints current guiview (needs rename)
  141. }
  142. public override void OnGUI(Rect rect)
  143. {
  144. if (s_Styles == null)
  145. s_Styles = new Styles();
  146. Event evt = Event.current;
  147. Rect contentRect = new Rect(0, 0, 1, CalcSize().y);
  148. m_ScrollPosition = GUI.BeginScrollView(rect, m_ScrollPosition, contentRect);
  149. {
  150. float curY = 0f;
  151. for (int i = 0; i <= maxIndex; ++i)
  152. {
  153. int itemControlID = i + 1000000;
  154. Rect itemRect = new Rect(0, curY, rect.width, LineHeight);
  155. Rect backRect = new Rect(rect.width - 16 - 1, curY, 16, LineHeight);
  156. bool addSeparator = Array.IndexOf(m_SeperatorIndices, i) >= 0;
  157. // Handle event
  158. switch (evt.type)
  159. {
  160. case EventType.Repaint:
  161. bool hover = false;
  162. if (m_HoverIndex == i)
  163. {
  164. if (itemRect.Contains(evt.mousePosition))
  165. {
  166. hover = true;
  167. }
  168. else
  169. m_HoverIndex = -1;
  170. }
  171. s_Styles.menuItem.Draw(itemRect, GUIContent.Temp(m_ItemProvider.GetName(i)), hover, false, i == selectedIndex, false);
  172. if (TilePaletteWhiteboxSamplesUtility.whiteboxSamples.Count > 0 && i == maxIndex)
  173. {
  174. GUI.Button(backRect, s_Styles.backIcon, Styles.sceneVisibilityStyle);
  175. }
  176. if (addSeparator)
  177. {
  178. const float margin = 4f;
  179. Rect seperatorRect = new Rect(itemRect.x + margin, itemRect.y + itemRect.height + SeperatorHeight * 0.5f, itemRect.width - 2 * margin, 1);
  180. DrawRect(seperatorRect, (EditorGUIUtility.isProSkin) ? new Color(0.32f, 0.32f, 0.32f, 1.333f) : new Color(0.6f, 0.6f, 0.6f, 1.333f)); // dark : light
  181. }
  182. break;
  183. case EventType.MouseDown:
  184. if (evt.button == 0 && itemRect.Contains(evt.mousePosition))
  185. {
  186. GUIUtility.hotControl = itemControlID;
  187. if (evt.clickCount == 1)
  188. {
  189. GUIUtility.hotControl = 0;
  190. SelectItem(i);
  191. editorWindow.Close();
  192. evt.Use();
  193. }
  194. }
  195. break;
  196. case EventType.MouseUp:
  197. if (GUIUtility.hotControl == itemControlID)
  198. {
  199. GUIUtility.hotControl = 0;
  200. }
  201. break;
  202. case EventType.MouseMove:
  203. if (itemRect.Contains(evt.mousePosition))
  204. {
  205. m_HoverIndex = i;
  206. HoverItem(itemRect, m_HoverIndex);
  207. }
  208. else if (m_HoverIndex == i)
  209. {
  210. m_HoverIndex = -1;
  211. }
  212. Repaint();
  213. break;
  214. }
  215. curY += LineHeight;
  216. if (addSeparator)
  217. curY += SeperatorHeight;
  218. } // end foreach item
  219. } GUI.EndScrollView();
  220. }
  221. private void SelectItem(int index)
  222. {
  223. selectedIndex = index;
  224. if (m_ItemClickedCallback != null && index >= 0)
  225. m_ItemClickedCallback(index, m_ItemProvider.GetItem(index));
  226. }
  227. private void HoverItem(Rect rect, int index)
  228. {
  229. if (m_ItemHoveredCallback != null && index >= 0)
  230. m_ItemHoveredCallback(index, rect);
  231. }
  232. }
  233. }