TimelineTreeView.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEditor.IMGUI.Controls;
  4. using UnityEngine;
  5. namespace UnityEditor.Timeline
  6. {
  7. class TimelineTreeView : ITreeViewGUI
  8. {
  9. float m_FoldoutWidth;
  10. Rect m_DraggingInsertionMarkerRect;
  11. readonly TreeViewController m_TreeView;
  12. List<Rect> m_RowRects = new List<Rect>();
  13. List<Rect> m_ExpandedRowRects = new List<Rect>();
  14. float m_MaxWidthOfRows;
  15. readonly WindowState m_State;
  16. static readonly float kMinTrackHeight = 25.0f;
  17. static readonly float kFoldOutOffset = 14.0f;
  18. static DirectorStyles m_Styles;
  19. public bool showInsertionMarker { get; set; }
  20. public virtual float topRowMargin { get; private set; }
  21. public virtual float bottomRowMargin { get; private set; }
  22. public TimelineTreeView(TimelineWindow sequencerWindow, TreeViewController treeView)
  23. {
  24. m_TreeView = treeView;
  25. m_TreeView.useExpansionAnimation = true;
  26. m_TreeView.selectionChangedCallback += SelectionChangedCallback;
  27. m_TreeView.contextClickOutsideItemsCallback += ContextClickOutsideItemsCallback;
  28. m_TreeView.itemDoubleClickedCallback += ItemDoubleClickedCallback;
  29. m_TreeView.contextClickItemCallback += ContextClickItemCallback;
  30. m_TreeView.SetConsumeKeyDownEvents(false);
  31. m_Styles = DirectorStyles.Instance;
  32. m_State = sequencerWindow.state;
  33. m_FoldoutWidth = DirectorStyles.Instance.foldout.fixedWidth;
  34. }
  35. void ItemDoubleClickedCallback(int id)
  36. {
  37. var trackGUI = m_TreeView.FindItem(id) as TimelineTrackGUI;
  38. if (trackGUI == null)
  39. return;
  40. if (trackGUI.track == null || trackGUI.track.lockedInHierarchy)
  41. return;
  42. var selection = SelectionManager.SelectedItems().ToList();
  43. var items = ItemsUtils.GetItems(trackGUI.track).ToList();
  44. var addToSelection = !selection.SequenceEqual(items);
  45. foreach (var i in items)
  46. {
  47. if (addToSelection)
  48. SelectionManager.Add(i);
  49. else
  50. SelectionManager.Remove(i);
  51. }
  52. }
  53. void ContextClickOutsideItemsCallback()
  54. {
  55. SequencerContextMenu.ShowNewTracksContextMenu(null, m_State);
  56. Event.current.Use();
  57. }
  58. void ContextClickItemCallback(int id)
  59. {
  60. // may not occur if another menu is active
  61. if (!m_TreeView.IsSelected(id))
  62. SelectionChangedCallback(new[] {id});
  63. SequencerContextMenu.ShowTrackContextMenu(SelectionManager.SelectedTracks().ToArray(), Event.current.mousePosition);
  64. Event.current.Use();
  65. }
  66. void SelectionChangedCallback(int[] ids)
  67. {
  68. if (Event.current.button == 1 && PickerUtils.PickedLayerableOfType<ISelectable>() != null)
  69. return;
  70. if (Event.current.command || Event.current.control || Event.current.shift)
  71. SelectionManager.UnSelectTracks();
  72. else
  73. SelectionManager.Clear();
  74. foreach (var id in ids)
  75. {
  76. var trackGUI = (TimelineTrackBaseGUI)m_TreeView.FindItem(id);
  77. SelectionManager.Add(trackGUI.track);
  78. }
  79. m_State.GetWindow().Repaint();
  80. }
  81. public void OnInitialize() {}
  82. public Rect GetRectForFraming(int row)
  83. {
  84. return GetRowRect(row, 1); // We ignore width by default when framing (only y scroll is affected)
  85. }
  86. protected virtual Vector2 GetSizeOfRow(TreeViewItem item)
  87. {
  88. if (item.displayName == "root")
  89. return new Vector2(m_TreeView.GetTotalRect().width, 0.0f);
  90. var trackGroupGui = item as TimelineGroupGUI;
  91. if (trackGroupGui != null)
  92. {
  93. return new Vector2(m_TreeView.GetTotalRect().width, trackGroupGui.GetHeight(m_State));
  94. }
  95. float height = TrackEditor.DefaultTrackHeight;
  96. if (item.hasChildren && m_TreeView.data.IsExpanded(item))
  97. {
  98. height = Mathf.Min(TrackEditor.DefaultTrackHeight, kMinTrackHeight);
  99. }
  100. return new Vector2(m_TreeView.GetTotalRect().width, height);
  101. }
  102. public virtual void BeginRowGUI()
  103. {
  104. if (m_TreeView.GetTotalRect().width != GetRowRect(0).width)
  105. {
  106. CalculateRowRects();
  107. }
  108. m_DraggingInsertionMarkerRect.x = -1;
  109. m_TreeView.SetSelection(SelectionManager.SelectedTrackGUI().Select(t => t.id).ToArray(), false);
  110. }
  111. public virtual void EndRowGUI()
  112. {
  113. // Draw row marker when dragging
  114. if (m_DraggingInsertionMarkerRect.x >= 0 && Event.current.type == EventType.Repaint)
  115. {
  116. Rect insertionRect = m_DraggingInsertionMarkerRect;
  117. const float insertionHeight = 1.0f;
  118. insertionRect.height = insertionHeight;
  119. if (m_TreeView.dragging.drawRowMarkerAbove)
  120. insertionRect.y -= insertionHeight * 0.5f + 2.0f;
  121. else
  122. insertionRect.y += m_DraggingInsertionMarkerRect.height - insertionHeight * 0.5f + 1.0f;
  123. EditorGUI.DrawRect(insertionRect, Color.white);
  124. }
  125. }
  126. public virtual void OnRowGUI(Rect rowRect, TreeViewItem item, int row, bool selected, bool focused)
  127. {
  128. using (new EditorGUI.DisabledScope(TimelineWindow.instance.currentMode.TrackState(TimelineWindow.instance.state) == TimelineModeGUIState.Disabled))
  129. {
  130. var sqvi = (TimelineTrackBaseGUI)item;
  131. sqvi.treeViewToWindowTransformation = m_TreeView.GetTotalRect().position - m_TreeView.state.scrollPos;
  132. // this may be called because an encompassing parent is visible
  133. if (!sqvi.visibleExpanded)
  134. return;
  135. Rect headerRect = rowRect;
  136. Rect contentRect = rowRect;
  137. headerRect.width = m_State.sequencerHeaderWidth - 2.0f;
  138. contentRect.xMin += m_State.sequencerHeaderWidth;
  139. contentRect.width = rowRect.width - m_State.sequencerHeaderWidth - 1.0f;
  140. Rect foldoutRect = rowRect;
  141. var indent = GetFoldoutIndent(item);
  142. var headerRectWithIndent = headerRect;
  143. headerRectWithIndent.xMin = indent;
  144. var rowRectWithIndent = new Rect(rowRect.x + indent, rowRect.y, rowRect.width - indent, rowRect.height);
  145. sqvi.Draw(headerRectWithIndent, contentRect, m_State);
  146. sqvi.DrawInsertionMarkers(rowRectWithIndent);
  147. if (Event.current.type == EventType.Repaint)
  148. {
  149. m_State.spacePartitioner.AddBounds(sqvi);
  150. // Show marker below this Item
  151. if (showInsertionMarker)
  152. {
  153. if (m_TreeView.dragging != null && m_TreeView.dragging.GetRowMarkerControlID() == TreeViewController.GetItemControlID(item))
  154. m_DraggingInsertionMarkerRect = rowRectWithIndent;
  155. }
  156. }
  157. // Draw foldout (after text content above to ensure drop down icon is rendered above selection highlight)
  158. DrawFoldout(item, foldoutRect, indent);
  159. sqvi.ClearDrawFlags();
  160. }
  161. }
  162. void DrawFoldout(TreeViewItem item, Rect foldoutRect, float indent)
  163. {
  164. var showFoldout = m_TreeView.data.IsExpandable(item);
  165. if (showFoldout)
  166. {
  167. foldoutRect.x = indent - kFoldOutOffset;
  168. foldoutRect.width = m_FoldoutWidth;
  169. EditorGUI.BeginChangeCheck();
  170. float foldoutIconHeight = DirectorStyles.Instance.foldout.fixedHeight;
  171. foldoutRect.y += foldoutIconHeight / 2.0f;
  172. foldoutRect.height = foldoutIconHeight;
  173. if (foldoutRect.xMax > m_State.sequencerHeaderWidth)
  174. return;
  175. //Override Disable state for TrakGroup toggle button to expand/collapse group.
  176. bool previousEnableState = GUI.enabled;
  177. GUI.enabled = true;
  178. bool newExpandedValue = GUI.Toggle(foldoutRect, m_TreeView.data.IsExpanded(item), GUIContent.none, m_Styles.foldout);
  179. GUI.enabled = previousEnableState;
  180. if (EditorGUI.EndChangeCheck())
  181. {
  182. if (Event.current.alt)
  183. m_TreeView.data.SetExpandedWithChildren(item, newExpandedValue);
  184. else
  185. m_TreeView.data.SetExpanded(item, newExpandedValue);
  186. }
  187. }
  188. }
  189. public Rect GetRenameRect(Rect rowRect, int row, TreeViewItem item)
  190. {
  191. return rowRect;
  192. }
  193. public void BeginPingItem(TreeViewItem item, float topPixelOfRow, float availableWidth) {}
  194. public void EndPingItem() {}
  195. public Rect GetRowRect(int row, float rowWidth)
  196. {
  197. return GetRowRect(row);
  198. }
  199. public Rect GetRowRect(int row)
  200. {
  201. if (m_RowRects.Count == 0)
  202. return new Rect();
  203. if (row >= m_RowRects.Count)
  204. return new Rect();
  205. return m_RowRects[row];
  206. }
  207. static float GetSpacing(TreeViewItem item)
  208. {
  209. var trackBase = item as TimelineTrackBaseGUI;
  210. if (trackBase != null)
  211. return trackBase.GetVerticalSpacingBetweenTracks();
  212. return 3.0f;
  213. }
  214. public void CalculateRowRects()
  215. {
  216. if (m_TreeView.isSearching)
  217. return;
  218. const float startY = 6.0f;
  219. IList<TreeViewItem> rows = m_TreeView.data.GetRows();
  220. m_RowRects = new List<Rect>(rows.Count);
  221. m_ExpandedRowRects = new List<Rect>(rows.Count);
  222. float curY = startY;
  223. m_MaxWidthOfRows = 1f;
  224. // first pass compute the row rects
  225. for (int i = 0; i < rows.Count; ++i)
  226. {
  227. var item = rows[i];
  228. if (i != 0)
  229. curY += GetSpacing(item);
  230. Vector2 rowSize = GetSizeOfRow(item);
  231. m_RowRects.Add(new Rect(0, curY, rowSize.x, rowSize.y));
  232. m_ExpandedRowRects.Add(m_RowRects[i]);
  233. curY += rowSize.y;
  234. if (rowSize.x > m_MaxWidthOfRows)
  235. m_MaxWidthOfRows = rowSize.x;
  236. // updated the expanded state
  237. var groupGUI = item as TimelineGroupGUI;
  238. if (groupGUI != null)
  239. groupGUI.SetExpanded(m_TreeView.data.IsExpanded(item));
  240. }
  241. float halfHeight = halfDropBetweenHeight;
  242. const float kGroupPad = 1.0f;
  243. const float kSkinPadding = 5.0f * 0.6f;
  244. // work bottom up and compute visible regions for groups
  245. for (int i = rows.Count - 1; i > 0; i--)
  246. {
  247. float height = 0;
  248. TimelineTrackBaseGUI item = (TimelineTrackBaseGUI)rows[i];
  249. if (item.isExpanded && item.children != null && item.children.Count > 0)
  250. {
  251. for (var j = 0; j < item.children.Count; j++)
  252. {
  253. var child = item.children[j];
  254. int index = rows.IndexOf(child);
  255. if (index > i)
  256. height += m_ExpandedRowRects[index].height + kSkinPadding;
  257. }
  258. height += kGroupPad;
  259. }
  260. m_ExpandedRowRects[i] = new Rect(m_RowRects[i].x, m_RowRects[i].y, m_RowRects[i].width, m_RowRects[i].height + height);
  261. var groupGUI = item as TimelineGroupGUI;
  262. if (groupGUI != null)
  263. {
  264. var spacing = GetSpacing(item) + 1;
  265. groupGUI.expandedRect = m_ExpandedRowRects[i];
  266. groupGUI.rowRect = m_RowRects[i];
  267. groupGUI.dropRect = new Rect(m_RowRects[i].x, m_RowRects[i].y - spacing, m_RowRects[i].width, m_RowRects[i].height + Mathf.Max(halfHeight, spacing));
  268. }
  269. }
  270. }
  271. public virtual bool BeginRename(TreeViewItem item, float delay)
  272. {
  273. return false;
  274. }
  275. public virtual void EndRename() {}
  276. protected virtual float GetFoldoutIndent(TreeViewItem item)
  277. {
  278. // Ignore depth when showing search results
  279. if (item.depth <= 1 || m_TreeView.isSearching)
  280. return DirectorStyles.kBaseIndent;
  281. int depth = item.depth;
  282. var trackGUI = item as TimelineTrackGUI;
  283. // first level subtracks are not indented
  284. if (trackGUI != null && trackGUI.track != null && trackGUI.track.isSubTrack)
  285. depth--;
  286. return depth * DirectorStyles.kBaseIndent;
  287. }
  288. public virtual float GetContentIndent(TreeViewItem item)
  289. {
  290. return GetFoldoutIndent(item);
  291. }
  292. public int GetNumRowsOnPageUpDown(TreeViewItem fromItem, bool pageUp, float heightOfTreeView)
  293. {
  294. return (int)Mathf.Floor(heightOfTreeView / 30); // return something
  295. }
  296. // Should return the row number of the first and last row thats fits in the pixel rect defined by top and height
  297. public void GetFirstAndLastRowVisible(out int firstRowVisible, out int lastRowVisible)
  298. {
  299. int rowCount = m_TreeView.data.rowCount;
  300. if (rowCount == 0)
  301. {
  302. firstRowVisible = lastRowVisible = -1;
  303. return;
  304. }
  305. if (rowCount != m_ExpandedRowRects.Count)
  306. {
  307. Debug.LogError("Mismatch in state: rows vs cached rects. Did you remember to hook up: dataSource.onVisibleRowsChanged += gui.CalculateRowRects ?");
  308. CalculateRowRects();
  309. }
  310. float topPixel = m_TreeView.state.scrollPos.y;
  311. float heightInPixels = m_TreeView.GetTotalRect().height;
  312. int firstVisible = -1;
  313. int lastVisible = -1;
  314. Rect visibleRect = new Rect(0, topPixel, m_ExpandedRowRects[0].width, heightInPixels);
  315. for (int i = 0; i < m_ExpandedRowRects.Count; ++i)
  316. {
  317. bool visible = visibleRect.Overlaps(m_ExpandedRowRects[i]);
  318. if (visible)
  319. {
  320. if (firstVisible == -1)
  321. firstVisible = i;
  322. lastVisible = i;
  323. }
  324. TimelineTrackBaseGUI gui = m_TreeView.data.GetItem(i) as TimelineTrackBaseGUI;
  325. if (gui != null)
  326. {
  327. gui.visibleExpanded = visible;
  328. gui.visibleRow = visibleRect.Overlaps(m_RowRects[i]);
  329. }
  330. }
  331. if (firstVisible != -1 && lastVisible != -1)
  332. {
  333. firstRowVisible = firstVisible;
  334. lastRowVisible = lastVisible;
  335. }
  336. else
  337. {
  338. firstRowVisible = 0;
  339. lastRowVisible = rowCount - 1;
  340. }
  341. }
  342. public Vector2 GetTotalSize()
  343. {
  344. if (m_RowRects.Count == 0)
  345. return new Vector2(0, 0);
  346. return new Vector2(m_MaxWidthOfRows, m_RowRects[m_RowRects.Count - 1].yMax);
  347. }
  348. public virtual float halfDropBetweenHeight
  349. {
  350. get { return 8f; }
  351. }
  352. }
  353. }