-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19599 from ItsShamed/kps
Add clicks/second counter to HUD
- Loading branch information
Showing
11 changed files
with
352 additions
and
12 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
osu.Game.Tests/Visual/Gameplay/TestSceneClicksPerSecondCalculator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// 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 NUnit.Framework; | ||
using osu.Framework.Audio; | ||
using osu.Framework.Bindables; | ||
using osu.Framework.Graphics; | ||
using osu.Framework.Testing; | ||
using osu.Framework.Timing; | ||
using osu.Game.Screens.Play; | ||
using osu.Game.Screens.Play.HUD.ClicksPerSecond; | ||
using osuTK; | ||
|
||
namespace osu.Game.Tests.Visual.Gameplay | ||
{ | ||
public class TestSceneClicksPerSecondCalculator : OsuTestScene | ||
{ | ||
private ClicksPerSecondCalculator calculator = null!; | ||
|
||
private TestGameplayClock manualGameplayClock = null!; | ||
|
||
[SetUpSteps] | ||
public void SetUpSteps() | ||
{ | ||
AddStep("create components", () => | ||
{ | ||
manualGameplayClock = new TestGameplayClock(); | ||
Child = new DependencyProvidingContainer | ||
{ | ||
RelativeSizeAxes = Axes.Both, | ||
CachedDependencies = new (Type, object)[] { (typeof(IGameplayClock), manualGameplayClock) }, | ||
Children = new Drawable[] | ||
{ | ||
calculator = new ClicksPerSecondCalculator(), | ||
new DependencyProvidingContainer | ||
{ | ||
RelativeSizeAxes = Axes.Both, | ||
CachedDependencies = new (Type, object)[] { (typeof(ClicksPerSecondCalculator), calculator) }, | ||
Child = new ClicksPerSecondCounter | ||
{ | ||
Anchor = Anchor.Centre, | ||
Origin = Anchor.Centre, | ||
Scale = new Vector2(5), | ||
} | ||
} | ||
}, | ||
}; | ||
}); | ||
} | ||
|
||
[Test] | ||
public void TestBasicConsistency() | ||
{ | ||
seek(1000); | ||
AddStep("add inputs in past", () => addInputs(new double[] { 0, 100, 200, 300, 400, 500, 600, 700, 800, 900 })); | ||
checkClicksPerSecondValue(10); | ||
} | ||
|
||
[Test] | ||
public void TestRateAdjustConsistency() | ||
{ | ||
seek(1000); | ||
AddStep("add inputs in past", () => addInputs(new double[] { 0, 100, 200, 300, 400, 500, 600, 700, 800, 900 })); | ||
checkClicksPerSecondValue(10); | ||
AddStep("set rate 0.5x", () => manualGameplayClock.TrueGameplayRate = 0.5); | ||
checkClicksPerSecondValue(5); | ||
} | ||
|
||
[Test] | ||
public void TestInputsDiscardedOnRewind() | ||
{ | ||
seek(1000); | ||
AddStep("add inputs in past", () => addInputs(new double[] { 0, 100, 200, 300, 400, 500, 600, 700, 800, 900 })); | ||
checkClicksPerSecondValue(10); | ||
seek(500); | ||
checkClicksPerSecondValue(6); | ||
seek(1000); | ||
checkClicksPerSecondValue(6); | ||
} | ||
|
||
private void checkClicksPerSecondValue(int i) => AddAssert("clicks/s is correct", () => calculator.Value, () => Is.EqualTo(i)); | ||
|
||
private void seekClockImmediately(double time) => manualGameplayClock.CurrentTime = time; | ||
|
||
private void seek(double time) => AddStep($"Seek to {time}ms", () => seekClockImmediately(time)); | ||
|
||
private void addInputs(IEnumerable<double> inputs) | ||
{ | ||
double baseTime = manualGameplayClock.CurrentTime; | ||
|
||
foreach (double timestamp in inputs) | ||
{ | ||
seekClockImmediately(timestamp); | ||
calculator.AddInputTimestamp(); | ||
} | ||
|
||
seekClockImmediately(baseTime); | ||
} | ||
|
||
private class TestGameplayClock : IGameplayClock | ||
{ | ||
public double CurrentTime { get; set; } | ||
|
||
public double Rate => 1; | ||
|
||
public bool IsRunning => true; | ||
|
||
public double TrueGameplayRate { set => adjustableAudioComponent.Tempo.Value = value; } | ||
|
||
private readonly AudioAdjustments adjustableAudioComponent = new AudioAdjustments(); | ||
|
||
public void ProcessFrame() | ||
{ | ||
} | ||
|
||
public double ElapsedFrameTime => throw new NotImplementedException(); | ||
public double FramesPerSecond => throw new NotImplementedException(); | ||
public FrameTimeInfo TimeInfo => throw new NotImplementedException(); | ||
public double StartTime => throw new NotImplementedException(); | ||
|
||
public IAdjustableAudioComponent AdjustmentsFromMods => adjustableAudioComponent; | ||
|
||
public IEnumerable<double> NonGameplayAdjustments => throw new NotImplementedException(); | ||
public IBindable<bool> IsPaused => throw new NotImplementedException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
osu.Game/Screens/Play/HUD/ClicksPerSecond/ClicksPerSecondCalculator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// 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.Collections.Generic; | ||
using osu.Framework.Allocation; | ||
using osu.Framework.Graphics; | ||
using osu.Game.Rulesets.UI; | ||
|
||
namespace osu.Game.Screens.Play.HUD.ClicksPerSecond | ||
{ | ||
public class ClicksPerSecondCalculator : Component | ||
{ | ||
private readonly List<double> timestamps = new List<double>(); | ||
|
||
[Resolved] | ||
private IGameplayClock gameplayClock { get; set; } = null!; | ||
|
||
[Resolved(canBeNull: true)] | ||
private DrawableRuleset? drawableRuleset { get; set; } | ||
|
||
public int Value { get; private set; } | ||
|
||
// Even though `FrameStabilityContainer` caches as a `GameplayClock`, we need to check it directly via `drawableRuleset` | ||
// as this calculator is not contained within the `FrameStabilityContainer` and won't see the dependency. | ||
private IGameplayClock clock => drawableRuleset?.FrameStableClock ?? gameplayClock; | ||
|
||
public ClicksPerSecondCalculator() | ||
{ | ||
RelativeSizeAxes = Axes.Both; | ||
} | ||
|
||
public void AddInputTimestamp() => timestamps.Add(clock.CurrentTime); | ||
|
||
protected override void Update() | ||
{ | ||
base.Update(); | ||
|
||
double latestValidTime = clock.CurrentTime; | ||
double earliestTimeValid = latestValidTime - 1000 * gameplayClock.GetTrueGameplayRate(); | ||
|
||
int count = 0; | ||
|
||
for (int i = timestamps.Count - 1; i >= 0; i--) | ||
{ | ||
// handle rewinding by removing future timestamps as we go | ||
if (timestamps[i] > latestValidTime) | ||
{ | ||
timestamps.RemoveAt(i); | ||
continue; | ||
} | ||
|
||
if (timestamps[i] >= earliestTimeValid) | ||
count++; | ||
} | ||
|
||
Value = count; | ||
} | ||
} | ||
} |
Oops, something went wrong.