DropDown.cs 13 KB

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