BindingSelector.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEditor.IMGUI.Controls;
  4. using UnityEditor.Timeline;
  5. using UnityEditorInternal;
  6. using UnityEngine;
  7. using UnityEngine.Timeline;
  8. namespace UnityEditor
  9. {
  10. class BindingSelector
  11. {
  12. TreeViewController m_TreeView;
  13. public TreeViewController treeViewController
  14. {
  15. get { return m_TreeView; }
  16. }
  17. TreeViewState m_TrackGlobalTreeViewState;
  18. TreeViewState m_TreeViewState;
  19. BindingTreeViewDataSource m_TreeViewDataSource;
  20. CurveDataSource m_CurveDataSource;
  21. TimelineWindow m_Window;
  22. CurveEditor m_CurveEditor;
  23. ReorderableList m_DopeLines;
  24. string[] m_StringList = {};
  25. int[] m_Selection;
  26. bool m_PartOfSelection;
  27. public BindingSelector(EditorWindow window, CurveEditor curveEditor, TreeViewState trackGlobalTreeViewState)
  28. {
  29. m_Window = window as TimelineWindow;
  30. m_CurveEditor = curveEditor;
  31. m_TrackGlobalTreeViewState = trackGlobalTreeViewState;
  32. m_DopeLines = new ReorderableList(m_StringList, typeof(string), false, false, false, false);
  33. m_DopeLines.drawElementBackgroundCallback = null;
  34. m_DopeLines.showDefaultBackground = false;
  35. m_DopeLines.index = 0;
  36. m_DopeLines.headerHeight = 0;
  37. m_DopeLines.elementHeight = 20;
  38. m_DopeLines.draggable = false;
  39. }
  40. public bool selectable { get { return true; } }
  41. public object selectableObject
  42. {
  43. get { return this; }
  44. }
  45. public bool selected
  46. {
  47. get { return m_PartOfSelection; }
  48. set
  49. {
  50. m_PartOfSelection = value;
  51. if (!m_PartOfSelection)
  52. {
  53. m_DopeLines.index = -1;
  54. }
  55. }
  56. }
  57. public virtual void Delete(WindowState state)
  58. {
  59. // we dont support deleting the summary
  60. if (m_DopeLines.index < 1)
  61. return;
  62. if (m_CurveDataSource == null)
  63. return;
  64. var clip = m_CurveDataSource.animationClip;
  65. if (clip == null)
  66. return;
  67. int curveIndexToDelete = m_DopeLines.index - 1;
  68. var bindings = AnimationUtility.GetCurveBindings(clip);
  69. if (curveIndexToDelete >= bindings.Length)
  70. return;
  71. TimelineUndo.PushUndo(clip, "Delete Curve");
  72. AnimationUtility.SetEditorCurve(clip, bindings[m_DopeLines.index - 1], null);
  73. state.rebuildGraph = true;
  74. }
  75. public void OnGUI(Rect targetRect)
  76. {
  77. if (m_TreeView == null)
  78. return;
  79. m_TreeView.OnEvent();
  80. m_TreeView.OnGUI(targetRect, GUIUtility.GetControlID(FocusType.Passive));
  81. }
  82. public void InitIfNeeded(Rect rect, CurveDataSource dataSource, bool isNewSelection)
  83. {
  84. if (Event.current.type != EventType.Layout)
  85. return;
  86. m_CurveDataSource = dataSource;
  87. var clip = dataSource.animationClip;
  88. List<EditorCurveBinding> allBindings = new List<EditorCurveBinding>();
  89. allBindings.Add(new EditorCurveBinding { propertyName = "Summary" });
  90. if (clip != null)
  91. allBindings.AddRange(AnimationUtility.GetCurveBindings(clip));
  92. m_DopeLines.list = allBindings.ToArray();
  93. if (m_TreeViewState != null)
  94. {
  95. if (isNewSelection)
  96. RefreshAll();
  97. return;
  98. }
  99. m_TreeViewState = m_TrackGlobalTreeViewState != null ? m_TrackGlobalTreeViewState : new TreeViewState();
  100. m_TreeView = new TreeViewController(m_Window, m_TreeViewState)
  101. {
  102. useExpansionAnimation = false,
  103. deselectOnUnhandledMouseDown = true
  104. };
  105. m_TreeView.selectionChangedCallback += OnItemSelectionChanged;
  106. m_TreeViewDataSource = new BindingTreeViewDataSource(m_TreeView, clip, m_CurveDataSource);
  107. m_TreeView.Init(rect, m_TreeViewDataSource, new BindingTreeViewGUI(m_TreeView), null);
  108. m_TreeViewDataSource.UpdateData();
  109. RefreshSelection();
  110. }
  111. void OnItemSelectionChanged(int[] selection)
  112. {
  113. RefreshSelection(selection);
  114. }
  115. void RefreshAll()
  116. {
  117. RefreshTree();
  118. RefreshSelection();
  119. }
  120. void RefreshSelection()
  121. {
  122. RefreshSelection(m_TreeViewState.selectedIDs != null ? m_TreeViewState.selectedIDs.ToArray() : null);
  123. }
  124. void RefreshSelection(int[] selection)
  125. {
  126. if (selection == null || selection.Length == 0)
  127. {
  128. // select all.
  129. if (m_TreeViewDataSource.GetRows().Count > 0)
  130. {
  131. m_Selection = m_TreeViewDataSource.GetRows().Select(r => r.id).ToArray();
  132. }
  133. }
  134. else
  135. {
  136. m_Selection = selection;
  137. }
  138. RefreshCurves();
  139. }
  140. public void RefreshCurves()
  141. {
  142. if (m_CurveDataSource == null || m_Selection == null)
  143. return;
  144. var bindings = new List<EditorCurveBinding>();
  145. foreach (int s in m_Selection)
  146. {
  147. var item = (CurveTreeViewNode)m_TreeView.FindItem(s);
  148. if (item != null && item.bindings != null)
  149. bindings.AddRange(item.bindings);
  150. }
  151. var wrappers = m_CurveDataSource.GenerateWrappers(bindings);
  152. m_CurveEditor.animationCurves = wrappers.ToArray();
  153. }
  154. public void RefreshTree()
  155. {
  156. if (m_TreeViewDataSource == null)
  157. return;
  158. if (m_Selection == null)
  159. m_Selection = new int[0];
  160. // get the names of the previous items
  161. var selected = m_Selection.Select(x => m_TreeViewDataSource.FindItem(x)).Where(t => t != null).Select(c => c.displayName).ToArray();
  162. // update the source
  163. m_TreeViewDataSource.UpdateData();
  164. // find the same items
  165. var reselected = m_TreeViewDataSource.GetRows().Where(x => selected.Contains(x.displayName)).Select(x => x.id).ToArray();
  166. if (!reselected.Any())
  167. {
  168. if (m_TreeViewDataSource.GetRows().Count > 0)
  169. {
  170. reselected = new[] { m_TreeViewDataSource.GetItem(0).id };
  171. }
  172. }
  173. // update the selection
  174. OnItemSelectionChanged(reselected);
  175. }
  176. internal virtual bool IsRenamingNodeAllowed(TreeViewItem node)
  177. {
  178. return false;
  179. }
  180. }
  181. }