2022-08-05 10:17:01 +08:00
|
|
|
// 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.Timing;
|
|
|
|
using osu.Game.Rulesets.Mods;
|
|
|
|
using osu.Game.Rulesets.UI;
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Play.HUD.KPSCounter
|
|
|
|
{
|
2022-08-07 06:53:00 +08:00
|
|
|
public class KeysPerSecondCalculator
|
2022-08-05 10:17:01 +08:00
|
|
|
{
|
|
|
|
public static void AddInput()
|
|
|
|
{
|
2022-08-07 06:53:00 +08:00
|
|
|
onNewInput?.Invoke();
|
2022-08-05 10:17:01 +08:00
|
|
|
}
|
|
|
|
|
2022-08-07 06:53:00 +08:00
|
|
|
private readonly List<double> timestamps;
|
|
|
|
private GameplayClock? gameplayClock;
|
|
|
|
private DrawableRuleset? drawableRuleset;
|
2022-08-05 10:17:01 +08:00
|
|
|
|
2022-08-07 06:53:00 +08:00
|
|
|
public GameplayClock? GameplayClock
|
|
|
|
{
|
|
|
|
get => gameplayClock;
|
|
|
|
set
|
2022-08-05 10:17:01 +08:00
|
|
|
{
|
2022-08-07 06:53:00 +08:00
|
|
|
onResetRequested?.Invoke();
|
|
|
|
|
|
|
|
if (value != null)
|
|
|
|
{
|
|
|
|
gameplayClock = value;
|
|
|
|
}
|
2022-08-05 10:17:01 +08:00
|
|
|
}
|
2022-08-07 06:53:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public DrawableRuleset? DrawableRuleset
|
|
|
|
{
|
|
|
|
get => drawableRuleset;
|
|
|
|
set
|
2022-08-05 10:17:01 +08:00
|
|
|
{
|
2022-08-07 06:53:00 +08:00
|
|
|
onResetRequested?.Invoke();
|
|
|
|
|
|
|
|
if (value != null)
|
|
|
|
{
|
|
|
|
drawableRuleset = value;
|
|
|
|
baseRate = (drawableRuleset.Mods.FirstOrDefault(m => m is ModRateAdjust) as ModRateAdjust)?.SpeedChange.Value
|
|
|
|
?? 1;
|
|
|
|
}
|
2022-08-05 10:17:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 06:53:00 +08:00
|
|
|
private static event Action? onNewInput;
|
|
|
|
private static event Action? onResetRequested;
|
2022-08-05 10:17:01 +08:00
|
|
|
|
2022-08-07 06:53:00 +08:00
|
|
|
private IClock? workingClock => drawableRuleset?.FrameStableClock;
|
2022-08-05 10:17:01 +08:00
|
|
|
|
2022-08-07 06:53:00 +08:00
|
|
|
private double baseRate;
|
2022-08-05 10:17:01 +08:00
|
|
|
|
2022-08-07 06:53:00 +08:00
|
|
|
private double rate
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (gameplayClock != null)
|
|
|
|
{
|
|
|
|
if (gameplayClock.TrueGameplayRate > 0)
|
|
|
|
{
|
|
|
|
baseRate = gameplayClock.TrueGameplayRate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return baseRate;
|
|
|
|
}
|
|
|
|
}
|
2022-08-05 10:17:01 +08:00
|
|
|
|
|
|
|
private double maxTime = double.NegativeInfinity;
|
|
|
|
|
|
|
|
public bool Ready => workingClock != null && gameplayClock != null;
|
|
|
|
public int Value => timestamps.Count(isTimestampWithinSpan);
|
|
|
|
|
2022-08-07 06:53:00 +08:00
|
|
|
public KeysPerSecondCalculator()
|
2022-08-05 10:17:01 +08:00
|
|
|
{
|
|
|
|
timestamps = new List<double>();
|
|
|
|
onNewInput += addTimestamp;
|
2022-08-07 06:53:00 +08:00
|
|
|
onResetRequested += cleanUp;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void cleanUp()
|
|
|
|
{
|
|
|
|
timestamps.Clear();
|
|
|
|
maxTime = double.NegativeInfinity;
|
2022-08-05 10:17:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void addTimestamp()
|
|
|
|
{
|
2022-08-07 06:53:00 +08:00
|
|
|
if (workingClock == null) return;
|
|
|
|
|
|
|
|
if (workingClock.CurrentTime >= maxTime)
|
2022-08-05 10:17:01 +08:00
|
|
|
{
|
|
|
|
timestamps.Add(workingClock.CurrentTime);
|
|
|
|
maxTime = workingClock.CurrentTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool isTimestampWithinSpan(double timestamp)
|
|
|
|
{
|
2022-08-07 06:53:00 +08:00
|
|
|
if (workingClock == null) return false;
|
2022-08-05 10:17:01 +08:00
|
|
|
|
|
|
|
double span = 1000 * rate;
|
|
|
|
double relativeTime = workingClock.CurrentTime - timestamp;
|
|
|
|
return relativeTime >= 0 && relativeTime <= span;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|