Merge pull request #2029 from smoogipoo/slider-tooltip-precision

Use bindable precision for OsuSliderBar's tooltip value
This commit is contained in:
Dean Herbert 2018-02-08 11:33:45 +09:00 committed by GitHub
commit 23d211e9b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 6 deletions

View File

@ -20,6 +20,11 @@ namespace osu.Game.Graphics.UserInterface
public class OsuSliderBar<T> : SliderBar<T>, IHasTooltip, IHasAccentColour
where T : struct, IEquatable<T>, IComparable, IConvertible
{
/// <summary>
/// Maximum number of decimal digits to be displayed in the tooltip.
/// </summary>
private const int max_decimal_digits = 5;
private SampleChannel sample;
private double lastSampleTime;
private T lastSampleValue;
@ -35,6 +40,7 @@ namespace osu.Game.Graphics.UserInterface
var bindableDouble = CurrentNumber as BindableNumber<double>;
var bindableFloat = CurrentNumber as BindableNumber<float>;
var floatValue = bindableDouble?.Value ?? bindableFloat?.Value;
var floatPrecision = bindableDouble?.Precision ?? bindableFloat?.Precision;
if (floatValue != null)
{
@ -44,7 +50,12 @@ namespace osu.Game.Graphics.UserInterface
if (floatMaxValue == 1 && (floatMinValue == 0 || floatMinValue == -1))
return floatValue.Value.ToString("P0");
return floatValue.Value.ToString("N1");
var decimalPrecision = normalise((decimal)floatPrecision, max_decimal_digits);
// Find the number of significant digits (we could have less than 5 after normalize())
var significantDigits = findPrecision(decimalPrecision);
return floatValue.Value.ToString($"N{significantDigits}");
}
var bindableInt = CurrentNumber as BindableNumber<int>;
@ -177,5 +188,31 @@ namespace osu.Game.Graphics.UserInterface
{
Nub.MoveToX(RangePadding + UsableWidth * value, 250, Easing.OutQuint);
}
/// <summary>
/// Removes all non-significant digits, keeping at most a requested number of decimal digits.
/// </summary>
/// <param name="d">The decimal to normalize.</param>
/// <param name="sd">The maximum number of decimal digits to keep. The final result may have fewer decimal digits than this value.</param>
/// <returns>The normalised decimal.</returns>
private decimal normalise(decimal d, int sd)
=> decimal.Parse(Math.Round(d, sd).ToString(string.Concat("0.", new string('#', sd)), CultureInfo.InvariantCulture), CultureInfo.InvariantCulture);
/// <summary>
/// Finds the number of digits after the decimal.
/// </summary>
/// <param name="d">The value to find the number of decimal digits for.</param>
/// <returns>The number decimal digits.</returns>
private int findPrecision(decimal d)
{
int precision = 0;
while (d != Math.Round(d))
{
d *= 10;
precision++;
}
return precision;
}
}
}

View File

@ -42,7 +42,6 @@ namespace osu.Game.Screens.Play.PlayerSettings
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Text = "1x",
Font = @"Exo2.0-Bold",
}
},
@ -54,12 +53,13 @@ namespace osu.Game.Screens.Play.PlayerSettings
Default = 1,
MinValue = 0.5,
MaxValue = 2,
Precision = 0.01,
Precision = 0.1,
},
}
};
sliderbar.Bindable.ValueChanged += rateMultiplier => multiplierText.Text = $"{rateMultiplier}x";
sliderbar.Bindable.ValueChanged += rateMultiplier => multiplierText.Text = $"{sliderbar.Bar.TooltipText}x";
sliderbar.Bindable.TriggerChange();
}
protected override void LoadComplete()

View File

@ -13,6 +13,8 @@ namespace osu.Game.Screens.Play.PlayerSettings
public class PlayerSliderBar<T> : SettingsSlider<T>
where T : struct, IEquatable<T>, IComparable, IConvertible
{
public OsuSliderBar<T> Bar => (OsuSliderBar<T>)Control;
protected override Drawable CreateControl() => new Sliderbar
{
Margin = new MarginPadding { Top = 5, Bottom = 5 },
@ -21,8 +23,6 @@ namespace osu.Game.Screens.Play.PlayerSettings
private class Sliderbar : OsuSliderBar<T>
{
public override string TooltipText => $"{CurrentNumber.Value}";
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{