Restructure OsuSliderBar to allow for custom tooltips

This commit is contained in:
Dean Herbert 2022-03-03 16:59:10 +09:00
parent a38eb426ef
commit 7ee30024e8

View File

@ -40,7 +40,7 @@ namespace osu.Game.Graphics.UserInterface
private readonly Box rightBox; private readonly Box rightBox;
private readonly Container nubContainer; private readonly Container nubContainer;
public virtual LocalisableString TooltipText { get; private set; } public virtual LocalisableString TooltipText { get; protected set; }
/// <summary> /// <summary>
/// Whether to format the tooltip as a percentage or the actual value. /// Whether to format the tooltip as a percentage or the actual value.
@ -148,7 +148,7 @@ namespace osu.Game.Graphics.UserInterface
protected override void LoadComplete() protected override void LoadComplete()
{ {
base.LoadComplete(); base.LoadComplete();
CurrentNumber.BindValueChanged(current => updateTooltipText(current.NewValue), true); CurrentNumber.BindValueChanged(current => TooltipText = GetTooltipText(current.NewValue), true);
} }
protected override bool OnHover(HoverEvent e) protected override bool OnHover(HoverEvent e)
@ -178,7 +178,7 @@ namespace osu.Game.Graphics.UserInterface
{ {
base.OnUserChange(value); base.OnUserChange(value);
playSample(value); playSample(value);
updateTooltipText(value); TooltipText = GetTooltipText(value);
} }
private void playSample(T value) private void playSample(T value)
@ -203,28 +203,22 @@ namespace osu.Game.Graphics.UserInterface
channel.Play(); channel.Play();
} }
private void updateTooltipText(T value) protected virtual LocalisableString GetTooltipText(T value)
{ {
if (CurrentNumber.IsInteger) if (CurrentNumber.IsInteger)
TooltipText = value.ToInt32(NumberFormatInfo.InvariantInfo).ToString("N0"); return value.ToInt32(NumberFormatInfo.InvariantInfo).ToString("N0");
else
{
double floatValue = value.ToDouble(NumberFormatInfo.InvariantInfo); double floatValue = value.ToDouble(NumberFormatInfo.InvariantInfo);
if (DisplayAsPercentage) if (DisplayAsPercentage)
{ return floatValue.ToString("0%");
TooltipText = floatValue.ToString("0%");
}
else
{
decimal decimalPrecision = normalise(CurrentNumber.Precision.ToDecimal(NumberFormatInfo.InvariantInfo), max_decimal_digits); 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()) // Find the number of significant digits (we could have less than 5 after normalize())
int significantDigits = FormatUtils.FindPrecision(decimalPrecision); int significantDigits = FormatUtils.FindPrecision(decimalPrecision);
TooltipText = floatValue.ToString($"N{significantDigits}"); return floatValue.ToString($"N{significantDigits}");
}
}
} }
protected override void UpdateAfterChildren() protected override void UpdateAfterChildren()