1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 10:52:53 +08:00

Add (int) flooring and handle potential NaN value

This commit is contained in:
Dean Herbert 2023-05-26 20:39:22 +09:00
parent b9be886ae1
commit 6b0e215246
2 changed files with 19 additions and 8 deletions

View File

@ -212,18 +212,29 @@ namespace osu.Game.Screens.Play
playInfoText.AddText("Retry count: ");
playInfoText.AddText(retries.ToString(), cp => cp.Font = cp.Font.With(weight: FontWeight.Bold));
if (gameplayState != null && gameplayClock != null)
if (getSongProgress() is int progress)
{
(double firstHitTime, double lastHitTime) = gameplayState.Beatmap.CalculatePlayableBounds();
double progress = Math.Clamp((gameplayClock.CurrentTime - firstHitTime) / (lastHitTime - firstHitTime), 0, 1);
playInfoText.NewLine();
playInfoText.AddText("Song progress: ");
playInfoText.AddText(progress.ToString("0%"), cp => cp.Font = cp.Font.With(weight: FontWeight.Bold));
playInfoText.AddText($"{progress}%", cp => cp.Font = cp.Font.With(weight: FontWeight.Bold));
}
}
private int? getSongProgress()
{
if (gameplayClock == null || gameplayState == null)
return null;
(double firstHitTime, double lastHitTime) = gameplayState.Beatmap.CalculatePlayableBounds();
double playableLength = (lastHitTime - firstHitTime);
if (playableLength == 0)
return 0;
return (int)Math.Clamp(((gameplayClock.CurrentTime - firstHitTime) / playableLength) * 100, 0, 100);
}
private partial class Button : DialogButton
{
// required to ensure keyboard navigation always starts from an extremity (unless the cursor is moved)

View File

@ -145,12 +145,12 @@ namespace osu.Game.Screens.Play.HUD
double time = gameplayClock?.CurrentTime ?? Time.Current;
double songCurrentTime = time - startTime;
int currentPercent = Math.Max(0, Math.Min(100, (int)(songCurrentTime / songLength * 100)));
int currentPercent = songLength == 0 ? 0 : Math.Max(0, Math.Min(100, (int)(songCurrentTime / songLength * 100)));
int currentSecond = (int)Math.Floor(songCurrentTime / 1000.0);
if (currentPercent != previousPercent)
{
progress.Text = currentPercent + @"%";
progress.Text = $@"{currentPercent}%";
previousPercent = currentPercent;
}