DropDown.cs 13 KB

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