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