2019-03-06 19:30:14 +08:00
|
|
|
// 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.
|
|
|
|
|
2021-04-15 18:07:25 +08:00
|
|
|
using System;
|
2019-03-06 19:30:14 +08:00
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Timing;
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Play
|
|
|
|
{
|
2021-04-15 18:07:25 +08:00
|
|
|
public abstract class GameplayClockContainer : Container, IAdjustableClock
|
2019-03-06 19:30:14 +08:00
|
|
|
{
|
2021-04-14 16:47:11 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The final clock which is exposed to underlying components.
|
|
|
|
/// </summary>
|
|
|
|
public GameplayClock GameplayClock { get; private set; }
|
2019-03-06 19:30:14 +08:00
|
|
|
|
|
|
|
public readonly BindableBool IsPaused = new BindableBool();
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The decoupled clock used for gameplay. Should be used for seeks and clock control.
|
|
|
|
/// </summary>
|
2021-04-14 16:47:11 +08:00
|
|
|
protected readonly DecoupleableInterpolatingFramedClock AdjustableClock;
|
2019-03-06 19:30:14 +08:00
|
|
|
|
2021-04-14 16:47:11 +08:00
|
|
|
protected GameplayClockContainer(IClock sourceClock)
|
2019-03-06 19:30:14 +08:00
|
|
|
{
|
2021-04-14 16:47:11 +08:00
|
|
|
RelativeSizeAxes = Axes.Both;
|
2019-03-06 19:30:14 +08:00
|
|
|
|
2021-04-14 16:47:11 +08:00
|
|
|
AdjustableClock = new DecoupleableInterpolatingFramedClock { IsCoupled = false };
|
2021-04-14 18:32:48 +08:00
|
|
|
AdjustableClock.ChangeSource(sourceClock);
|
2019-04-03 15:58:20 +08:00
|
|
|
|
2021-04-14 18:50:22 +08:00
|
|
|
IsPaused.BindValueChanged(OnIsPausedChanged);
|
2021-04-14 16:47:11 +08:00
|
|
|
}
|
2019-03-06 19:30:14 +08:00
|
|
|
|
2021-04-14 16:47:11 +08:00
|
|
|
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
|
2019-03-06 19:30:14 +08:00
|
|
|
{
|
2021-04-14 16:47:11 +08:00
|
|
|
var dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
|
2020-08-06 17:31:08 +08:00
|
|
|
|
2021-04-14 16:47:11 +08:00
|
|
|
dependencies.CacheAs(GameplayClock = CreateGameplayClock(AdjustableClock));
|
2021-04-14 17:29:34 +08:00
|
|
|
GameplayClock.IsPaused.BindTo(IsPaused);
|
2019-03-06 19:30:14 +08:00
|
|
|
|
2021-04-14 16:47:11 +08:00
|
|
|
return dependencies;
|
|
|
|
}
|
2019-03-06 19:30:14 +08:00
|
|
|
|
2021-04-14 16:47:11 +08:00
|
|
|
public virtual void Start()
|
|
|
|
{
|
|
|
|
if (!AdjustableClock.IsRunning)
|
|
|
|
{
|
|
|
|
// 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
|
|
|
|
Seek(GameplayClock.CurrentTime);
|
2019-03-06 19:30:14 +08:00
|
|
|
|
2021-04-14 16:47:11 +08:00
|
|
|
AdjustableClock.Start();
|
|
|
|
}
|
2019-03-06 19:30:14 +08:00
|
|
|
|
2021-04-14 16:47:11 +08:00
|
|
|
IsPaused.Value = false;
|
|
|
|
}
|
2019-03-25 19:25:47 +08:00
|
|
|
|
2021-04-14 16:47:11 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Seek to a specific time in gameplay.
|
|
|
|
/// <remarks>
|
|
|
|
/// Adjusts for any offsets which have been applied (so the seek may not be the expected point in time on the underlying audio track).
|
|
|
|
/// </remarks>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="time">The destination time to seek to.</param>
|
|
|
|
public virtual void Seek(double time) => AdjustableClock.Seek(time);
|
2020-12-10 16:42:47 +08:00
|
|
|
|
2021-04-14 16:47:11 +08:00
|
|
|
public virtual void Stop() => IsPaused.Value = true;
|
2020-12-10 16:42:47 +08:00
|
|
|
|
2021-04-14 16:47:11 +08:00
|
|
|
public virtual void Restart()
|
2020-12-10 16:42:47 +08:00
|
|
|
{
|
2021-04-14 16:47:11 +08:00
|
|
|
AdjustableClock.Seek(0);
|
|
|
|
AdjustableClock.Stop();
|
|
|
|
|
|
|
|
if (!IsPaused.Value)
|
|
|
|
Start();
|
2019-03-06 19:30:14 +08:00
|
|
|
}
|
|
|
|
|
2021-04-14 20:15:14 +08:00
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
if (!IsPaused.Value)
|
|
|
|
ClockToProcess.ProcessFrame();
|
|
|
|
|
|
|
|
base.Update();
|
|
|
|
}
|
|
|
|
|
2021-04-14 18:50:22 +08:00
|
|
|
protected abstract void OnIsPausedChanged(ValueChangedEvent<bool> isPaused);
|
2021-04-14 16:47:11 +08:00
|
|
|
|
2021-04-14 20:15:14 +08:00
|
|
|
protected virtual IFrameBasedClock ClockToProcess => AdjustableClock;
|
|
|
|
|
|
|
|
protected abstract GameplayClock CreateGameplayClock(IFrameBasedClock source);
|
2021-04-15 18:07:25 +08:00
|
|
|
|
|
|
|
#region IAdjustableClock
|
|
|
|
|
|
|
|
bool IAdjustableClock.Seek(double position)
|
|
|
|
{
|
|
|
|
Seek(position);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IAdjustableClock.Reset()
|
|
|
|
{
|
|
|
|
Restart();
|
|
|
|
Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ResetSpeedAdjustments()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
double IAdjustableClock.Rate
|
|
|
|
{
|
|
|
|
get => GameplayClock.Rate;
|
|
|
|
set => throw new NotSupportedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
double IClock.Rate => GameplayClock.Rate;
|
|
|
|
|
|
|
|
public double CurrentTime => GameplayClock.CurrentTime;
|
|
|
|
|
|
|
|
public bool IsRunning => GameplayClock.IsRunning;
|
|
|
|
|
|
|
|
#endregion
|
2021-04-14 16:47:11 +08:00
|
|
|
}
|
2019-03-06 19:30:14 +08:00
|
|
|
}
|