DropDown.cs 11 KB

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