Code cleanup (CPS)

This commit is contained in:
Ryuki
2022-08-24 17:12:52 +02:00
parent f3847b90fd
commit 5cddc7ed1f
4 changed files with 42 additions and 101 deletions

View File

@ -29,7 +29,6 @@ namespace osu.Game.Tests.Visual.Gameplay
{ {
private DependencyProvidingContainer dependencyContainer = null!; private DependencyProvidingContainer dependencyContainer = null!;
private ClicksPerSecondCalculator calculator = null!; private ClicksPerSecondCalculator calculator = null!;
private ManualInputListener? listener;
private GameplayClockContainer gameplayClockContainer = null!; private GameplayClockContainer gameplayClockContainer = null!;
private ManualClock manualClock = null!; private ManualClock manualClock = null!;
private DrawableRuleset? drawableRuleset; private DrawableRuleset? drawableRuleset;
@ -151,7 +150,6 @@ namespace osu.Game.Tests.Visual.Gameplay
} }
} }
}; };
calculator.Listener = listener = new ManualInputListener(calculator);
}); });
} }
@ -189,7 +187,7 @@ namespace osu.Game.Tests.Visual.Gameplay
foreach (double timestamp in inputs) foreach (double timestamp in inputs)
{ {
seekAllClocks(timestamp); seekAllClocks(timestamp);
listener?.AddInput(); calculator.AddTimestamp();
} }
seekAllClocks(baseTime); seekAllClocks(baseTime);
@ -270,18 +268,6 @@ namespace osu.Game.Tests.Visual.Gameplay
public IBindable<bool> WaitingOnFrames => new Bindable<bool>(); public IBindable<bool> WaitingOnFrames => new Bindable<bool>();
} }
private class ManualInputListener : ClicksPerSecondCalculator.InputListener
{
public void AddInput() => Calculator.AddTimestamp();
public ManualInputListener(ClicksPerSecondCalculator calculator)
: base(calculator)
{
}
}
#nullable disable
[SuppressMessage("ReSharper", "UnassignedGetOnlyAutoProperty")] [SuppressMessage("ReSharper", "UnassignedGetOnlyAutoProperty")]
private class TestDrawableRuleset : DrawableRuleset private class TestDrawableRuleset : DrawableRuleset
{ {
@ -299,24 +285,19 @@ namespace osu.Game.Tests.Visual.Gameplay
remove => throw new InvalidOperationException($"{nameof(RevertResult)} operations not supported in test context"); remove => throw new InvalidOperationException($"{nameof(RevertResult)} operations not supported in test context");
} }
public override Playfield Playfield => null; public override Playfield Playfield => null!;
public override Container Overlays => null; public override Container Overlays => null!;
public override Container FrameStableComponents => null; public override Container FrameStableComponents => null!;
public override IFrameStableClock FrameStableClock { get; } public override IFrameStableClock FrameStableClock { get; }
internal override bool FrameStablePlayback { get; set; } internal override bool FrameStablePlayback { get; set; }
public override IReadOnlyList<Mod> Mods => Array.Empty<Mod>(); public override IReadOnlyList<Mod> Mods => Array.Empty<Mod>();
public override double GameplayStartTime => 0; public override double GameplayStartTime => 0;
public override GameplayCursorContainer Cursor => null; public override GameplayCursorContainer Cursor => null!;
public TestDrawableRuleset()
: base(new OsuRuleset())
{
}
public TestDrawableRuleset(IFrameStableClock frameStableClock) public TestDrawableRuleset(IFrameStableClock frameStableClock)
: this() : base(new OsuRuleset())
{ {
FrameStableClock = frameStableClock; FrameStableClock = frameStableClock;
} }

View File

