-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathReplayTimeline.cs
More file actions
202 lines (163 loc) · 6.51 KB
/
ReplayTimeline.cs
File metadata and controls
202 lines (163 loc) · 6.51 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
using System;
using UnityEngine;
namespace Netick.Unity
{
/// <summary>
/// A helper script that provides a simple built-in replay timeline UI.
/// </summary>
[AddComponentMenu("Netick/Replay Timeline")]
public class ReplayTimeline : NetworkEventsListener
{
[Header("Layout")]
public float ReplayBarHeight = 60f;
public float ButtonWidth = 50f;
public float ButtonHeight = 30f;
public float Spacing = 0f;
[Header("Colors")]
public Color ButtonForegroundColor = Color.white;
public Color ButtonBackgroundColor = new(0.2f, 0.2f, 0.2f, 1f);
public Color ButtonHoverColor = new(0.1f, 0.1f, 0.1f, 1f);
public Color BarBackgroundColor = new(0.1f, 0.1f, 0.1f, 0.8f);
private GUIStyle _centeredLabelStyle;
private GUIStyle _barBackgroundStyle;
private GUIStyle _buttonStyle;
private GUIStyle _boldButtonStyle;
private Texture2D _barTexture;
private Texture2D _buttonNormalTexture;
private Texture2D _buttonHoverTexture;
private float _currentPointerPos;
private int _targetFrame = -1;
private float _timeScale = 1f;
private bool IsInReplay => Sandbox != null && Sandbox.IsRunning && Sandbox.IsReplay && Sandbox.Replay.Playback.FrameCount > 0;
private bool IsPlaying => Time.timeScale != 0f;
private void Awake()
{
_barBackgroundStyle = null;
}
private void OnDestroy()
{
if (_barTexture != null)
Destroy(_barTexture);
if (_buttonNormalTexture != null)
Destroy(_buttonNormalTexture);
if (_buttonHoverTexture != null)
Destroy(_buttonHoverTexture);
}
private void Update()
{
if (!IsInReplay)
return;
if (Input.GetKeyDown(KeyCode.Space))
TogglePaused();
}
private void TogglePaused()
{
Time.timeScale = IsPlaying ? 0 : Mathf.Max(0f, _timeScale);
}
private void EnsureStylesInitialized()
{
if (_barBackgroundStyle != null)
return;
_barTexture = new Texture2D(1, 1);
_barTexture.SetPixel(0, 0, BarBackgroundColor);
_barTexture.Apply();
_barBackgroundStyle = new GUIStyle(GUI.skin.box)
{
normal = { background = _barTexture },
border = new RectOffset(0, 0, 0, 0),
padding = new RectOffset(4, 4, 0, 0)
};
_buttonNormalTexture = new Texture2D(1, 1);
_buttonNormalTexture.SetPixel(0, 0, ButtonBackgroundColor);
_buttonNormalTexture.Apply();
_buttonHoverTexture = new Texture2D(1, 1);
_buttonHoverTexture.SetPixel(0, 0, ButtonHoverColor);
_buttonHoverTexture.Apply();
_buttonStyle = new GUIStyle(GUI.skin.button)
{
normal = { background = _buttonNormalTexture, textColor = ButtonForegroundColor },
hover = { background = _buttonHoverTexture, textColor = ButtonForegroundColor },
active = { background = _buttonHoverTexture, textColor = Color.gray },
border = new RectOffset(0, 0, 0, 0)
};
_boldButtonStyle = new GUIStyle(_buttonStyle) { fontStyle = FontStyle.Bold };
_centeredLabelStyle = new GUIStyle(GUI.skin.label)
{
alignment = TextAnchor.MiddleCenter,
normal = { textColor = ButtonForegroundColor }
};
}
private void OnGUI()
{
if (!IsInReplay)
return;
EnsureStylesInitialized();
GUILayout.BeginArea(new Rect(0, Screen.height - ReplayBarHeight, Screen.width, ReplayBarHeight), _barBackgroundStyle);
GUILayout.BeginVertical(GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
DrawTimelineBar();
GUILayout.Space(Spacing);
DrawControls();
GUILayout.EndVertical();
GUILayout.EndArea();
}
private void DrawTimelineBar()
{
var playback = Sandbox.Replay.Playback;
float smoothPos = Mathf.Min(playback.Position + Sandbox.LocalAlpha * Sandbox.FixedDeltaTime, playback.Duration - Sandbox.FixedDeltaTime);
float previousPos = _currentPointerPos;
_currentPointerPos = GUILayout.HorizontalSlider(_currentPointerPos, 0f, playback.Duration, GUILayout.ExpandWidth(true));
if (Mathf.Abs(_currentPointerPos - previousPos) > Mathf.Epsilon)
{
playback.SeekToTime(_currentPointerPos);
_targetFrame = playback.TimeToFrameIndex(_currentPointerPos);
}
if (_targetFrame == -1)
_currentPointerPos = smoothPos;
else if (_targetFrame != playback.FrameIndex)
_targetFrame = -1;
}
private void DrawControls()
{
var playback = Sandbox.Replay.Playback;
float jumpTime = Mathf.Min(playback.Duration * 0.2f, 5f);
GUILayout.BeginHorizontal();
if (GUILayout.Button(IsPlaying ? "||" : ">", _buttonStyle, GUILayout.Width(ButtonWidth), GUILayout.Height(ButtonHeight)))
TogglePaused();
GUILayout.Label($"{FormatTime(playback.Position)} / {FormatTime(playback.Duration)}", _centeredLabelStyle, GUILayout.Width(150f), GUILayout.Height(ButtonHeight));
if (GUILayout.Button("<<", _buttonStyle, GUILayout.Width(ButtonWidth), GUILayout.Height(ButtonHeight)) && IsPlaying)
playback.SeekToTimeRelative(-jumpTime);
if (GUILayout.Button(">>", _buttonStyle, GUILayout.Width(ButtonWidth), GUILayout.Height(ButtonHeight)) && IsPlaying)
playback.SeekToTimeRelative(jumpTime);
GUILayout.Space(20f);
GUILayout.Label("Speed:", _centeredLabelStyle, GUILayout.Width(60f), GUILayout.Height(ButtonHeight));
DrawSpeedButton("1/4x", 0.25f);
DrawSpeedButton("1/2x", 0.5f);
DrawSpeedButton("1x", 1f);
DrawSpeedButton("2x", 2f);
DrawSpeedButton("4x", 4f);
DrawSpeedButton("8x", 8f);
GUILayout.EndHorizontal();
}
private void DrawSpeedButton(string label, float scale)
{
var style = Mathf.Approximately(_timeScale, scale) ? _boldButtonStyle : _buttonStyle;
if (GUILayout.Button(label, style, GUILayout.Width(ButtonWidth), GUILayout.Height(ButtonHeight)))
{
_timeScale = scale;
if (IsPlaying)
Time.timeScale = Mathf.Max(0f, _timeScale);
}
}
string FormatTime(float seconds)
{
int totalSeconds = Mathf.FloorToInt(seconds);
int hours = totalSeconds / 3600;
int minutes = (totalSeconds % 3600) / 60;
int secs = totalSeconds % 60;
if (hours > 0)
return $"{hours}:{minutes:00}:{secs:00}";
else
return $"{minutes}:{secs:00}";
}
}
}