TimelineWindowTimeControl.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using System;
  2. using UnityEditorInternal;
  3. using UnityEngine;
  4. using UnityEngine.Timeline;
  5. using Object = UnityEngine.Object;
  6. namespace UnityEditor.Timeline
  7. {
  8. class TimelineWindowTimeControl : IAnimationWindowControl
  9. {
  10. [Serializable]
  11. public struct ClipData
  12. {
  13. public double start;
  14. public double duration;
  15. public TrackAsset track;
  16. }
  17. [SerializeField] ClipData m_ClipData;
  18. [SerializeField] TimelineClip m_Clip;
  19. [SerializeField] AnimationWindowState m_AnimWindowState;
  20. TrackAsset track
  21. {
  22. get
  23. {
  24. if (m_Clip != null)
  25. {
  26. return m_Clip.parentTrack;
  27. }
  28. return m_ClipData.track;
  29. }
  30. }
  31. static TimelineWindow window
  32. {
  33. get
  34. {
  35. return TimelineWindow.instance;
  36. }
  37. }
  38. static WindowState state
  39. {
  40. get
  41. {
  42. if (window != null)
  43. return window.state;
  44. return null;
  45. }
  46. }
  47. void OnStateChange()
  48. {
  49. if (state != null && state.dirtyStamp > 0 && m_AnimWindowState != null)
  50. m_AnimWindowState.Repaint();
  51. }
  52. public void Init(AnimationWindowState animState, TimelineClip clip)
  53. {
  54. m_Clip = clip;
  55. m_AnimWindowState = animState;
  56. }
  57. public void Init(AnimationWindowState animState, ClipData clip)
  58. {
  59. m_ClipData = clip;
  60. m_AnimWindowState = animState;
  61. }
  62. public override void OnEnable()
  63. {
  64. if (state != null)
  65. state.OnTimeChange += OnStateChange;
  66. base.OnEnable();
  67. }
  68. public void OnDisable()
  69. {
  70. if (state != null)
  71. state.OnTimeChange -= OnStateChange;
  72. }
  73. public override AnimationKeyTime time
  74. {
  75. get
  76. {
  77. if (state == null)
  78. return AnimationKeyTime.Time(0.0f, 0.0f);
  79. return AnimationKeyTime.Time(ToAnimationClipTime(state.editSequence.time), state.referenceSequence.frameRate);
  80. }
  81. }
  82. void ChangeTime(float newTime)
  83. {
  84. if (state != null && state.editSequence.director != null)
  85. {
  86. // avoid rounding errors
  87. var finalTime = ToGlobalTime(newTime);
  88. if (TimeUtility.OnFrameBoundary(finalTime, state.referenceSequence.frameRate, TimeUtility.kFrameRateEpsilon))
  89. finalTime = TimeUtility.RoundToFrame(finalTime, state.referenceSequence.frameRate);
  90. state.editSequence.time = finalTime;
  91. window.Repaint();
  92. }
  93. }
  94. static void ChangeFrame(int frame)
  95. {
  96. if (state != null)
  97. {
  98. state.editSequence.frame = frame;
  99. window.Repaint();
  100. }
  101. }
  102. public override void GoToTime(float newTime)
  103. {
  104. ChangeTime(newTime);
  105. }
  106. public override void GoToFrame(int frame)
  107. {
  108. ChangeFrame(frame);
  109. }
  110. public override void StartScrubTime() {}
  111. public override void EndScrubTime() {}
  112. public override void ScrubTime(float newTime)
  113. {
  114. ChangeTime(newTime);
  115. }
  116. public override void GoToPreviousFrame()
  117. {
  118. if (state != null)
  119. ChangeFrame(state.editSequence.frame - 1);
  120. }
  121. public override void GoToNextFrame()
  122. {
  123. if (state != null)
  124. ChangeFrame(state.editSequence.frame + 1);
  125. }
  126. AnimationWindowCurve[] GetCurves()
  127. {
  128. var curves =
  129. (m_AnimWindowState.showCurveEditor &&
  130. m_AnimWindowState.activeCurves.Count > 0) ? m_AnimWindowState.activeCurves : m_AnimWindowState.allCurves;
  131. return curves.ToArray();
  132. }
  133. public override void GoToPreviousKeyframe()
  134. {
  135. var newTime = AnimationWindowUtility.GetPreviousKeyframeTime(GetCurves(), time.time, m_AnimWindowState.clipFrameRate);
  136. GoToTime(m_AnimWindowState.SnapToFrame(newTime, AnimationWindowState.SnapMode.SnapToClipFrame));
  137. }
  138. public override void GoToNextKeyframe()
  139. {
  140. var newTime = AnimationWindowUtility.GetNextKeyframeTime(GetCurves(), time.time, m_AnimWindowState.clipFrameRate);
  141. GoToTime(m_AnimWindowState.SnapToFrame(newTime, AnimationWindowState.SnapMode.SnapToClipFrame));
  142. }
  143. public override void GoToFirstKeyframe()
  144. {
  145. GoToTime(0);
  146. }
  147. public override void GoToLastKeyframe()
  148. {
  149. double animClipTime = 0;
  150. if (m_Clip != null)
  151. {
  152. var curves = m_Clip.curves;
  153. var animAsset = m_Clip.asset as AnimationPlayableAsset;
  154. if (animAsset != null)
  155. {
  156. animClipTime = animAsset.clip != null ? animAsset.clip.length : 0;
  157. }
  158. else if (curves != null)
  159. {
  160. animClipTime = curves.length;
  161. }
  162. else
  163. {
  164. animClipTime = m_Clip.clipAssetDuration;
  165. }
  166. }
  167. else
  168. {
  169. animClipTime = m_ClipData.duration;
  170. }
  171. GoToTime((float)animClipTime);
  172. }
  173. public override bool canPlay
  174. {
  175. get
  176. {
  177. return state != null && state.previewMode;
  178. }
  179. }
  180. public override bool playing
  181. {
  182. get
  183. {
  184. return state != null && state.playing;
  185. }
  186. }
  187. static void SetPlaybackState(bool playbackState)
  188. {
  189. if (state == null || playbackState == state.playing)
  190. return;
  191. state.SetPlaying(playbackState);
  192. }
  193. public override bool StartPlayback()
  194. {
  195. SetPlaybackState(true);
  196. return state != null && state.playing;
  197. }
  198. public override void StopPlayback()
  199. {
  200. SetPlaybackState(false);
  201. }
  202. public override bool PlaybackUpdate() { return state != null && state.playing; }
  203. public override bool canRecord
  204. {
  205. get { return state != null && state.canRecord; }
  206. }
  207. public override bool recording
  208. {
  209. get { return state != null && state.recording; }
  210. }
  211. public override bool canPreview
  212. {
  213. get { return false; }
  214. }
  215. public override bool previewing
  216. {
  217. get { return false; }
  218. }
  219. public override bool StartRecording(Object targetObject)
  220. {
  221. if (!canRecord)
  222. return false;
  223. if (track != null && state != null && !state.ignorePreview)
  224. {
  225. state.ArmForRecord(track);
  226. return state.recording;
  227. }
  228. return false;
  229. }
  230. public override void StopRecording()
  231. {
  232. if (track != null && state != null && !state.ignorePreview)
  233. state.UnarmForRecord(track);
  234. }
  235. public override void OnSelectionChanged() {}
  236. public override void ResampleAnimation() {}
  237. public override bool StartPreview()
  238. {
  239. if (state != null)
  240. state.previewMode = true;
  241. return state != null && state.previewMode;
  242. }
  243. public override void StopPreview()
  244. {
  245. if (state != null)
  246. state.previewMode = false;
  247. }
  248. public override void ProcessCandidates() {}
  249. public override void ClearCandidates() {}
  250. double ToGlobalTime(float localTime)
  251. {
  252. if (m_Clip != null)
  253. return Math.Max(0, m_Clip.FromLocalTimeUnbound(localTime));
  254. return Math.Max(0, m_ClipData.start + localTime);
  255. }
  256. float ToAnimationClipTime(double globalTime)
  257. {
  258. if (m_Clip != null)
  259. return (float)m_Clip.ToLocalTimeUnbound(globalTime);
  260. return (float)(globalTime - m_ClipData.start);
  261. }
  262. }
  263. }