DropDown.cs 13 KB

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