// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. #nullable disable using System; using System.Collections.Generic; using System.Linq; using osu.Framework.Input.Handlers; using osu.Framework.Input.StateChanges; using osu.Framework.Input.StateChanges.Events; using osu.Framework.Input.States; using osu.Framework.Platform; using osu.Game.Rulesets.Replays; using osu.Game.Rulesets.UI; using osuTK; namespace osu.Game.Input.Handlers { public abstract class ReplayInputHandler : InputHandler { /// /// A function that converts coordinates from gamefield to screen space. /// public Func GamefieldToScreenSpace { protected get; set; } /// /// 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. /// /// The time which we should use for finding the current frame. /// The usable time value. If null, we should not advance time as we do not have enough data. public abstract double? SetFrameFromTime(double time); public override bool Initialize(GameHost host) => true; public class ReplayState : IInput where T : struct { public List PressedActions; public void Apply(InputState state, IInputStateChangeHandler handler) { if (!(state is RulesetInputManagerInputState inputState)) throw new InvalidOperationException($"{nameof(ReplayState)} should only be applied to a {nameof(RulesetInputManagerInputState)}"); T[] released = Array.Empty(); T[] pressed = Array.Empty(); 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(state, this, released, pressed)); } } public class ReplayStateChangeEvent : 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; } } /// /// An that is triggered when a frame containing replay statistics arrives. /// public class ReplayStatisticsFrameInput : IInput { /// /// The frame containing the statistics. /// public ReplayFrame Frame; public void Apply(InputState state, IInputStateChangeHandler handler) { handler.HandleInputStateChange(new ReplayStatisticsFrameEvent(state, this, Frame)); } } /// /// An that is triggered when a frame containing replay statistics arrives. /// public class ReplayStatisticsFrameEvent : InputStateChangeEvent { /// /// The frame containing the statistics. /// public readonly ReplayFrame Frame; public ReplayStatisticsFrameEvent(InputState state, IInput input, ReplayFrame frame) : base(state, input) { Frame = frame; } } } }