1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-24 06:33:21 +08:00
osu-lazer/osu.Game/Input/Handlers/ReplayInputHandler.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

120 lines
4.4 KiB
C#
Raw Normal View History

// 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 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Input.Handlers;
2018-07-21 10:38:28 +08:00
using osu.Framework.Input.StateChanges;
using osu.Framework.Input.StateChanges.Events;
2018-07-21 10:38:28 +08:00
using osu.Framework.Input.States;
using osu.Framework.Platform;
using osu.Game.Rulesets.Replays;
using osu.Game.Rulesets.UI;
2018-11-20 15:51:59 +08:00
using osuTK;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Input.Handlers
{
public abstract class ReplayInputHandler : InputHandler
{
/// <summary>
/// A function that converts coordinates from gamefield to screen space.
/// </summary>
public Func<Vector2, Vector2> GamefieldToScreenSpace { protected get; set; }
2018-04-13 17:19:50 +08: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>
2017-03-06 13:15:43 +08:00
/// <returns>The usable time value. If null, we should not advance time as we do not have enough data.</returns>
public abstract double? SetFrameFromTime(double time);
2018-04-13 17:19:50 +08:00
public override bool Initialize(GameHost host) => true;
2018-04-13 17:19:50 +08:00
2018-06-11 22:00:26 +08:00
public class ReplayState<T> : IInput
where T : struct
{
public List<T> PressedActions;
2018-04-13 17:19:50 +08:00
2018-06-11 22:00:26 +08:00
public void Apply(InputState state, IInputStateChangeHandler handler)
{
if (!(state is RulesetInputManagerInputState<T> inputState))
throw new InvalidOperationException($"{nameof(ReplayState<T>)} should only be applied to a {nameof(RulesetInputManagerInputState<T>)}");
T[] released = Array.Empty<T>();
T[] pressed = Array.Empty<T>();
var lastPressed = inputState.LastReplayState?.PressedActions;
if (lastPressed == null || lastPressed.Count == 0)
{
pressed = PressedActions.ToArray();
}
else if (PressedActions.Count == 0)
{
released = lastPressed.ToArray();
}
else if (!lastPressed.SequenceEqual(PressedActions))
{
released = lastPressed.Except(PressedActions).ToArray();
pressed = PressedActions.Except(lastPressed).ToArray();
}
inputState.LastReplayState = this;
handler.HandleInputStateChange(new ReplayStateChangeEvent<T>(state, this, released, pressed));
}
}
public class ReplayStateChangeEvent<T> : InputStateChangeEvent
{
public readonly T[] ReleasedActions;
public readonly T[] PressedActions;
public ReplayStateChangeEvent(InputState state, IInput input, T[] releasedActions, T[] pressedActions)
: base(state, input)
{
ReleasedActions = releasedActions;
PressedActions = pressedActions;
}
}
/// <summary>
/// An <see cref="IInput"/> that is triggered when a frame containing replay statistics arrives.
/// </summary>
public class ReplayStatisticsFrameInput : IInput
{
/// <summary>
/// The frame containing the statistics.
/// </summary>
public ReplayFrame Frame;
public void Apply(InputState state, IInputStateChangeHandler handler)
{
handler.HandleInputStateChange(new ReplayStatisticsFrameEvent(state, this, Frame));
}
}
/// <summary>
/// An <see cref="InputStateChangeEvent"/> that is triggered when a frame containing replay statistics arrives.
/// </summary>
public class ReplayStatisticsFrameEvent : InputStateChangeEvent
{
/// <summary>
/// The frame containing the statistics.
/// </summary>
public readonly ReplayFrame Frame;
public ReplayStatisticsFrameEvent(InputState state, IInput input, ReplayFrame frame)
: base(state, input)
{
Frame = frame;
}
}
}
2018-01-05 19:21:19 +08:00
}