Expose a NumberFormatInfo for more extensibility

This commit is contained in:
smoogipoo 2018-01-02 17:00:38 +09:00
parent b84f83cf16
commit cbf4852438

View File

@ -28,18 +28,15 @@ namespace osu.Game.Graphics.UserInterface
private readonly Box leftBox; private readonly Box leftBox;
private readonly Box rightBox; private readonly Box rightBox;
private int tooltipDecimalDigits = 1; private NumberFormatInfo format;
/// <summary> public NumberFormatInfo Format
/// The amount of decimal digits to display for <see cref="OsuSliderBar{T}"/>s with floating point values.
/// </summary>
public int TooltipDecimalDigits
{ {
get => tooltipDecimalDigits; get => format ?? (format = createDefaultFormat());
set set
{ {
if (tooltipDecimalDigits == value) if (format == value)
return; return;
tooltipDecimalDigits = value; format = value;
if (IsLoaded) if (IsLoaded)
{ {
@ -63,17 +60,14 @@ namespace osu.Game.Graphics.UserInterface
var floatMaxValue = bindableDouble?.MaxValue ?? bindableFloat.MaxValue; var floatMaxValue = bindableDouble?.MaxValue ?? bindableFloat.MaxValue;
if (floatMaxValue == 1 && (floatMinValue == 0 || floatMinValue == -1)) if (floatMaxValue == 1 && (floatMinValue == 0 || floatMinValue == -1))
return floatValue.Value.ToString(@"P0"); return floatValue.Value.ToString("P", Format);
var nfi = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone(); return floatValue.Value.ToString("F", Format);
nfi.NumberDecimalDigits = TooltipDecimalDigits;
return string.Format(nfi, "{0:F}", floatValue.Value);
} }
var bindableInt = CurrentNumber as BindableNumber<int>; var bindableInt = CurrentNumber as BindableNumber<int>;
if (bindableInt != null) if (bindableInt != null)
return bindableInt.Value.ToString(@"n0"); return bindableInt.Value.ToString("N0");
return Current.Value.ToString(); return Current.Value.ToString();
} }
@ -137,6 +131,15 @@ namespace osu.Game.Graphics.UserInterface
AccentColour = colours.Pink; AccentColour = colours.Pink;
} }
private NumberFormatInfo createDefaultFormat()
{
var nfi = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
nfi.PercentDecimalDigits = 0;
nfi.NumberDecimalDigits = 1;
return nfi;
}
protected override bool OnHover(InputState state) protected override bool OnHover(InputState state)
{ {
Nub.Glowing = true; Nub.Glowing = true;