BindingSelector.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor.IMGUI.Controls;
  5. using UnityEditorInternal;
  6. using UnityEngine;
  7. using UnityEngine.Timeline;
  8. namespace UnityEditor.Timeline
  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 void OnGUI(Rect targetRect)
  41. {
  42. if (m_TreeView == null)
  43. return;
  44. m_TreeView.OnEvent();
  45. m_TreeView.OnGUI(targetRect, GUIUtility.GetControlID(FocusType.Passive));
  46. }
  47. public void InitIfNeeded(Rect rect, CurveDataSource dataSource, bool isNewSelection)
  48. {
  49. if (Event.current.type != EventType.Layout)
  50. return;
  51. m_CurveDataSource = dataSource;
  52. var clip = dataSource.animationClip;
  53. List<EditorCurveBinding> allBindings = new List<EditorCurveBinding>();
  54. allBindings.Add(new EditorCurveBinding { propertyName = "Summary" });
  55. if (clip != null)
  56. allBindings.AddRange(AnimationUtility.GetCurveBindings(clip));
  57. m_DopeLines.list = allBindings.ToArray();
  58. if (m_TreeViewState != null)
  59. {
  60. if (isNewSelection)
  61. RefreshAll();
  62. return;
  63. }
  64. m_TreeViewState = m_TrackGlobalTreeViewState != null ? m_TrackGlobalTreeViewState : new TreeViewState();
  65. m_TreeView = new TreeViewController(m_Window, m_TreeViewState)
  66. {
  67. useExpansionAnimation = false,
  68. deselectOnUnhandledMouseDown = true
  69. };
  70. m_TreeView.selectionChangedCallback += OnItemSelectionChanged;
  71. m_TreeViewDataSource = new BindingTreeViewDataSource(m_TreeView, clip, m_CurveDataSource);
  72. m_TreeView.Init(rect, m_TreeViewDataSource, new BindingTreeViewGUI(m_TreeView), null);
  73. m_TreeViewDataSource.UpdateData();
  74. RefreshSelection();
  75. }
  76. void OnItemSelectionChanged(int[] selection)
  77. {
  78. RefreshSelection(selection);
  79. }
  80. void RefreshAll()
  81. {
  82. RefreshTree();
  83. RefreshSelection();
  84. }
  85. void RefreshSelection()
  86. {
  87. RefreshSelection(m_TreeViewState.selectedIDs != null ? m_TreeViewState.selectedIDs.ToArray() : null);
  88. }
  89. void RefreshSelection(int[] selection)
  90. {
  91. if (selection == null || selection.Length == 0)
  92. {
  93. // select all.
  94. if (m_TreeViewDataSource.GetRows().Count > 0)
  95. {
  96. m_Selection = m_TreeViewDataSource.GetRows().Select(r => r.id).ToArray();
  97. }
  98. }
  99. else
  100. {
  101. m_Selection = selection;
  102. }
  103. RefreshCurves();
  104. }
  105. public void RefreshCurves()
  106. {
  107. if (m_CurveDataSource == null || m_Selection == null)
  108. return;
  109. var bindings = new HashSet<EditorCurveBinding>(AnimationPreviewUtilities.EditorCurveBindingComparer.Instance);
  110. foreach (int s in m_Selection)
  111. {
  112. var item = (CurveTreeViewNode)m_TreeView.FindItem(s);
  113. if (item != null && item.bindings != null)
  114. bindings.UnionWith(item.bindings);
  115. }
  116. var wrappers = m_CurveDataSource.GenerateWrappers(bindings);
  117. m_CurveEditor.animationCurves = wrappers.ToArray();
  118. }
  119. public void RefreshTree()
  120. {
  121. if (m_TreeViewDataSource == null)
  122. return;
  123. if (m_Selection == null)
  124. m_Selection = new int[0];
  125. // get the names of the previous items
  126. var selected = m_Selection.Select(x => m_TreeViewDataSource.FindItem(x)).Where(t => t != null).Select(c => c.displayName).ToArray();
  127. // update the source
  128. m_TreeViewDataSource.UpdateData();
  129. // find the same items
  130. var reselected = m_TreeViewDataSource.GetRows().Where(x => selected.Contains(x.displayName)).Select(x => x.id).ToArray();
  131. if (!reselected.Any())
  132. {
  133. if (m_TreeViewDataSource.GetRows().Count > 0)
  134. {
  135. reselected = new[] { m_TreeViewDataSource.GetItem(0).id };
  136. }
  137. }
  138. // update the selection
  139. OnItemSelectionChanged(reselected);
  140. }
  141. internal virtual bool IsRenamingNodeAllowed(TreeViewItem node)
  142. {
  143. return false;
  144. }
  145. }
  146. }