From 9afc8681ef12f9ddcb8f021f1629e97a8ff10804 Mon Sep 17 00:00:00 2001 From: mk56-spn Date: Thu, 26 Jan 2023 12:25:05 +0100 Subject: [PATCH] Extract tooltip implementation into base OsuSliderBar.cs from NormalSliderBar.cs --- .../Graphics/UserInterface/NormalSliderBar.cs | 37 +----------- .../Graphics/UserInterface/OsuSliderBar.cs | 56 ++++++++++++++++++- 2 files changed, 56 insertions(+), 37 deletions(-) diff --git a/osu.Game/Graphics/UserInterface/NormalSliderBar.cs b/osu.Game/Graphics/UserInterface/NormalSliderBar.cs index 0d06995ccf..1a894046d9 100644 --- a/osu.Game/Graphics/UserInterface/NormalSliderBar.cs +++ b/osu.Game/Graphics/UserInterface/NormalSliderBar.cs @@ -14,24 +14,16 @@ using osu.Framework.Audio.Sample; using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; -using osu.Framework.Graphics.Cursor; using osu.Framework.Graphics.Shapes; using osu.Framework.Input.Events; -using osu.Framework.Localisation; using osu.Framework.Utils; using osu.Game.Overlays; -using osu.Game.Utils; namespace osu.Game.Graphics.UserInterface { - public partial class NormalSliderBar : OsuSliderBar, IHasTooltip, IHasAccentColour + public partial class NormalSliderBar : OsuSliderBar, IHasAccentColour where T : struct, IEquatable, IComparable, IConvertible { - /// - /// Maximum number of decimal digits to be displayed in the tooltip. - /// - private const int max_decimal_digits = 5; - private Sample sample; private double lastSampleTime; private T lastSampleValue; @@ -41,17 +33,10 @@ namespace osu.Game.Graphics.UserInterface protected readonly Box RightBox; private readonly Container nubContainer; - public virtual LocalisableString TooltipText { get; private set; } - public bool PlaySamplesOnAdjust { get; set; } = true; private readonly HoverClickSounds hoverClickSounds; - /// - /// Whether to format the tooltip as a percentage or the actual value. - /// - public bool DisplayAsPercentage { get; set; } - private Color4 accentColour; public Color4 AccentColour @@ -150,7 +135,6 @@ namespace osu.Game.Graphics.UserInterface protected override void LoadComplete() { base.LoadComplete(); - CurrentNumber.BindValueChanged(current => TooltipText = getTooltipText(current.NewValue), true); Current.BindDisabledChanged(disabled => { @@ -189,7 +173,6 @@ namespace osu.Game.Graphics.UserInterface { base.OnUserChange(value); playSample(value); - TooltipText = getTooltipText(value); } private void playSample(T value) @@ -217,24 +200,6 @@ namespace osu.Game.Graphics.UserInterface channel.Play(); } - private LocalisableString getTooltipText(T value) - { - if (CurrentNumber.IsInteger) - return value.ToInt32(NumberFormatInfo.InvariantInfo).ToString("N0"); - - double floatValue = value.ToDouble(NumberFormatInfo.InvariantInfo); - - if (DisplayAsPercentage) - return floatValue.ToString("0%"); - - decimal decimalPrecision = normalise(CurrentNumber.Precision.ToDecimal(NumberFormatInfo.InvariantInfo), max_decimal_digits); - - // Find the number of significant digits (we could have less than 5 after normalize()) - int significantDigits = FormatUtils.FindPrecision(decimalPrecision); - - return floatValue.ToString($"N{significantDigits}"); - } - protected override void UpdateAfterChildren() { base.UpdateAfterChildren(); diff --git a/osu.Game/Graphics/UserInterface/OsuSliderBar.cs b/osu.Game/Graphics/UserInterface/OsuSliderBar.cs index d82850d181..6a8f81a4b6 100644 --- a/osu.Game/Graphics/UserInterface/OsuSliderBar.cs +++ b/osu.Game/Graphics/UserInterface/OsuSliderBar.cs @@ -2,12 +2,66 @@ // See the LICENCE file in the repository root for full licence text. using System; +using System.Globalization; +using osu.Framework.Graphics.Cursor; using osu.Framework.Graphics.UserInterface; +using osu.Framework.Localisation; +using osu.Game.Utils; namespace osu.Game.Graphics.UserInterface { - public abstract partial class OsuSliderBar : SliderBar + public abstract partial class OsuSliderBar : SliderBar, IHasTooltip where T : struct, IEquatable, IComparable, IConvertible { + public virtual LocalisableString TooltipText { get; private set; } + + /// + /// Maximum number of decimal digits to be displayed in the tooltip. + /// + private const int max_decimal_digits = 5; + + /// + /// Whether to format the tooltip as a percentage or the actual value. + /// + public bool DisplayAsPercentage { get; set; } + + protected override void LoadComplete() + { + base.LoadComplete(); + CurrentNumber.BindValueChanged(current => TooltipText = getTooltipText(current.NewValue), true); + } + + protected override void OnUserChange(T value) + { + base.OnUserChange(value); + TooltipText = getTooltipText(value); + } + + private LocalisableString getTooltipText(T value) + { + if (CurrentNumber.IsInteger) + return value.ToInt32(NumberFormatInfo.InvariantInfo).ToString("N0"); + + double floatValue = value.ToDouble(NumberFormatInfo.InvariantInfo); + + if (DisplayAsPercentage) + return floatValue.ToString("0%"); + + decimal decimalPrecision = normalise(CurrentNumber.Precision.ToDecimal(NumberFormatInfo.InvariantInfo), max_decimal_digits); + + // Find the number of significant digits (we could have less than 5 after normalize()) + int significantDigits = FormatUtils.FindPrecision(decimalPrecision); + + return floatValue.ToString($"N{significantDigits}"); + } + + /// + /// Removes all non-significant digits, keeping at most a requested number of decimal digits. + /// + /// The decimal to normalize. + /// The maximum number of decimal digits to keep. The final result may have fewer decimal digits than this value. + /// The normalised decimal. + private decimal normalise(decimal d, int sd) + => decimal.Parse(Math.Round(d, sd).ToString(string.Concat("0.", new string('#', sd)), CultureInfo.InvariantCulture), CultureInfo.InvariantCulture); } }