Skip to content

Commit fbeeb77

Browse files
authored
Merge pull request #152 from martinmmueller/master
added pause/resume to Tracker.cs
2 parents 3872e72 + 39c59f9 commit fbeeb77

File tree

1 file changed

+68
-12
lines changed

1 file changed

+68
-12
lines changed

Assets/UXF/Scripts/Etc/Tracker.cs

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
namespace UXF
88
{
99
/// <summary>
10-
/// Create a new class that inherits from this component to create custom tracking behaviour on a frame-by-frame basis.
10+
/// Create a new class that inherits from this component to create custom tracking behaviour
11+
/// on a frame-by-frame basis.
1112
/// </summary>
1213
public abstract class Tracker : MonoBehaviour
1314
{
14-
private bool recording = false;
1515
private static string[] baseHeaders = new string[] { "time" };
16+
private TrackerState currentState = TrackerState.Stopped;
1617

1718
/// <summary>
1819
/// Name of the object used in saving
@@ -36,20 +37,27 @@ public string DataName
3637
{
3738
get
3839
{
39-
Debug.AssertFormat(MeasurementDescriptor.Length > 0, "No measurement descriptor has been specified for this Tracker!");
40+
Debug.AssertFormat(MeasurementDescriptor.Length > 0,
41+
"No measurement descriptor has been specified for this Tracker!");
4042
return string.Join("_", new string[]{ objectName, MeasurementDescriptor });
4143
}
4244
}
4345

44-
public bool Recording { get { return recording; } }
46+
public bool Recording { get { return currentState == TrackerState.Recording; } }
4547

4648
public UXFDataTable Data { get; private set; } = new UXFDataTable();
47-
49+
50+
/// <summary>
51+
/// The current state of the tracker.
52+
/// </summary>
53+
public TrackerState CurrentState { get => currentState; }
4854

4955
/// <summary>
5056
/// When the tracker should take measurements.
5157
/// </summary>
52-
[Tooltip("When the measurements should be taken.\n\nManual should only be selected if the user is calling the RecordRow method either from another script or a custom Tracker class.")]
58+
[Tooltip("When the measurements should be taken." +
59+
"\n\nManual should only be selected if the user is calling the RecordRow method" +
60+
" either from another script or a custom Tracker class.")]
5361
public TrackerUpdateType updateType = TrackerUpdateType.LateUpdate;
5462

5563
// called when component is added
@@ -61,21 +69,22 @@ void Reset()
6169
// called by unity just before rendering the frame
6270
void LateUpdate()
6371
{
64-
if (recording && updateType == TrackerUpdateType.LateUpdate) RecordRow();
72+
if (Recording && updateType == TrackerUpdateType.LateUpdate) RecordRow();
6573
}
6674

6775
// called by unity when physics simulations are run
6876
void FixedUpdate()
6977
{
70-
if (recording && updateType == TrackerUpdateType.FixedUpdate) RecordRow();
78+
if (Recording && updateType == TrackerUpdateType.FixedUpdate) RecordRow();
7179
}
7280

7381
/// <summary>
7482
/// Records a new row of data at current time.
7583
/// </summary>
7684
public void RecordRow()
7785
{
78-
if (!recording) throw new System.InvalidOperationException("Tracker measurements cannot be taken when not recording!");
86+
if (!Recording) throw new System.InvalidOperationException(
87+
"Tracker measurements cannot be taken when not recording!");
7988

8089
UXFDataRow newRow = GetCurrentValues();
8190
newRow.Add(("time", Time.time));
@@ -87,17 +96,54 @@ public void RecordRow()
8796
/// </summary>
8897
public void StartRecording()
8998
{
99+
if (currentState == TrackerState.Recording)
100+
{
101+
Utilities.UXFDebugLogWarning($"Start command received for tracker in state: '{TrackerState.Recording}'." +
102+
" This will dump exisiting data! " +
103+
$"If you want to restart a paused tracker, use '{nameof(ResumeRecording)}()' instead.");
104+
}
90105
var header = baseHeaders.Concat(CustomHeader);
91106
Data = new UXFDataTable(header.ToArray());
92-
recording = true;
107+
currentState = TrackerState.Recording;
93108
}
94109

95110
/// <summary>
96111
/// Stops recording.
97112
/// </summary>
98113
public void StopRecording()
99114
{
100-
recording = false;
115+
if (currentState != TrackerState.Recording)
116+
{
117+
Utilities.UXFDebugLogWarning($"Stop command received for tracker in state: '{currentState}'." +
118+
$" This should only be called when tracker is in state '{TrackerState.Recording}'");
119+
}
120+
currentState = TrackerState.Stopped;
121+
}
122+
123+
/// <summary>
124+
/// Pauses recording.
125+
/// </summary>
126+
public void PauseRecording()
127+
{
128+
if (currentState != TrackerState.Recording)
129+
{
130+
Utilities.UXFDebugLogWarning($"Pause command received for tracker in state: '{currentState}'." +
131+
$"This should only be called when tracker is in state '{TrackerState.Recording}'");
132+
}
133+
currentState = TrackerState.Paused;
134+
}
135+
136+
/// <summary>
137+
/// Resumes recording.
138+
/// </summary>
139+
public void ResumeRecording()
140+
{
141+
if (currentState != TrackerState.Paused)
142+
{
143+
Utilities.UXFDebugLogWarning($"Resume command received for tracker in state: '{currentState}'." +
144+
$"This should only be called when tracker is in state '{TrackerState.Paused}'");
145+
}
146+
currentState = TrackerState.Recording;
101147
}
102148

103149
/// <summary>
@@ -109,10 +155,20 @@ public void StopRecording()
109155
}
110156

111157
/// <summary>
112-
/// When the tracker should collect new measurements. Manual should only be selected if the user is calling the RecordRow method either from another script or a custom Tracker class.
158+
/// When the tracker should collect new measurements.
159+
/// Manual should only be selected if the user is calling the RecordRow method
160+
/// either from another script or a custom Tracker class.
113161
/// </summary>
114162
public enum TrackerUpdateType
115163
{
116164
LateUpdate, FixedUpdate, Manual
117165
}
166+
167+
/// <summary>
168+
/// The possible states a tracker can be in.
169+
/// </summary>
170+
public enum TrackerState
171+
{
172+
Recording, Paused, Stopped
173+
}
118174
}

0 commit comments

Comments
 (0)