2019-03-16 04:47:11 +00: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;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
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 03:50:00 +00:00
|
|
|
/// Will ensure a minimum of 50 frames per clock second is maintained, regardless of any system lag or seeks.
|
2019-03-16 04:47:11 +00:00
|
|
|
/// </summary>
|
|
|
|
public class FrameStabilityContainer : Container, IHasReplayHandler
|
|
|
|
{
|
2019-05-09 07:36:47 +00:00
|
|
|
private readonly double gameplayStartTime;
|
|
|
|
|
2019-05-09 07:37:34 +00: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 09:48:39 +00:00
|
|
|
|
2019-08-15 09:25:31 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Whether to enable frame-stable playback.
|
|
|
|
/// </summary>
|
|
|
|
internal bool FrameStablePlayback = true;
|
|
|
|
|
2020-05-21 01:58:30 +00:00
|
|
|
public GameplayClock GameplayClock => stabilityGameplayClock;
|
|
|
|
|
|
|
|
[Cached(typeof(GameplayClock))]
|
|
|
|
private readonly StabilityGameplayClock stabilityGameplayClock;
|
2019-05-09 07:37:34 +00:00
|
|
|
|
2019-05-09 07:36:47 +00:00
|
|
|
public FrameStabilityContainer(double gameplayStartTime = double.MinValue)
|
2019-03-16 04:47:11 +00:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
2019-05-09 09:06:11 +00:00
|
|
|
|
2020-05-21 01:58:30 +00:00
|
|
|
stabilityGameplayClock = new StabilityGameplayClock(framedClock = new FramedClock(manualClock = new ManualClock()));
|
2019-05-09 07:36:47 +00:00
|
|
|
|
|
|
|
this.gameplayStartTime = gameplayStartTime;
|
2019-03-16 04:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private readonly ManualClock manualClock;
|
|
|
|
|
|
|
|
private readonly FramedClock framedClock;
|
|
|
|
|
|
|
|
private IFrameBasedClock parentGameplayClock;
|
|
|
|
|
2019-10-03 07:00:47 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The current direction of playback to be exposed to frame stable children.
|
|
|
|
/// </summary>
|
|
|
|
private int direction;
|
|
|
|
|
2019-03-16 04:47:11 +00:00
|
|
|
[BackgroundDependencyLoader(true)]
|
|
|
|
private void load(GameplayClock clock)
|
|
|
|
{
|
|
|
|
if (clock != null)
|
2019-03-25 11:25:47 +00:00
|
|
|
{
|
2020-05-21 01:58:30 +00:00
|
|
|
stabilityGameplayClock.ParentGameplayClock = parentGameplayClock = clock;
|
2019-05-09 09:06:11 +00:00
|
|
|
GameplayClock.IsPaused.BindTo(clock.IsPaused);
|
2019-03-25 11:25:47 +00:00
|
|
|
}
|
2019-03-16 04:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
setClock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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>
|
|
|
|
private bool requireMoreUpdateLoops;
|
|
|
|
|
|
|
|
/// <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>
|
|
|
|
private bool validState;
|
|
|
|
|
|
|
|
protected override bool RequiresChildrenUpdate => base.RequiresChildrenUpdate && validState;
|
|
|
|
|
|
|
|
private bool isAttached => ReplayInputHandler != null;
|
|
|
|
|
|
|
|
private const double sixty_frame_time = 1000.0 / 60;
|
|
|
|
|
2019-04-24 04:44:21 +00:00
|
|
|
private bool firstConsumption = true;
|
|
|
|
|
2019-03-16 04:47:11 +00:00
|
|
|
public override bool UpdateSubTree()
|
|
|
|
{
|
|
|
|
requireMoreUpdateLoops = true;
|
2019-05-09 09:06:11 +00:00
|
|
|
validState = !GameplayClock.IsPaused.Value;
|
2019-03-16 04:47:11 +00:00
|
|
|
|
|
|
|
int loops = 0;
|
|
|
|
|
2019-05-09 07:37:34 +00:00
|
|
|
while (validState && requireMoreUpdateLoops && loops++ < MaxCatchUpFrames)
|
2019-03-16 04:47:11 +00:00
|
|
|
{
|
|
|
|
updateClock();
|
|
|
|
|
|
|
|
if (validState)
|
|
|
|
{
|
|
|
|
base.UpdateSubTree();
|
|
|
|
UpdateSubTreeMasking(this, ScreenSpaceDrawQuad.AABBFloat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateClock()
|
|
|
|
{
|
|
|
|
if (parentGameplayClock == null)
|
|
|
|
setClock(); // LoadComplete may not be run yet, but we still want the clock.
|
|
|
|
|
|
|
|
validState = true;
|
2019-10-04 05:42:06 +00:00
|
|
|
requireMoreUpdateLoops = false;
|
2019-03-16 04:47:11 +00:00
|
|
|
|
|
|
|
var newProposedTime = parentGameplayClock.CurrentTime;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2019-08-16 11:21:00 +00:00
|
|
|
if (!FrameStablePlayback)
|
|
|
|
return;
|
2019-10-04 05:42:06 +00:00
|
|
|
|
|
|
|
if (firstConsumption)
|
2019-04-24 04:44:21 +00:00
|
|
|
{
|
|
|
|
// On the first update, frame-stability seeking would result in unexpected/unwanted behaviour.
|
|
|
|
// Instead we perform an initial seek to the proposed time.
|
2019-04-24 06:23:00 +00:00
|
|
|
|
2019-10-03 07:00:47 +00:00
|
|
|
// process frame (in addition to finally clause) to clear out ElapsedTime
|
|
|
|
manualClock.CurrentTime = newProposedTime;
|
2019-04-24 06:23:00 +00:00
|
|
|
framedClock.ProcessFrame();
|
|
|
|
|
2019-04-24 04:44:21 +00:00
|
|
|
firstConsumption = false;
|
|
|
|
}
|
2019-05-09 07:36:47 +00:00
|
|
|
else if (manualClock.CurrentTime < gameplayStartTime)
|
|
|
|
manualClock.CurrentTime = newProposedTime = Math.Min(gameplayStartTime, newProposedTime);
|
2019-04-24 04:44:21 +00:00
|
|
|
else if (Math.Abs(manualClock.CurrentTime - newProposedTime) > sixty_frame_time * 1.2f)
|
2019-03-16 04:47:11 +00:00
|
|
|
{
|
2019-04-16 08:47:00 +00:00
|
|
|
newProposedTime = newProposedTime > manualClock.CurrentTime
|
2019-03-16 04:47:11 +00:00
|
|
|
? Math.Min(newProposedTime, manualClock.CurrentTime + sixty_frame_time)
|
|
|
|
: Math.Max(newProposedTime, manualClock.CurrentTime - sixty_frame_time);
|
|
|
|
}
|
|
|
|
|
2019-10-03 07:00:47 +00:00
|
|
|
if (isAttached)
|
2019-03-16 04:47:11 +00:00
|
|
|
{
|
|
|
|
double? newTime = ReplayInputHandler.SetFrameFromTime(newProposedTime);
|
|
|
|
|
|
|
|
if (newTime == null)
|
|
|
|
{
|
|
|
|
// we shouldn't execute for this time value. probably waiting on more replay data.
|
|
|
|
validState = false;
|
|
|
|
requireMoreUpdateLoops = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-04 05:32:47 +00:00
|
|
|
newProposedTime = newTime.Value;
|
2019-03-16 04:47:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2019-10-03 07:00:47 +00:00
|
|
|
if (newProposedTime != manualClock.CurrentTime)
|
|
|
|
direction = newProposedTime > manualClock.CurrentTime ? 1 : -1;
|
|
|
|
|
|
|
|
manualClock.CurrentTime = newProposedTime;
|
|
|
|
manualClock.Rate = Math.Abs(parentGameplayClock.Rate) * direction;
|
|
|
|
manualClock.IsRunning = parentGameplayClock.IsRunning;
|
|
|
|
|
2019-10-04 05:42:06 +00:00
|
|
|
requireMoreUpdateLoops |= manualClock.CurrentTime != parentGameplayClock.CurrentTime;
|
|
|
|
|
2019-03-16 04:47:11 +00:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setClock()
|
|
|
|
{
|
|
|
|
// in case a parent gameplay clock isn't available, just use the parent clock.
|
|
|
|
if (parentGameplayClock == null)
|
|
|
|
parentGameplayClock = Clock;
|
|
|
|
|
2019-05-09 09:06:11 +00:00
|
|
|
Clock = GameplayClock;
|
2019-03-16 04:47:11 +00:00
|
|
|
ProcessCustomClock = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ReplayInputHandler ReplayInputHandler { get; set; }
|
2020-05-21 01:58:30 +00:00
|
|
|
|
|
|
|
private class StabilityGameplayClock : GameplayClock
|
|
|
|
{
|
|
|
|
public IFrameBasedClock ParentGameplayClock;
|
|
|
|
|
|
|
|
public StabilityGameplayClock(FramedClock underlyingClock)
|
|
|
|
: base(underlyingClock)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool IsSeeking => ParentGameplayClock != null && Math.Abs(CurrentTime - ParentGameplayClock.CurrentTime) > 200;
|
|
|
|
}
|
2019-03-16 04:47:11 +00:00
|
|
|
}
|
|
|
|
}
|