From b4bb87fee39516895d80bd431a6c98c18e233229 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 20 Feb 2019 12:08:30 +0900 Subject: [PATCH] Make TrackVirtualTracking more accurate on seeks/stops --- osu.Game/Tests/Beatmaps/TestWorkingBeatmap.cs | 41 +++++++++++++++---- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/osu.Game/Tests/Beatmaps/TestWorkingBeatmap.cs b/osu.Game/Tests/Beatmaps/TestWorkingBeatmap.cs index e6de9d37b2..90f2449f53 100644 --- a/osu.Game/Tests/Beatmaps/TestWorkingBeatmap.cs +++ b/osu.Game/Tests/Beatmaps/TestWorkingBeatmap.cs @@ -50,28 +50,32 @@ namespace osu.Game.Tests.Beatmaps public class TrackVirtualManual : Track { private readonly IFrameBasedClock referenceClock; - private readonly ManualClock clock; + + private readonly ManualClock clock = new ManualClock(); private bool running; + + /// + /// Local offset added to the reference clock to resolve correct time. + /// private double offset; public TrackVirtualManual(IFrameBasedClock referenceClock) { this.referenceClock = referenceClock; Length = double.PositiveInfinity; - clock = new ManualClock(); } public override bool Seek(double seek) { - offset = MathHelper.Clamp(seek, 0, Length) - referenceClock.CurrentTime; + offset = MathHelper.Clamp(seek, 0, Length); + lastReferenceTime = null; return true; } public override void Start() { running = true; - Seek(0); } public override void Reset() @@ -82,18 +86,41 @@ namespace osu.Game.Tests.Beatmaps public override void Stop() { - running = false; + if (running) + { + 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 double CurrentTime => running ? clock.CurrentTime : 0; + private double? lastReferenceTime; + + public override double CurrentTime => clock.CurrentTime; protected override void 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) {