1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 06:52:55 +08:00

Remove remaining usage of GameplayClock

This commit is contained in:
Dean Herbert 2022-08-15 19:46:29 +09:00
parent 43442dbf65
commit 704568ae3b
7 changed files with 42 additions and 134 deletions

View File

@ -1,12 +1,8 @@
// 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.
#nullable disable
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Bindables;
using osu.Framework.Timing;
using osu.Game.Screens.Play;
@ -20,20 +16,16 @@ namespace osu.Game.Tests.NonVisual
public void TestTrueGameplayRateWithZeroAdjustment(double underlyingClockRate)
{
var framedClock = new FramedClock(new ManualClock { Rate = underlyingClockRate });
var gameplayClock = new TestGameplayClock(framedClock);
gameplayClock.MutableNonGameplayAdjustments.Add(new BindableDouble());
var gameplayClock = new TestGameplayClockContainer(framedClock);
Assert.That(gameplayClock.TrueGameplayRate, Is.EqualTo(0));
}
private class TestGameplayClock : GameplayClock
private class TestGameplayClockContainer : GameplayClockContainer
{
public List<Bindable<double>> MutableNonGameplayAdjustments { get; } = new List<Bindable<double>>();
public override IEnumerable<double> NonGameplayAdjustments => new[] { 0.0 };
public override IEnumerable<double> NonGameplayAdjustments => MutableNonGameplayAdjustments.Select(b => b.Value);
public TestGameplayClock(IFrameBasedClock underlyingClock)
public TestGameplayClockContainer(IFrameBasedClock underlyingClock)
: base(underlyingClock)
{
}

View File

@ -39,7 +39,7 @@ namespace osu.Game.Tests.Visual.Gameplay
private GameplayState gameplayState = TestGameplayState.Create(new OsuRuleset());
[Cached(typeof(IGameplayClock))]
private readonly IGameplayClock gameplayClock = new GameplayClock(new FramedClock());
private readonly IGameplayClock gameplayClock = new GameplayClockContainer(new FramedClock());
// best way to check without exposing.
private Drawable hideTarget => hudOverlay.KeyCounter;

View File

@ -30,7 +30,7 @@ namespace osu.Game.Tests.Visual.Gameplay
private GameplayState gameplayState = TestGameplayState.Create(new OsuRuleset());
[Cached(typeof(IGameplayClock))]
private readonly IGameplayClock gameplayClock = new GameplayClock(new FramedClock());
private readonly IGameplayClock gameplayClock = new GameplayClockContainer(new FramedClock());
[SetUpSteps]
public void SetUpSteps()

View File

@ -37,7 +37,7 @@ namespace osu.Game.Tests.Visual.Gameplay
private GameplayState gameplayState = TestGameplayState.Create(new OsuRuleset());
[Cached(typeof(IGameplayClock))]
private readonly IGameplayClock gameplayClock = new GameplayClock(new FramedClock());
private readonly IGameplayClock gameplayClock = new GameplayClockContainer(new FramedClock());
private IEnumerable<HUDOverlay> hudOverlays => CreatedDrawables.OfType<HUDOverlay>();

View File

@ -1,76 +0,0 @@
// 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.
#nullable disable
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Bindables;
using osu.Framework.Timing;
using osu.Framework.Utils;
namespace osu.Game.Screens.Play
{
/// <summary>
/// A clock which is used for gameplay elements that need to follow audio time 1:1.
/// Exposed via DI by <see cref="GameplayClockContainer"/>.
/// <remarks>
/// The main purpose of this clock is to stop components using it from accidentally processing the main
/// <see cref="IFrameBasedClock"/>, as this should only be done once to ensure accuracy.
/// </remarks>
/// </summary>
public class GameplayClock : IGameplayClock
{
internal readonly IFrameBasedClock UnderlyingClock;
public readonly BindableBool IsPaused = new BindableBool();
IBindable<bool> IGameplayClock.IsPaused => IsPaused;
public virtual IEnumerable<double> NonGameplayAdjustments => Enumerable.Empty<double>();
public GameplayClock(IFrameBasedClock underlyingClock)
{
UnderlyingClock = underlyingClock;
}
public double? StartTime { get; internal set; }
public double CurrentTime => UnderlyingClock.CurrentTime;
public double Rate => UnderlyingClock.Rate;
public double TrueGameplayRate
{
get
{
double baseRate = Rate;
foreach (double adjustment in NonGameplayAdjustments)
{
if (Precision.AlmostEquals(adjustment, 0))
return 0;
baseRate /= adjustment;
}
return baseRate;
}
}
public bool IsRunning => UnderlyingClock.IsRunning;
public void ProcessFrame()
{
// intentionally not updating the underlying clock (handled externally).
}
public double ElapsedFrameTime => UnderlyingClock.ElapsedFrameTime;
public double FramesPerSecond => UnderlyingClock.FramesPerSecond;
public FrameTimeInfo TimeInfo => UnderlyingClock.TimeInfo;
public IClock Source => UnderlyingClock;
}
}

View File

@ -3,13 +3,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Logging;
using osu.Framework.Timing;
using osu.Framework.Utils;
namespace osu.Game.Screens.Play
{
@ -40,8 +41,6 @@ namespace osu.Game.Screens.Play
/// </summary>
public event Action? OnSeek;
private double? startTime;
/// <summary>
/// The time from which the clock should start. Will be seeked to on calling <see cref="Reset"/>.
/// </summary>
@ -49,24 +48,14 @@ namespace osu.Game.Screens.Play
/// If not set, a value of zero will be used.
/// Importantly, the value will be inferred from the current ruleset in <see cref="MasterGameplayClockContainer"/> unless specified.
/// </remarks>
public double? StartTime
{
get => startTime;
set
{
startTime = value;
public double? StartTime { get; set; }
if (GameplayClock.IsNotNull())
GameplayClock.StartTime = value;
}
}
public IEnumerable<double> NonGameplayAdjustments => GameplayClock.NonGameplayAdjustments;
public virtual IEnumerable<double> NonGameplayAdjustments => Enumerable.Empty<double>();
/// <summary>
/// The final clock which is exposed to gameplay components.
/// </summary>
protected GameplayClock GameplayClock { get; private set; } = null!;
protected IFrameBasedClock GameplayClock { get; private set; } = null!;
/// <summary>
/// Creates a new <see cref="GameplayClockContainer"/>.
@ -90,9 +79,6 @@ namespace osu.Game.Screens.Play
dependencies.CacheAs<IGameplayClock>(this);
GameplayClock.StartTime = StartTime;
GameplayClock.IsPaused.BindTo(isPaused);
return dependencies;
}
@ -126,7 +112,7 @@ namespace osu.Game.Screens.Play
AdjustableSource.Seek(time);
// Manually process to make sure the gameplay clock is correctly updated after a seek.
GameplayClock.UnderlyingClock.ProcessFrame();
GameplayClock.ProcessFrame();
OnSeek?.Invoke();
}
@ -174,7 +160,7 @@ namespace osu.Game.Screens.Play
protected override void Update()
{
if (!IsPaused.Value)
GameplayClock.UnderlyingClock.ProcessFrame();
GameplayClock.ProcessFrame();
base.Update();
}
@ -199,7 +185,7 @@ namespace osu.Game.Screens.Play
/// </remarks>
/// <param name="source">The <see cref="IFrameBasedClock"/> providing the source time.</param>
/// <returns>The final <see cref="GameplayClock"/>.</returns>
protected virtual GameplayClock CreateGameplayClock(IFrameBasedClock source) => new GameplayClock(source);
protected virtual IFrameBasedClock CreateGameplayClock(IFrameBasedClock source) => source;
#region IAdjustableClock
@ -238,6 +224,22 @@ namespace osu.Game.Screens.Play
public FrameTimeInfo TimeInfo => GameplayClock.TimeInfo;
public double TrueGameplayRate => GameplayClock.TrueGameplayRate;
public double TrueGameplayRate
{
get
{
double baseRate = Rate;
foreach (double adjustment in NonGameplayAdjustments)
{
if (Precision.AlmostEquals(adjustment, 0))
return 0;
baseRate /= adjustment;
}
return baseRate;
}
}
}
}

