Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BindingSelector.cs 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. private BindingTreeViewGUI m_TreeViewGUI;
  14. public TreeViewController treeViewController
  15. {
  16. get { return m_TreeView; }
  17. }
  18. TreeViewState m_TrackGlobalTreeViewState;
  19. TreeViewState m_TreeViewState;
  20. BindingTreeViewDataSource m_TreeViewDataSource;
  21. CurveDataSource m_CurveDataSource;
  22. TimelineWindow m_Window;
  23. CurveEditor m_CurveEditor;
  24. ReorderableList m_DopeLines;
  25. string[] m_StringList = { };
  26. int[] m_Selection;
  27. bool m_PartOfSelection;
  28. public BindingSelector(EditorWindow window, CurveEditor curveEditor, TreeViewState trackGlobalTreeViewState)
  29. {
  30. m_Window = window as TimelineWindow;
  31. m_CurveEditor = curveEditor;
  32. m_TrackGlobalTreeViewState = trackGlobalTreeViewState;
  33. m_DopeLines = new ReorderableList(m_StringList, typeof(string), false, false, false, false);
  34. m_DopeLines.drawElementBackgroundCallback = null;
  35. m_DopeLines.showDefaultBackground = false;
  36. m_DopeLines.index = 0;
  37. m_DopeLines.headerHeight = 0;
  38. m_DopeLines.elementHeight = 20;
  39. m_DopeLines.draggable = false;
  40. }
  41. public void OnGUI(Rect targetRect)
  42. {
  43. if (m_TreeView == null)
  44. return;
  45. m_TreeView.OnEvent();
  46. m_TreeViewGUI.parentWidth = targetRect.width;
  47. m_TreeView.OnGUI(targetRect, GUIUtility.GetControlID(FocusType.Passive));
  48. }
  49. public void InitIfNeeded(Rect rect, CurveDataSource dataSource, bool isNewSelection)
  50. {
  51. if (Event.current.type != EventType.Layout)
  52. return;
  53. m_CurveDataSource = dataSource;
  54. var clip = dataSource.animationClip;
  55. List<EditorCurveBinding> allBindings = new List<EditorCurveBinding>();
  56. allBindings.Add(new EditorCurveBinding { propertyName = "Summary" });
  57. if (clip != null)
  58. allBindings.AddRange(AnimationUtility.GetCurveBindings(clip));
  59. m_DopeLines.list = allBindings.ToArray();
  60. if (m_TreeViewState != null)
  61. {
  62. if (isNewSelection)
  63. RefreshAll();
  64. return;
  65. }
  66. m_TreeViewState = m_TrackGlobalTreeViewState != null ? m_TrackGlobalTreeViewState : new TreeViewState();
  67. m_TreeView = new TreeViewController(m_Window, m_TreeViewState)
  68. {
  69. useExpansionAnimation = false,
  70. deselectOnUnhandledMouseDown = true
  71. };
  72. m_TreeView.selectionChangedCallback += OnItemSelectionChanged;
  73. m_TreeViewDataSource = new BindingTreeViewDataSource(m_TreeView, clip, m_CurveDataSource);
  74. m_TreeViewGUI = new BindingTreeViewGUI(m_TreeView);
  75. m_TreeView.Init(rect, m_TreeViewDataSource, m_TreeViewGUI, null);
  76. m_TreeViewDataSource.UpdateData();
  77. RefreshSelection();
  78. }
  79. void OnItemSelectionChanged(int[] selection)
  80. {
  81. RefreshSelection(selection);
  82. }
  83. void RefreshAll()
  84. {
  85. RefreshTree();
  86. RefreshSelection();
  87. }
  88. void RefreshSelection()
  89. {
  90. RefreshSelection(m_TreeViewState.selectedIDs != null ? m_TreeViewState.selectedIDs.ToArray() : null);
  91. }
  92. void RefreshSelection(int[] selection)
  93. {
  94. if (selection == null || selection.Length == 0)
  95. {
  96. // select all.
  97. if (m_TreeViewDataSource.GetRows().Count > 0)
  98. {
  99. m_Selection = m_TreeViewDataSource.GetRows().Select(r => r.id).ToArray();
  100. }
  101. }
  102. else
  103. {
  104. m_Selection = selection;
  105. }
  106. RefreshCurves();
  107. }
  108. public void RefreshCurves()
  109. {
  110. if (m_CurveDataSource == null || m_Selection == null)
  111. return;
  112. var bindings = new HashSet<EditorCurveBinding>(AnimationPreviewUtilities.EditorCurveBindingComparer.Instance);
  113. foreach (int s in m_Selection)
  114. {
  115. var item = (CurveTreeViewNode)m_TreeView.FindItem(s);
  116. if (item != null && item.bindings != null)
  117. bindings.UnionWith(item.bindings);
  118. }
  119. var wrappers = m_CurveDataSource.GenerateWrappers(bindings);
  120. m_CurveEditor.animationCurves = wrappers.ToArray();
  121. }
  122. public void RefreshTree()
  123. {
  124. if (m_TreeViewDataSource == null)
  125. return;
  126. if (m_Selection == null)
  127. m_Selection = new int[0];
  128. // get the names of the previous items
  129. var selected = m_Selection.Select(x => m_TreeViewDataSource.FindItem(x)).Where(t => t != null).Select(c => c.displayName).ToArray();
  130. // update the source
  131. m_TreeViewDataSource.UpdateData();
  132. // find the same items
  133. var reselected = m_TreeViewDataSource.GetRows().Where(x => selected.Contains(x.displayName)).Select(x => x.id).ToArray();
  134. if (!reselected.Any())
  135. {
  136. if (m_TreeViewDataSource.GetRows().Count > 0)
  137. {
  138. reselected = new[] { m_TreeViewDataSource.GetItem(0).id };
  139. }
  140. }
  141. // update the selection
  142. OnItemSelectionChanged(reselected);
  143. }
  144. internal virtual bool IsRenamingNodeAllowed(TreeViewItem node)
  145. {
  146. return false;
  147. }
  148. }
  149. }