DropDown.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using System;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. using DropdownSelectArgs = DropdownHelper.DropdownSelectArgs;
  6. using DropdownCloseArgs = DropdownHelper.DropdownCloseArgs;
  7. public class Dropdown : BaseControl
  8. {
  9. public event EventHandler SelectionChange;
  10. public event EventHandler DropdownOpen;
  11. public event EventHandler DropdownClose;
  12. private bool clickedYou = false;
  13. private bool showDropdown = false;
  14. public string[] DropdownList { get; private set; }
  15. public int DropdownID { get; private set; }
  16. private Vector2 scrollPos;
  17. public Vector2 ScrollPos
  18. {
  19. get => scrollPos;
  20. private set => scrollPos = value;
  21. }
  22. private Rect buttonRect;
  23. public Rect ButtonRect
  24. {
  25. get => buttonRect;
  26. private set => buttonRect = value;
  27. }
  28. private Vector2 elementSize;
  29. public Vector2 ElementSize
  30. {
  31. get => elementSize;
  32. set => elementSize = value;
  33. }
  34. private int selectedItemIndex = 0;
  35. public int SelectedItemIndex
  36. {
  37. get => selectedItemIndex;
  38. set
  39. {
  40. this.selectedItemIndex = Mathf.Clamp(value, 0, DropdownList.Length);
  41. OnDropdownEvent(SelectionChange);
  42. }
  43. }
  44. public Dropdown(string[] itemList, int selectedItemIndex = 0)
  45. {
  46. DropdownID = DropdownHelper.DropdownID;
  47. SetDropdownItems(itemList, selectedItemIndex);
  48. DropdownHelper.SelectionChange += OnChangeSelection;
  49. DropdownHelper.DropdownClose += OnCloseDropdown;
  50. }
  51. ~Dropdown()
  52. {
  53. DropdownHelper.SelectionChange -= OnChangeSelection;
  54. DropdownHelper.DropdownClose -= OnCloseDropdown;
  55. }
  56. public void SetDropdownItems(string[] itemList, int selectedItemIndex = 0)
  57. {
  58. this.scrollPos = (this.elementSize = Vector2.zero);
  59. this.DropdownList = itemList;
  60. this.SelectedItemIndex = selectedItemIndex;
  61. }
  62. public void Step(int dir)
  63. {
  64. dir = (int)Mathf.Sign(dir);
  65. this.SelectedItemIndex = Utility.Wrap(this.SelectedItemIndex + dir, 0, this.DropdownList.Length);
  66. }
  67. public void Draw(GUIStyle buttonStyle, params GUILayoutOption[] layoutOptions)
  68. {
  69. bool clicked = GUILayout.Button(DropdownList[selectedItemIndex], buttonStyle, layoutOptions);
  70. if (clicked)
  71. {
  72. showDropdown = !clickedYou;
  73. clickedYou = false;
  74. }
  75. if (showDropdown)
  76. {
  77. if (Event.current.type == EventType.Repaint) InitializeDropdown();
  78. }
  79. }
  80. public override void Draw(params GUILayoutOption[] layoutOptions)
  81. {
  82. GUIStyle buttonStyle = new GUIStyle(GUI.skin.button);
  83. buttonStyle.alignment = TextAnchor.MiddleLeft;
  84. this.Draw(buttonStyle, layoutOptions);
  85. }
  86. private void OnChangeSelection(object sender, DropdownSelectArgs args)
  87. {
  88. if (args.DropdownID == this.DropdownID)
  89. {
  90. SelectedItemIndex = args.SelectedItemIndex;
  91. }
  92. }
  93. private void OnCloseDropdown(object sender, DropdownCloseArgs args)
  94. {
  95. if (args.DropdownID == this.DropdownID)
  96. {
  97. scrollPos = args.ScrollPos;
  98. clickedYou = args.ClickedYou;
  99. if (clickedYou) OnDropdownEvent(SelectionChange);
  100. OnDropdownEvent(DropdownClose);
  101. }
  102. }
  103. private void InitializeDropdown()
  104. {
  105. showDropdown = false;
  106. this.buttonRect = GUILayoutUtility.GetLastRect();
  107. Vector2 rectPos = GUIUtility.GUIToScreenPoint(new Vector2(buttonRect.x, buttonRect.y));
  108. buttonRect.x = rectPos.x;
  109. buttonRect.y = rectPos.y;
  110. if (this.elementSize == Vector2.zero)
  111. {
  112. this.elementSize = DropdownHelper.CalculateElementSize(this.DropdownList);
  113. }
  114. DropdownHelper.Set(this);
  115. OnDropdownEvent(DropdownOpen);
  116. }
  117. private void OnDropdownEvent(EventHandler handler)
  118. {
  119. handler?.Invoke(this, EventArgs.Empty);
  120. }
  121. }
  122. public static class DropdownHelper
  123. {
  124. public static event EventHandler<DropdownSelectArgs> SelectionChange;
  125. public static event EventHandler<DropdownCloseArgs> DropdownClose;
  126. private static int dropdownID = 100;
  127. public static int DropdownID => dropdownID++;
  128. private static GUIStyle dropdownStyle;
  129. private static GUIStyle windowStyle;
  130. private static Rect buttonRect;
  131. private static string[] dropdownList;
  132. private static Vector2 scrollPos;
  133. private static int currentDropdownID;
  134. private static int selectedItemIndex;
  135. private static bool initialized = false;
  136. public static bool Visible { get; private set; }
  137. private static bool onScrollBar = false;
  138. public static Rect dropdownRect;
  139. public static Vector2 CalculateElementSize(string[] list)
  140. {
  141. if (!initialized) InitializeStyle();
  142. Vector2 calculatedSize = dropdownStyle.CalcSize(new GUIContent(list[0]));
  143. for (int i = 1; i < list.Length; i++)
  144. {
  145. string word = list[i];
  146. Vector2 calcSize = dropdownStyle.CalcSize(new GUIContent(word));
  147. if (calcSize.x > calculatedSize.x) calculatedSize = calcSize;
  148. }
  149. return calculatedSize;
  150. }
  151. public static void Set(Dropdown dropdown)
  152. {
  153. currentDropdownID = dropdown.DropdownID;
  154. dropdownList = dropdown.DropdownList;
  155. scrollPos = dropdown.ScrollPos;
  156. selectedItemIndex = dropdown.SelectedItemIndex;
  157. scrollPos = dropdown.ScrollPos;
  158. buttonRect = dropdown.ButtonRect;
  159. Vector2 calculatedSize = dropdown.ElementSize;
  160. float calculatedListHeight = calculatedSize.y * dropdownList.Length;
  161. float heightAbove = buttonRect.y;
  162. float heightBelow = Screen.height - heightAbove - buttonRect.height;
  163. float rectWidth = Mathf.Max(calculatedSize.x + 5, buttonRect.width);
  164. float rectHeight = Mathf.Min(calculatedListHeight, Mathf.Max(heightAbove, heightBelow));
  165. if (calculatedListHeight > heightBelow && heightAbove > heightBelow)
  166. {
  167. dropdownRect = new Rect(buttonRect.x, buttonRect.y - rectHeight, rectWidth + 18, rectHeight);
  168. }
  169. else
  170. {
  171. if (calculatedListHeight > heightBelow) rectHeight -= calculatedSize.y;
  172. dropdownRect = new Rect(buttonRect.x, buttonRect.y + buttonRect.height, rectWidth + 18, rectHeight);
  173. }
  174. dropdownRect.x = Mathf.Clamp(dropdownRect.x, 0, Screen.width - rectWidth - 18);
  175. Visible = true;
  176. }
  177. public static void HandleDropdown()
  178. {
  179. if (Visible)
  180. {
  181. GUILayout.Window(Constants.dropdownWindowID, dropdownRect, GUIFunc, "", windowStyle);
  182. }
  183. }
  184. private static void GUIFunc(int id)
  185. {
  186. bool clicked = false;
  187. if (Event.current.type == EventType.MouseUp)
  188. {
  189. clicked = true;
  190. }
  191. scrollPos = GUILayout.BeginScrollView(scrollPos);
  192. int selection = GUILayout.SelectionGrid(selectedItemIndex, dropdownList, 1, dropdownStyle);
  193. GUILayout.EndScrollView();
  194. bool clickedYou = false;
  195. if (Utility.AnyMouseDown())
  196. {
  197. Vector2 mousePos = GUIUtility.GUIToScreenPoint(Event.current.mousePosition);
  198. bool clickedMe = dropdownRect.Contains(mousePos);
  199. onScrollBar = mousePos.x > dropdownRect.x + dropdownRect.width - 12f;
  200. if (buttonRect.Contains(mousePos)) clickedYou = true;
  201. if (!clickedMe) Visible = false;
  202. }
  203. if (selection != selectedItemIndex || (clicked && !onScrollBar))
  204. {
  205. SelectionChange?.Invoke(null, new DropdownSelectArgs(currentDropdownID, selection));
  206. Visible = false;
  207. }
  208. if (!Visible) DropdownClose?.Invoke(null, new DropdownCloseArgs(currentDropdownID, scrollPos, clickedYou));
  209. }
  210. private static void InitializeStyle()
  211. {
  212. dropdownStyle = new GUIStyle(GUI.skin.button);
  213. dropdownStyle.alignment = TextAnchor.MiddleLeft;
  214. dropdownStyle.margin = new RectOffset(0, 0, 0, 0);
  215. dropdownStyle.padding.top = dropdownStyle.padding.bottom = 2;
  216. dropdownStyle.normal.background = Utility.MakeTex(2, 2, new Color(0f, 0f, 0f, 0.5f));
  217. dropdownStyle.onHover.background = dropdownStyle.hover.background = dropdownStyle.onNormal.background = new Texture2D(2, 2);
  218. dropdownStyle.onHover.textColor = dropdownStyle.onNormal.textColor = dropdownStyle.hover.textColor = Color.black;
  219. windowStyle = new GUIStyle(GUI.skin.box);
  220. windowStyle.padding = new RectOffset(0, 0, 0, 0);
  221. windowStyle.alignment = TextAnchor.UpperRight;
  222. initialized = true;
  223. }
  224. public class DropdownEventArgs : EventArgs
  225. {
  226. public int DropdownID { get; }
  227. public DropdownEventArgs(int dropdownID)
  228. {
  229. this.DropdownID = dropdownID;
  230. }
  231. }
  232. public class DropdownSelectArgs : DropdownEventArgs
  233. {
  234. public int SelectedItemIndex { get; }
  235. public DropdownSelectArgs(int dropdownID, int selection) : base(dropdownID)
  236. {
  237. this.SelectedItemIndex = selection;
  238. }
  239. }
  240. public class DropdownCloseArgs : DropdownEventArgs
  241. {
  242. public Vector2 ScrollPos { get; }
  243. public bool ClickedYou { get; }
  244. public DropdownCloseArgs(int dropdownID, Vector2 scrollPos, bool clickedYou = false) : base(dropdownID)
  245. {
  246. this.ScrollPos = scrollPos;
  247. this.ClickedYou = clickedYou;
  248. }
  249. }
  250. }
  251. }