No Description
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.

GridBrushesDropdown.cs 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System;
  2. using UnityEngine;
  3. namespace UnityEditor.Tilemaps
  4. {
  5. internal class GridBrushesDropdown : PopupWindowContent
  6. {
  7. class Styles
  8. {
  9. public GUIStyle menuItem = "MenuItem";
  10. }
  11. static Styles s_Styles;
  12. MenuItemProvider m_ItemProvider;
  13. FlexibleMenuModifyItemUI m_ModifyItemUI;
  14. readonly Action<int, object> m_ItemClickedCallback;
  15. Vector2 m_ScrollPosition = Vector2.zero;
  16. bool m_ShowAddNewPresetItem;
  17. int m_HoverIndex;
  18. int[] m_SeperatorIndices;
  19. float m_CachedWidth = -1f;
  20. float m_MinTextWidth = 200f;
  21. const float LineHeight = 18f;
  22. const float SeparatorHeight = 8f;
  23. int maxIndex { get { return m_ShowAddNewPresetItem ? m_ItemProvider.Count() : m_ItemProvider.Count() - 1; } }
  24. public int selectedIndex { get; set; }
  25. protected float minTextWidth { get { return m_MinTextWidth; } set { m_MinTextWidth = value; ClearCachedWidth(); } }
  26. internal class MenuItemProvider : IFlexibleMenuItemProvider
  27. {
  28. public int Count()
  29. {
  30. return GridPaletteBrushes.brushes.Count;
  31. }
  32. public object GetItem(int index)
  33. {
  34. return GridPaletteBrushes.brushes[index];
  35. }
  36. public int Add(object obj)
  37. {
  38. throw new NotImplementedException();
  39. }
  40. public void Replace(int index, object newPresetObject)
  41. {
  42. throw new NotImplementedException();
  43. }
  44. public void Remove(int index)
  45. {
  46. throw new NotImplementedException();
  47. }
  48. public object Create()
  49. {
  50. throw new NotImplementedException();
  51. }
  52. public void Move(int index, int destIndex, bool insertAfterDestIndex)
  53. {
  54. throw new NotImplementedException();
  55. }
  56. public string GetName(int index)
  57. {
  58. return GridPaletteBrushes.brushNames[index];
  59. }
  60. public string GetTooltip(int index)
  61. {
  62. return GridPaletteBrushes.brushTooltips[index];
  63. }
  64. public bool IsModificationAllowed(int index)
  65. {
  66. return false;
  67. }
  68. public int[] GetSeperatorIndices()
  69. {
  70. return new int[0];
  71. }
  72. }
  73. public GridBrushesDropdown(Action<int, object> itemClickedCallback, float minWidth)
  74. {
  75. m_ItemProvider = new MenuItemProvider();
  76. m_ModifyItemUI = null;
  77. m_ItemClickedCallback = itemClickedCallback;
  78. m_SeperatorIndices = m_ItemProvider.GetSeperatorIndices();
  79. selectedIndex = GridPaletteBrushes.brushes.IndexOf(GridPaintingState.gridBrush);
  80. m_ShowAddNewPresetItem = m_ModifyItemUI != null;
  81. m_MinTextWidth = minWidth;
  82. }
  83. public override Vector2 GetWindowSize()
  84. {
  85. return CalcSize();
  86. }
  87. public override void OnGUI(Rect rect)
  88. {
  89. if (s_Styles == null)
  90. s_Styles = new Styles();
  91. Event evt = Event.current;
  92. Rect contentRect = new Rect(0, 0, 1, CalcSize().y);
  93. m_ScrollPosition = GUI.BeginScrollView(rect, m_ScrollPosition, contentRect);
  94. {
  95. float curY = 0f;
  96. for (int i = 0; i <= maxIndex; ++i)
  97. {
  98. int itemControlID = i + 1000000;
  99. Rect fullRect = new Rect(0, curY, rect.width, LineHeight);
  100. Rect itemRect = fullRect;
  101. bool addSeparator = Array.IndexOf(m_SeperatorIndices, i) >= 0;
  102. // Handle event
  103. switch (evt.type)
  104. {
  105. case EventType.Repaint:
  106. bool hover = false;
  107. if (m_HoverIndex == i)
  108. {
  109. if (fullRect.Contains(evt.mousePosition))
  110. hover = true;
  111. else
  112. m_HoverIndex = -1;
  113. }
  114. var tooltip = m_ItemProvider.GetTooltip(i);
  115. if (!String.IsNullOrWhiteSpace(tooltip))
  116. EditorGUI.LabelField(itemRect, GUIContent.Temp("", tooltip)); // Use empty label to overlay tooltip
  117. s_Styles.menuItem.Draw(itemRect, GUIContent.Temp(m_ItemProvider.GetName(i)), hover, false, i == selectedIndex, false);
  118. break;
  119. case EventType.MouseDown:
  120. if (evt.button == 0 && itemRect.Contains(evt.mousePosition))
  121. {
  122. GUIUtility.hotControl = itemControlID;
  123. if (evt.clickCount == 1)
  124. {
  125. GUIUtility.hotControl = 0;
  126. SelectItem(i);
  127. editorWindow.Close();
  128. evt.Use();
  129. }
  130. }
  131. break;
  132. case EventType.MouseUp:
  133. if (GUIUtility.hotControl == itemControlID)
  134. {
  135. GUIUtility.hotControl = 0;
  136. }
  137. break;
  138. case EventType.MouseMove:
  139. if (fullRect.Contains(evt.mousePosition))
  140. {
  141. if (m_HoverIndex != i)
  142. {
  143. m_HoverIndex = i;
  144. Repaint();
  145. }
  146. }
  147. else if (m_HoverIndex == i)
  148. {
  149. m_HoverIndex = -1;
  150. Repaint();
  151. }
  152. break;
  153. }
  154. curY += LineHeight;
  155. if (addSeparator)
  156. curY += SeparatorHeight;
  157. } // end foreach item
  158. } GUI.EndScrollView();
  159. }
  160. void SelectItem(int index)
  161. {
  162. selectedIndex = index;
  163. if (m_ItemClickedCallback != null && index >= 0)
  164. m_ItemClickedCallback(index, m_ItemProvider.GetItem(index));
  165. }
  166. protected Vector2 CalcSize()
  167. {
  168. float height = (maxIndex + 1) * LineHeight + m_SeperatorIndices.Length * SeparatorHeight;
  169. if (m_CachedWidth < 0)
  170. m_CachedWidth = Math.Max(m_MinTextWidth, CalcWidth());
  171. return new Vector2(m_CachedWidth, height);
  172. }
  173. void ClearCachedWidth()
  174. {
  175. m_CachedWidth = -1f;
  176. }
  177. float CalcWidth()
  178. {
  179. if (s_Styles == null)
  180. s_Styles = new Styles();
  181. float maxWidth = 0;
  182. for (int i = 0; i < m_ItemProvider.Count(); ++i)
  183. {
  184. float w = s_Styles.menuItem.CalcSize(GUIContent.Temp(m_ItemProvider.GetName(i))).x;
  185. maxWidth = Mathf.Max(w, maxWidth);
  186. }
  187. const float rightMargin = 6f;
  188. return maxWidth + rightMargin;
  189. }
  190. void Repaint()
  191. {
  192. HandleUtility.Repaint(); // repaints current guiview (needs rename)
  193. }
  194. }
  195. }