1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 21:27:24 +08:00

Use new InputStateChangeEvent for RulesetInputManager

This commit is contained in:
ekrctb 2018-09-18 17:05:26 +09:00
parent 492b5edb02
commit c8e9d9375f
2 changed files with 37 additions and 23 deletions

View File

@ -3,10 +3,13 @@
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.UI;
using OpenTK;
namespace osu.Game.Input.Handlers
@ -40,7 +43,29 @@ namespace osu.Game.Input.Handlers
public void Apply(InputState state, IInputStateChangeHandler handler)
{
handler.HandleCustomInput(state, this);
if (!(state is RulesetInputManagerInputState<T> inputState))
throw new InvalidOperationException($"{nameof(ReplayState<T>)} should only be applied to a {nameof(RulesetInputManagerInputState<T>)}");
var lastPressed = inputState.LastReplayState?.PressedActions ?? new List<T>();
var released = lastPressed.Except(PressedActions).ToArray();
var 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;
}
}
}

View File

@ -10,7 +10,9 @@ using osu.Framework.Graphics.Containers;
using osu.Framework.Input;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.EventArgs;
using osu.Framework.Input.Events;
using osu.Framework.Input.StateChanges;
using osu.Framework.Input.StateChanges.Events;
using osu.Framework.Input.States;
using osu.Framework.Timing;
using osu.Game.Configuration;
@ -56,33 +58,20 @@ namespace osu.Game.Rulesets.UI
#region Action mapping (for replays)
private List<T> lastPressedActions = new List<T>();
public override void HandleCustomInput(InputState state, IInput input)
public override void HandleInputStateChange(InputStateChangeEvent inputStateChange)
{
if (!(input is ReplayState<T> replayState))
if (inputStateChange is ReplayStateChangeEvent<T> replayStateChanged)
{
base.HandleCustomInput(state, input);
return;
}
foreach (var action in replayStateChanged.ReleasedActions)
KeyBindingContainer.TriggerReleased(action);
if (state is RulesetInputManagerInputState<T> inputState)
foreach (var action in replayStateChanged.PressedActions)
KeyBindingContainer.TriggerPressed(action);
}
else
{
inputState.LastReplayState = replayState;
base.HandleInputStateChange(inputStateChange);
}
// Here we handle states specifically coming from a replay source.
// These have extra action information rather than keyboard keys or mouse buttons.
List<T> newActions = replayState.PressedActions;
foreach (var released in lastPressedActions.Except(newActions))
KeyBindingContainer.TriggerReleased(released);
foreach (var pressed in newActions.Except(lastPressedActions))
KeyBindingContainer.TriggerPressed(pressed);
lastPressedActions = newActions;
}
#endregion