DropDown.cs 13 KB

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