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