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 JetBrains.Annotations;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
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
|
|
|
|
{
|
|
|
|
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-06 02:16:58 +08:00
|
|
|
private readonly List<double> hitOffsets = new List<double>();
|
2021-11-05 04:27:48 +08:00
|
|
|
|
2021-11-06 02:16:58 +08:00
|
|
|
//May be able to remove the CanBeNull as ScoreProcessor should exist everywhere, for example, in the skin editor it is cached.
|
2021-11-04 12:22:44 +08:00
|
|
|
[CanBeNull]
|
|
|
|
[Resolved(CanBeNull = true)]
|
|
|
|
private ScoreProcessor scoreProcessor { get; set; }
|
|
|
|
|
|
|
|
public UnstableRateCounter()
|
|
|
|
{
|
2021-11-05 04:27:48 +08:00
|
|
|
Current.Value = 0.0;
|
2021-11-04 12:22:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(OsuColour colours, BeatmapDifficultyCache difficultyCache)
|
|
|
|
{
|
|
|
|
Colour = colours.BlueLighter;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
2021-11-06 02:16:58 +08:00
|
|
|
if (scoreProcessor == null) return;
|
|
|
|
|
|
|
|
scoreProcessor.NewJudgement += onJudgementAdded;
|
|
|
|
scoreProcessor.JudgementReverted += onJudgementReverted;
|
2021-11-04 12:22:44 +08:00
|
|
|
}
|
|
|
|
|
2021-11-06 02:16:58 +08:00
|
|
|
private bool isValid;
|
|
|
|
|
2021-11-05 04:27:48 +08:00
|
|
|
private void setValid(bool valid)
|
|
|
|
{
|
2021-11-05 04:32:28 +08:00
|
|
|
if (isValid == valid) return;
|
2021-11-06 02:16:58 +08:00
|
|
|
|
2021-11-05 11:24:25 +08:00
|
|
|
DrawableCount.FadeTo(valid ? 1 : alpha_when_invalid, 1000, Easing.OutQuint);
|
2021-11-05 04:32:28 +08:00
|
|
|
isValid = valid;
|
2021-11-05 04:27:48 +08:00
|
|
|
}
|
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-05 04:27:48 +08:00
|
|
|
if (!(judgement.HitObject.HitWindows is HitWindows.EmptyHitWindows) && judgement.IsHit)
|
2021-11-04 12:22:44 +08:00
|
|
|
{
|
2021-11-06 02:16:58 +08:00
|
|
|
hitOffsets.Add(judgement.TimeOffset);
|
2021-11-04 12:22:44 +08:00
|
|
|
}
|
2021-11-06 02:16:58 +08:00
|
|
|
|
|
|
|
updateUr();
|
2021-11-04 12:22:44 +08:00
|
|
|
}
|
|
|
|
|
2021-11-06 02:16:58 +08:00
|
|
|
// If a judgement was reverted successfully, remove the item from the hitOffsets list.
|
|
|
|
private void onJudgementReverted(JudgementResult judgement)
|
2021-11-04 12:22:44 +08:00
|
|
|
{
|
2021-11-06 02:16:58 +08:00
|
|
|
//Score Processor Conditions to revert
|
|
|
|
if (judgement.FailedAtJudgement || !judgement.Type.IsScorable())
|
|
|
|
return;
|
|
|
|
//UR Conditions to Revert
|
|
|
|
if (judgement.HitObject.HitWindows is HitWindows.EmptyHitWindows || !judgement.IsHit)
|
|
|
|
return;
|
|
|
|
|
|
|
|
hitOffsets.RemoveAt(hitOffsets.Count - 1);
|
|
|
|
updateUr();
|
2021-11-05 04:27:48 +08:00
|
|
|
}
|
|
|
|
|
2021-11-06 02:16:58 +08:00
|
|
|
private void updateUr()
|
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();
|
|
|
|
Current.Value = Math.Sqrt(squares / hitOffsets.Count) * 10;
|
2021-11-05 04:27:48 +08:00
|
|
|
setValid(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-05 04:27:48 +08:00
|
|
|
setValid(false);
|
2021-11-04 12:22:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override LocalisableString FormatCount(double count)
|
|
|
|
{
|
2021-11-06 02:16:58 +08:00
|
|
|
return count.ToString("0.00 UR");
|
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-06 02:16:58 +08:00
|
|
|
if (scoreProcessor == null) return;
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
get => intPart.Text;
|
2021-11-06 02:16:58 +08:00
|
|
|
set
|
|
|
|
{
|
2021-11-05 04:27:48 +08:00
|
|
|
//Not too sure about this, is there a better way to go about doing this?
|
|
|
|
splitValue = value.ToString().Split('.');
|
|
|
|
intPart.Text = splitValue[0];
|
2021-11-06 02:16:58 +08:00
|
|
|
decimalPart.Text = splitValue[1];
|
2021-11-05 04:27:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private string[] splitValue;
|
|
|
|
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)
|
|
|
|
},
|
2021-11-06 02:16:58 +08:00
|
|
|
new OsuSpriteText
|
|
|
|
{
|
|
|
|
Anchor = Anchor.BottomLeft,
|
|
|
|
Origin = Anchor.BottomLeft,
|
|
|
|
Font = OsuFont.Numeric.With(size: 8, fixedWidth: true),
|
|
|
|
Text = ".",
|
|
|
|
Padding = new MarginPadding { Bottom = 1.5f },
|
|
|
|
},
|
2021-11-05 04:27:48 +08:00
|
|
|
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 12:22:44 +08:00
|
|
|
}
|
|
|
|
}
|