DropDown.cs 11 KB

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