DropDown.cs 11 KB

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