Use DI to provide dependencies for KPS Calculator and improve input

gathering

KPS Calculator now uses DI to retrieve the clocks. Using `HUDOverlay` it
is now cached for `KeysPerSecondCounter`s to resolve it. This also
allows to make an "Attach" flow like `KeyCounter`.
This commit is contained in:
Ryuki
2022-08-08 21:27:46 +02:00
parent b2557a8d2d
commit b52a07c16a
6 changed files with 90 additions and 54 deletions

View File

@ -20,11 +20,12 @@ using osu.Game.Input.Bindings;
using osu.Game.Input.Handlers;
using osu.Game.Rulesets.Scoring;
using osu.Game.Screens.Play;
using osu.Game.Screens.Play.HUD.KPSCounter;
using static osu.Game.Input.Handlers.ReplayInputHandler;
namespace osu.Game.Rulesets.UI
{
public abstract class RulesetInputManager<T> : PassThroughInputManager, ICanAttachKeyCounter, IHasReplayHandler, IHasRecordingHandler
public abstract class RulesetInputManager<T> : PassThroughInputManager, ICanAttachKeyCounter, IHasReplayHandler, IHasRecordingHandler, ICanAttachKpsCalculator
where T : struct
{
public readonly KeyBindingContainer<T> KeyBindingContainer;
@ -186,6 +187,35 @@ namespace osu.Game.Rulesets.UI
#endregion
#region KPS Counter Attachment
public void Attach(KeysPerSecondCalculator kps)
{
var listener = new ActionListener();
KeyBindingContainer.Add(listener);
kps.Listener = listener;
}
public class ActionListener : KeysPerSecondCalculator.InputListener, IKeyBindingHandler<T>
{
public override event Action OnNewInput;
public bool OnPressed(KeyBindingPressEvent<T> e)
{
OnNewInput?.Invoke();
return false;
}
public void OnReleased(KeyBindingReleaseEvent<T> e)
{
}
}
#endregion
protected virtual KeyBindingContainer<T> CreateKeyBindingContainer(RulesetInfo ruleset, int variant, SimultaneousBindingMode unique)
=> new RulesetKeyBindingContainer(ruleset, variant, unique);
@ -229,6 +259,11 @@ namespace osu.Game.Rulesets.UI
void Attach(KeyCounterDisplay keyCounter);
}
public interface ICanAttachKpsCalculator
{
void Attach(KeysPerSecondCalculator keysPerSecondCalculator);
}
public class RulesetInputManagerInputState<T> : InputState
where T : struct
{