osu/osu.Game/Rulesets/Replays/FramedReplayInputHandler.cs

184 lines
6.3 KiB
C#
Raw Normal View History

2019-03-29 03:38:47 +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.
2018-04-13 09:19:50 +00:00
using System;
using System.Collections.Generic;
2020-10-28 05:35:42 +00:00
using System.Diagnostics;
2019-03-30 16:33:56 +00:00
using JetBrains.Annotations;
2018-04-13 09:19:50 +00:00
using osu.Game.Input.Handlers;
2018-11-28 08:20:37 +00:00
using osu.Game.Replays;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Rulesets.Replays
{
/// <summary>
/// The ReplayHandler will take a replay and handle the propagation of updates to the input stack.
/// It handles logic of any frames which *must* be executed.
/// </summary>
public abstract class FramedReplayInputHandler<TFrame> : ReplayInputHandler
where TFrame : ReplayFrame
{
private readonly Replay replay;
protected List<ReplayFrame> Frames => replay.Frames;
public TFrame CurrentFrame
{
get
{
if (!HasFrames || !currentFrameIndex.HasValue)
return null;
return (TFrame)Frames[currentFrameIndex.Value];
}
}
public TFrame NextFrame
{
get
{
if (!HasFrames)
return null;
if (!currentFrameIndex.HasValue)
return (TFrame)Frames[0];
if (currentDirection > 0)
return currentFrameIndex == Frames.Count - 1 ? null : (TFrame)Frames[currentFrameIndex.Value + 1];
else
return currentFrameIndex == 0 ? null : (TFrame)Frames[nextFrameIndex];
}
}
2018-04-13 09:19:50 +00:00
private int? currentFrameIndex;
2018-04-13 09:19:50 +00:00
private int nextFrameIndex => currentFrameIndex.HasValue ? Math.Clamp(currentFrameIndex.Value + (currentDirection > 0 ? 1 : -1), 0, Frames.Count - 1) : 0;
2018-04-13 09:19:50 +00:00
protected FramedReplayInputHandler(Replay replay)
{
this.replay = replay;
}
private bool advanceFrame()
{
int newFrame = nextFrameIndex;
2020-05-05 01:31:11 +00:00
// ensure we aren't at an extent.
2018-04-13 09:19:50 +00:00
if (newFrame == currentFrameIndex) return false;
currentFrameIndex = newFrame;
return true;
}
private const double sixty_frame_time = 1000.0 / 60;
protected virtual double AllowedImportantTimeSpan => sixty_frame_time * 1.2;
protected double? CurrentTime { get; private set; }
2018-04-13 09:19:50 +00:00
private int currentDirection;
/// <summary>
/// When set, we will ensure frames executed by nested drawables are frame-accurate to replay data.
/// Disabling this can make replay playback smoother (useful for autoplay, currently).
/// </summary>
public bool FrameAccuratePlayback = false;
2018-04-13 09:19:50 +00:00
2020-10-27 05:47:15 +00:00
public bool HasFrames => Frames.Count > 0;
2018-04-13 09:19:50 +00:00
2019-03-30 16:33:56 +00:00
private bool inImportantSection
{
get
{
if (!HasFrames || !FrameAccuratePlayback)
return false;
2019-04-01 01:37:02 +00:00
var frame = currentDirection > 0 ? CurrentFrame : NextFrame;
2019-03-30 16:33:56 +00:00
2019-04-01 01:37:02 +00:00
if (frame == null)
2019-03-30 16:33:56 +00:00
return false;
2020-05-05 01:31:11 +00:00
return IsImportant(frame) && // a button is in a pressed state
Math.Abs(CurrentTime - NextFrame?.Time ?? 0) <= AllowedImportantTimeSpan; // the next frame is within an allowable time span
2019-03-30 16:33:56 +00:00
}
}
2018-04-13 09:19:50 +00:00
2019-03-30 16:33:56 +00:00
protected virtual bool IsImportant([NotNull] TFrame frame) => false;
2018-04-13 09:19:50 +00:00
/// <summary>
/// Update the current frame based on an incoming time value.
/// There are cases where we return a "must-use" time value that is different from the input.
/// This is to ensure accurate playback of replay data.
/// </summary>
/// <param name="time">The time which we should use for finding the current frame.</param>
/// <returns>The usable time value. If null, we should not advance time as we do not have enough data.</returns>
public override double? SetFrameFromTime(double time)
{
2020-10-28 05:35:42 +00:00
updateDirection(time);
Debug.Assert(currentDirection != 0);
2018-04-13 09:19:50 +00:00
if (HasFrames)
{
var next = NextFrame;
// check if the next frame is valid for the current playback direction.
// validity is if the next frame is equal or "earlier"
var compare = time.CompareTo(next?.Time);
if (next != null && (compare == 0 || compare == currentDirection))
{
if (advanceFrame())
return CurrentTime = CurrentFrame.Time;
}
else
2018-04-13 09:19:50 +00:00
{
// this is the case where the frame can't be advanced (in the replay).
// even so, we may be able to move the clock forward due to being at the end of the replay or
// in a section where replay accuracy doesn't matter.
// important section is always respected to block the update loop.
2018-04-13 09:19:50 +00:00
if (inImportantSection)
return null;
if (next == null)
{
// in the case we have no more frames and haven't received the full replay, block.
if (!replay.HasReceivedAllFrames)
return null;
// else allow play to end.
}
else if (next.Time.CompareTo(time) != currentDirection)
{
// in the case we have more frames, block if the next frame's time is less than the current time.
return null;
}
// if we didn't change frames, we need to ensure we are allowed to run frames in between, else return null.
2018-04-13 09:19:50 +00:00
}
}
else
{
// if we never received frames and are expecting to, block.
if (!replay.HasReceivedAllFrames)
return null;
}
2018-04-13 09:19:50 +00:00
return CurrentTime = time;
}
2020-10-28 05:35:42 +00:00
private void updateDirection(double time)
{
if (!CurrentTime.HasValue)
{
currentDirection = 1;
}
else
{
currentDirection = time.CompareTo(CurrentTime);
if (currentDirection == 0) currentDirection = 1;
}
}
2018-04-13 09:19:50 +00:00
}
}