1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-27 15:53:19 +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[] dependencyContainer!.Children = new Drawable[]
{ {
calculator = new KeysPerSecondCalculator calculator = new KeysPerSecondCalculator(),
{
Listener = listener = new ManualInputListener()
},
new DependencyProvidingContainer new DependencyProvidingContainer
{ {
RelativeSizeAxes = Axes.Both, 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 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 private class MockFrameBasedClock : ManualClock, IFrameBasedClock

View File

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

View File

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