TimelineTreeViewGUI.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEditor.IMGUI.Controls;
  4. using UnityEngine;
  5. using UnityEngine.Timeline;
  6. namespace UnityEditor.Timeline
  7. {
  8. class TimelineTreeViewGUI
  9. {
  10. readonly TimelineAsset m_Timeline;
  11. readonly TreeViewController m_TreeView;
  12. readonly TimelineTreeView m_TimelineTreeView;
  13. readonly TimelineWindow m_Window;
  14. readonly TimelineDataSource m_DataSource;
  15. TreeViewItem root
  16. {
  17. get { return m_DataSource.root; }
  18. }
  19. TimelineTrackBaseGUI[] visibleTrackGuis
  20. {
  21. get
  22. {
  23. int firstRow;
  24. int lastRow;
  25. var visibleRows = new List<TimelineTrackBaseGUI>();
  26. m_TreeView.gui.GetFirstAndLastRowVisible(out firstRow, out lastRow);
  27. for (int r = firstRow; r <= lastRow; r++)
  28. {
  29. var track = m_TreeView.data.GetItem(r) as TimelineTrackBaseGUI;
  30. if (track != null && track != root)
  31. {
  32. AddVisibleTrackRecursive(ref visibleRows, track);
  33. }
  34. }
  35. return visibleRows.ToArray();
  36. }
  37. }
  38. public TrackAsset[] visibleTracks
  39. {
  40. get { return visibleTrackGuis.Select(x => x.track).ToArray(); }
  41. }
  42. public List<TimelineClipGUI> allClipGuis
  43. {
  44. get
  45. {
  46. TimelineDataSource dataSource = m_TreeView.data as TimelineDataSource;
  47. if (dataSource != null && dataSource.allTrackGuis != null)
  48. return dataSource.allTrackGuis.OfType<TimelineTrackGUI>().SelectMany(x => x.clips).ToList();
  49. return null;
  50. }
  51. }
  52. public List<TimelineTrackBaseGUI> allTrackGuis
  53. {
  54. get
  55. {
  56. var dataSource = m_TreeView.data as TimelineDataSource;
  57. if (dataSource != null)
  58. return dataSource.allTrackGuis;
  59. return null;
  60. }
  61. }
  62. public Vector2 contentSize
  63. {
  64. get { return m_TreeView.GetContentSize(); }
  65. }
  66. public Vector2 scrollPosition
  67. {
  68. get { return m_TreeView.state.scrollPos; }
  69. set
  70. {
  71. Rect r = m_TreeView.GetTotalRect();
  72. Vector2 visibleContent = m_TreeView.GetContentSize();
  73. m_TreeView.state.scrollPos = new Vector2(value.x, Mathf.Clamp(value.y, 0, Mathf.Max(0, visibleContent.y - r.height)));
  74. }
  75. }
  76. public bool showingVerticalScrollBar
  77. {
  78. get { return m_TreeView.showingVerticalScrollBar; }
  79. }
  80. public void FrameItem(TreeViewItem item)
  81. {
  82. m_TreeView.Frame(item.id, true, false, true);
  83. }
  84. public TimelineDragging timelineDragging { get {return m_TreeView.dragging as TimelineDragging; }}
  85. public TimelineTreeViewGUI(TimelineWindow sequencerWindow, TimelineAsset timeline, Rect rect)
  86. {
  87. m_Timeline = timeline;
  88. m_Window = sequencerWindow;
  89. var treeviewState = new TreeViewState();
  90. treeviewState.scrollPos = new Vector2(treeviewState.scrollPos.x, TimelineWindowViewPrefs.GetOrCreateViewModel(m_Timeline).verticalScroll);
  91. m_TreeView = new TreeViewController(sequencerWindow, treeviewState);
  92. m_TreeView.horizontalScrollbarStyle = GUIStyle.none;
  93. m_TreeView.scrollViewStyle = GUI.skin.scrollView;
  94. m_TreeView.keyboardInputCallback = sequencerWindow.TreeViewKeyboardCallback;
  95. m_TimelineTreeView = new TimelineTreeView(sequencerWindow, m_TreeView);
  96. var dragging = new TimelineDragging(m_TreeView, m_Window, m_Timeline);
  97. m_DataSource = new TimelineDataSource(this, m_TreeView, sequencerWindow);
  98. m_DataSource.onVisibleRowsChanged += m_TimelineTreeView.CalculateRowRects;
  99. m_TreeView.Init(rect, m_DataSource, m_TimelineTreeView, dragging);
  100. m_DataSource.ExpandItems(m_DataSource.root);
  101. }
  102. public ITreeViewGUI gui
  103. {
  104. get { return m_TimelineTreeView; }
  105. }
  106. public ITreeViewDataSource data
  107. {
  108. get { return m_TreeView == null ? null : m_TreeView.data; }
  109. }
  110. public TimelineWindow TimelineWindow
  111. {
  112. get { return m_Window; }
  113. }
  114. public void CalculateRowRects()
  115. {
  116. m_TimelineTreeView.CalculateRowRects();
  117. }
  118. public void Reload()
  119. {
  120. AnimationClipCurveCache.Instance.ClearCachedProxyClips();
  121. m_TreeView.ReloadData();
  122. m_DataSource.ExpandItems(m_DataSource.root);
  123. m_TimelineTreeView.CalculateRowRects();
  124. }
  125. public void OnGUI(Rect rect)
  126. {
  127. int keyboardControl = GUIUtility.GetControlID(FocusType.Passive, rect);
  128. m_TreeView.OnGUI(rect, keyboardControl);
  129. TimelineWindowViewPrefs.GetOrCreateViewModel(m_Timeline).verticalScroll = m_TreeView.state.scrollPos.y;
  130. }
  131. public Rect GetRowRect(int row)
  132. {
  133. return m_TimelineTreeView.GetRowRect(row);
  134. }
  135. static void AddVisibleTrackRecursive(ref List<TimelineTrackBaseGUI> list, TimelineTrackBaseGUI track)
  136. {
  137. if (track == null)
  138. return;
  139. list.Add(track);
  140. if (!track.isExpanded)
  141. return;
  142. if (track.children != null)
  143. {
  144. foreach (var c in track.children)
  145. {
  146. AddVisibleTrackRecursive(ref list, c as TimelineTrackBaseGUI);
  147. }
  148. }
  149. }
  150. }
  151. }