2019-01-24 16:43:03 +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.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-10-10 15:04:41 +08:00
|
|
|
|
using System;
|
2020-10-19 13:05:28 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
2021-07-24 04:37:08 +08:00
|
|
|
|
using osu.Framework.Localisation;
|
2020-08-04 01:14:17 +08:00
|
|
|
|
using osu.Game.Graphics.Sprites;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.UserInterface
|
|
|
|
|
{
|
2021-05-07 15:10:57 +08:00
|
|
|
|
public abstract class ScoreCounter : RollingCounter<double>
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
protected override double RollingDuration => 1000;
|
|
|
|
|
protected override Easing RollingEasing => Easing.Out;
|
|
|
|
|
|
2020-10-15 15:31:21 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether comma separators should be displayed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool UseCommaSeparator { get; }
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2020-10-19 13:05:28 +08:00
|
|
|
|
public Bindable<int> RequiredDisplayDigits { get; } = new Bindable<int>();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Displays score.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="leading">How many leading zeroes the counter will have.</param>
|
2020-10-15 15:31:21 +08:00
|
|
|
|
/// <param name="useCommaSeparator">Whether comma separators should be displayed.</param>
|
2020-10-19 13:05:28 +08:00
|
|
|
|
protected ScoreCounter(int leading = 0, bool useCommaSeparator = false)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2020-10-15 15:31:21 +08:00
|
|
|
|
UseCommaSeparator = useCommaSeparator;
|
2020-10-19 13:05:28 +08:00
|
|
|
|
|
2021-10-10 15:04:41 +08:00
|
|
|
|
if (useCommaSeparator && leading > 0)
|
|
|
|
|
throw new ArgumentException("Should not mix leading zeroes and comma separators as it doesn't make sense");
|
|
|
|
|
|
2020-10-19 13:05:28 +08:00
|
|
|
|
RequiredDisplayDigits.Value = leading;
|
|
|
|
|
RequiredDisplayDigits.BindValueChanged(_ => UpdateDisplay());
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override double GetProportionalDuration(double currentValue, double newValue)
|
|
|
|
|
{
|
|
|
|
|
return currentValue > newValue ? currentValue - newValue : newValue - currentValue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-24 04:37:08 +08:00
|
|
|
|
protected override LocalisableString FormatCount(double count)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2020-10-19 13:05:28 +08:00
|
|
|
|
string format = new string('0', RequiredDisplayDigits.Value);
|
2021-10-10 15:04:41 +08:00
|
|
|
|
var output = ((long)count).ToString(format);
|
2019-11-11 20:05:36 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
if (UseCommaSeparator)
|
2019-11-11 19:53:22 +08:00
|
|
|
|
{
|
2021-10-10 15:04:41 +08:00
|
|
|
|
for (int i = output.Length - 3; i > 0; i -= 3)
|
|
|
|
|
output = output.Insert(i, @",");
|
2019-11-11 19:53:22 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-10-10 15:04:41 +08:00
|
|
|
|
return output;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-04 01:14:17 +08:00
|
|
|
|
protected override OsuSpriteText CreateSpriteText()
|
2020-08-19 12:44:45 +08:00
|
|
|
|
=> base.CreateSpriteText().With(s => s.Font = s.Font.With(fixedWidth: true));
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|