FR2_TreeUI2.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace vietlabs.fr2
  7. {
  8. public class FR2_TreeUI2
  9. {
  10. internal Drawer drawer;
  11. private Vector2 position;
  12. private TreeItem rootItem;
  13. internal Rect visibleRect;
  14. public float itemPaddingRight = 0f;
  15. public FR2_TreeUI2(Drawer drawer)
  16. {
  17. this.drawer = drawer;
  18. }
  19. public void Reset(params string[] root)
  20. {
  21. position = Vector2.zero;
  22. rootItem = new TreeItem
  23. {
  24. tree = this,
  25. id = "$root",
  26. height = 0,
  27. depth = -1,
  28. _isOpen = true,
  29. highlight = false,
  30. childCount = root.Length
  31. };
  32. rootItem.RefreshChildren(root);
  33. rootItem.DeepOpen();
  34. }
  35. public void Draw(Rect rect)
  36. {
  37. if (rect.width > 0)
  38. {
  39. visibleRect = rect;
  40. }
  41. var contentRect = new Rect(0f, 0f, 1f, rootItem.childrenHeight);
  42. bool noScroll = contentRect.height < visibleRect.height;
  43. if (noScroll)
  44. {
  45. position = Vector2.zero;
  46. }
  47. var minY = (int) position.y;
  48. var maxY = (int) (position.y + visibleRect.height);
  49. contentRect.x -= FR2_Setting.TreeIndent;
  50. TreeItem.DrawCall = 0;
  51. TreeItem.DrawRender = 0;
  52. position = GUI.BeginScrollView(visibleRect, position, contentRect);
  53. {
  54. var r = new Rect(0, 0, rect.width - (noScroll ? 4f : 18f)-itemPaddingRight, 16f);
  55. var index = 0;
  56. rootItem.Draw(ref index, ref r, minY, maxY);
  57. }
  58. GUI.EndScrollView();
  59. }
  60. public void DrawLayout()
  61. {
  62. EventType evtType = Event.current.type;
  63. Rect r = GUILayoutUtility.GetRect(1f, Screen.width, 16f, Screen.height);
  64. if (evtType != EventType.Layout)
  65. {
  66. visibleRect = r;
  67. }
  68. Draw(visibleRect);
  69. }
  70. public bool NoScroll()
  71. {
  72. return rootItem.childrenHeight < visibleRect.height;
  73. }
  74. // ------------------------ DELEGATE --------------
  75. public class Drawer
  76. {
  77. public virtual int GetHeight(string id)
  78. {
  79. return 16;
  80. }
  81. public virtual int GetChildCount(string id)
  82. {
  83. return 0;
  84. }
  85. public virtual string[] GetChildren(string id)
  86. {
  87. return null;
  88. }
  89. public virtual void Draw(Rect r, TreeItem item)
  90. {
  91. GUI.Label(r, item.id);
  92. }
  93. }
  94. public class GroupDrawer : Drawer
  95. {
  96. public Action<Rect, string, int> drawGroup;
  97. public Action<Rect, string> drawItem;
  98. private Dictionary<string, List<string>> groupDict;
  99. internal FR2_TreeUI2 tree;
  100. public GroupDrawer(Action<Rect, string, int> drawGroup, Action<Rect, string> drawItem)
  101. {
  102. this.drawItem = drawItem;
  103. this.drawGroup = drawGroup;
  104. }
  105. // ----------------- TREE WRAPPER ------------------
  106. public bool TreeNoScroll()
  107. {
  108. return tree.NoScroll();
  109. }
  110. public bool hideGroupIfPossible;
  111. public void Reset<T>(List<T> items, Func<T, string> idFunc, Func<T, string> groupFunc,
  112. Action<List<string>> customGroupSort = null)
  113. {
  114. groupDict = new Dictionary<string, List<string>>();
  115. for (var i = 0; i < items.Count; i++)
  116. {
  117. List<string> list;
  118. string groupName = groupFunc(items[i]);
  119. if (groupName == null) continue; // do not exclude groupName string.Empty
  120. string itemId = idFunc(items[i]);
  121. if (string.IsNullOrEmpty(itemId)) continue; // ignore items without id
  122. if (!groupDict.TryGetValue(groupName, out list))
  123. {
  124. list = new List<string>();
  125. groupDict.Add(groupName, list);
  126. }
  127. list.Add(itemId);
  128. }
  129. if (tree == null)
  130. {
  131. tree = new FR2_TreeUI2(this);
  132. }
  133. List<string> groups = groupDict.Keys.ToList();
  134. if (hideGroupIfPossible && groups.Count == 1) //single group : Flat list
  135. {
  136. var v = groupDict[groups[0]];
  137. tree.Reset(v.ToArray());
  138. groupDict.Clear();
  139. } else { //multiple groups
  140. if (customGroupSort != null)
  141. {
  142. customGroupSort(groups);
  143. }
  144. else
  145. {
  146. groups.Sort();
  147. }
  148. tree.Reset(groups.ToArray());
  149. }
  150. }
  151. public void Draw(Rect r)
  152. {
  153. if (tree != null) tree.Draw(r);
  154. }
  155. public bool hasChildren
  156. {
  157. get {
  158. return tree != null && tree.rootItem.childCount > 0;
  159. }
  160. }
  161. public bool hasValidTree
  162. {
  163. get { return groupDict != null && tree != null; }
  164. }
  165. public void DrawLayout()
  166. {
  167. if (tree != null) tree.DrawLayout();
  168. }
  169. // ----------------- DRAWER WRAPPER ------------------
  170. public override int GetChildCount(string id)
  171. {
  172. if (string.IsNullOrEmpty(id)) return 0;
  173. List<string> group;
  174. if (groupDict.TryGetValue(id, out group))
  175. {
  176. return group.Count;
  177. }
  178. return 0;
  179. }
  180. public override string[] GetChildren(string id)
  181. {
  182. List<string> group;
  183. if (groupDict.TryGetValue(id, out group))
  184. {
  185. return group.ToArray();
  186. }
  187. return null;
  188. }
  189. public override void Draw(Rect r, TreeItem item)
  190. {
  191. List<string> group;
  192. if (groupDict.TryGetValue(item.id, out group))
  193. {
  194. drawGroup(r, item.id, item.childCount);
  195. return;
  196. }
  197. drawItem(r, item.id);
  198. }
  199. }
  200. // ------------------------ TreeItem2 --------------
  201. public class TreeItem
  202. {
  203. public static int DrawCall;
  204. public static int DrawRender;
  205. internal bool _isOpen;
  206. public int childCount;
  207. public List<TreeItem> children;
  208. public int childrenHeight;
  209. public int depth; // item depth
  210. public int height;
  211. public bool highlight;
  212. public string id; // item id
  213. internal TreeItem parent;
  214. //static Color COLOR = new Color(0f, 0f, 0f, 0.05f);
  215. internal FR2_TreeUI2 tree;
  216. public bool IsOpen
  217. {
  218. get { return _isOpen; }
  219. set
  220. {
  221. if (_isOpen == value || childCount == 0)
  222. {
  223. return;
  224. }
  225. _isOpen = value;
  226. if (_isOpen)
  227. {
  228. if (children == null)
  229. {
  230. RefreshChildren(tree.drawer.GetChildren(id));
  231. }
  232. //Update height for all parents
  233. TreeItem p = parent;
  234. while (p != null)
  235. {
  236. p.childrenHeight += childrenHeight;
  237. p = p.parent;
  238. }
  239. }
  240. else
  241. {
  242. //Update height for all parents
  243. TreeItem p = parent;
  244. while (p != null)
  245. {
  246. p.childrenHeight -= childrenHeight;
  247. p = p.parent;
  248. }
  249. }
  250. }
  251. }
  252. internal void DeepOpen()
  253. {
  254. IsOpen = true;
  255. if (children == null)
  256. {
  257. return;
  258. }
  259. for (var i = 0; i < children.Count; i++)
  260. {
  261. children[i].DeepOpen();
  262. }
  263. }
  264. internal void Draw(ref int index, ref Rect rect, int minY, int maxY)
  265. {
  266. DrawCall++;
  267. // if (DrawCall < 10)
  268. // {
  269. // Debug.Log(index + ":" + rect + ":" + minY + ":" + maxY + ":" + height + ":" + childrenHeight);
  270. // }
  271. //var skipDraw = (rect.y >= maxY) || (height <=0);
  272. float min = rect.y;
  273. float max = rect.y + height;
  274. bool interMin = min >= minY && min <= maxY;
  275. bool interMax = max >= minY && max <= maxY;
  276. if (height > 0 && (interMin || interMax))
  277. {
  278. DrawRender++;
  279. rect.height = height;
  280. if (index % 2 == 1 && FR2_Setting.AlternateRowColor)
  281. {
  282. Color o = GUI.color;
  283. GUI.color = FR2_Setting.RowColor;
  284. // GUI.DrawTexture(rect, EditorGUIUtility.whiteTexture);
  285. GUI.DrawTexture(new Rect(rect.x - FR2_Setting.TreeIndent, rect.y, rect.width, rect.height),
  286. EditorGUIUtility.whiteTexture);
  287. GUI.color = o;
  288. }
  289. float x = (depth + 1) * 16f;
  290. tree.drawer.Draw(new Rect(x, rect.y, rect.width - x, rect.height), this);
  291. if (childCount > 0)
  292. {
  293. IsOpen = GUI.Toggle(new Rect(rect.x + x - 16f, rect.y, 16f, 16f), IsOpen, string.Empty,
  294. EditorStyles.foldout);
  295. }
  296. index++;
  297. rect.y += height;
  298. }
  299. else
  300. {
  301. rect.y += height;
  302. }
  303. if (_isOpen && rect.y <= maxY) //draw children
  304. {
  305. for (var i = 0; i < children.Count; i++)
  306. {
  307. children[i].Draw(ref index, ref rect, minY, maxY);
  308. if (rect.y > maxY)
  309. {
  310. break;
  311. }
  312. }
  313. }
  314. }
  315. internal void RefreshChildren(string[] childrenIDs)
  316. {
  317. childCount = childrenIDs.Length;
  318. childrenHeight = 0;
  319. children = new List<TreeItem>();
  320. for (var i = 0; i < childCount; i++)
  321. {
  322. string itemId = childrenIDs[i];
  323. var item = new TreeItem
  324. {
  325. tree = tree,
  326. parent = this,
  327. id = itemId,
  328. depth = depth + 1,
  329. highlight = false,
  330. height = tree.drawer.GetHeight(itemId),
  331. childCount = tree.drawer.GetChildCount(itemId)
  332. };
  333. childrenHeight += item.height;
  334. children.Add(item);
  335. }
  336. }
  337. }
  338. }
  339. }