View File

@ -54,7 +54,6 @@ namespace osu.Game.Screens.Play
private HardwareCorrectionOffsetClock userGlobalOffsetClock = null!;
private HardwareCorrectionOffsetClock userBeatmapOffsetClock = null!;
private HardwareCorrectionOffsetClock platformOffsetClock = null!;
private MasterGameplayClock masterGameplayClock = null!;
private Bindable<double> userAudioOffset = null!;
private IDisposable? beatmapOffsetSubscription;
@ -150,7 +149,7 @@ namespace osu.Game.Screens.Play
// We must also process underlying gameplay clocks to update rate-adjusted offsets with the new frequency adjustment.
// Without doing this, an initial seek may be performed with the wrong offset.
GameplayClock.UnderlyingClock.ProcessFrame();
GameplayClock.ProcessFrame();
}
}
@ -191,7 +190,7 @@ namespace osu.Game.Screens.Play
Seek(skipTarget);
}
protected override GameplayClock CreateGameplayClock(IFrameBasedClock source)
protected override IFrameBasedClock CreateGameplayClock(IFrameBasedClock source)
{
// Lazer's audio timings in general doesn't match stable. This is the result of user testing, albeit limited.
// This only seems to be required on windows. We need to eventually figure out why, with a bit of luck.
@ -199,9 +198,7 @@ namespace osu.Game.Screens.Play
// the final usable gameplay clock with user-set offsets applied.
userGlobalOffsetClock = new HardwareCorrectionOffsetClock(platformOffsetClock, pauseFreqAdjust);
userBeatmapOffsetClock = new HardwareCorrectionOffsetClock(userGlobalOffsetClock, pauseFreqAdjust);
return masterGameplayClock = new MasterGameplayClock(userBeatmapOffsetClock);
return userBeatmapOffsetClock = new HardwareCorrectionOffsetClock(userGlobalOffsetClock, pauseFreqAdjust);
}
/// <summary>
@ -224,8 +221,8 @@ namespace osu.Game.Screens.Play
Track.AddAdjustment(AdjustableProperty.Frequency, pauseFreqAdjust);
Track.AddAdjustment(AdjustableProperty.Tempo, UserPlaybackRate);
masterGameplayClock.MutableNonGameplayAdjustments.Add(pauseFreqAdjust);
masterGameplayClock.MutableNonGameplayAdjustments.Add(UserPlaybackRate);
nonGameplayAdjustments.Add(pauseFreqAdjust);
nonGameplayAdjustments.Add(UserPlaybackRate);
speedAdjustmentsApplied = true;
}
@ -238,8 +235,8 @@ namespace osu.Game.Screens.Play
Track.RemoveAdjustment(AdjustableProperty.Frequency, pauseFreqAdjust);
Track.RemoveAdjustment(AdjustableProperty.Tempo, UserPlaybackRate);
masterGameplayClock.MutableNonGameplayAdjustments.Remove(pauseFreqAdjust);
masterGameplayClock.MutableNonGameplayAdjustments.Remove(UserPlaybackRate);
nonGameplayAdjustments.Remove(pauseFreqAdjust);
nonGameplayAdjustments.Remove(UserPlaybackRate);
speedAdjustmentsApplied = false;
}
@ -252,7 +249,7 @@ namespace osu.Game.Screens.Play
}
ControlPointInfo IBeatSyncProvider.ControlPoints => beatmap.Beatmap.ControlPointInfo;
IClock IBeatSyncProvider.Clock => GameplayClock;
IClock IBeatSyncProvider.Clock => this;
ChannelAmplitudes IHasAmplitudes.CurrentAmplitudes => beatmap.TrackLoaded ? beatmap.Track.CurrentAmplitudes : ChannelAmplitudes.Empty;
private class HardwareCorrectionOffsetClock : FramedOffsetClock
@ -300,15 +297,8 @@ namespace osu.Game.Screens.Play
}
}
private class MasterGameplayClock : GameplayClock
{
public readonly List<Bindable<double>> MutableNonGameplayAdjustments = new List<Bindable<double>>();
public override IEnumerable<double> NonGameplayAdjustments => MutableNonGameplayAdjustments.Select(b => b.Value);
private readonly List<Bindable<double>> nonGameplayAdjustments = new List<Bindable<double>>();
public MasterGameplayClock(FramedOffsetClock underlyingClock)
: base(underlyingClock)
{
}
}
public override IEnumerable<double> NonGameplayAdjustments => nonGameplayAdjustments.Select(b => b.Value);
}
}