mirror of
https://github.com/ppy/osu.git
synced 2024-11-11 15:47:26 +08:00
Remove number format specified from OsuSliderBar, override ToolTipText
Better/cleaner solution.
This commit is contained in:
parent
17e7f75aca
commit
c010b48b29
@ -1,123 +0,0 @@
|
||||
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
||||
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Configuration;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Framework.Graphics.Sprites;
|
||||
using osu.Game.Graphics.Sprites;
|
||||
using osu.Game.Graphics.UserInterface;
|
||||
using OpenTK;
|
||||
|
||||
namespace osu.Game.Tests.Visual
|
||||
{
|
||||
public class TestCaseSliderBarPercentage : OsuTestCase
|
||||
{
|
||||
public override IReadOnlyList<Type> RequiredTypes => new[] { typeof(OsuSliderBar<>) };
|
||||
|
||||
private readonly BindableFloat floatValue;
|
||||
private readonly BindableDouble doubleValue;
|
||||
|
||||
private readonly TestSliderBar<float> floatSliderBar;
|
||||
private readonly TestSliderBar<double> doubleSliderBar;
|
||||
|
||||
public TestCaseSliderBarPercentage()
|
||||
{
|
||||
floatValue = new BindableFloat
|
||||
{
|
||||
MinValue = -1,
|
||||
MaxValue = 1,
|
||||
};
|
||||
|
||||
doubleValue = new BindableDouble
|
||||
{
|
||||
MinValue = -1,
|
||||
MaxValue = 1
|
||||
};
|
||||
|
||||
Child = new FillFlowContainer
|
||||
{
|
||||
AutoSizeAxes = Axes.Y,
|
||||
Width = 300,
|
||||
Direction = FillDirection.Vertical,
|
||||
Spacing = new Vector2(0, 20),
|
||||
Children = new Drawable[]
|
||||
{
|
||||
floatSliderBar = new TestSliderBar<float> { RelativeSizeAxes = Axes.X },
|
||||
doubleSliderBar = new TestSliderBar<double> { RelativeSizeAxes = Axes.X }
|
||||
}
|
||||
};
|
||||
|
||||
floatSliderBar.Current.BindTo(floatValue);
|
||||
doubleSliderBar.Current.BindTo(doubleValue);
|
||||
|
||||
floatValue.ValueChanged += setValue;
|
||||
doubleValue.ValueChanged += setValue;
|
||||
|
||||
AddStep("Digits = 0", () => setPercentageDigits(0));
|
||||
AddStep("Value = 0", () => setValue(0));
|
||||
AddAssert("Check 0%", () => checkExact(0));
|
||||
|
||||
AddStep("Value = 0.5", () => setValue(0.5));
|
||||
AddAssert("Check 50%", () => checkExact(0.5m));
|
||||
|
||||
AddStep("Value = 0.54", () => setValue(0.54));
|
||||
AddAssert("Check 54%", () => checkExact(0.54m));
|
||||
|
||||
AddStep("Value = 0.544", () => setValue(0.544));
|
||||
AddAssert("Check 54%", () => checkExact(0.54m));
|
||||
|
||||
AddStep("Value = 0.548", () => setValue(0.548));
|
||||
AddAssert("Check 55%", () => checkExact(0.55m));
|
||||
|
||||
AddStep("Digits = 1", () => setPercentageDigits(1));
|
||||
AddAssert("Check 54.8%", () => checkExact(0.548m));
|
||||
|
||||
AddSliderStep("Percentage", -1.0, 1.0, 0.0, setValue);
|
||||
AddSliderStep("Digits", 0, 7, 1, setPercentageDigits);
|
||||
}
|
||||
|
||||
private bool checkExact(decimal percentage)
|
||||
{
|
||||
string expectedValue = percentage.ToString("P", floatSliderBar.Format);
|
||||
return floatSliderBar.TooltipText == expectedValue && doubleSliderBar.TooltipText == expectedValue;
|
||||
}
|
||||
|
||||
private void setValue<T>(T value)
|
||||
{
|
||||
floatValue.Value = Convert.ToSingle(value);
|
||||
doubleValue.Value = Convert.ToDouble(value);
|
||||
}
|
||||
|
||||
private void setPercentageDigits(int digits)
|
||||
{
|
||||
floatSliderBar.Format.PercentDecimalDigits = digits;
|
||||
doubleSliderBar.Format.PercentDecimalDigits = digits;
|
||||
|
||||
// Make sure that the text referenced in TestSliderBar is updated
|
||||
// This doesn't break any assertions if missing, but breaks the visual display
|
||||
floatSliderBar.Current.TriggerChange();
|
||||
doubleSliderBar.Current.TriggerChange();
|
||||
}
|
||||
|
||||
private class TestSliderBar<T> : OsuSliderBar<T>
|
||||
where T : struct, IEquatable<T>, IComparable, IConvertible
|
||||
{
|
||||
public TestSliderBar()
|
||||
{
|
||||
SpriteText valueText;
|
||||
AddInternal(valueText = new OsuSpriteText
|
||||
{
|
||||
Anchor = Anchor.CentreRight,
|
||||
Origin = Anchor.CentreLeft,
|
||||
X = 5,
|
||||
Text = TooltipText
|
||||
});
|
||||
|
||||
Current.ValueChanged += v => valueText.Text = TooltipText;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,172 +0,0 @@
|
||||
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
||||
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using osu.Framework.Configuration;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Framework.Graphics.Sprites;
|
||||
using osu.Game.Graphics.Sprites;
|
||||
using osu.Game.Graphics.UserInterface;
|
||||
using OpenTK;
|
||||
|
||||
namespace osu.Game.Tests.Visual
|
||||
{
|
||||
public class TestCaseSliderBarPrecision : OsuTestCase
|
||||
{
|
||||
public override IReadOnlyList<Type> RequiredTypes => new[] { typeof(OsuSliderBar<>) };
|
||||
|
||||
private readonly BindableInt intValue;
|
||||
private readonly BindableFloat floatValue;
|
||||
private readonly BindableDouble doubleValue;
|
||||
|
||||
private readonly TestSliderBar<int> intSliderBar;
|
||||
private readonly TestSliderBar<float> floatSliderBar;
|
||||
private readonly TestSliderBar<double> doubleSliderBar;
|
||||
|
||||
public TestCaseSliderBarPrecision()
|
||||
{
|
||||
intValue = new BindableInt
|
||||
{
|
||||
MinValue = -1000,
|
||||
MaxValue = 1000,
|
||||
};
|
||||
|
||||
floatValue = new BindableFloat
|
||||
{
|
||||
MinValue = -1000,
|
||||
MaxValue = 1000,
|
||||
};
|
||||
|
||||
doubleValue = new BindableDouble
|
||||
{
|
||||
MinValue = -1000,
|
||||
MaxValue = 1000
|
||||
};
|
||||
|
||||
Child = new FillFlowContainer
|
||||
{
|
||||
AutoSizeAxes = Axes.Y,
|
||||
Width = 300,
|
||||
Direction = FillDirection.Vertical,
|
||||
Spacing = new Vector2(0, 20),
|
||||
Children = new Drawable[]
|
||||
{
|
||||
intSliderBar = new TestSliderBar<int> { RelativeSizeAxes = Axes.X },
|
||||
floatSliderBar = new TestSliderBar<float> { RelativeSizeAxes = Axes.X },
|
||||
doubleSliderBar = new TestSliderBar<double> { RelativeSizeAxes = Axes.X }
|
||||
}
|
||||
};
|
||||
|
||||
intSliderBar.Current.BindTo(intValue);
|
||||
floatSliderBar.Current.BindTo(floatValue);
|
||||
doubleSliderBar.Current.BindTo(doubleValue);
|
||||
|
||||
intValue.ValueChanged += setValue;
|
||||
floatValue.ValueChanged += setValue;
|
||||
doubleValue.ValueChanged += setValue;
|
||||
|
||||
AddStep("Value = 0", () => setValue(0));
|
||||
AddStep("Digits = 0", () => setDecimalDigits(0));
|
||||
AddAssert("Check all 0", () => checkExact("0"));
|
||||
|
||||
AddStep("Digits = 3", () => setDecimalDigits(3));
|
||||
AddAssert("Check 0.000", () => checkExact(0.000m));
|
||||
|
||||
AddStep("Value = 0.5", () => setValue(0.5));
|
||||
AddAssert("Check 0.500", () => checkExact(0.500m));
|
||||
|
||||
AddStep("Value = 123.4567", () => setValue(123.4567));
|
||||
AddAssert("Check 123.457", () => checkExact(123.457m));
|
||||
|
||||
AddStep("Value = 765.4312", () => setValue(765.4312));
|
||||
AddAssert("Check 765.431", () => checkExact(765.431m));
|
||||
|
||||
AddStep("Value = -12.3456", () => setValue(-12.3456));
|
||||
AddAssert("Check -12.346", () => checkExact(-12.346m));
|
||||
AddStep("Digits = 1", () => setDecimalDigits(1));
|
||||
AddAssert("Check -12.3", () => checkExact(-12.3m));
|
||||
AddStep("Digits = 0", () => setDecimalDigits(0));
|
||||
AddAssert("Check -12", () => checkExact(-12m));
|
||||
|
||||
AddStep("Value = -12.8", () => setValue(-12.8));
|
||||
AddAssert("Check -13", () => checkExact(-13m));
|
||||
AddStep("Digits = 1", () => setDecimalDigits(1));
|
||||
AddAssert("Check -12.8", () => checkExact(-12.8m));
|
||||
|
||||
AddSliderStep("Digits", 0, 7, 1, setDecimalDigits);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether all sliderbar tooltips display an exact value.
|
||||
/// </summary>
|
||||
/// <param name="value">The expected value that should be displayed.</param>
|
||||
private bool checkExact(string value)
|
||||
=> intSliderBar.TooltipText == value
|
||||
&& floatSliderBar.TooltipText == value
|
||||
&& doubleSliderBar.TooltipText == value;
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether all sliderbar tooltips display an exact value.
|
||||
/// </summary>
|
||||
/// <param name="value">The expected value that should be displayed.</param>
|
||||
private bool checkExact(decimal value)
|
||||
{
|
||||
var expectedDecimal = value.ToString(intSliderBar.Format);
|
||||
|
||||
return intSliderBar.TooltipText == Convert.ToInt32(value).ToString("N0")
|
||||
&& floatSliderBar.TooltipText == expectedDecimal
|
||||
&& doubleSliderBar.TooltipText == expectedDecimal;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether all floating-point sliderbar tooltips have a certain number of decimal digits.
|
||||
/// </summary>
|
||||
/// <param name="decimals">The expected number of decimal digits.</param>
|
||||
private bool checkDecimalDigits(int decimals)
|
||||
=> checkDecimalDigits(decimals, floatSliderBar.TooltipText)
|
||||
&& checkDecimalDigits(decimals, doubleSliderBar.TooltipText);
|
||||
|
||||
private bool checkDecimalDigits(int decimals, string value)
|
||||
=> value.Length - value.IndexOf(intSliderBar.Format.NumberDecimalSeparator, StringComparison.InvariantCulture) - 1 == decimals;
|
||||
|
||||
private void setValue<T>(T value)
|
||||
{
|
||||
intValue.Value = Convert.ToInt32(value);
|
||||
floatValue.Value = Convert.ToSingle(value);
|
||||
doubleValue.Value = Convert.ToDouble(value);
|
||||
}
|
||||
|
||||
private void setDecimalDigits(int digits)
|
||||
{
|
||||
intSliderBar.Format.NumberDecimalDigits = digits;
|
||||
floatSliderBar.Format.NumberDecimalDigits = digits;
|
||||
doubleSliderBar.Format.NumberDecimalDigits = digits;
|
||||
|
||||
// Make sure that the text referenced in TestSliderBar is updated
|
||||
// This doesn't break any assertions if missing, but breaks the visual display
|
||||
intSliderBar.Current.TriggerChange();
|
||||
floatSliderBar.Current.TriggerChange();
|
||||
doubleSliderBar.Current.TriggerChange();
|
||||
}
|
||||
|
||||
private class TestSliderBar<T> : OsuSliderBar<T>
|
||||
where T : struct, IEquatable<T>, IComparable, IConvertible
|
||||
{
|
||||
public TestSliderBar()
|
||||
{
|
||||
SpriteText valueText;
|
||||
AddInternal(valueText = new OsuSpriteText
|
||||
{
|
||||
Anchor = Anchor.CentreRight,
|
||||
Origin = Anchor.CentreLeft,
|
||||
X = 5,
|
||||
Text = TooltipText
|
||||
});
|
||||
|
||||
Current.ValueChanged += v => valueText.Text = TooltipText;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -132,8 +132,6 @@
|
||||
<Compile Include="Visual\TestCaseOnScreenDisplay.cs" />
|
||||
<Compile Include="Visual\TestCaseAllPlayers.cs" />
|
||||
<Compile Include="Visual\TestCaseOsuGame.cs" />
|
||||
<Compile Include="Visual\TestCaseSliderBarPercentage.cs" />
|
||||
<Compile Include="Visual\TestCaseSliderBarPrecision.cs" />
|
||||
<Compile Include="Visual\TestCasePlaybackControl.cs" />
|
||||
<Compile Include="Visual\TestCasePlaySongSelect.cs" />
|
||||
<Compile Include="Visual\TestCasePopupDialog.cs" />
|
||||
|
@ -28,24 +28,6 @@ namespace osu.Game.Graphics.UserInterface
|
||||
private readonly Box leftBox;
|
||||
private readonly Box rightBox;
|
||||
|
||||
private NumberFormatInfo format;
|
||||
public NumberFormatInfo Format
|
||||
{
|
||||
get => format ?? (format = createDefaultFormat());
|
||||
set
|
||||
{
|
||||
if (format == value)
|
||||
return;
|
||||
format = value;
|
||||
|
||||
if (IsLoaded)
|
||||
{
|
||||
// Some users may want to see the updated ToolTipText
|
||||
Current.TriggerChange();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual string TooltipText
|
||||
{
|
||||
get
|
||||
@ -60,9 +42,9 @@ namespace osu.Game.Graphics.UserInterface
|
||||
var floatMaxValue = bindableDouble?.MaxValue ?? bindableFloat.MaxValue;
|
||||
|
||||
if (floatMaxValue == 1 && (floatMinValue == 0 || floatMinValue == -1))
|
||||
return floatValue.Value.ToString("P", Format);
|
||||
return floatValue.Value.ToString("P0");
|
||||
|
||||
return floatValue.Value.ToString("F", Format);
|
||||
return floatValue.Value.ToString("N1");
|
||||
}
|
||||
|
||||
var bindableInt = CurrentNumber as BindableNumber<int>;
|
||||
|
@ -2,7 +2,6 @@
|
||||
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Game.Graphics.UserInterface;
|
||||
@ -24,15 +23,6 @@ namespace osu.Game.Overlays.Settings
|
||||
RelativeSizeAxes = Axes.X
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// The format that will be used for the tooltip when the sliderbar is hovered.
|
||||
/// </summary>
|
||||
public NumberFormatInfo Format
|
||||
{
|
||||
get => ((U)Control).Format;
|
||||
set => ((U)Control).Format = value;
|
||||
}
|
||||
|
||||
public float KeyboardStep;
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
|
@ -59,7 +59,6 @@ namespace osu.Game.Screens.Play.ReplaySettings
|
||||
}
|
||||
};
|
||||
|
||||
sliderbar.Format.NumberDecimalDigits = 2;
|
||||
sliderbar.Bindable.ValueChanged += rateMultiplier => multiplierText.Text = $"{rateMultiplier}x";
|
||||
}
|
||||
|
||||
@ -73,5 +72,6 @@ namespace osu.Game.Screens.Play.ReplaySettings
|
||||
var clockRate = AdjustableClock.Rate;
|
||||
sliderbar.Bindable.ValueChanged += rateMultiplier => AdjustableClock.Rate = clockRate * rateMultiplier;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -21,6 +21,8 @@ namespace osu.Game.Screens.Play.ReplaySettings
|
||||
|
||||
private class Sliderbar : OsuSliderBar<T>
|
||||
{
|
||||
public override string TooltipText => $"{CurrentNumber.Value}";
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(OsuColour colours)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user