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;
|
2022-08-09 03:27:46 +08:00
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Graphics;
|
2022-08-05 10:17:01 +08:00
|
|
|
using osu.Framework.Timing;
|
|
|
|
using osu.Game.Rulesets.UI;
|
|
|
|
|
2022-08-15 02:12:11 +08:00
|
|
|
namespace osu.Game.Screens.Play.HUD.ClicksPerSecond
|
2022-08-05 10:17:01 +08:00
|
|
|
{
|
2022-08-15 02:12:11 +08:00
|
|
|
public class ClicksPerSecondCalculator : Component
|
2022-08-05 10:17:01 +08:00
|
|
|
{
|
2022-08-07 06:53:00 +08:00
|
|
|
private readonly List<double> timestamps;
|
2022-08-05 10:17:01 +08:00
|
|
|
|
2022-08-09 03:27:46 +08:00
|
|
|
private InputListener? listener;
|
2022-08-07 06:53:00 +08:00
|
|
|
|
2022-08-09 03:27:46 +08:00
|
|
|
[Resolved]
|
|
|
|
private GameplayClock? gameplayClock { get; set; }
|
|
|
|
|
|
|
|
[Resolved(canBeNull: true)]
|
|
|
|
private DrawableRuleset? drawableRuleset { get; set; }
|
2022-08-07 06:53:00 +08:00
|
|
|
|
2022-08-09 03:27:46 +08:00
|
|
|
public InputListener Listener
|
2022-08-07 06:53:00 +08:00
|
|
|
{
|
|
|
|
set
|
2022-08-05 10:17:01 +08:00
|
|
|
{
|
2022-08-07 06:53:00 +08:00
|
|
|
onResetRequested?.Invoke();
|
2022-08-09 03:27:46 +08:00
|
|
|
listener = value;
|
2022-08-05 10:17:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-09 03:27:46 +08:00
|
|
|
private 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
|
|
|
|
{
|
2022-08-15 01:09:34 +08:00
|
|
|
if (gameplayClock?.TrueGameplayRate > 0)
|
2022-08-07 06:53:00 +08:00
|
|
|
{
|
2022-08-15 01:09:34 +08:00
|
|
|
baseRate = gameplayClock.TrueGameplayRate;
|
2022-08-07 06:53:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return baseRate;
|
|
|
|
}
|
|
|
|
}
|
2022-08-05 10:17:01 +08:00
|
|
|
|
|
|
|
private double maxTime = double.NegativeInfinity;
|
|
|
|
|
2022-08-11 06:43:15 +08:00
|
|
|
public bool Ready => workingClock != null && gameplayClock != null && listener != null;
|
2022-08-05 10:17:01 +08:00
|
|
|
public int Value => timestamps.Count(isTimestampWithinSpan);
|
|
|
|
|
2022-08-15 02:12:11 +08:00
|
|
|
public ClicksPerSecondCalculator()
|
2022-08-05 10:17:01 +08:00
|
|
|
{
|
2022-08-09 03:27:46 +08:00
|
|
|
RelativeSizeAxes = Axes.Both;
|
2022-08-05 10:17:01 +08:00
|
|
|
timestamps = new List<double>();
|
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
|
|
|
}
|
|
|
|
|
2022-08-15 01:09:34 +08:00
|
|
|
public void AddTimestamp()
|
2022-08-05 10:17:01 +08:00
|
|
|
{
|
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;
|
2022-08-11 06:42:22 +08:00
|
|
|
return relativeTime > 0 && relativeTime <= span;
|
2022-08-05 10:17:01 +08:00
|
|
|
}
|
2022-08-09 03:27:46 +08:00
|
|
|
|
|
|
|
public abstract class InputListener : Component
|
|
|
|
{
|
2022-08-15 02:12:11 +08:00
|
|
|
protected ClicksPerSecondCalculator Calculator;
|
2022-08-15 01:09:34 +08:00
|
|
|
|
2022-08-15 02:12:11 +08:00
|
|
|
protected InputListener(ClicksPerSecondCalculator calculator)
|
2022-08-09 03:27:46 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
Depth = float.MinValue;
|
2022-08-15 01:09:34 +08:00
|
|
|
Calculator = calculator;
|
2022-08-09 03:27:46 +08:00
|
|
|
}
|
|
|
|
}
|
2022-08-05 10:17:01 +08:00
|
|
|
}
|
|
|
|
}
|