1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 03:12:54 +08:00
osu-lazer/osu.Game/Tests/Beatmaps/TestWorkingBeatmap.cs

134 lines
4.4 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 17:19:50 +08:00
using System;
2018-04-13 17:19:50 +08:00
using osu.Framework.Audio.Track;
using osu.Framework.Graphics.Textures;
using osu.Framework.Timing;
2018-04-13 17:19:50 +08:00
using osu.Game.Beatmaps;
using osu.Game.Rulesets;
using osuTK;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Tests.Beatmaps
{
public class TestWorkingBeatmap : WorkingBeatmap
{
private readonly TrackVirtualManual track;
private readonly IBeatmap beatmap;
/// <summary>
/// Create an instance which creates a <see cref="TestBeatmap"/> for the provided ruleset when requested.
/// </summary>
/// <param name="ruleset">The target ruleset.</param>
2019-02-19 11:38:45 +08:00
/// <param name="referenceClock">A clock which should be used instead of a stopwatch for virtual time progression.</param>
public TestWorkingBeatmap(RulesetInfo ruleset, IFrameBasedClock referenceClock)
: this(new TestBeatmap(ruleset), referenceClock)
2018-04-13 17:19:50 +08:00
{
}
/// <summary>
/// Create an instance which provides the <see cref="IBeatmap"/> when requested.
/// </summary>
/// <param name="beatmap">The beatmap</param>
/// <param name="referenceClock">An optional clock which should be used instead of a stopwatch for virtual time progression.</param>
public TestWorkingBeatmap(IBeatmap beatmap, IFrameBasedClock referenceClock = null)
2018-04-13 17:19:50 +08:00
: base(beatmap.BeatmapInfo)
{
this.beatmap = beatmap;
if (referenceClock != null)
track = new TrackVirtualManual(referenceClock);
2018-04-13 17:19:50 +08:00
}
2018-04-19 19:44:38 +08:00
protected override IBeatmap GetBeatmap() => beatmap;
2018-04-13 17:19:50 +08:00
protected override Texture GetBackground() => null;
protected override Track GetTrack() => track;
2019-02-19 11:35:52 +08:00
/// <summary>
/// A virtual track which tracks a reference clock.
/// </summary>
public class TrackVirtualManual : Track
{
2019-02-19 11:35:52 +08:00
private readonly IFrameBasedClock referenceClock;
private readonly ManualClock clock = new ManualClock();
2019-02-19 11:35:52 +08:00
private bool running;
/// <summary>
/// Local offset added to the reference clock to resolve correct time.
/// </summary>
2019-02-19 11:35:52 +08:00
private double offset;
2019-02-19 11:35:52 +08:00
public TrackVirtualManual(IFrameBasedClock referenceClock)
{
this.referenceClock = referenceClock;
Length = double.PositiveInfinity;
}
2019-02-19 11:35:52 +08:00
public override bool Seek(double seek)
{
offset = MathHelper.Clamp(seek, 0, Length);
lastReferenceTime = null;
2019-02-19 11:35:52 +08:00
return true;
}
2019-02-19 11:35:52 +08:00
public override void Start()
{
running = true;
}
2019-02-19 11:35:52 +08:00
public override void Reset()
{
Seek(0);
base.Reset();
}
2019-02-19 11:35:52 +08:00
public override void Stop()
{
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;
}
2019-02-19 11:35:52 +08:00
}
2019-02-19 11:35:52 +08:00
public override bool IsRunning => running;
private double? lastReferenceTime;
public override double CurrentTime => clock.CurrentTime;
2019-02-19 11:35:52 +08:00
protected override void UpdateState()
{
2019-02-19 11:35:52 +08:00
base.UpdateState();
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 = refTime;
}
clock.CurrentTime = Math.Min((lastReferenceTime ?? 0) + offset, Length);
2019-02-19 11:35:52 +08:00
if (CurrentTime >= Length)
{
Stop();
RaiseCompleted();
}
}
}
2018-04-13 17:19:50 +08:00
}
}