DropDown.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 dropdownWindow;
  139. private static Rect dropdownScrollRect;
  140. private static Rect dropdownRect;
  141. public static Vector2 CalculateElementSize(string[] list)
  142. {
  143. if (!initialized) InitializeStyle();
  144. Vector2 calculatedSize = dropdownStyle.CalcSize(new GUIContent(list[0]));
  145. for (int i = 1; i < list.Length; i++)
  146. {
  147. string word = list[i];
  148. Vector2 calcSize = dropdownStyle.CalcSize(new GUIContent(word));
  149. if (calcSize.x > calculatedSize.x) calculatedSize = calcSize;
  150. }
  151. return calculatedSize;
  152. }
  153. public static void Set(Dropdown dropdown)
  154. {
  155. currentDropdownID = dropdown.DropdownID;
  156. dropdownList = dropdown.DropdownList;
  157. scrollPos = dropdown.ScrollPos;
  158. selectedItemIndex = dropdown.SelectedItemIndex;
  159. scrollPos = dropdown.ScrollPos;
  160. buttonRect = dropdown.ButtonRect;
  161. Vector2 calculatedSize = dropdown.ElementSize;
  162. float calculatedListHeight = calculatedSize.y * dropdownList.Length;
  163. float heightAbove = buttonRect.y;
  164. float heightBelow = Screen.height - heightAbove - buttonRect.height;
  165. float rectWidth = Mathf.Max(calculatedSize.x + 5, buttonRect.width);
  166. float rectHeight = Mathf.Min(calculatedListHeight, Mathf.Max(heightAbove, heightBelow));
  167. if (calculatedListHeight > heightBelow && heightAbove > heightBelow)
  168. {
  169. dropdownWindow = new Rect(buttonRect.x, buttonRect.y - rectHeight, rectWidth + 18, rectHeight);
  170. }
  171. else
  172. {
  173. if (calculatedListHeight > heightBelow) rectHeight -= calculatedSize.y;
  174. dropdownWindow = new Rect(buttonRect.x, buttonRect.y + buttonRect.height, rectWidth + 18, rectHeight);
  175. }
  176. dropdownWindow.x = Mathf.Clamp(dropdownWindow.x, 0, Screen.width - rectWidth - 18);
  177. dropdownScrollRect = new Rect(0, 0, dropdownWindow.width, dropdownWindow.height);
  178. dropdownRect = new Rect(0, 0, dropdownWindow.width - 18, calculatedListHeight);
  179. Visible = true;
  180. }
  181. public static void HandleDropdown()
  182. {
  183. dropdownWindow = GUI.Window(Constants.dropdownWindowID, dropdownWindow, GUIFunc, "", windowStyle);
  184. }
  185. private static void GUIFunc(int id)
  186. {
  187. bool clicked = false;
  188. if (Event.current.type == EventType.MouseUp)
  189. {
  190. clicked = true;
  191. }
  192. scrollPos = GUI.BeginScrollView(dropdownScrollRect, scrollPos, dropdownRect);
  193. int selection = GUI.SelectionGrid(dropdownRect, selectedItemIndex, dropdownList, 1, dropdownStyle);
  194. GUI.EndScrollView();
  195. bool clickedYou = false;
  196. if (Utility.AnyMouseDown())
  197. {
  198. Vector2 mousePos = GUIUtility.GUIToScreenPoint(Event.current.mousePosition);
  199. bool clickedMe = dropdownWindow.Contains(mousePos);
  200. onScrollBar = mousePos.x > dropdownWindow.x + dropdownWindow.width - 12f;
  201. if (buttonRect.Contains(mousePos)) clickedYou = true;
  202. if (!clickedMe) Visible = false;
  203. }
  204. if (selection != selectedItemIndex || (clicked && !onScrollBar))
  205. {
  206. SelectionChange?.Invoke(null, new DropdownSelectArgs(currentDropdownID, selection));
  207. Visible = false;
  208. }
  209. if (!Visible) DropdownClose?.Invoke(null, new DropdownCloseArgs(currentDropdownID, scrollPos, clickedYou));
  210. }
  211. private static void InitializeStyle()
  212. {
  213. dropdownStyle = new GUIStyle(GUI.skin.button);
  214. dropdownStyle.alignment = TextAnchor.MiddleLeft;
  215. dropdownStyle.margin = new RectOffset(0, 0, 0, 0);
  216. dropdownStyle.padding.top = dropdownStyle.padding.bottom = 2;
  217. dropdownStyle.normal.background = Utility.MakeTex(2, 2, new Color(0f, 0f, 0f, 0.5f));
  218. dropdownStyle.onHover.background = dropdownStyle.hover.background = dropdownStyle.onNormal.background = new Texture2D(2, 2);
  219. dropdownStyle.onHover.textColor = dropdownStyle.onNormal.textColor = dropdownStyle.hover.textColor = Color.black;
  220. windowStyle = new GUIStyle(GUI.skin.box);
  221. windowStyle.padding = new RectOffset(0, 0, 0, 0);
  222. windowStyle.alignment = TextAnchor.UpperRight;
  223. initialized = true;
  224. }
  225. public class DropdownEventArgs : EventArgs
  226. {
  227. public int DropdownID { get; }
  228. public DropdownEventArgs(int dropdownID)
  229. {
  230. this.DropdownID = dropdownID;
  231. }
  232. }
  233. public class DropdownSelectArgs : DropdownEventArgs
  234. {
  235. public int SelectedItemIndex { get; }
  236. public DropdownSelectArgs(int dropdownID, int selection) : base(dropdownID)
  237. {
  238. this.SelectedItemIndex = selection;
  239. }
  240. }
  241. public class DropdownCloseArgs : DropdownEventArgs
  242. {
  243. public Vector2 ScrollPos { get; }
  244. public bool ClickedYou { get; }
  245. public DropdownCloseArgs(int dropdownID, Vector2 scrollPos, bool clickedYou = false) : base(dropdownID)
  246. {
  247. this.ScrollPos = scrollPos;
  248. this.ClickedYou = clickedYou;
  249. }
  250. }
  251. }
  252. }