-
Notifications
You must be signed in to change notification settings - Fork 340
Expand file tree
/
Copy pathTimeControl.cs
More file actions
317 lines (282 loc) · 11.5 KB
/
TimeControl.cs
File metadata and controls
317 lines (282 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
using System;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
namespace ToolKits
{
public class TimeControl
{
// currentTime will be clamped to preview range.
// Make sure it's initially at the beginning, even if the clip start is negative.
public float currentTime = Mathf.NegativeInfinity;
public float nextCurrentTime
{
set { deltaTime = value - currentTime; m_NextCurrentTimeSet = true; }
}
private bool m_NextCurrentTimeSet = false;
public float startTime = 0.0f;
public float stopTime = 1.0f;
public bool playSelection = false;
public bool loop = true;
public float playbackSpeed = 1.0f;
private float m_DeltaTime = 0.0f;
private bool m_DeltaTimeSet = false;
class Styles2
{
public GUIStyle timelineTick = "AnimationTimelineTick";
public GUIStyle labelTickMarks = "CurveEditorLabelTickMarks";
public GUIStyle playhead = "AnimationPlayHead";
}
static Styles2 timeAreaStyles;
public Action<bool> PlayStatusChanged
{
get;
set;
}
public Action OnPlayEnd
{
get;
set;
}
public float deltaTime
{
get { return m_DeltaTime; }
set { m_DeltaTime = value; m_DeltaTimeSet = true; }
}
public float normalizedTime
{
// Don't use InverseLerp and Lerp since they clamp between 0 and 1
get { return (stopTime == startTime) ? 0 : ((currentTime - startTime) / (stopTime - startTime)); }
set { currentTime = startTime * (1 - value) + stopTime * value; }
}
public bool playing
{
get { return m_Playing; }
set
{
if (m_Playing != value)
{
// Start Playing
if (value)
{
m_LastFrameEditorTime = EditorApplication.timeSinceStartup;
if (m_ResetOnPlay)
{
nextCurrentTime = startTime;
m_ResetOnPlay = false;
}
}
// Stop Playing
else
{
}
PlayStatusChanged?.Invoke(value);
}
m_Playing = value;
}
}
private double m_LastFrameEditorTime = 0.0f;
private bool m_Playing = false;
private bool m_ResetOnPlay = false;
private float m_MouseDrag = 0.0f;
private bool m_WrapForwardDrag = false;
private const float kStepTime = 0.01f;
private const float kScrubberHeight = 21;
private const float kPlayButtonWidth = 33;
private class Styles
{
public GUIContent playIcon = EditorGUIUtility.IconContent("PlayButton");
public GUIContent pauseIcon = EditorGUIUtility.IconContent("PauseButton");
public GUIStyle playButton = "TimeScrubberButton";
public GUIStyle timeScrubber = "TimeScrubber";
}
private static Styles s_Styles;
private static readonly int kScrubberIDHash = "ScrubberIDHash".GetHashCode();
public void DoTimeControl(Rect rect)
{
if (s_Styles == null)
s_Styles = new Styles();
var evt = Event.current;
int id = EditorGUIUtility.GetControlID(kScrubberIDHash, FocusType.Keyboard);
// Play/Pause Button + Scrubber
Rect timelineRect = rect;
timelineRect.height = kScrubberHeight;
// Only Scrubber
Rect scrubberRect = timelineRect;
scrubberRect.xMin += kPlayButtonWidth;
// Handle Input
switch (evt.GetTypeForControl(id))
{
case EventType.MouseDown:
if (rect.Contains(evt.mousePosition))
{
EditorGUIUtility.keyboardControl = id;
}
if (scrubberRect.Contains(evt.mousePosition))
{
EditorGUIUtility.SetWantsMouseJumping(1);
EditorGUIUtility.hotControl = id;
m_MouseDrag = evt.mousePosition.x - scrubberRect.xMin;
nextCurrentTime = (m_MouseDrag * (stopTime - startTime) / scrubberRect.width + startTime);
m_WrapForwardDrag = false;
evt.Use();
}
break;
case EventType.MouseDrag:
if (EditorGUIUtility.hotControl == id)
{
m_MouseDrag += evt.delta.x * playbackSpeed;
// We want to not wrap if we immediately drag to the beginning, but we do want to wrap if we drag past the end.
if (loop && ((m_MouseDrag < 0.0f && m_WrapForwardDrag) || (m_MouseDrag > scrubberRect.width)))
{
// scrubing out of range was generating a big deltaTime in wrong time direction
// this new code prevent this and it is compliant with new and more robust v5.0 root motion looping of animation clip
if (m_MouseDrag > scrubberRect.width)
{
currentTime -= (stopTime - startTime);
OnPlayEnd?.Invoke();
}
else if (m_MouseDrag < 0)
{
currentTime += (stopTime - startTime);
}
m_WrapForwardDrag = true;
m_MouseDrag = Mathf.Repeat(m_MouseDrag, scrubberRect.width);
}
nextCurrentTime = (Mathf.Clamp(m_MouseDrag, 0.0f, scrubberRect.width) * (stopTime - startTime) / scrubberRect.width + startTime);
evt.Use();
}
break;
case EventType.MouseUp:
if (EditorGUIUtility.hotControl == id)
{
EditorGUIUtility.SetWantsMouseJumping(0);
EditorGUIUtility.hotControl = 0;
evt.Use();
}
break;
case EventType.KeyDown:
if (EditorGUIUtility.keyboardControl == id)
{
// TODO: loop?
if (evt.keyCode == KeyCode.LeftArrow)
{
if (currentTime - startTime > kStepTime)
deltaTime = -kStepTime;
evt.Use();
}
if (evt.keyCode == KeyCode.RightArrow)
{
if (stopTime - currentTime > kStepTime)
deltaTime = kStepTime;
evt.Use();
}
}
break;
}
// background
GUI.Box(timelineRect, GUIContent.none, s_Styles.timeScrubber);
// Play/Pause Button
playing = GUI.Toggle(timelineRect, playing, playing ? s_Styles.pauseIcon : s_Styles.playIcon, s_Styles.playButton);
// Current time indicator
float normalizedPosition = Mathf.Lerp(scrubberRect.x, scrubberRect.xMax, normalizedTime);
DrawPlayhead(normalizedPosition, scrubberRect.yMin, scrubberRect.yMax, 2f, (EditorGUIUtility.keyboardControl == id) ? 1f : 0.5f);
}
public void OnDisable()
{
playing = false;
}
public void Update()
{
// If the deltaTime was not set, update it when playing
if (!m_DeltaTimeSet)
{
if (playing)
{
double timeSinceStartup = EditorApplication.timeSinceStartup;
deltaTime = (float)(timeSinceStartup - m_LastFrameEditorTime) * playbackSpeed;
m_LastFrameEditorTime = timeSinceStartup;
}
else
deltaTime = 0;
}
currentTime += deltaTime;
// If the nextCurrentTime was set explicitly, we don't want to loop
bool wrap = loop && playing && !m_NextCurrentTimeSet;
if (wrap)
{
if (normalizedTime >= 1f)
{
OnPlayEnd?.Invoke();
}
normalizedTime = Mathf.Repeat(normalizedTime, 1.0f);
}
else
{
if (normalizedTime > 1)
{
OnPlayEnd?.Invoke();
playing = false;
m_ResetOnPlay = true;
}
normalizedTime = Mathf.Clamp01(normalizedTime);
}
m_DeltaTimeSet = false;
m_NextCurrentTimeSet = false;
}
public static void DrawPlayhead(float x, float yMin, float yMax, float thickness, float alpha)
{
if (Event.current.type != EventType.Repaint)
return;
InitStyles();
float halfThickness = thickness * 0.5f;
Color lineColor = AlphaMultiplied(timeAreaStyles.playhead.normal.textColor,alpha);
if (thickness > 1f)
{
Rect labelRect = Rect.MinMaxRect(x - halfThickness, yMin, x + halfThickness, yMax);
EditorGUI.DrawRect(labelRect, lineColor);
}
else
{
DrawVerticalLine(x, yMin, yMax, lineColor);
}
}
private static void InitStyles()
{
if (timeAreaStyles == null)
timeAreaStyles = new Styles2();
}
private static Color AlphaMultiplied(Color color,float multiplier) { return new Color(color.r, color.g, color.b, color.a * multiplier); }
public static void DrawVerticalLine(float x, float minY, float maxY, Color color)
{
if (Event.current.type != EventType.Repaint)
return;
Color backupCol = Handles.color;
ReflectionHelper.Instance.HandleUtility.ApplyWireMaterial();
if (Application.platform == RuntimePlatform.WindowsEditor)
GL.Begin(GL.QUADS);
else
GL.Begin(GL.LINES);
DrawVerticalLineFast(x, minY, maxY, color);
GL.End();
Handles.color = backupCol;
}
public static void DrawVerticalLineFast(float x, float minY, float maxY, Color color)
{
if (Application.platform == RuntimePlatform.WindowsEditor)
{
GL.Color(color);
GL.Vertex(new Vector3(x - 0.5f, minY, 0));
GL.Vertex(new Vector3(x + 0.5f, minY, 0));
GL.Vertex(new Vector3(x + 0.5f, maxY, 0));
GL.Vertex(new Vector3(x - 0.5f, maxY, 0));
}
else
{
GL.Color(color);
GL.Vertex(new Vector3(x, minY, 0));
GL.Vertex(new Vector3(x, maxY, 0));
}
}
}
}