Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

AdvancedDropdownGUI.cs 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #if UNITY_EDITOR
  2. using System;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEditor.IMGUI.Controls;
  6. namespace UnityEngine.InputSystem.Editor
  7. {
  8. internal class AdvancedDropdownGUI
  9. {
  10. private static class Styles
  11. {
  12. public static readonly GUIStyle toolbarSearchField = EditorStyles.toolbarSearchField;
  13. public static readonly GUIStyle itemStyle = new GUIStyle("PR Label")
  14. .WithAlignment(TextAnchor.MiddleLeft)
  15. .WithPadding(new RectOffset())
  16. .WithMargin(new RectOffset())
  17. .WithFixedHeight(17);
  18. public static readonly GUIStyle richTextItemStyle = new GUIStyle("PR Label")
  19. .WithAlignment(TextAnchor.MiddleLeft)
  20. .WithPadding(new RectOffset())
  21. .WithMargin(new RectOffset())
  22. .WithFixedHeight(17)
  23. .WithRichText();
  24. public static readonly GUIStyle header = new GUIStyle("In BigTitle")
  25. .WithFont(EditorStyles.boldLabel.font)
  26. .WithMargin(new RectOffset())
  27. .WithBorder(new RectOffset(0, 0, 3, 3))
  28. .WithPadding(new RectOffset(6, 6, 6, 6))
  29. .WithContentOffset(Vector2.zero);
  30. public static readonly GUIStyle headerArrow = new GUIStyle()
  31. .WithAlignment(TextAnchor.MiddleCenter)
  32. .WithFontSize(20)
  33. .WithNormalTextColor(Color.gray);
  34. public static readonly GUIStyle checkMark = new GUIStyle("PR Label")
  35. .WithAlignment(TextAnchor.MiddleCenter)
  36. .WithPadding(new RectOffset())
  37. .WithMargin(new RectOffset())
  38. .WithFixedHeight(17);
  39. public static readonly GUIContent arrowRightContent = new GUIContent("▸");
  40. public static readonly GUIContent arrowLeftContent = new GUIContent("◂");
  41. }
  42. //This should ideally match line height
  43. private static readonly Vector2 s_IconSize = new Vector2(13, 13);
  44. internal Rect m_SearchRect;
  45. internal Rect m_HeaderRect;
  46. private bool m_FocusSet;
  47. internal virtual float searchHeight => m_SearchRect.height;
  48. internal virtual float headerHeight => m_HeaderRect.height;
  49. internal virtual GUIStyle lineStyle => Styles.itemStyle;
  50. internal virtual GUIStyle richTextLineStyle => Styles.richTextItemStyle;
  51. internal GUIStyle headerStyle => Styles.header;
  52. internal virtual Vector2 iconSize => s_IconSize;
  53. internal AdvancedDropdownState state { get; set; }
  54. private readonly SearchField m_SearchField = new SearchField();
  55. public void Init()
  56. {
  57. m_FocusSet = false;
  58. }
  59. private const float k_IndentPerLevel = 20f;
  60. internal virtual void BeginDraw(EditorWindow window)
  61. {
  62. }
  63. internal virtual void EndDraw(EditorWindow window)
  64. {
  65. }
  66. internal virtual void DrawItem(AdvancedDropdownItem item, string name, Texture2D icon, bool enabled,
  67. bool drawArrow, bool selected, bool hasSearch, bool richText = false)
  68. {
  69. var content = new GUIContent(name, icon);
  70. var imgTemp = content.image;
  71. //we need to pretend we have an icon to calculate proper width in case
  72. if (content.image == null)
  73. content.image = Texture2D.whiteTexture;
  74. var style = richText ? richTextLineStyle : lineStyle;
  75. var rect = GUILayoutUtility.GetRect(content, style, GUILayout.ExpandWidth(true));
  76. content.image = imgTemp;
  77. if (Event.current.type != EventType.Repaint)
  78. return;
  79. style.Draw(rect, GUIContent.none, false, false, selected, selected);
  80. if (!hasSearch)
  81. {
  82. rect.x += item.indent * k_IndentPerLevel;
  83. rect.width -= item.indent * k_IndentPerLevel;
  84. }
  85. var imageTemp = content.image;
  86. if (content.image == null)
  87. {
  88. style.Draw(rect, GUIContent.none, false, false, selected, selected);
  89. rect.x += iconSize.x + 1;
  90. rect.width -= iconSize.x + 1;
  91. }
  92. rect.x += EditorGUIUtility.standardVerticalSpacing;
  93. rect.width -= EditorGUIUtility.standardVerticalSpacing;
  94. EditorGUI.BeginDisabledGroup(!enabled);
  95. style.Draw(rect, content, false, false, selected, selected);
  96. content.image = imageTemp;
  97. if (drawArrow)
  98. {
  99. var size = style.lineHeight;
  100. var arrowRect = new Rect(rect.x + rect.width - size, rect.y, size, size);
  101. style.Draw(arrowRect, Styles.arrowRightContent, false, false, false, false);
  102. }
  103. EditorGUI.EndDisabledGroup();
  104. }
  105. internal virtual void DrawHeader(AdvancedDropdownItem group, Action backButtonPressed, bool hasParent)
  106. {
  107. var content = new GUIContent(group.name, group.icon);
  108. m_HeaderRect = GUILayoutUtility.GetRect(content, Styles.header, GUILayout.ExpandWidth(true));
  109. if (Event.current.type == EventType.Repaint)
  110. Styles.header.Draw(m_HeaderRect, content, false, false, false, false);
  111. // Back button
  112. if (hasParent)
  113. {
  114. var arrowWidth = 13;
  115. var arrowRect = new Rect(m_HeaderRect.x, m_HeaderRect.y, arrowWidth, m_HeaderRect.height);
  116. if (Event.current.type == EventType.Repaint)
  117. Styles.headerArrow.Draw(arrowRect, Styles.arrowLeftContent, false, false, false, false);
  118. if (Event.current.type == EventType.MouseDown && m_HeaderRect.Contains(Event.current.mousePosition))
  119. {
  120. backButtonPressed();
  121. Event.current.Use();
  122. }
  123. }
  124. }
  125. internal virtual void DrawFooter(AdvancedDropdownItem selectedItem)
  126. {
  127. }
  128. internal void DrawSearchField(bool isSearchFieldDisabled, string searchString, Action<string> searchChanged)
  129. {
  130. if (!isSearchFieldDisabled && !m_FocusSet)
  131. {
  132. m_FocusSet = true;
  133. m_SearchField.SetFocus();
  134. }
  135. using (new EditorGUI.DisabledScope(isSearchFieldDisabled))
  136. {
  137. var newSearch = DrawSearchFieldControl(searchString);
  138. if (newSearch != searchString)
  139. {
  140. searchChanged(newSearch);
  141. }
  142. }
  143. }
  144. internal virtual string DrawSearchFieldControl(string searchString)
  145. {
  146. var paddingX = 8f;
  147. var paddingY = 2f;
  148. var rect = GUILayoutUtility.GetRect(0, 0, Styles.toolbarSearchField);
  149. //rect.x += paddingX;
  150. rect.y += paddingY + 1; // Add one for the border
  151. rect.height += Styles.toolbarSearchField.fixedHeight + paddingY * 3;
  152. rect.width -= paddingX;// * 2;
  153. m_SearchRect = rect;
  154. searchString = m_SearchField.OnToolbarGUI(m_SearchRect, searchString);
  155. return searchString;
  156. }
  157. internal Rect GetAnimRect(Rect position, float anim)
  158. {
  159. // Calculate rect for animated area
  160. var rect = new Rect(position);
  161. rect.x = position.x + position.width * anim;
  162. rect.y += searchHeight;
  163. rect.height -= searchHeight;
  164. return rect;
  165. }
  166. internal Vector2 CalculateContentSize(AdvancedDropdownDataSource dataSource)
  167. {
  168. var maxWidth = 0f;
  169. var maxHeight = 0f;
  170. var includeArrow = false;
  171. var arrowWidth = 0f;
  172. foreach (var child in dataSource.mainTree.children)
  173. {
  174. var content = new GUIContent(child.name, child.icon);
  175. var a = lineStyle.CalcSize(content);
  176. a.x += iconSize.x + 1;
  177. if (maxWidth < a.x)
  178. {
  179. maxWidth = a.x + 1;
  180. includeArrow |= child.children.Any();
  181. }
  182. if (child.IsSeparator())
  183. {
  184. maxHeight += GUIHelpers.Styles.lineSeparator.CalcHeight(content, maxWidth) + GUIHelpers.Styles.lineSeparator.margin.vertical;
  185. }
  186. else
  187. {
  188. maxHeight += lineStyle.CalcHeight(content, maxWidth);
  189. }
  190. if (arrowWidth == 0)
  191. {
  192. lineStyle.CalcMinMaxWidth(Styles.arrowRightContent, out arrowWidth, out arrowWidth);
  193. }
  194. }
  195. if (includeArrow)
  196. {
  197. maxWidth += arrowWidth;
  198. }
  199. return new Vector2(maxWidth, maxHeight);
  200. }
  201. internal float GetSelectionHeight(AdvancedDropdownDataSource dataSource, Rect buttonRect)
  202. {
  203. if (state.GetSelectedIndex(dataSource.mainTree) == -1)
  204. return 0;
  205. var height = 0f;
  206. for (var i = 0; i < dataSource.mainTree.children.Count(); i++)
  207. {
  208. var child = dataSource.mainTree.children.ElementAt(i);
  209. var content = new GUIContent(child.name, child.icon);
  210. if (state.GetSelectedIndex(dataSource.mainTree) == i)
  211. {
  212. var diff = (lineStyle.CalcHeight(content, 0) - buttonRect.height) / 2f;
  213. return height + diff;
  214. }
  215. if (child.IsSeparator())
  216. {
  217. height += GUIHelpers.Styles.lineSeparator.CalcHeight(content, 0) + GUIHelpers.Styles.lineSeparator.margin.vertical;
  218. }
  219. else
  220. {
  221. height += lineStyle.CalcHeight(content, 0);
  222. }
  223. }
  224. return height;
  225. }
  226. }
  227. }
  228. #endif // UNITY_EDITOR