TreeViewItem.cs.bak 9.9 KB

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