1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-15 07:22:55 +08:00

Make TrackVirtualTracking more accurate on seeks/stops

This commit is contained in:
Dean Herbert 2019-02-20 12:08:30 +09:00
parent f2ad2931c7
commit b4bb87fee3

View File

@ -50,28 +50,32 @@ namespace osu.Game.Tests.Beatmaps
public class TrackVirtualManual : Track public class TrackVirtualManual : Track
{ {
private readonly IFrameBasedClock referenceClock; private readonly IFrameBasedClock referenceClock;
private readonly ManualClock clock;
private readonly ManualClock clock = new ManualClock();
private bool running; private bool running;
/// <summary>
/// Local offset added to the reference clock to resolve correct time.
/// </summary>
private double offset; private double offset;
public TrackVirtualManual(IFrameBasedClock referenceClock) public TrackVirtualManual(IFrameBasedClock referenceClock)
{ {
this.referenceClock = referenceClock; this.referenceClock = referenceClock;
Length = double.PositiveInfinity; Length = double.PositiveInfinity;
clock = new ManualClock();
} }
public override bool Seek(double seek) public override bool Seek(double seek)
{ {
offset = MathHelper.Clamp(seek, 0, Length) - referenceClock.CurrentTime; offset = MathHelper.Clamp(seek, 0, Length);
lastReferenceTime = null;
return true; return true;
} }
public override void Start() public override void Start()
{ {
running = true; running = true;
Seek(0);
} }
public override void Reset() public override void Reset()
@ -81,19 +85,42 @@ namespace osu.Game.Tests.Beatmaps
} }
public override void Stop() public override void Stop()
{
if (running)
{ {
running = false; running = false;
// on stopping, the current value should be transferred out of the clock, as we can no longer rely on
// the referenceClock (which will still be counting time).
offset = clock.CurrentTime;
lastReferenceTime = null;
}
} }
public override bool IsRunning => running; public override bool IsRunning => running;
public override double CurrentTime => running ? clock.CurrentTime : 0; private double? lastReferenceTime;
public override double CurrentTime => clock.CurrentTime;
protected override void UpdateState() protected override void UpdateState()
{ {
base.UpdateState(); base.UpdateState();
clock.CurrentTime = Math.Min(referenceClock.CurrentTime + offset, Length); if (running)
{
double refTime = referenceClock.CurrentTime;
if (!lastReferenceTime.HasValue)
{
// if the clock just started running, the current value should be transferred to the offset
// (to zero the progression of time).
offset -= refTime;
}
lastReferenceTime = referenceClock.CurrentTime;
}
clock.CurrentTime = Math.Min((lastReferenceTime ?? 0) + offset, Length);
if (CurrentTime >= Length) if (CurrentTime >= Length)
{ {