2021-11-04 12:22:44 +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.
|
|
|
|
|
2021-11-05 04:27:48 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2021-11-04 12:22:44 +08:00
|
|
|
using osu.Framework.Allocation;
|
2021-11-08 09:06:13 +08:00
|
|
|
using osu.Framework.Bindables;
|
2021-11-04 12:22:44 +08:00
|
|
|
using osu.Framework.Graphics;
|
2021-11-05 04:27:48 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2021-11-04 12:22:44 +08: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
|
|
|
|
{
|
2021-11-08 21:27:25 +08:00
|
|
|
public class UnstableRateCounter : RollingCounter<int>, ISkinnableDrawable
|
2021-11-04 12:22:44 +08:00
|
|
|
{
|
|
|
|
public bool UsesFixedAnchor { get; set; }
|
|
|
|
|
|
|
|
protected override double RollingDuration => 750;
|
|
|
|
|
|
|
|
private const float alpha_when_invalid = 0.3f;
|
2021-11-08 09:06:13 +08:00
|
|
|
private readonly Bindable<bool> valid = new Bindable<bool>();
|
2021-11-04 12:22:44 +08:00
|
|
|
|
2021-11-06 02:16:58 +08:00
|
|
|
private readonly List<double> hitOffsets = new List<double>();
|
2021-11-05 04:27:48 +08:00
|
|
|
|
2021-11-08 08:44:50 +08:00
|
|
|
[Resolved]
|
2021-11-04 12:22:44 +08:00
|
|
|
private ScoreProcessor scoreProcessor { get; set; }
|
|
|
|
|
|
|
|
public UnstableRateCounter()
|
|
|
|
{
|
2021-11-08 21:27:25 +08:00
|
|
|
Current.Value = 0;
|
2021-11-04 12:22:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(OsuColour colours, BeatmapDifficultyCache difficultyCache)
|
|
|
|
{
|
|
|
|
Colour = colours.BlueLighter;
|
2021-11-08 09:06:13 +08:00
|
|
|
valid.BindValueChanged(e =>
|
|
|
|
DrawableCount.FadeTo(e.NewValue ? 1 : alpha_when_invalid, 1000, Easing.OutQuint));
|
2021-11-04 12:22:44 +08:00
|
|
|
}
|
|
|
|
|
2021-11-08 21:27:25 +08:00
|
|
|
private bool changesUnstableRate(JudgementResult judgement)
|
|
|
|
=> !(judgement.HitObject.HitWindows is HitWindows.EmptyHitWindows) && judgement.IsHit;
|
2021-11-08 08:44:50 +08:00
|
|
|
|
2021-11-04 12:22:44 +08:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
2021-11-06 02:16:58 +08:00
|
|
|
scoreProcessor.NewJudgement += onJudgementAdded;
|
|
|
|
scoreProcessor.JudgementReverted += onJudgementReverted;
|
2021-11-04 12:22:44 +08:00
|
|
|
}
|
|
|
|
|
2021-11-05 04:27:48 +08:00
|
|
|
private void onJudgementAdded(JudgementResult judgement)
|
2021-11-04 12:22:44 +08:00
|
|
|
{
|
2021-11-08 21:27:25 +08:00
|
|
|
if (!changesUnstableRate(judgement)) return;
|
2021-11-06 02:16:58 +08:00
|
|
|
|
2021-11-08 08:44:50 +08:00
|
|
|
hitOffsets.Add(judgement.TimeOffset);
|
2021-11-08 21:27:25 +08:00
|
|
|
updateDisplay();
|
2021-11-04 12:22:44 +08:00
|
|
|
}
|
|
|
|
|
2021-11-06 02:16:58 +08:00
|
|
|
private void onJudgementReverted(JudgementResult judgement)
|
2021-11-04 12:22:44 +08:00
|
|
|
{
|
2021-11-08 21:27:25 +08:00
|
|
|
if (judgement.FailedAtJudgement || !changesUnstableRate(judgement)) return;
|
2021-11-06 02:16:58 +08:00
|
|
|
|
|
|
|
hitOffsets.RemoveAt(hitOffsets.Count - 1);
|
2021-11-08 21:27:25 +08:00
|
|
|
updateDisplay();
|
2021-11-05 04:27:48 +08:00
|
|
|
}
|
|
|
|
|
2021-11-08 21:27:25 +08:00
|
|
|
private void updateDisplay()
|
2021-11-05 04:27:48 +08:00
|
|
|
{
|
2021-11-06 02:16:58 +08: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 12:22:44 +08:00
|
|
|
{
|
2021-11-06 02:16:58 +08:00
|
|
|
double mean = hitOffsets.Average();
|
|
|
|
double squares = hitOffsets.Select(offset => Math.Pow(offset - mean, 2)).Sum();
|
2021-11-08 21:27:25 +08:00
|
|
|
Current.Value = (int)(Math.Sqrt(squares / hitOffsets.Count) * 10);
|
2021-11-08 09:06:13 +08:00
|
|
|
valid.Value = true;
|
2021-11-04 12:22:44 +08:00
|
|
|
}
|
2021-11-05 04:27:48 +08:00
|
|
|
else
|
2021-11-04 12:22:44 +08:00
|
|
|
{
|
2021-11-06 02:16:58 +08:00
|
|
|
Current.Value = 0;
|
2021-11-08 09:06:13 +08:00
|
|
|
valid.Value = false;
|
2021-11-04 12:22:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-05 04:27:48 +08:00
|
|
|
protected override IHasText CreateText() => new TextComponent
|
|
|
|
{
|
|
|
|
Alpha = alpha_when_invalid,
|
|
|
|
};
|
2021-11-04 12:22:44 +08:00
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
2021-11-08 21:27:25 +08:00
|
|
|
if (scoreProcessor == null) return;
|
|
|
|
|
2021-11-06 02:16:58 +08:00
|
|
|
scoreProcessor.NewJudgement -= onJudgementAdded;
|
|
|
|
scoreProcessor.JudgementReverted -= onJudgementReverted;
|
2021-11-04 12:22:44 +08:00
|
|
|
}
|
2021-11-05 04:27:48 +08:00
|
|
|
|
|
|
|
private class TextComponent : CompositeDrawable, IHasText
|
|
|
|
{
|
|
|
|
public LocalisableString Text
|
|
|
|
{
|
2021-11-08 21:27:25 +08:00
|
|
|
get => text.Text;
|
|
|
|
set => text.Text = value;
|
2021-11-05 04:27:48 +08:00
|
|
|
}
|
|
|
|
|
2021-11-08 21:27:25 +08:00
|
|
|
private readonly OsuSpriteText text;
|
2021-11-05 04:27:48 +08:00
|
|
|
|
|
|
|
public TextComponent()
|
|
|
|
{
|
|
|
|
AutoSizeAxes = Axes.Both;
|
|
|
|
|
|
|
|
InternalChild = new FillFlowContainer
|
|
|
|
{
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
2021-11-08 21:27:25 +08:00
|
|
|
text = new OsuSpriteText
|
2021-11-05 04:27:48 +08:00
|
|
|
{
|
|
|
|
Anchor = Anchor.BottomLeft,
|
|
|
|
Origin = Anchor.BottomLeft,
|
|
|
|
Font = OsuFont.Numeric.With(size: 16, fixedWidth: true)
|
|
|
|
},
|
2021-11-08 21:27:25 +08:00
|
|
|
new OsuSpriteText
|
2021-11-05 04:27:48 +08:00
|
|
|
{
|
|
|
|
Anchor = Anchor.BottomLeft,
|
|
|
|
Origin = Anchor.BottomLeft,
|
|
|
|
Font = OsuFont.Numeric.With(size: 8, fixedWidth: true),
|
2021-11-08 21:27:25 +08:00
|
|
|
Text = "UR",
|
2021-11-05 04:27:48 +08:00
|
|
|
Padding = new MarginPadding { Bottom = 1.5f },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2021-11-04 12:22:44 +08:00
|
|
|
}
|
|
|
|
}
|