TimelineDataSource.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor.IMGUI.Controls;
  5. using UnityEngine;
  6. using UnityEngine.Playables;
  7. using UnityEngine.Timeline;
  8. namespace UnityEditor.Timeline
  9. {
  10. class TimelineDataSource : TreeViewDataSource
  11. {
  12. readonly TimelineWindow m_TimelineWindow;
  13. readonly TimelineTreeViewGUI m_ParentGUI;
  14. public List<TimelineTrackBaseGUI> allTrackGuis { get; private set; }
  15. TreeViewItem treeroot
  16. {
  17. get { return m_RootItem; }
  18. }
  19. public TimelineDataSource(TimelineTreeViewGUI parentGUI, TreeViewController treeView, TimelineWindow sequencerWindow)
  20. : base(treeView)
  21. {
  22. m_TreeView.useExpansionAnimation = false;
  23. m_TimelineWindow = sequencerWindow;
  24. m_ParentGUI = parentGUI;
  25. FetchData();
  26. }
  27. public override bool IsExpanded(TreeViewItem item)
  28. {
  29. if (!IsExpandable(item))
  30. return true;
  31. return IsExpanded(item.id);
  32. }
  33. public override bool IsExpandable(TreeViewItem item)
  34. {
  35. var expandable = false;
  36. var track = item as TimelineTrackBaseGUI;
  37. if (track != null)
  38. expandable = track.expandable;
  39. return expandable && item.hasChildren;
  40. }
  41. public sealed override void FetchData()
  42. {
  43. // create root item
  44. m_RootItem = new TimelineGroupGUI(m_TreeView, m_ParentGUI, 1, 0, null, "root", null, true);
  45. var tree = new Dictionary<TrackAsset, TimelineTrackBaseGUI>();
  46. var filteredView = m_TimelineWindow.state.editSequence.asset.trackObjects;
  47. allTrackGuis = new List<TimelineTrackBaseGUI>(filteredView.Count());
  48. foreach (var t in filteredView)
  49. {
  50. CreateItem(t, ref tree, filteredView.OfType<TrackAsset>(), m_RootItem);
  51. }
  52. m_NeedRefreshRows = true;
  53. SetExpanded(m_RootItem, true);
  54. }
  55. TimelineTrackBaseGUI CreateItem(ScriptableObject scriptableObject, ref Dictionary<TrackAsset, TimelineTrackBaseGUI> tree, IEnumerable<TrackAsset> selectedRows, TreeViewItem parentTreeViewItem)
  56. {
  57. // if a script doesn't load correctly, the trackAsset will be NULL, but the scriptableObject __should_ be intact (but == null will be true)
  58. var trackAsset = scriptableObject as TrackAsset;
  59. if (tree == null)
  60. throw new ArgumentNullException("tree");
  61. if (selectedRows == null)
  62. throw new ArgumentNullException("selectedRows");
  63. if (trackAsset != null && tree.ContainsKey(trackAsset))
  64. return tree[trackAsset];
  65. TimelineTrackBaseGUI parentItem = parentTreeViewItem as TimelineTrackBaseGUI;
  66. // should we create the parent?
  67. TrackAsset parentTrack = trackAsset != null ? (trackAsset.parent as TrackAsset) : null;
  68. if (trackAsset != null && parentTrack != null && selectedRows.Contains(parentTrack))
  69. {
  70. parentItem = CreateItem(parentTrack, ref tree, selectedRows, parentTreeViewItem);
  71. }
  72. int theDepth = -1;
  73. if (parentItem != null)
  74. theDepth = parentItem.depth;
  75. theDepth++;
  76. TimelineTrackBaseGUI newItem;
  77. if (trackAsset == null)
  78. {
  79. PlayableAsset parent = m_TimelineWindow.state.editSequence.asset;
  80. if (parentItem != null && parentItem.track != null)
  81. parent = parentItem.track;
  82. newItem = new TimelineTrackErrorGUI(m_TreeView, m_ParentGUI, 0, theDepth, parentItem, "ERROR", scriptableObject, parent);
  83. }
  84. else if (trackAsset.GetType() != typeof(GroupTrack))
  85. {
  86. newItem = new TimelineTrackGUI(m_TreeView, m_ParentGUI, trackAsset.GetInstanceID(), theDepth, parentItem, trackAsset.name, trackAsset);
  87. }
  88. else
  89. {
  90. newItem = new TimelineGroupGUI(m_TreeView, m_ParentGUI, trackAsset.GetInstanceID(), theDepth, parentItem, trackAsset.name, trackAsset, false);
  91. }
  92. allTrackGuis.Add(newItem);
  93. if (parentItem != null)
  94. {
  95. if (parentItem.children == null)
  96. parentItem.children = new List<TreeViewItem>();
  97. parentItem.children.Add(newItem);
  98. }
  99. else
  100. {
  101. m_RootItem = newItem;
  102. SetExpanded(m_RootItem, true);
  103. }
  104. if (trackAsset != null)
  105. tree[trackAsset] = newItem;
  106. var actorAsAnimTrack = newItem.track as AnimationTrack;
  107. bool isEditableInfiniteClip = actorAsAnimTrack != null && actorAsAnimTrack.ShouldShowInfiniteClipEditor();
  108. if (isEditableInfiniteClip)
  109. {
  110. if (newItem.children == null)
  111. newItem.children = new List<TreeViewItem>();
  112. }
  113. else if (trackAsset != null)
  114. {
  115. // check if clips on this track have animation, if so we inline a animationEditorTrack
  116. bool clipHasAnimatableAnimationCurves = false;
  117. for (var i = 0; i != newItem.track.clips.Length; ++i)
  118. {
  119. var curveClip = newItem.track.clips[i].curves;
  120. var animationClip = newItem.track.clips[i].animationClip;
  121. // prune out clip with zero curves
  122. if (curveClip != null && curveClip.empty)
  123. curveClip = null;
  124. if (animationClip != null && animationClip.empty)
  125. animationClip = null;
  126. // prune out clips coming from FBX
  127. if (animationClip != null && ((animationClip.hideFlags & HideFlags.NotEditable) != 0))
  128. animationClip = null;
  129. if (!newItem.track.clips[i].recordable)
  130. animationClip = null;
  131. clipHasAnimatableAnimationCurves = (curveClip != null) || (animationClip != null);
  132. if (clipHasAnimatableAnimationCurves)
  133. break;
  134. }
  135. if (clipHasAnimatableAnimationCurves)
  136. {
  137. if (newItem.children == null)
  138. newItem.children = new List<TreeViewItem>();
  139. }
  140. }
  141. if (trackAsset != null)
  142. {
  143. // Here we are using the internal subTrackObject so we can properly handle tracks whose script
  144. // can't load (via ScriptableObject)
  145. foreach (var subTrack in trackAsset.subTracksObjects)
  146. {
  147. CreateItem(subTrack, ref tree, selectedRows, newItem);
  148. }
  149. }
  150. return newItem;
  151. }
  152. public override bool CanBeParent(TreeViewItem item)
  153. {
  154. // will prevent track becoming subtracks via dragging
  155. TimelineTrackGUI track = item as TimelineTrackGUI;
  156. if (track != null)
  157. return false;
  158. return true;
  159. }
  160. public void ExpandItems(TreeViewItem item)
  161. {
  162. if (treeroot == item)
  163. {
  164. SetExpanded(treeroot, true);
  165. }
  166. TimelineGroupGUI gui = item as TimelineGroupGUI;
  167. if (gui != null && gui.track != null)
  168. {
  169. SetExpanded(item, !gui.track.GetCollapsed());
  170. }
  171. if (item.children != null)
  172. {
  173. for (int c = 0; c < item.children.Count; c++)
  174. {
  175. ExpandItems(item.children[c]);
  176. }
  177. }
  178. }
  179. }
  180. }