Açıklama Yok
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.

TimelineWindowTimeControl.cs 8.6KB

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