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

Code cleanup (CPS)

This commit is contained in:
Ryuki 2022-08-24 17:12:52 +02:00
parent f3847b90fd
commit 5cddc7ed1f
No known key found for this signature in database
GPG Key ID: A353889EAEACBF49
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 ClicksPerSecondCalculator calculator = null!;
private ManualInputListener? listener;
private GameplayClockContainer gameplayClockContainer = null!;
private ManualClock manualClock = null!;
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)
{
seekAllClocks(timestamp);
listener?.AddInput();
calculator.AddTimestamp();
}
seekAllClocks(baseTime);
@ -270,18 +268,6 @@ namespace osu.Game.Tests.Visual.Gameplay
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")]
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");
}
public override Playfield Playfield => null;
public override Container Overlays => null;
public override Container FrameStableComponents => null;
public override Playfield Playfield => null!;
public override Container Overlays => null!;
public override Container FrameStableComponents => null!;
public override IFrameStableClock FrameStableClock { get; }
internal override bool FrameStablePlayback { get; set; }
public override IReadOnlyList<Mod> Mods => Array.Empty<Mod>();
public override double GameplayStartTime => 0;
public override GameplayCursorContainer Cursor => null;
public TestDrawableRuleset()
: base(new OsuRuleset())
{
}
public override GameplayCursorContainer Cursor => null!;
public TestDrawableRuleset(IFrameStableClock frameStableClock)
: this()
: base(new OsuRuleset())
{
FrameStableClock = frameStableClock;
}

View File

@ -194,20 +194,20 @@ namespace osu.Game.Rulesets.UI
var listener = new ActionListener(calculator);
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)
: base(calculator)
{
this.calculator = calculator;
}
public bool OnPressed(KeyBindingPressEvent<T> e)
{
Calculator.AddTimestamp();
calculator.AddTimestamp();
return false;
}

View File

@ -1,12 +1,10 @@
// 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.
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Timing;
using osu.Game.Rulesets.UI;
namespace osu.Game.Screens.Play.HUD.ClicksPerSecond
@ -15,89 +13,53 @@ namespace osu.Game.Screens.Play.HUD.ClicksPerSecond
{
private readonly List<double> timestamps;
private InputListener? listener;
[Resolved]
private IGameplayClock gameplayClock { get; set; } = null!;
[Resolved]
private IGameplayClock? gameplayClock { get; set; }
private DrawableRuleset drawableRuleset { get; set; } = null!;
[Resolved(canBeNull: true)]
private DrawableRuleset? drawableRuleset { get; set; }
private double rate;
public InputListener Listener
{
set
{
onResetRequested?.Invoke();
listener = value;
}
}
// The latest timestamp GC seeked. Does not affect normal gameplay
// but prevents duplicate inputs on replays.
private double latestTime = double.NegativeInfinity;
private event Action? onResetRequested;
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 int Value { get; private set; }
public ClicksPerSecondCalculator()
{
RelativeSizeAxes = Axes.Both;
timestamps = new List<double>();
onResetRequested += cleanUp;
}
private void cleanUp()
protected override void Update()
{
timestamps.Clear();
maxTime = double.NegativeInfinity;
base.Update();
// 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()
{
if (workingClock == null) return;
if (workingClock.CurrentTime >= maxTime)
// Discard inputs if current gameplay time is not the latest
// to prevent duplicate inputs
if (drawableRuleset.FrameStableClock.CurrentTime >= latestTime)
{
timestamps.Add(workingClock.CurrentTime);
maxTime = workingClock.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;
timestamps.Add(drawableRuleset.FrameStableClock.CurrentTime);
latestTime = drawableRuleset.FrameStableClock.CurrentTime;
}
}
}

View File

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