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

Reduce exposed properties in GameplayClockContainer

This commit is contained in:
Dean Herbert 2022-08-15 19:59:08 +09:00
parent 704568ae3b
commit 1696a905ba
3 changed files with 50 additions and 46 deletions

View File

@ -9,7 +9,7 @@ using osu.Game.Screens.Play;
namespace osu.Game.Tests.NonVisual namespace osu.Game.Tests.NonVisual
{ {
[TestFixture] [TestFixture]
public class GameplayClockTest public class GameplayClockContainerTest
{ {
[TestCase(0)] [TestCase(0)]
[TestCase(1)] [TestCase(1)]

View File

@ -15,7 +15,7 @@ using osu.Framework.Utils;
namespace osu.Game.Screens.Play namespace osu.Game.Screens.Play
{ {
/// <summary> /// <summary>
/// Encapsulates gameplay timing logic and provides a <see cref="GameplayClock"/> via DI for gameplay components to use. /// Encapsulates gameplay timing logic and provides a <see cref="IGameplayClock"/> via DI for gameplay components to use.
/// </summary> /// </summary>
public class GameplayClockContainer : Container, IAdjustableClock, IGameplayClock public class GameplayClockContainer : Container, IAdjustableClock, IGameplayClock
{ {
@ -24,15 +24,8 @@ namespace osu.Game.Screens.Play
/// </summary> /// </summary>
public IBindable<bool> IsPaused => isPaused; public IBindable<bool> IsPaused => isPaused;
private readonly BindableBool isPaused = new BindableBool(true);
/// <summary> /// <summary>
/// The adjustable source clock used for gameplay. Should be used for seeks and clock control. /// The source clock. Should generally not be used for any timekeeping purposes.
/// </summary>
protected readonly DecoupleableInterpolatingFramedClock AdjustableSource;
/// <summary>
/// The source clock.
/// </summary> /// </summary>
public IClock SourceClock { get; private set; } public IClock SourceClock { get; private set; }
@ -55,7 +48,14 @@ namespace osu.Game.Screens.Play
/// <summary> /// <summary>
/// The final clock which is exposed to gameplay components. /// The final clock which is exposed to gameplay components.
/// </summary> /// </summary>
protected IFrameBasedClock GameplayClock { get; private set; } = null!; protected IFrameBasedClock FramedClock { get; private set; } = null!;
private readonly BindableBool isPaused = new BindableBool(true);
/// <summary>
/// The adjustable source clock used for gameplay. Should be used for seeks and clock control.
/// </summary>
private readonly DecoupleableInterpolatingFramedClock decoupledClock;
/// <summary> /// <summary>
/// Creates a new <see cref="GameplayClockContainer"/>. /// Creates a new <see cref="GameplayClockContainer"/>.
@ -67,7 +67,7 @@ namespace osu.Game.Screens.Play
RelativeSizeAxes = Axes.Both; RelativeSizeAxes = Axes.Both;
AdjustableSource = new DecoupleableInterpolatingFramedClock { IsCoupled = false }; decoupledClock = new DecoupleableInterpolatingFramedClock { IsCoupled = false };
IsPaused.BindValueChanged(OnIsPausedChanged); IsPaused.BindValueChanged(OnIsPausedChanged);
} }
@ -75,7 +75,7 @@ namespace osu.Game.Screens.Play
{ {
var dependencies = new DependencyContainer(base.CreateChildDependencies(parent)); var dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
GameplayClock = CreateGameplayClock(AdjustableSource); FramedClock = CreateGameplayClock(decoupledClock);
dependencies.CacheAs<IGameplayClock>(this); dependencies.CacheAs<IGameplayClock>(this);
@ -89,13 +89,13 @@ namespace osu.Game.Screens.Play
{ {
ensureSourceClockSet(); ensureSourceClockSet();
if (!AdjustableSource.IsRunning) if (!decoupledClock.IsRunning)
{ {
// Seeking the decoupled clock to its current time ensures that its source clock will be seeked to the same time // Seeking the decoupled clock to its current time ensures that its source clock will be seeked to the same time
// This accounts for the clock source potentially taking time to enter a completely stopped state // This accounts for the clock source potentially taking time to enter a completely stopped state
Seek(GameplayClock.CurrentTime); Seek(FramedClock.CurrentTime);
AdjustableSource.Start(); decoupledClock.Start();
} }
isPaused.Value = false; isPaused.Value = false;
@ -109,10 +109,10 @@ namespace osu.Game.Screens.Play
{ {
Logger.Log($"{nameof(GameplayClockContainer)} seeking to {time}"); Logger.Log($"{nameof(GameplayClockContainer)} seeking to {time}");
AdjustableSource.Seek(time); decoupledClock.Seek(time);
// Manually process to make sure the gameplay clock is correctly updated after a seek. // Manually process to make sure the gameplay clock is correctly updated after a seek.
GameplayClock.ProcessFrame(); FramedClock.ProcessFrame();
OnSeek?.Invoke(); OnSeek?.Invoke();
} }
@ -129,7 +129,7 @@ namespace osu.Game.Screens.Play
public void Reset(bool startClock = false) public void Reset(bool startClock = false)
{ {
// Manually stop the source in order to not affect the IsPaused state. // Manually stop the source in order to not affect the IsPaused state.
AdjustableSource.Stop(); decoupledClock.Stop();
if (!IsPaused.Value || startClock) if (!IsPaused.Value || startClock)
Start(); Start();
@ -142,10 +142,10 @@ namespace osu.Game.Screens.Play
/// Changes the source clock. /// Changes the source clock.
/// </summary> /// </summary>
/// <param name="sourceClock">The new source.</param> /// <param name="sourceClock">The new source.</param>
protected void ChangeSource(IClock sourceClock) => AdjustableSource.ChangeSource(SourceClock = sourceClock); protected void ChangeSource(IClock sourceClock) => decoupledClock.ChangeSource(SourceClock = sourceClock);
/// <summary> /// <summary>
/// Ensures that the <see cref="AdjustableSource"/> is set to <see cref="SourceClock"/>, if it hasn't been given a source yet. /// Ensures that the <see cref="decoupledClock"/> is set to <see cref="SourceClock"/>, if it hasn't been given a source yet.
/// This is usually done before a seek to avoid accidentally seeking only the adjustable source in decoupled mode, /// This is usually done before a seek to avoid accidentally seeking only the adjustable source in decoupled mode,
/// but not the actual source clock. /// but not the actual source clock.
/// That will pretty much only happen on the very first call of this method, as the source clock is passed in the constructor, /// That will pretty much only happen on the very first call of this method, as the source clock is passed in the constructor,
@ -153,38 +153,38 @@ namespace osu.Game.Screens.Play
/// </summary> /// </summary>
private void ensureSourceClockSet() private void ensureSourceClockSet()
{ {
if (AdjustableSource.Source == null) if (decoupledClock.Source == null)
ChangeSource(SourceClock); ChangeSource(SourceClock);
} }
protected override void Update() protected override void Update()
{ {
if (!IsPaused.Value) if (!IsPaused.Value)
GameplayClock.ProcessFrame(); FramedClock.ProcessFrame();
base.Update(); base.Update();
} }
/// <summary> /// <summary>
/// Invoked when the value of <see cref="IsPaused"/> is changed to start or stop the <see cref="AdjustableSource"/> clock. /// Invoked when the value of <see cref="IsPaused"/> is changed to start or stop the <see cref="decoupledClock"/> clock.
/// </summary> /// </summary>
/// <param name="isPaused">Whether the clock should now be paused.</param> /// <param name="isPaused">Whether the clock should now be paused.</param>
protected virtual void OnIsPausedChanged(ValueChangedEvent<bool> isPaused) protected virtual void OnIsPausedChanged(ValueChangedEvent<bool> isPaused)
{ {
if (isPaused.NewValue) if (isPaused.NewValue)
AdjustableSource.Stop(); decoupledClock.Stop();
else else
AdjustableSource.Start(); decoupledClock.Start();
} }
/// <summary> /// <summary>
/// Creates the final <see cref="GameplayClock"/> which is exposed via DI to be used by gameplay components. /// Creates the final <see cref="FramedClock"/> which is exposed via DI to be used by gameplay components.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// Any intermediate clocks such as platform offsets should be applied here. /// Any intermediate clocks such as platform offsets should be applied here.
/// </remarks> /// </remarks>
/// <param name="source">The <see cref="IFrameBasedClock"/> providing the source time.</param> /// <param name="source">The <see cref="IFrameBasedClock"/> providing the source time.</param>
/// <returns>The final <see cref="GameplayClock"/>.</returns> /// <returns>The final <see cref="FramedClock"/>.</returns>
protected virtual IFrameBasedClock CreateGameplayClock(IFrameBasedClock source) => source; protected virtual IFrameBasedClock CreateGameplayClock(IFrameBasedClock source) => source;
#region IAdjustableClock #region IAdjustableClock
@ -201,15 +201,15 @@ namespace osu.Game.Screens.Play
double IAdjustableClock.Rate double IAdjustableClock.Rate
{ {
get => GameplayClock.Rate; get => FramedClock.Rate;
set => throw new NotSupportedException(); set => throw new NotSupportedException();
} }
public double Rate => GameplayClock.Rate; public double Rate => FramedClock.Rate;
public double CurrentTime => GameplayClock.CurrentTime; public double CurrentTime => FramedClock.CurrentTime;
public bool IsRunning => GameplayClock.IsRunning; public bool IsRunning => FramedClock.IsRunning;
#endregion #endregion
@ -218,11 +218,11 @@ namespace osu.Game.Screens.Play
// Handled via update. Don't process here to safeguard from external usages potentially processing frames additional times. // Handled via update. Don't process here to safeguard from external usages potentially processing frames additional times.
} }
public double ElapsedFrameTime => GameplayClock.ElapsedFrameTime; public double ElapsedFrameTime => FramedClock.ElapsedFrameTime;
public double FramesPerSecond => GameplayClock.FramesPerSecond; public double FramesPerSecond => FramedClock.FramesPerSecond;
public FrameTimeInfo TimeInfo => GameplayClock.TimeInfo; public FrameTimeInfo TimeInfo => FramedClock.TimeInfo;
public double TrueGameplayRate public double TrueGameplayRate
{ {

View File

@ -35,8 +35,6 @@ namespace osu.Game.Screens.Play
/// </summary> /// </summary>
public const double MINIMUM_SKIP_TIME = 1000; public const double MINIMUM_SKIP_TIME = 1000;
protected Track Track => (Track)SourceClock;
public readonly BindableNumber<double> UserPlaybackRate = new BindableDouble(1) public readonly BindableNumber<double> UserPlaybackRate = new BindableDouble(1)
{ {
Default = 1, Default = 1,
@ -133,7 +131,7 @@ namespace osu.Game.Screens.Play
this.TransformBindableTo(pauseFreqAdjust, 0, 200, Easing.Out).OnComplete(_ => this.TransformBindableTo(pauseFreqAdjust, 0, 200, Easing.Out).OnComplete(_ =>
{ {
if (IsPaused.Value == isPaused.NewValue) if (IsPaused.Value == isPaused.NewValue)
AdjustableSource.Stop(); base.OnIsPausedChanged(isPaused);
}); });
} }
else else
@ -142,14 +140,14 @@ namespace osu.Game.Screens.Play
else else
{ {
if (isPaused.NewValue) if (isPaused.NewValue)
AdjustableSource.Stop(); base.OnIsPausedChanged(isPaused);
// If not yet loaded, we still want to ensure relevant state is correct, as it is used for offset calculations. // If not yet loaded, we still want to ensure relevant state is correct, as it is used for offset calculations.
pauseFreqAdjust.Value = isPaused.NewValue ? 0 : 1; pauseFreqAdjust.Value = isPaused.NewValue ? 0 : 1;
// We must also process underlying gameplay clocks to update rate-adjusted offsets with the new frequency adjustment. // 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. // Without doing this, an initial seek may be performed with the wrong offset.
GameplayClock.ProcessFrame(); FramedClock.ProcessFrame();
} }
} }
@ -178,12 +176,12 @@ namespace osu.Game.Screens.Play
/// </summary> /// </summary>
public void Skip() public void Skip()
{ {
if (GameplayClock.CurrentTime > skipTargetTime - MINIMUM_SKIP_TIME) if (FramedClock.CurrentTime > skipTargetTime - MINIMUM_SKIP_TIME)
return; return;
double skipTarget = skipTargetTime - MINIMUM_SKIP_TIME; double skipTarget = skipTargetTime - MINIMUM_SKIP_TIME;
if (GameplayClock.CurrentTime < 0 && skipTarget > 6000) if (FramedClock.CurrentTime < 0 && skipTarget > 6000)
// double skip exception for storyboards with very long intros // double skip exception for storyboards with very long intros
skipTarget = 0; skipTarget = 0;
@ -218,8 +216,11 @@ namespace osu.Game.Screens.Play
if (speedAdjustmentsApplied) if (speedAdjustmentsApplied)
return; return;
Track.AddAdjustment(AdjustableProperty.Frequency, pauseFreqAdjust); if (SourceClock is Track track)
Track.AddAdjustment(AdjustableProperty.Tempo, UserPlaybackRate); {
track.AddAdjustment(AdjustableProperty.Frequency, pauseFreqAdjust);
track.AddAdjustment(AdjustableProperty.Tempo, UserPlaybackRate);
}
nonGameplayAdjustments.Add(pauseFreqAdjust); nonGameplayAdjustments.Add(pauseFreqAdjust);
nonGameplayAdjustments.Add(UserPlaybackRate); nonGameplayAdjustments.Add(UserPlaybackRate);
@ -232,8 +233,11 @@ namespace osu.Game.Screens.Play
if (!speedAdjustmentsApplied) if (!speedAdjustmentsApplied)
return; return;
Track.RemoveAdjustment(AdjustableProperty.Frequency, pauseFreqAdjust); if (SourceClock is Track track)
Track.RemoveAdjustment(AdjustableProperty.Tempo, UserPlaybackRate); {
track.RemoveAdjustment(AdjustableProperty.Frequency, pauseFreqAdjust);
track.RemoveAdjustment(AdjustableProperty.Tempo, UserPlaybackRate);
}
nonGameplayAdjustments.Remove(pauseFreqAdjust); nonGameplayAdjustments.Remove(pauseFreqAdjust);
nonGameplayAdjustments.Remove(UserPlaybackRate); nonGameplayAdjustments.Remove(UserPlaybackRate);