TreeViewItem.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [Serializable]
  5. public class TreeViewItem
  6. {
  7. public string Header = string.Empty;
  8. public bool IsExpanded = true;
  9. public bool IsCheckBox = false;
  10. public bool IsChecked = false;
  11. public bool IsHover = false;
  12. public bool IsSelected = false;
  13. public List<TreeViewItem> Items = new List<TreeViewItem>();
  14. public TreeViewControl ParentControl = null;
  15. public TreeViewItem Parent = null;
  16. public object DataContext = null;
  17. public class ClickEventArgs : System.EventArgs
  18. {
  19. }
  20. public EventHandler Click = null;
  21. public class CheckedEventArgs : System.EventArgs
  22. {
  23. }
  24. public EventHandler Checked = null;
  25. public class UncheckedEventArgs : System.EventArgs
  26. {
  27. }
  28. public EventHandler Unchecked = null;
  29. public class SelectedEventArgs : System.EventArgs
  30. {
  31. }
  32. public EventHandler Selected = null;
  33. public class UnselectedEventArgs : System.EventArgs
  34. {
  35. }
  36. public EventHandler Unselected = null;
  37. /// <summary>
  38. /// The distance to the hover item
  39. /// </summary>
  40. float m_hoverTime = 0f;
  41. public TreeViewItem(TreeViewControl parentControl, TreeViewItem parent)
  42. {
  43. ParentControl = parentControl;
  44. Parent = parent;
  45. if (null == parentControl)
  46. {
  47. return;
  48. }
  49. }
  50. public TreeViewItem AddItem(string header)
  51. {
  52. TreeViewItem item = new TreeViewItem(ParentControl, this) { Header = header };
  53. Items.Add(item);
  54. return item;
  55. }
  56. public TreeViewItem AddItem(string header, bool isExpanded)
  57. {
  58. TreeViewItem item = new TreeViewItem(ParentControl, this) { Header = header, IsExpanded = isExpanded };
  59. Items.Add(item);
  60. return item;
  61. }
  62. public TreeViewItem AddItem(string header, bool isExpanded, bool isChecked)
  63. {
  64. TreeViewItem item = new TreeViewItem(ParentControl, this) { Header = header, IsExpanded = isExpanded, IsCheckBox = true, IsChecked = isChecked };
  65. Items.Add(item);
  66. return item;
  67. }
  68. public bool HasChildItems()
  69. {
  70. if (null == Items ||
  71. Items.Count == 0)
  72. {
  73. return false;
  74. }
  75. else
  76. {
  77. return true;
  78. }
  79. }
  80. public enum SiblingOrder
  81. {
  82. FIRST_CHILD,
  83. MIDDLE_CHILD,
  84. LAST_CHILD,
  85. }
  86. public enum TextureIcons
  87. {
  88. BLANK,
  89. GUIDE,
  90. LAST_SIBLING_COLLAPSED,
  91. LAST_SIBLING_EXPANDED,
  92. LAST_SIBLING_NO_CHILD,
  93. MIDDLE_SIBLING_COLLAPSED,
  94. MIDDLE_SIBLING_EXPANDED,
  95. MIDDLE_SIBLING_NO_CHILD,
  96. NORMAL_CHECKED,
  97. NORMAL_UNCHECKED,
  98. }
  99. float CalculateHoverTime(Rect rect, Vector3 mousePos)
  100. {
  101. if (rect.Contains(mousePos))
  102. {
  103. return 0f;
  104. }
  105. float midPoint = (rect.yMin + rect.yMax) * 0.5f;
  106. float pointA = mousePos.y;
  107. return Mathf.Abs(midPoint - pointA) / 50f;
  108. }
  109. public void DisplayItem(int levels, SiblingOrder siblingOrder)
  110. {
  111. if (null == ParentControl)
  112. {
  113. return;
  114. }
  115. GUILayout.BeginHorizontal();
  116. for (int index = 0; index < levels; ++index)
  117. {
  118. ParentControl.Button(TextureIcons.GUIDE);
  119. }
  120. if (!HasChildItems())
  121. {
  122. bool result;
  123. switch (siblingOrder)
  124. {
  125. case SiblingOrder.FIRST_CHILD:
  126. result = ParentControl.Button(TextureIcons.MIDDLE_SIBLING_NO_CHILD);
  127. break;
  128. case SiblingOrder.MIDDLE_CHILD:
  129. result = ParentControl.Button(TextureIcons.MIDDLE_SIBLING_NO_CHILD);
  130. break;
  131. case SiblingOrder.LAST_CHILD:
  132. result = ParentControl.Button(TextureIcons.LAST_SIBLING_NO_CHILD);
  133. break;
  134. default:
  135. result = false;
  136. break;
  137. }
  138. if (result)
  139. {
  140. IsExpanded = !IsExpanded;
  141. }
  142. }
  143. else
  144. {
  145. if (IsExpanded)
  146. {
  147. bool result;
  148. switch (siblingOrder)
  149. {
  150. case SiblingOrder.FIRST_CHILD:
  151. result = ParentControl.Button(TextureIcons.MIDDLE_SIBLING_EXPANDED);
  152. break;
  153. case SiblingOrder.MIDDLE_CHILD:
  154. result = ParentControl.Button(TextureIcons.MIDDLE_SIBLING_EXPANDED);
  155. break;
  156. case SiblingOrder.LAST_CHILD:
  157. result = ParentControl.Button(TextureIcons.LAST_SIBLING_EXPANDED);
  158. break;
  159. default:
  160. result = false;
  161. break;
  162. }
  163. if (result)
  164. {
  165. IsExpanded = !IsExpanded;
  166. }
  167. }
  168. else
  169. {
  170. bool result;
  171. switch (siblingOrder)
  172. {
  173. case SiblingOrder.FIRST_CHILD:
  174. result = ParentControl.Button(TextureIcons.MIDDLE_SIBLING_COLLAPSED);
  175. break;
  176. case SiblingOrder.MIDDLE_CHILD:
  177. result = ParentControl.Button(TextureIcons.MIDDLE_SIBLING_COLLAPSED);
  178. break;
  179. case SiblingOrder.LAST_CHILD:
  180. result = ParentControl.Button(TextureIcons.LAST_SIBLING_COLLAPSED);
  181. break;
  182. default:
  183. result = false;
  184. break;
  185. }
  186. if (result)
  187. {
  188. IsExpanded = !IsExpanded;
  189. }
  190. }
  191. }
  192. // display the text for the tree view
  193. if (!string.IsNullOrEmpty(Header))
  194. {
  195. bool isSelected;
  196. if (ParentControl.SelectedItem == this &&
  197. !ParentControl.m_forceDefaultSkin)
  198. {
  199. //use selected skin
  200. GUI.skin = ParentControl.m_skinSelected;
  201. isSelected = true;
  202. }
  203. else
  204. {
  205. isSelected = false;
  206. }
  207. if (IsCheckBox)
  208. {
  209. if (IsChecked &&
  210. ParentControl.Button(TextureIcons.NORMAL_CHECKED))
  211. {
  212. IsChecked = false;
  213. if (ParentControl.SelectedItem != this)
  214. {
  215. ParentControl.SelectedItem = this;
  216. this.IsSelected = true;
  217. if (null != Selected)
  218. {
  219. Selected.Invoke(this, new SelectedEventArgs());
  220. }
  221. }
  222. if (null != Unchecked)
  223. {
  224. Unchecked.Invoke(this, new UncheckedEventArgs());
  225. }
  226. }
  227. else if (!IsChecked &&
  228. ParentControl.Button(TextureIcons.NORMAL_UNCHECKED))
  229. {
  230. IsChecked = true;
  231. if (ParentControl.SelectedItem != this)
  232. {
  233. ParentControl.SelectedItem = this;
  234. this.IsSelected = true;
  235. if (null != Selected)
  236. {
  237. Selected.Invoke(this, new SelectedEventArgs());
  238. }
  239. }
  240. if (null != Checked)
  241. {
  242. Checked.Invoke(this, new CheckedEventArgs());
  243. }
  244. }
  245. ParentControl.Button(TextureIcons.BLANK);
  246. }
  247. if (ParentControl.IsHoverEnabled)
  248. {
  249. GUISkin oldSkin = GUI.skin;
  250. if (isSelected)
  251. {
  252. GUI.skin = ParentControl.m_skinSelected;
  253. }
  254. else if (IsHover)
  255. {
  256. GUI.skin = ParentControl.m_skinHover;
  257. }
  258. else
  259. {
  260. GUI.skin = ParentControl.m_skinUnselected;
  261. }
  262. if (ParentControl.IsHoverAnimationEnabled)
  263. {
  264. GUI.skin.button.fontSize = (int)Mathf.Lerp(20f, 12f, m_hoverTime);
  265. }
  266. GUI.SetNextControlName("toggleButton"); //workaround to dirty GUI
  267. if (GUILayout.Button(Header))
  268. {
  269. GUI.FocusControl("toggleButton"); //workaround to dirty GUI
  270. if (ParentControl.SelectedItem != this)
  271. {
  272. ParentControl.SelectedItem = this;
  273. this.IsSelected = true;
  274. if (null != Selected)
  275. {
  276. Selected.Invoke(this, new SelectedEventArgs());
  277. }
  278. }
  279. if (null != Click)
  280. {
  281. Click.Invoke(this, new ClickEventArgs());
  282. }
  283. }
  284. GUI.skin = oldSkin;
  285. }
  286. else
  287. {
  288. GUI.SetNextControlName("toggleButton"); //workaround to dirty GUI
  289. if (GUILayout.Button(Header))
  290. {
  291. GUI.FocusControl("toggleButton"); //workaround to dirty GUI
  292. if (ParentControl.SelectedItem != this)
  293. {
  294. ParentControl.SelectedItem = this;
  295. this.IsSelected = true;
  296. if (null != Selected)
  297. {
  298. Selected.Invoke(this, new SelectedEventArgs());
  299. }
  300. }
  301. if (null != Click)
  302. {
  303. Click.Invoke(this, new ClickEventArgs());
  304. }
  305. }
  306. }
  307. if (isSelected &&
  308. !ParentControl.m_forceDefaultSkin)
  309. {
  310. //return to default skin
  311. GUI.skin = ParentControl.m_skinUnselected;
  312. }
  313. }
  314. GUILayout.EndHorizontal();
  315. if (ParentControl.IsHoverEnabled)
  316. {
  317. if (null != Event.current &&
  318. Event.current.type == EventType.Repaint)
  319. {
  320. Vector2 mousePos = Event.current.mousePosition;
  321. if (ParentControl.HasFocus(mousePos))
  322. {
  323. Rect lastRect = GUILayoutUtility.GetLastRect();
  324. if (lastRect.Contains(mousePos))
  325. {
  326. IsHover = true;
  327. ParentControl.HoverItem = this;
  328. }
  329. else
  330. {
  331. IsHover = false;
  332. }
  333. if (ParentControl.IsHoverEnabled &&
  334. ParentControl.IsHoverAnimationEnabled)
  335. {
  336. m_hoverTime = CalculateHoverTime(lastRect, Event.current.mousePosition);
  337. }
  338. }
  339. }
  340. }
  341. if (HasChildItems() &&
  342. IsExpanded)
  343. {
  344. for (int index = 0; index < Items.Count; ++index)
  345. {
  346. TreeViewItem child = Items[index];
  347. child.Parent = this;
  348. if ((index + 1) == Items.Count)
  349. {
  350. child.DisplayItem(levels + 1, SiblingOrder.LAST_CHILD);
  351. }
  352. else if (index == 0)
  353. {
  354. child.DisplayItem(levels + 1, SiblingOrder.FIRST_CHILD);
  355. }
  356. else
  357. {
  358. child.DisplayItem(levels + 1, SiblingOrder.MIDDLE_CHILD);
  359. }
  360. }
  361. }
  362. if (IsSelected &&
  363. ParentControl.SelectedItem != this)
  364. {
  365. if (null != Unselected)
  366. {
  367. Unselected.Invoke(this, new UnselectedEventArgs());
  368. }
  369. }
  370. IsSelected = ParentControl.SelectedItem == this;
  371. }
  372. }