2019-03-16 12:47:11 +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.
|
|
|
|
|
|
|
|
using System;
|
2020-10-27 13:10:12 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2019-03-16 12:47:11 +08:00
|
|
|
using osu.Framework.Allocation;
|
2020-10-27 13:10:12 +08:00
|
|
|
using osu.Framework.Bindables;
|
2019-03-16 12:47:11 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Timing;
|
|
|
|
using osu.Game.Input.Handlers;
|
|
|
|
using osu.Game.Screens.Play;
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.UI
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// A container which consumes a parent gameplay clock and standardises frame counts for children.
|
2020-03-26 11:50:00 +08:00
|
|
|
/// Will ensure a minimum of 50 frames per clock second is maintained, regardless of any system lag or seeks.
|
2019-03-16 12:47:11 +08:00
|
|
|
/// </summary>
|
2020-10-27 12:54:33 +08:00
|
|
|
public class FrameStabilityContainer : Container, IHasReplayHandler
|
2019-03-16 12:47:11 +08:00
|
|
|
{
|
2019-05-09 15:36:47 +08:00
|
|
|
private readonly double gameplayStartTime;
|
|
|
|
|
2019-05-09 15:37:34 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The number of frames (per parent frame) which can be run in an attempt to catch-up to real-time.
|
|
|
|
/// </summary>
|
|
|
|
public int MaxCatchUpFrames { get; set; } = 5;
|
2019-05-10 17:48:39 +08:00
|
|
|
|
2019-08-15 17:25:31 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Whether to enable frame-stable playback.
|
|
|
|
/// </summary>
|
|
|
|
internal bool FrameStablePlayback = true;
|
|
|
|
|
2020-10-27 13:10:12 +08:00
|
|
|
public IFrameStableClock FrameStableClock => frameStableClock;
|
|
|
|
|
2020-05-21 09:58:30 +08:00
|
|
|
[Cached(typeof(GameplayClock))]
|
2020-10-27 13:10:12 +08:00
|
|
|
private readonly FrameStabilityClock frameStableClock;
|
2019-05-09 15:37:34 +08:00
|
|
|
|
2019-05-09 15:36:47 +08:00
|
|
|
public FrameStabilityContainer(double gameplayStartTime = double.MinValue)
|
2019-03-16 12:47:11 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
2019-05-09 17:06:11 +08:00
|
|
|
|
2020-10-27 13:10:12 +08:00
|
|
|
frameStableClock = new FrameStabilityClock(framedClock = new FramedClock(manualClock = new ManualClock()));
|
2019-05-09 15:36:47 +08:00
|
|
|
|
|
|
|
this.gameplayStartTime = gameplayStartTime;
|
2019-03-16 12:47:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private readonly ManualClock manualClock;
|
|
|
|
|
|
|
|
private readonly FramedClock framedClock;
|
|
|
|
|
|
|
|
private IFrameBasedClock parentGameplayClock;
|
|
|
|
|
2019-10-03 15:00:47 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The current direction of playback to be exposed to frame stable children.
|
|
|
|
/// </summary>
|
|
|
|
private int direction;
|
|
|
|
|
2019-03-16 12:47:11 +08:00
|
|
|
[BackgroundDependencyLoader(true)]
|
2020-10-05 15:20:29 +08:00
|
|
|
private void load(GameplayClock clock, ISamplePlaybackDisabler sampleDisabler)
|
2019-03-16 12:47:11 +08:00
|
|
|
{
|
|
|
|
if (clock != null)
|
2019-03-25 19:25:47 +08:00
|
|
|
{
|
2020-10-27 13:10:12 +08:00
|
|
|
parentGameplayClock = frameStableClock.ParentGameplayClock = clock;
|
|
|
|
frameStableClock.IsPaused.BindTo(clock.IsPaused);
|
2019-03-25 19:25:47 +08:00
|
|
|
}
|
2019-03-16 12:47:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
setClock();
|
|
|
|
}
|
|
|
|
|
2020-10-28 14:11:53 +08:00
|
|
|
private PlaybackState state;
|
2019-03-16 12:47:11 +08:00
|
|
|
|
2020-10-28 14:11:53 +08:00
|
|
|
protected override bool RequiresChildrenUpdate => base.RequiresChildrenUpdate && state != PlaybackState.NotValid;
|
2019-03-16 12:47:11 +08:00
|
|
|
|
2020-10-28 13:53:31 +08:00
|
|
|
private bool hasReplayAttached => ReplayInputHandler != null;
|
2019-03-16 12:47:11 +08:00
|
|
|
|
|
|
|
private const double sixty_frame_time = 1000.0 / 60;
|
|
|
|
|
2019-04-24 12:44:21 +08:00
|
|
|
private bool firstConsumption = true;
|
|
|
|
|
2019-03-16 12:47:11 +08:00
|
|
|
public override bool UpdateSubTree()
|
|
|
|
{
|
2020-10-28 14:11:53 +08:00
|
|
|
state = frameStableClock.IsPaused.Value ? PlaybackState.NotValid : PlaybackState.Valid;
|
2020-10-14 18:39:48 +08:00
|
|
|
|
2020-10-28 14:11:53 +08:00
|
|
|
int loops = MaxCatchUpFrames;
|
2019-03-16 12:47:11 +08:00
|
|
|
|
2020-10-28 14:11:53 +08:00
|
|
|
while (state != PlaybackState.NotValid && loops-- > 0)
|
2019-03-16 12:47:11 +08:00
|
|
|
{
|
|
|
|
updateClock();
|
|
|
|
|
2020-10-28 14:11:53 +08:00
|
|
|
if (state == PlaybackState.NotValid)
|
|
|
|
break;
|
|
|
|
|
|
|
|
base.UpdateSubTree();
|
|
|
|
UpdateSubTreeMasking(this, ScreenSpaceDrawQuad.AABBFloat);
|
2019-03-16 12:47:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateClock()
|
|
|
|
{
|
|
|
|
if (parentGameplayClock == null)
|
|
|
|
setClock(); // LoadComplete may not be run yet, but we still want the clock.
|
|
|
|
|
2020-10-28 13:42:23 +08:00
|
|
|
// each update start with considering things in valid state.
|
2020-10-28 14:11:53 +08:00
|
|
|
state = PlaybackState.Valid;
|
2019-03-16 12:47:11 +08:00
|
|
|
|
2020-10-28 13:42:23 +08:00
|
|
|
// our goal is to catch up to the time provided by the parent clock.
|
|
|
|
var proposedTime = parentGameplayClock.CurrentTime;
|
2019-03-16 12:47:11 +08:00
|
|
|
|
2020-10-28 14:12:39 +08:00
|
|
|
if (FrameStablePlayback)
|
|
|
|
// if we require frame stability, the proposed time will be adjusted to move at most one known
|
|
|
|
// frame interval in the current direction.
|
|
|
|
applyFrameStability(ref proposedTime);
|
2019-03-16 12:47:11 +08:00
|
|
|
|
2020-10-28 14:12:39 +08:00
|
|
|
if (hasReplayAttached)
|
|
|
|
state = updateReplay(ref proposedTime);
|
2019-10-03 15:00:47 +08:00
|
|
|
|
2020-10-28 14:12:39 +08:00
|
|
|
if (proposedTime != manualClock.CurrentTime)
|
2020-10-28 14:31:57 +08:00
|
|
|
direction = proposedTime > manualClock.CurrentTime ? 1 : -1;
|
2019-10-03 15:00:47 +08:00
|
|
|
|
2020-10-28 14:12:39 +08:00
|
|
|
manualClock.CurrentTime = proposedTime;
|
|
|
|
manualClock.Rate = Math.Abs(parentGameplayClock.Rate) * direction;
|
|
|
|
manualClock.IsRunning = parentGameplayClock.IsRunning;
|
2019-10-04 13:42:06 +08:00
|
|
|
|
2020-10-28 14:12:39 +08:00
|
|
|
double timeBehind = Math.Abs(manualClock.CurrentTime - parentGameplayClock.CurrentTime);
|
2020-10-27 16:14:41 +08:00
|
|
|
|
2020-10-28 14:12:39 +08:00
|
|
|
// determine whether catch-up is required.
|
|
|
|
if (state != PlaybackState.NotValid)
|
|
|
|
state = timeBehind > 0 ? PlaybackState.RequiresCatchUp : PlaybackState.Valid;
|
2020-10-27 13:10:12 +08:00
|
|
|
|
2020-10-28 14:12:39 +08:00
|
|
|
frameStableClock.IsCatchingUp.Value = timeBehind > 200;
|
|
|
|
|
|
|
|
// The manual clock time has changed in the above code. The framed clock now needs to be updated
|
|
|
|
// to ensure that the its time is valid for our children before input is processed
|
|
|
|
framedClock.ProcessFrame();
|
2019-03-16 12:47:11 +08:00
|
|
|
}
|
|
|
|
|
2020-10-28 13:53:31 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Attempt to advance replay playback for a given time.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="proposedTime">The time which is to be displayed.</param>
|
2020-10-28 14:11:53 +08:00
|
|
|
private PlaybackState updateReplay(ref double proposedTime)
|
2020-10-28 13:53:31 +08:00
|
|
|
{
|
|
|
|
double? newTime;
|
|
|
|
|
|
|
|
if (FrameStablePlayback)
|
|
|
|
{
|
|
|
|
// when stability is turned on, we shouldn't execute for time values the replay is unable to satisfy.
|
2020-10-28 14:11:53 +08:00
|
|
|
newTime = ReplayInputHandler.SetFrameFromTime(proposedTime);
|
2020-10-28 13:53:31 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// when stability is disabled, we don't really care about accuracy.
|
|
|
|
// looping over the replay will allow it to catch up and feed out the required values
|
|
|
|
// for the current time.
|
|
|
|
while ((newTime = ReplayInputHandler.SetFrameFromTime(proposedTime)) != proposedTime)
|
|
|
|
{
|
|
|
|
if (newTime == null)
|
|
|
|
{
|
|
|
|
// special case for when the replay actually can't arrive at the required time.
|
|
|
|
// protects from potential endless loop.
|
2020-10-28 14:11:53 +08:00
|
|
|
break;
|
2020-10-28 13:53:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 14:11:53 +08:00
|
|
|
if (newTime == null)
|
|
|
|
return PlaybackState.NotValid;
|
|
|
|
|
2020-10-28 13:53:31 +08:00
|
|
|
proposedTime = newTime.Value;
|
2020-10-28 14:11:53 +08:00
|
|
|
return PlaybackState.Valid;
|
2020-10-28 13:53:31 +08:00
|
|
|
}
|
|
|
|
|
2020-10-28 13:42:23 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Apply frame stability modifier to a time.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="proposedTime">The time which is to be displayed.</param>
|
|
|
|
private void applyFrameStability(ref double proposedTime)
|
|
|
|
{
|
|
|
|
if (firstConsumption)
|
|
|
|
{
|
|
|
|
// On the first update, frame-stability seeking would result in unexpected/unwanted behaviour.
|
|
|
|
// Instead we perform an initial seek to the proposed time.
|
|
|
|
|
|
|
|
// process frame (in addition to finally clause) to clear out ElapsedTime
|
|
|
|
manualClock.CurrentTime = proposedTime;
|
|
|
|
framedClock.ProcessFrame();
|
|
|
|
|
|
|
|
firstConsumption = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (manualClock.CurrentTime < gameplayStartTime)
|
|
|
|
manualClock.CurrentTime = proposedTime = Math.Min(gameplayStartTime, proposedTime);
|
|
|
|
else if (Math.Abs(manualClock.CurrentTime - proposedTime) > sixty_frame_time * 1.2f)
|
|
|
|
{
|
|
|
|
proposedTime = proposedTime > manualClock.CurrentTime
|
|
|
|
? Math.Min(proposedTime, manualClock.CurrentTime + sixty_frame_time)
|
|
|
|
: Math.Max(proposedTime, manualClock.CurrentTime - sixty_frame_time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 12:47:11 +08:00
|
|
|
private void setClock()
|
|
|
|
{
|
2020-10-05 11:47:00 +08:00
|
|
|
if (parentGameplayClock == null)
|
|
|
|
{
|
|
|
|
// in case a parent gameplay clock isn't available, just use the parent clock.
|
|
|
|
parentGameplayClock ??= Clock;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-27 13:10:12 +08:00
|
|
|
Clock = frameStableClock;
|
2020-10-05 11:47:00 +08:00
|
|
|
}
|
2019-03-16 12:47:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public ReplayInputHandler ReplayInputHandler { get; set; }
|
2020-10-27 13:10:12 +08:00
|
|
|
|
2020-10-28 14:11:53 +08:00
|
|
|
private enum PlaybackState
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Playback is not possible. Child hierarchy should not be processed.
|
|
|
|
/// </summary>
|
|
|
|
NotValid,
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Whether we are running up-to-date with our parent clock.
|
|
|
|
/// If not, we will need to keep processing children until we catch up.
|
|
|
|
/// </summary>
|
|
|
|
RequiresCatchUp,
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Whether we are in a valid state (ie. should we keep processing children frames).
|
|
|
|
/// This should be set to false when the replay is, for instance, waiting for future frames to arrive.
|
|
|
|
/// </summary>
|
|
|
|
Valid
|
|
|
|
}
|
|
|
|
|
2020-10-27 13:10:12 +08:00
|
|
|
private class FrameStabilityClock : GameplayClock, IFrameStableClock
|
|
|
|
{
|
|
|
|
public GameplayClock ParentGameplayClock;
|
|
|
|
|
|
|
|
public readonly Bindable<bool> IsCatchingUp = new Bindable<bool>();
|
|
|
|
|
|
|
|
public override IEnumerable<Bindable<double>> NonGameplayAdjustments => ParentGameplayClock?.NonGameplayAdjustments ?? Enumerable.Empty<Bindable<double>>();
|
|
|
|
|
|
|
|
public FrameStabilityClock(FramedClock underlyingClock)
|
|
|
|
: base(underlyingClock)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
IBindable<bool> IFrameStableClock.IsCatchingUp => IsCatchingUp;
|
|
|
|
}
|
2019-03-16 12:47:11 +08:00
|
|
|
}
|
|
|
|
}
|