Split score counter class into two distinct classes to simplify usages

This commit is contained in:
Dean Herbert 2021-10-10 17:56:32 +09:00
parent 06cce0119c
commit 794b4c46cf
4 changed files with 30 additions and 20 deletions

View File

@ -127,12 +127,11 @@ namespace osu.Game.Tournament.Screens.Gameplay.Components
score2Text.X = Math.Max(5 + score2Text.DrawWidth / 2, score2Bar.DrawWidth); score2Text.X = Math.Max(5 + score2Text.DrawWidth / 2, score2Bar.DrawWidth);
} }
private class MatchScoreCounter : ScoreCounter private class MatchScoreCounter : CommaSeparatedScoreCounter
{ {
private OsuSpriteText displayedSpriteText; private OsuSpriteText displayedSpriteText;
public MatchScoreCounter() public MatchScoreCounter()
: base(useCommaSeparator: true)
{ {
Margin = new MarginPadding { Top = bar_height, Horizontal = 10 }; Margin = new MarginPadding { Top = bar_height, Horizontal = 10 };
} }

View File

@ -0,0 +1,24 @@
// 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 osu.Framework.Extensions.LocalisationExtensions;
using osu.Framework.Graphics;
using osu.Framework.Localisation;
using osu.Game.Graphics.Sprites;
namespace osu.Game.Graphics.UserInterface
{
public abstract class CommaSeparatedScoreCounter : RollingCounter<double>
{
protected override double RollingDuration => 1000;
protected override Easing RollingEasing => Easing.Out;
protected override double GetProportionalDuration(double currentValue, double newValue) =>
currentValue > newValue ? currentValue - newValue : newValue - currentValue;
protected override LocalisableString FormatCount(double count) => ((long)count).ToLocalisableString(@"N0");
protected override OsuSpriteText CreateSpriteText()
=> base.CreateSpriteText().With(s => s.Font = s.Font.With(fixedWidth: true));
}
}

View File

@ -1,7 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Extensions.LocalisationExtensions; using osu.Framework.Extensions.LocalisationExtensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
@ -17,23 +16,14 @@ namespace osu.Game.Graphics.UserInterface
public Bindable<int> RequiredDisplayDigits { get; } = new Bindable<int>(); public Bindable<int> RequiredDisplayDigits { get; } = new Bindable<int>();
private string formatString = string.Empty; private string formatString;
/// <summary> /// <summary>
/// Displays score. /// Displays score.
/// </summary> /// </summary>
/// <param name="leading">How many leading zeroes the counter will have.</param> /// <param name="leading">How many leading zeroes the counter will have.</param>
/// <param name="useCommaSeparator">Whether comma separators should be displayed.</param> protected ScoreCounter(int leading = 0)
protected ScoreCounter(int leading = 0, bool useCommaSeparator = false)
{ {
if (useCommaSeparator)
{
if (leading > 0)
throw new ArgumentException("Should not mix leading zeroes and comma separators as it doesn't make sense");
formatString = @"N0";
}
RequiredDisplayDigits.Value = leading; RequiredDisplayDigits.Value = leading;
RequiredDisplayDigits.BindValueChanged(displayDigitsChanged, true); RequiredDisplayDigits.BindValueChanged(displayDigitsChanged, true);
} }
@ -44,10 +34,8 @@ namespace osu.Game.Graphics.UserInterface
UpdateDisplay(); UpdateDisplay();
} }
protected override double GetProportionalDuration(double currentValue, double newValue) protected override double GetProportionalDuration(double currentValue, double newValue) =>
{ currentValue > newValue ? currentValue - newValue : newValue - currentValue;
return currentValue > newValue ? currentValue - newValue : newValue - currentValue;
}
protected override LocalisableString FormatCount(double count) => ((long)count).ToLocalisableString(formatString); protected override LocalisableString FormatCount(double count) => ((long)count).ToLocalisableString(formatString);

View File

@ -146,12 +146,11 @@ namespace osu.Game.Screens.Play.HUD
Score2Text.X = Math.Max(5 + Score2Text.DrawWidth / 2, score2Bar.DrawWidth); Score2Text.X = Math.Max(5 + Score2Text.DrawWidth / 2, score2Bar.DrawWidth);
} }
protected class MatchScoreCounter : ScoreCounter protected class MatchScoreCounter : CommaSeparatedScoreCounter
{ {
private OsuSpriteText displayedSpriteText; private OsuSpriteText displayedSpriteText;
public MatchScoreCounter() public MatchScoreCounter()
: base(useCommaSeparator: true)
{ {
Margin = new MarginPadding { Top = bar_height, Horizontal = 10 }; Margin = new MarginPadding { Top = bar_height, Horizontal = 10 };
} }