2018-01-05 19:21:19 +08:00
|
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
2017-03-31 14:59:53 +08:00
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
|
|
using System;
|
2017-03-31 14:32:34 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using osu.Framework.Input;
|
|
|
|
|
using osu.Framework.MathUtils;
|
|
|
|
|
using osu.Game.Input.Handlers;
|
|
|
|
|
using OpenTK;
|
|
|
|
|
using OpenTK.Input;
|
|
|
|
|
using KeyboardState = osu.Framework.Input.KeyboardState;
|
|
|
|
|
using MouseState = osu.Framework.Input.MouseState;
|
|
|
|
|
|
2017-04-18 15:05:58 +08:00
|
|
|
|
namespace osu.Game.Rulesets.Replays
|
2017-03-31 14:32:34 +08:00
|
|
|
|
{
|
|
|
|
|
/// <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>
|
2017-08-24 14:36:42 +08:00
|
|
|
|
public abstract class FramedReplayInputHandler : ReplayInputHandler
|
2017-03-31 14:32:34 +08:00
|
|
|
|
{
|
2017-03-31 14:59:53 +08:00
|
|
|
|
private readonly Replay replay;
|
|
|
|
|
|
|
|
|
|
protected List<ReplayFrame> Frames => replay.Frames;
|
2017-03-31 14:32:34 +08:00
|
|
|
|
|
2017-03-31 14:59:53 +08:00
|
|
|
|
public ReplayFrame CurrentFrame => !hasFrames ? null : Frames[currentFrameIndex];
|
|
|
|
|
public ReplayFrame NextFrame => !hasFrames ? null : Frames[nextFrameIndex];
|
2017-03-31 14:32:34 +08:00
|
|
|
|
|
|
|
|
|
private int currentFrameIndex;
|
|
|
|
|
|
2017-03-31 14:59:53 +08:00
|
|
|
|
private int nextFrameIndex => MathHelper.Clamp(currentFrameIndex + (currentDirection > 0 ? 1 : -1), 0, Frames.Count - 1);
|
2017-03-31 14:32:34 +08:00
|
|
|
|
|
2017-08-24 14:36:42 +08:00
|
|
|
|
protected FramedReplayInputHandler(Replay replay)
|
2017-03-31 14:32:34 +08:00
|
|
|
|
{
|
2017-03-31 14:59:53 +08:00
|
|
|
|
this.replay = replay;
|
2017-03-31 14:32:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool advanceFrame()
|
|
|
|
|
{
|
|
|
|
|
int newFrame = nextFrameIndex;
|
|
|
|
|
|
|
|
|
|
//ensure we aren't at an extent.
|
|
|
|
|
if (newFrame == currentFrameIndex) return false;
|
|
|
|
|
|
|
|
|
|
currentFrameIndex = newFrame;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetPosition(Vector2 pos)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-24 14:36:42 +08:00
|
|
|
|
protected Vector2? Position
|
2017-03-31 14:32:34 +08:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (!hasFrames)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return Interpolation.ValueAt(currentTime, CurrentFrame.Position, NextFrame.Position, CurrentFrame.Time, NextFrame.Time);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-24 14:36:42 +08:00
|
|
|
|
public override List<InputState> GetPendingStates() => new List<InputState>();
|
2017-03-31 14:32:34 +08:00
|
|
|
|
|
2017-03-31 14:59:53 +08:00
|
|
|
|
public bool AtLastFrame => currentFrameIndex == Frames.Count - 1;
|
2017-03-31 14:32:34 +08:00
|
|
|
|
public bool AtFirstFrame => currentFrameIndex == 0;
|
|
|
|
|
|
|
|
|
|
public Vector2 Size => new Vector2(512, 384);
|
|
|
|
|
|
|
|
|
|
private const double sixty_frame_time = 1000.0 / 60;
|
|
|
|
|
|
|
|
|
|
private double currentTime;
|
|
|
|
|
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 = true;
|
|
|
|
|
|
2017-03-31 14:59:53 +08:00
|
|
|
|
private bool hasFrames => Frames.Count > 0;
|
2017-03-31 14:32:34 +08:00
|
|
|
|
|
|
|
|
|
private bool inImportantSection =>
|
|
|
|
|
FrameAccuratePlayback &&
|
|
|
|
|
//a button is in a pressed state
|
|
|
|
|
((currentDirection > 0 ? CurrentFrame : NextFrame)?.IsImportant ?? false) &&
|
|
|
|
|
//the next frame is within an allowable time span
|
|
|
|
|
Math.Abs(currentTime - NextFrame?.Time ?? 0) <= sixty_frame_time * 1.2;
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
|
|
|
|
currentDirection = time.CompareTo(currentTime);
|
|
|
|
|
if (currentDirection == 0) currentDirection = 1;
|
|
|
|
|
|
|
|
|
|
if (hasFrames)
|
|
|
|
|
{
|
2018-02-01 17:49:07 +08:00
|
|
|
|
// check if the next frame is in the "future" for the current playback direction
|
|
|
|
|
if (currentDirection != time.CompareTo(NextFrame.Time))
|
|
|
|
|
{
|
|
|
|
|
// if we didn't change frames, we need to ensure we are allowed to run frames in between, else return null.
|
|
|
|
|
if (inImportantSection)
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
else if (advanceFrame())
|
2017-11-10 21:11:25 +08:00
|
|
|
|
{
|
|
|
|
|
// If going backwards, we need to execute once _before_ the frame time to reverse any judgements
|
|
|
|
|
// that would occur as a result of this frame in forward playback
|
|
|
|
|
if (currentDirection == -1)
|
|
|
|
|
return currentTime = CurrentFrame.Time - 1;
|
2017-03-31 14:32:34 +08:00
|
|
|
|
return currentTime = CurrentFrame.Time;
|
2017-11-10 21:11:25 +08:00
|
|
|
|
}
|
2017-03-31 14:32:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return currentTime = time;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected class ReplayMouseState : MouseState
|
|
|
|
|
{
|
2017-08-24 14:36:42 +08:00
|
|
|
|
public ReplayMouseState(Vector2 position)
|
2017-03-31 14:32:34 +08:00
|
|
|
|
{
|
|
|
|
|
Position = position;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected class ReplayKeyboardState : KeyboardState
|
|
|
|
|
{
|
|
|
|
|
public ReplayKeyboardState(List<Key> keys)
|
|
|
|
|
{
|
|
|
|
|
Keys = keys;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-10 00:24:50 +08:00
|
|
|
|
}
|