CurveDataSource.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.Timeline;
  4. namespace UnityEditor.Timeline
  5. {
  6. abstract class CurveDataSource
  7. {
  8. public static CurveDataSource Create(IRowGUI trackGUI)
  9. {
  10. if (trackGUI.asset is AnimationTrack)
  11. return new InfiniteClipCurveDataSource(trackGUI);
  12. return new TrackParametersCurveDataSource(trackGUI);
  13. }
  14. public static CurveDataSource Create(TimelineClipGUI clipGUI)
  15. {
  16. if (clipGUI.clip.animationClip != null)
  17. return new ClipAnimationCurveDataSource(clipGUI);
  18. return new ClipParametersCurveDataSource(clipGUI);
  19. }
  20. int? m_ID = null;
  21. public int id
  22. {
  23. get
  24. {
  25. if (!m_ID.HasValue)
  26. m_ID = CreateHashCode();
  27. return m_ID.Value;
  28. }
  29. }
  30. readonly IRowGUI m_TrackGUI;
  31. protected IRowGUI trackGUI { get { return m_TrackGUI; } }
  32. protected CurveDataSource(IRowGUI trackGUI)
  33. {
  34. m_TrackGUI = trackGUI;
  35. }
  36. public abstract AnimationClip animationClip { get; }
  37. public abstract float start { get; }
  38. public abstract float timeScale { get; }
  39. public abstract string groupingName { get; }
  40. public virtual void UpdateCurves(List<CurveWrapper> updatedCurves) {}
  41. public virtual void RebuildCurves() {} // Only necessary when using proxies
  42. public Rect GetBackgroundRect(WindowState state)
  43. {
  44. var trackRect = m_TrackGUI.boundingRect;
  45. return new Rect(
  46. state.timeAreaTranslation.x + trackRect.xMin,
  47. trackRect.y,
  48. (float)state.editSequence.asset.duration * state.timeAreaScale.x,
  49. trackRect.height
  50. );
  51. }
  52. public List<CurveWrapper> GenerateWrappers(List<EditorCurveBinding> bindings)
  53. {
  54. var wrappers = new List<CurveWrapper>(bindings.Count);
  55. int curveWrapperId = 0;
  56. foreach (EditorCurveBinding b in bindings)
  57. {
  58. // General configuration
  59. var wrapper = new CurveWrapper
  60. {
  61. id = curveWrapperId++,
  62. binding = b,
  63. groupId = -1,
  64. hidden = false,
  65. readOnly = false,
  66. getAxisUiScalarsCallback = () => new Vector2(1, 1)
  67. };
  68. // Specific configuration
  69. ConfigureCurveWrapper(wrapper);
  70. wrappers.Add(wrapper);
  71. }
  72. return wrappers;
  73. }
  74. protected virtual void ConfigureCurveWrapper(CurveWrapper wrapper)
  75. {
  76. wrapper.color = CurveUtility.GetPropertyColor(wrapper.binding.propertyName);
  77. wrapper.renderer = new NormalCurveRenderer(AnimationUtility.GetEditorCurve(animationClip, wrapper.binding));
  78. wrapper.renderer.SetCustomRange(0.0f, animationClip.length);
  79. }
  80. protected virtual int CreateHashCode()
  81. {
  82. return m_TrackGUI.asset.GetHashCode();
  83. }
  84. }
  85. class ClipAnimationCurveDataSource : CurveDataSource
  86. {
  87. static readonly string k_GroupingName = L10n.Tr("Animated Values");
  88. readonly TimelineClipGUI m_ClipGUI;
  89. public ClipAnimationCurveDataSource(TimelineClipGUI clipGUI) : base(clipGUI.parent)
  90. {
  91. m_ClipGUI = clipGUI;
  92. }
  93. public override AnimationClip animationClip
  94. {
  95. get { return m_ClipGUI.clip.animationClip; }
  96. }
  97. public override float start
  98. {
  99. get { return (float)m_ClipGUI.clip.FromLocalTimeUnbound(0.0); }
  100. }
  101. public override float timeScale
  102. {
  103. get { return (float)m_ClipGUI.clip.timeScale; }
  104. }
  105. public override string groupingName
  106. {
  107. get { return k_GroupingName; }
  108. }
  109. protected override int CreateHashCode()
  110. {
  111. return base.CreateHashCode().CombineHash(m_ClipGUI.clip.GetHashCode());
  112. }
  113. }
  114. class ClipParametersCurveDataSource : CurveDataSource
  115. {
  116. static readonly string k_GroupingName = L10n.Tr("Clip Properties");
  117. readonly TimelineClipGUI m_ClipGUI;
  118. readonly CurvesProxy m_CurvesProxy;
  119. public ClipParametersCurveDataSource(TimelineClipGUI clipGUI) : base(clipGUI.parent)
  120. {
  121. m_ClipGUI = clipGUI;
  122. m_CurvesProxy = new CurvesProxy(clipGUI.clip);
  123. }
  124. public override AnimationClip animationClip
  125. {
  126. get { return m_CurvesProxy.curves; }
  127. }
  128. public override float start
  129. {
  130. get { return (float)m_ClipGUI.clip.FromLocalTimeUnbound(0.0); }
  131. }
  132. public override float timeScale
  133. {
  134. get { return (float)m_ClipGUI.clip.timeScale; }
  135. }
  136. public override string groupingName
  137. {
  138. get { return k_GroupingName; }
  139. }
  140. public override void UpdateCurves(List<CurveWrapper> updatedCurves)
  141. {
  142. m_CurvesProxy.UpdateCurves(updatedCurves);
  143. }
  144. public override void RebuildCurves()
  145. {
  146. m_CurvesProxy.RebuildCurves();
  147. }
  148. protected override void ConfigureCurveWrapper(CurveWrapper wrapper)
  149. {
  150. m_CurvesProxy.ConfigureCurveWrapper(wrapper);
  151. }
  152. protected override int CreateHashCode()
  153. {
  154. return base.CreateHashCode().CombineHash(m_ClipGUI.clip.GetHashCode());
  155. }
  156. }
  157. class InfiniteClipCurveDataSource : CurveDataSource
  158. {
  159. static readonly string k_GroupingName = L10n.Tr("Animated Values");
  160. readonly AnimationTrack m_AnimationTrack;
  161. public InfiniteClipCurveDataSource(IRowGUI trackGui) : base(trackGui)
  162. {
  163. m_AnimationTrack = trackGui.asset as AnimationTrack;
  164. }
  165. public override AnimationClip animationClip
  166. {
  167. get { return m_AnimationTrack.infiniteClip; }
  168. }
  169. public override float start
  170. {
  171. get { return 0.0f; }
  172. }
  173. public override float timeScale
  174. {
  175. get { return 1.0f; }
  176. }
  177. public override string groupingName
  178. {
  179. get { return k_GroupingName; }
  180. }
  181. }
  182. class TrackParametersCurveDataSource : CurveDataSource
  183. {
  184. static readonly string k_GroupingName = L10n.Tr("Track Properties");
  185. readonly CurvesProxy m_CurvesProxy;
  186. public TrackParametersCurveDataSource(IRowGUI trackGui) : base(trackGui)
  187. {
  188. m_CurvesProxy = new CurvesProxy(trackGui.asset);
  189. }
  190. public override AnimationClip animationClip
  191. {
  192. get { return m_CurvesProxy.curves; }
  193. }
  194. public override float start
  195. {
  196. get { return 0.0f; }
  197. }
  198. public override float timeScale
  199. {
  200. get { return 1.0f; }
  201. }
  202. public override string groupingName
  203. {
  204. get { return k_GroupingName; }
  205. }
  206. public override void UpdateCurves(List<CurveWrapper> updatedCurves)
  207. {
  208. m_CurvesProxy.UpdateCurves(updatedCurves);
  209. }
  210. public override void RebuildCurves()
  211. {
  212. m_CurvesProxy.RebuildCurves();
  213. }
  214. protected override void ConfigureCurveWrapper(CurveWrapper wrapper)
  215. {
  216. m_CurvesProxy.ConfigureCurveWrapper(wrapper);
  217. }
  218. }
  219. }