1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 17:07:38 +08:00

Make ActionListener and KeysPerSecondCalculator not rely on events to add timestamps

This commit is contained in:
Ryuki 2022-08-14 19:09:34 +02:00
parent d5f10cbb9d
commit 9dc806506e
No known key found for this signature in database
GPG Key ID: A353889EAEACBF49
3 changed files with 21 additions and 28 deletions

View File

@ -61,10 +61,7 @@ namespace osu.Game.Tests.Visual.Gameplay
{
dependencyContainer!.Children = new Drawable[]
{
calculator = new KeysPerSecondCalculator
{
Listener = listener = new ManualInputListener()
},
calculator = new KeysPerSecondCalculator(),
new DependencyProvidingContainer
{
RelativeSizeAxes = Axes.Both,
@ -77,6 +74,7 @@ namespace osu.Game.Tests.Visual.Gameplay
}
}
};
calculator!.Listener = listener = new ManualInputListener(calculator!);
});
}
@ -208,9 +206,12 @@ namespace osu.Game.Tests.Visual.Gameplay
private class ManualInputListener : KeysPerSecondCalculator.InputListener
{
public override event Action? OnNewInput;
public void AddInput() => Calculator.AddTimestamp();
public void AddInput() => OnNewInput?.Invoke();
public ManualInputListener(KeysPerSecondCalculator calculator)
: base(calculator)
{
}
}
private class MockFrameBasedClock : ManualClock, IFrameBasedClock

View File

@ -193,7 +193,7 @@ namespace osu.Game.Rulesets.UI
{
if (calculator == null) return;
var listener = new ActionListener();
var listener = new ActionListener(calculator);
KeyBindingContainer.Add(listener);
@ -202,11 +202,9 @@ namespace osu.Game.Rulesets.UI
public class ActionListener : KeysPerSecondCalculator.InputListener, IKeyBindingHandler<T>
{
public override event Action OnNewInput;
public bool OnPressed(KeyBindingPressEvent<T> e)
{
OnNewInput?.Invoke();
Calculator.AddTimestamp();
return false;
}
@ -214,6 +212,11 @@ namespace osu.Game.Rulesets.UI
public void OnReleased(KeyBindingReleaseEvent<T> e)
{
}
public ActionListener(KeysPerSecondCalculator calculator)
: base(calculator)
{
}
}
#endregion

View File

@ -29,7 +29,6 @@ namespace osu.Game.Screens.Play.HUD.KeysPerSecond
{
onResetRequested?.Invoke();
listener = value;
listener.OnNewInput += addTimestamp;
}
}
@ -43,13 +42,10 @@ namespace osu.Game.Screens.Play.HUD.KeysPerSecond
{
get
{
if (gameplayClock != null)
{
if (gameplayClock.TrueGameplayRate > 0)
if (gameplayClock?.TrueGameplayRate > 0)
{
baseRate = gameplayClock.TrueGameplayRate;
}
}
return baseRate;
}
@ -71,12 +67,9 @@ namespace osu.Game.Screens.Play.HUD.KeysPerSecond
{
timestamps.Clear();
maxTime = double.NegativeInfinity;
if (listener != null)
listener.OnNewInput -= addTimestamp;
}
private void addTimestamp()
public void AddTimestamp()
{
if (workingClock == null) return;
@ -96,20 +89,16 @@ namespace osu.Game.Screens.Play.HUD.KeysPerSecond
return relativeTime > 0 && relativeTime <= span;
}
~KeysPerSecondCalculator()
{
cleanUp();
}
public abstract class InputListener : Component
{
protected InputListener()
protected KeysPerSecondCalculator Calculator;
protected InputListener(KeysPerSecondCalculator calculator)
{
RelativeSizeAxes = Axes.Both;
Depth = float.MinValue;
Calculator = calculator;
}
public abstract event Action? OnNewInput;
}
}
}