@ -194,20 +194,20 @@ namespace osu.Game.Rulesets.UI
var listener = new ActionListener(calculator); var listener = new ActionListener(calculator);
KeyBindingContainer.Add(listener); KeyBindingContainer.Add(listener);
calculator.Listener = listener;
} }
private class ActionListener : ClicksPerSecondCalculator.InputListener, IKeyBindingHandler<T> private class ActionListener : Component, IKeyBindingHandler<T>
{ {
private readonly ClicksPerSecondCalculator calculator;
public ActionListener(ClicksPerSecondCalculator calculator) public ActionListener(ClicksPerSecondCalculator calculator)
: base(calculator)
{ {
this.calculator = calculator;
} }
public bool OnPressed(KeyBindingPressEvent<T> e) public bool OnPressed(KeyBindingPressEvent<T> e)
{ {
Calculator.AddTimestamp(); calculator.AddTimestamp();
return false; return false;
} }

View File

@ -1,12 +1,10 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Timing;
using osu.Game.Rulesets.UI; using osu.Game.Rulesets.UI;
namespace osu.Game.Screens.Play.HUD.ClicksPerSecond namespace osu.Game.Screens.Play.HUD.ClicksPerSecond
@ -15,89 +13,53 @@ namespace osu.Game.Screens.Play.HUD.ClicksPerSecond
{ {
private readonly List<double> timestamps; private readonly List<double> timestamps;
private InputListener? listener; [Resolved]
private IGameplayClock gameplayClock { get; set; } = null!;
[Resolved] [Resolved]
private IGameplayClock? gameplayClock { get; set; } private DrawableRuleset drawableRuleset { get; set; } = null!;
[Resolved(canBeNull: true)] private double rate;
private DrawableRuleset? drawableRuleset { get; set; }
public InputListener Listener // The latest timestamp GC seeked. Does not affect normal gameplay
{ // but prevents duplicate inputs on replays.
set private double latestTime = double.NegativeInfinity;
{
onResetRequested?.Invoke();
listener = value;
}
}
private event Action? onResetRequested; public int Value { get; private set; }
private IClock? workingClock => drawableRuleset?.FrameStableClock;
private double baseRate;
private double rate
{
get
{
if (gameplayClock?.TrueGameplayRate > 0)
{
baseRate = gameplayClock.TrueGameplayRate;
}
return baseRate;
}
}
private double maxTime = double.NegativeInfinity;
public bool Ready => workingClock != null && gameplayClock != null && listener != null;
public int Value => timestamps.Count(isTimestampWithinSpan);
public ClicksPerSecondCalculator() public ClicksPerSecondCalculator()
{ {
RelativeSizeAxes = Axes.Both; RelativeSizeAxes = Axes.Both;
timestamps = new List<double>(); timestamps = new List<double>();
onResetRequested += cleanUp;
} }
private void cleanUp() protected override void Update()
{ {
timestamps.Clear(); base.Update();
maxTime = double.NegativeInfinity;
// When pausing in replays (using the space bar) GC.TrueGameplayRate returns 0
// To prevent CPS value being 0, we store and use the last non-zero TrueGameplayRate
if (gameplayClock.TrueGameplayRate > 0)
{
rate = gameplayClock.TrueGameplayRate;
}
Value = timestamps.Count(timestamp =>
{
double window = 1000 * rate;
double relativeTime = drawableRuleset.FrameStableClock.CurrentTime - timestamp;
return relativeTime > 0 && relativeTime <= window;
});
} }
public void AddTimestamp() public void AddTimestamp()
{ {
if (workingClock == null) return; // Discard inputs if current gameplay time is not the latest
// to prevent duplicate inputs
if (workingClock.CurrentTime >= maxTime) if (drawableRuleset.FrameStableClock.CurrentTime >= latestTime)
{ {
timestamps.Add(workingClock.CurrentTime); timestamps.Add(drawableRuleset.FrameStableClock.CurrentTime);
maxTime = workingClock.CurrentTime; latestTime = drawableRuleset.FrameStableClock.CurrentTime;
}
}
private bool isTimestampWithinSpan(double timestamp)
{
if (workingClock == null) return false;
double span = 1000 * rate;
double relativeTime = workingClock.CurrentTime - timestamp;
return relativeTime > 0 && relativeTime <= span;
}
public abstract class InputListener : Component
{
protected ClicksPerSecondCalculator Calculator;
protected InputListener(ClicksPerSecondCalculator calculator)
{
RelativeSizeAxes = Axes.Both;
Depth = float.MinValue;
Calculator = calculator;
} }
} }
} }

View File

@ -16,9 +16,7 @@ namespace osu.Game.Screens.Play.HUD.ClicksPerSecond
{ {
public class ClicksPerSecondCounter : RollingCounter<int>, ISkinnableDrawable public class ClicksPerSecondCounter : RollingCounter<int>, ISkinnableDrawable
{ {
private const float alpha_when_invalid = 0.3f; [Resolved]
[Resolved(canBeNull: false)]
private ClicksPerSecondCalculator calculator { get; set; } = null!; private ClicksPerSecondCalculator calculator { get; set; } = null!;
protected override double RollingDuration => 350; protected override double RollingDuration => 350;
@ -40,7 +38,7 @@ namespace osu.Game.Screens.Play.HUD.ClicksPerSecond
{ {
base.Update(); base.Update();
Current.Value = calculator.Ready ? calculator.Value : 0; Current.Value = calculator.Value;
} }
protected override IHasText CreateText() => new TextComponent(); protected override IHasText CreateText() => new TextComponent();