DropDown.cs 12 KB

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