2021-11-04 04:22:44 +00: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.
|
|
|
|
|
2021-11-04 20:27:48 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2021-11-04 04:22:44 +00:00
|
|
|
using osu.Framework.Allocation;
|
2021-11-08 00:44:50 +00:00
|
|
|
using osu.Framework.Extensions.LocalisationExtensions;
|
2021-11-04 04:22:44 +00:00
|
|
|
using osu.Framework.Graphics;
|
2021-11-04 20:27:48 +00:00
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2021-11-04 04:22:44 +00:00
|
|
|
using osu.Framework.Localisation;
|
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Graphics;
|
|
|
|
using osu.Game.Graphics.Sprites;
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
|
|
|
using osu.Game.Rulesets.Judgements;
|
|
|
|
using osu.Game.Rulesets.Scoring;
|
|
|
|
using osu.Game.Skinning;
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Play.HUD
|
|
|
|
{
|
|
|
|
public class UnstableRateCounter : RollingCounter<double>, ISkinnableDrawable
|
|
|
|
{
|
|
|
|
public bool UsesFixedAnchor { get; set; }
|
|
|
|
|
|
|
|
protected override double RollingDuration => 750;
|
|
|
|
|
|
|
|
private const float alpha_when_invalid = 0.3f;
|
|
|
|
|
2021-11-05 18:16:58 +00:00
|
|
|
private readonly List<double> hitOffsets = new List<double>();
|
2021-11-04 20:27:48 +00:00
|
|
|
|
2021-11-08 00:44:50 +00:00
|
|
|
[Resolved]
|
2021-11-04 04:22:44 +00:00
|
|
|
private ScoreProcessor scoreProcessor { get; set; }
|
|
|
|
|
|
|
|
public UnstableRateCounter()
|
|
|
|
{
|
2021-11-04 20:27:48 +00:00
|
|
|
Current.Value = 0.0;
|
2021-11-04 04:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(OsuColour colours, BeatmapDifficultyCache difficultyCache)
|
|
|
|
{
|
|
|
|
Colour = colours.BlueLighter;
|
|
|
|
}
|
|
|
|
|
2021-11-08 00:44:50 +00:00
|
|
|
private bool isUrInvalid(JudgementResult judgement)
|
|
|
|
{
|
|
|
|
return judgement.HitObject.HitWindows is HitWindows.EmptyHitWindows || !judgement.IsHit;
|
|
|
|
}
|
|
|
|
|
2021-11-04 04:22:44 +00:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
2021-11-05 18:16:58 +00:00
|
|
|
scoreProcessor.NewJudgement += onJudgementAdded;
|
|
|
|
scoreProcessor.JudgementReverted += onJudgementReverted;
|
2021-11-04 04:22:44 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 18:16:58 +00:00
|
|
|
private bool isValid;
|
|
|
|
|
2021-11-04 20:27:48 +00:00
|
|
|
private void setValid(bool valid)
|
|
|
|
{
|
2021-11-04 20:32:28 +00:00
|
|
|
if (isValid == valid) return;
|
2021-11-05 18:16:58 +00:00
|
|
|
|
2021-11-05 03:24:25 +00:00
|
|
|
DrawableCount.FadeTo(valid ? 1 : alpha_when_invalid, 1000, Easing.OutQuint);
|
2021-11-04 20:32:28 +00:00
|
|
|
isValid = valid;
|
2021-11-04 20:27:48 +00:00
|
|
|
}
|
2021-11-04 04:22:44 +00:00
|
|
|
|
2021-11-04 20:27:48 +00:00
|
|
|
private void onJudgementAdded(JudgementResult judgement)
|
2021-11-04 04:22:44 +00:00
|
|
|
{
|
2021-11-08 00:44:50 +00:00
|
|
|
if (isUrInvalid(judgement)) return;
|
2021-11-05 18:16:58 +00:00
|
|
|
|
2021-11-08 00:44:50 +00:00
|
|
|
hitOffsets.Add(judgement.TimeOffset);
|
2021-11-05 18:16:58 +00:00
|
|
|
updateUr();
|
2021-11-04 04:22:44 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 18:16:58 +00:00
|
|
|
private void onJudgementReverted(JudgementResult judgement)
|
2021-11-04 04:22:44 +00:00
|
|
|
{
|
2021-11-08 00:44:50 +00:00
|
|
|
if (judgement.FailedAtJudgement || isUrInvalid(judgement)) return;
|
2021-11-05 18:16:58 +00:00
|
|
|
|
|
|
|
hitOffsets.RemoveAt(hitOffsets.Count - 1);
|
|
|
|
updateUr();
|
2021-11-04 20:27:48 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 18:16:58 +00:00
|
|
|
private void updateUr()
|
2021-11-04 20:27:48 +00:00
|
|
|
{
|
2021-11-05 18:16:58 +00:00
|
|
|
// At Count = 0, we get NaN, While we are allowing count = 1, it will be 0 since average = offset.
|
|
|
|
if (hitOffsets.Count > 0)
|
2021-11-04 04:22:44 +00:00
|
|
|
{
|
2021-11-05 18:16:58 +00:00
|
|
|
double mean = hitOffsets.Average();
|
|
|
|
double squares = hitOffsets.Select(offset => Math.Pow(offset - mean, 2)).Sum();
|
|
|
|
Current.Value = Math.Sqrt(squares / hitOffsets.Count) * 10;
|
2021-11-04 20:27:48 +00:00
|
|
|
setValid(true);
|
2021-11-04 04:22:44 +00:00
|
|
|
}
|
2021-11-04 20:27:48 +00:00
|
|
|
else
|
2021-11-04 04:22:44 +00:00
|
|
|
{
|
2021-11-05 18:16:58 +00:00
|
|
|
Current.Value = 0;
|
2021-11-04 20:27:48 +00:00
|
|
|
setValid(false);
|
2021-11-04 04:22:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override LocalisableString FormatCount(double count)
|
|
|
|
{
|
2021-11-08 00:44:50 +00:00
|
|
|
return count.ToString("0.00");
|
2021-11-04 04:22:44 +00:00
|
|
|
}
|
|
|
|
|
2021-11-04 20:27:48 +00:00
|
|
|
protected override IHasText CreateText() => new TextComponent
|
|
|
|
{
|
|
|
|
Alpha = alpha_when_invalid,
|
|
|
|
};
|
2021-11-04 04:22:44 +00:00
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
2021-11-05 18:16:58 +00:00
|
|
|
scoreProcessor.NewJudgement -= onJudgementAdded;
|
|
|
|
scoreProcessor.JudgementReverted -= onJudgementReverted;
|
2021-11-04 04:22:44 +00:00
|
|
|
}
|
2021-11-04 20:27:48 +00:00
|
|
|
|
|
|
|
private class TextComponent : CompositeDrawable, IHasText
|
|
|
|
{
|
|
|
|
public LocalisableString Text
|
|
|
|
{
|
2021-11-08 00:44:50 +00:00
|
|
|
get => fullValue.ToLocalisableString("0.00 UR");
|
2021-11-05 18:16:58 +00:00
|
|
|
set
|
|
|
|
{
|
2021-11-08 00:44:50 +00:00
|
|
|
fullValue = Convert.ToDouble(value.ToString());
|
|
|
|
intPart.Text = fullValue.ToLocalisableString("0");
|
|
|
|
decimalPart.Text = (fullValue - Math.Truncate(fullValue))
|
|
|
|
.ToLocalisableString(".00 UR");
|
2021-11-04 20:27:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 00:44:50 +00:00
|
|
|
private double fullValue;
|
|
|
|
|
2021-11-04 20:27:48 +00:00
|
|
|
private readonly OsuSpriteText intPart;
|
|
|
|
private readonly OsuSpriteText decimalPart;
|
|
|
|
|
|
|
|
public TextComponent()
|
|
|
|
{
|
|
|
|
AutoSizeAxes = Axes.Both;
|
|
|
|
|
|
|
|
InternalChild = new FillFlowContainer
|
|
|
|
{
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
intPart = new OsuSpriteText
|
|
|
|
{
|
|
|
|
Anchor = Anchor.BottomLeft,
|
|
|
|
Origin = Anchor.BottomLeft,
|
|
|
|
Font = OsuFont.Numeric.With(size: 16, fixedWidth: true)
|
|
|
|
},
|
|
|
|
decimalPart = new OsuSpriteText
|
|
|
|
{
|
|
|
|
Anchor = Anchor.BottomLeft,
|
|
|
|
Origin = Anchor.BottomLeft,
|
|
|
|
Font = OsuFont.Numeric.With(size: 8, fixedWidth: true),
|
|
|
|
Padding = new MarginPadding { Bottom = 1.5f },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2021-11-04 04:22:44 +00:00
|
|
|
}
|
|
|
|
}
|