No Description
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.

BindingTreeViewDataSource.cs 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. #if !UNITY_2020_2_OR_NEWER
  9. using L10n = UnityEditor.Timeline.L10n;
  10. #endif
  11. namespace UnityEditor.Timeline
  12. {
  13. class BindingTreeViewDataSource : TreeViewDataSource
  14. {
  15. struct BindingGroup : IEquatable<BindingGroup>, IComparable<BindingGroup>
  16. {
  17. public readonly string GroupName;
  18. public readonly string Path;
  19. public readonly Type Type;
  20. public BindingGroup(string path, string groupName, Type type)
  21. {
  22. Path = path;
  23. GroupName = groupName;
  24. Type = type;
  25. }
  26. public string groupDisplayName => string.IsNullOrEmpty(Path) ? GroupName : string.Format($"{Path} : {GroupName}");
  27. public bool Equals(BindingGroup other) => GroupName == other.GroupName && Type == other.Type && Path == other.Path;
  28. public int CompareTo(BindingGroup other) => GetHashCode() - other.GetHashCode();
  29. public override bool Equals(object obj) => obj is BindingGroup other && Equals(other);
  30. public override int GetHashCode()
  31. {
  32. return HashUtility.CombineHash(GroupName != null ? GroupName.GetHashCode() : 0, Type != null ? Type.GetHashCode() : 0, Path != null ? Path.GetHashCode() : 0);
  33. }
  34. }
  35. static readonly string s_DefaultValue = L10n.Tr("{0} (Default Value)");
  36. public const int RootID = int.MinValue;
  37. public const int GroupID = -1;
  38. private readonly AnimationClip m_Clip;
  39. private readonly CurveDataSource m_CurveDataSource;
  40. public BindingTreeViewDataSource(
  41. TreeViewController treeView, AnimationClip clip, CurveDataSource curveDataSource)
  42. : base(treeView)
  43. {
  44. m_Clip = clip;
  45. showRootItem = false;
  46. m_CurveDataSource = curveDataSource;
  47. }
  48. void SetupRootNodeSettings()
  49. {
  50. showRootItem = false;
  51. SetExpanded(RootID, true);
  52. SetExpanded(GroupID, true);
  53. }
  54. public override void FetchData()
  55. {
  56. if (m_Clip == null)
  57. return;
  58. var bindings = AnimationUtility.GetCurveBindings(m_Clip)
  59. .Union(AnimationUtility.GetObjectReferenceCurveBindings(m_Clip))
  60. .ToArray();
  61. // a sorted linear list of nodes
  62. var results = bindings.GroupBy(GetBindingGroup, p => p, CreateTuple)
  63. .OrderBy(t => t.Item1.Path)
  64. .ThenBy(NamePrioritySort)
  65. // this makes component ordering match the animation window
  66. .ThenBy(t => t.Item1.Type.ToString())
  67. .ThenBy(t => t.Item1.GroupName).ToArray();
  68. m_RootItem = new CurveTreeViewNode(RootID, null, "root", null)
  69. {
  70. children = new List<TreeViewItem>(1)
  71. };
  72. if (results.Any())
  73. {
  74. var groupingNode = new CurveTreeViewNode(GroupID, m_RootItem, m_CurveDataSource.groupingName, bindings)
  75. {
  76. children = new List<TreeViewItem>()
  77. };
  78. m_RootItem.children.Add(groupingNode);
  79. foreach (var r in results)
  80. {
  81. var key = r.Item1;
  82. var nodeBindings = r.Item2;
  83. FillMissingTransformCurves(nodeBindings);
  84. if (nodeBindings.Count == 1)
  85. groupingNode.children.Add(CreateLeafNode(nodeBindings[0], groupingNode, PropertyName(nodeBindings[0], true)));
  86. else if (nodeBindings.Count > 1)
  87. {
  88. var childBindings = nodeBindings.OrderBy(BindingSort).ToArray();
  89. var parent = new CurveTreeViewNode(key.GetHashCode(), groupingNode, key.groupDisplayName, childBindings) { children = new List<TreeViewItem>() };
  90. groupingNode.children.Add(parent);
  91. foreach (var b in childBindings)
  92. parent.children.Add(CreateLeafNode(b, parent, PropertyName(b, false)));
  93. }
  94. }
  95. SetupRootNodeSettings();
  96. }
  97. m_NeedRefreshRows = true;
  98. }
  99. public void UpdateData()
  100. {
  101. m_TreeView.ReloadData();
  102. }
  103. string GroupName(EditorCurveBinding binding)
  104. {
  105. var propertyName = m_CurveDataSource.ModifyPropertyDisplayName(binding.path, binding.propertyName);
  106. return CleanUpArrayBinding(AnimationWindowUtility.NicifyPropertyGroupName(binding.type, propertyName), true);
  107. }
  108. static string CleanUpArrayBinding(string propertyName, bool isGroup)
  109. {
  110. const string arrayIndicator = ".Array.data[";
  111. const string arrayDisplay = ".data[";
  112. var arrayIndex = propertyName.LastIndexOf(arrayIndicator, StringComparison.Ordinal);
  113. if (arrayIndex == -1)
  114. return propertyName;
  115. if (isGroup)
  116. propertyName = propertyName.Substring(0, arrayIndex);
  117. return propertyName.Replace(arrayIndicator, arrayDisplay);
  118. }
  119. string PropertyName(EditorCurveBinding binding, bool prependPathName)
  120. {
  121. var propertyName = m_CurveDataSource.ModifyPropertyDisplayName(binding.path, binding.propertyName);
  122. propertyName = CleanUpArrayBinding(AnimationWindowUtility.GetPropertyDisplayName(propertyName), false);
  123. if (binding.isPhantom)
  124. propertyName = string.Format(s_DefaultValue, propertyName);
  125. if (prependPathName && !string.IsNullOrEmpty(binding.path))
  126. propertyName = $"{binding.path} : {propertyName}";
  127. return propertyName;
  128. }
  129. BindingGroup GetBindingGroup(EditorCurveBinding binding)
  130. {
  131. return new BindingGroup(binding.path ?? string.Empty, GroupName(binding), binding.type);
  132. }
  133. static CurveTreeViewNode CreateLeafNode(EditorCurveBinding binding, TreeViewItem parent, string displayName)
  134. {
  135. return new CurveTreeViewNode(binding.GetHashCode(), parent, displayName, new[] { binding }, AnimationWindowUtility.ForceGrouping(binding));
  136. }
  137. static void FillMissingTransformCurves(List<EditorCurveBinding> bindings)
  138. {
  139. if (!AnimationWindowUtility.IsActualTransformCurve(bindings[0]) || bindings.Count >= 3)
  140. return;
  141. var binding = bindings[0];
  142. var prefixPropertyName = binding.propertyName.Split('.').First();
  143. binding.isPhantom = true;
  144. if (!bindings.Any(p => p.propertyName.EndsWith(".x")))
  145. {
  146. binding.propertyName = prefixPropertyName + ".x";
  147. bindings.Insert(0, binding);
  148. }
  149. if (!bindings.Any(p => p.propertyName.EndsWith(".y")))
  150. {
  151. binding.propertyName = prefixPropertyName + ".y";
  152. bindings.Insert(1, binding);
  153. }
  154. if (!bindings.Any(p => p.propertyName.EndsWith(".z")))
  155. {
  156. binding.propertyName = prefixPropertyName + ".z";
  157. bindings.Insert(2, binding);
  158. }
  159. }
  160. // make sure vectors and colors are sorted correctly in their subgroups
  161. static int BindingSort(EditorCurveBinding b)
  162. {
  163. return AnimationWindowUtility.GetComponentIndex(b.propertyName);
  164. }
  165. static int NamePrioritySort(ValueTuple<BindingGroup, List<EditorCurveBinding>> group)
  166. {
  167. if (group.Item1.Type != typeof(Transform))
  168. return 0;
  169. switch (group.Item1.GroupName)
  170. {
  171. case "Position": return Int32.MinValue;
  172. case "Rotation": return Int32.MinValue + 1;
  173. case "Scale": return Int32.MinValue + 2;
  174. default: return 0;
  175. }
  176. }
  177. static ValueTuple<BindingGroup, List<EditorCurveBinding>> CreateTuple(BindingGroup key, IEnumerable<EditorCurveBinding> items)
  178. {
  179. return new ValueTuple<BindingGroup, List<EditorCurveBinding>>()
  180. {
  181. Item1 = key,
  182. Item2 = items.ToList()
  183. };
  184. }
  185. }
  186. }