2020-09-30 08:52:12 +00:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
using System;
|
2022-01-08 15:12:52 +00:00
|
|
|
using System.Globalization;
|
2020-09-30 08:52:12 +00:00
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Graphics.UserInterface;
|
2021-10-30 16:50:13 +00:00
|
|
|
using osu.Framework.Localisation;
|
2020-09-30 08:52:12 +00:00
|
|
|
using osu.Game.Overlays.Settings;
|
2022-01-08 15:12:52 +00:00
|
|
|
using osu.Game.Utils;
|
2022-05-24 09:02:37 +00:00
|
|
|
using osuTK;
|
2020-09-30 08:52:12 +00:00
|
|
|
|
2023-07-31 22:22:08 +00:00
|
|
|
namespace osu.Game.Graphics.UserInterfaceV2
|
2020-09-30 08:52:12 +00:00
|
|
|
{
|
2020-10-06 08:18:41 +00:00
|
|
|
public partial class SliderWithTextBoxInput<T> : CompositeDrawable, IHasCurrentValue<T>
|
2020-09-30 08:52:12 +00:00
|
|
|
where T : struct, IEquatable<T>, IComparable<T>, IConvertible
|
|
|
|
{
|
|
|
|
private readonly SettingsSlider<T> slider;
|
|
|
|
|
2021-10-30 16:50:13 +00:00
|
|
|
public SliderWithTextBoxInput(LocalisableString labelText)
|
2020-09-30 08:52:12 +00:00
|
|
|
{
|
2021-12-10 05:15:00 +00:00
|
|
|
LabelledTextBox textBox;
|
2020-09-30 08:52:12 +00:00
|
|
|
|
|
|
|
RelativeSizeAxes = Axes.X;
|
|
|
|
AutoSizeAxes = Axes.Y;
|
|
|
|
|
|
|
|
InternalChildren = new Drawable[]
|
|
|
|
{
|
|
|
|
new FillFlowContainer
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
Direction = FillDirection.Vertical,
|
2022-05-24 09:02:37 +00:00
|
|
|
Spacing = new Vector2(20),
|
2020-09-30 08:52:12 +00:00
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
2021-12-10 05:15:00 +00:00
|
|
|
textBox = new LabelledTextBox
|
2020-09-30 08:52:12 +00:00
|
|
|
{
|
|
|
|
Label = labelText,
|
|
|
|
},
|
|
|
|
slider = new SettingsSlider<T>
|
|
|
|
{
|
2020-10-02 08:59:57 +00:00
|
|
|
TransferValueOnCommit = true,
|
2020-09-30 08:52:12 +00:00
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-12-10 05:15:00 +00:00
|
|
|
textBox.OnCommit += (t, isNew) =>
|
2020-09-30 08:52:12 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-12-01 07:08:14 +00:00
|
|
|
switch (slider.Current)
|
|
|
|
{
|
|
|
|
case Bindable<int> bindableInt:
|
|
|
|
bindableInt.Value = int.Parse(t.Text);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Bindable<double> bindableDouble:
|
|
|
|
bindableDouble.Value = double.Parse(t.Text);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
slider.Current.Parse(t.Text);
|
|
|
|
break;
|
|
|
|
}
|
2020-09-30 08:52:12 +00:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
2020-10-01 04:06:24 +00:00
|
|
|
// TriggerChange below will restore the previous text value on failure.
|
2020-09-30 08:52:12 +00:00
|
|
|
}
|
2020-10-01 04:06:24 +00:00
|
|
|
|
|
|
|
// This is run regardless of parsing success as the parsed number may not actually trigger a change
|
|
|
|
// due to bindable clamping. Even in such a case we want to update the textbox to a sane visual state.
|
|
|
|
Current.TriggerChange();
|
2020-09-30 08:52:12 +00:00
|
|
|
};
|
|
|
|
|
2022-06-24 12:25:23 +00:00
|
|
|
Current.BindValueChanged(_ =>
|
2020-09-30 08:52:12 +00:00
|
|
|
{
|
2022-01-08 15:12:52 +00:00
|
|
|
decimal decimalValue = slider.Current.Value.ToDecimal(NumberFormatInfo.InvariantInfo);
|
|
|
|
textBox.Text = decimalValue.ToString($@"N{FormatUtils.FindPrecision(decimalValue)}");
|
2020-09-30 08:52:12 +00:00
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
|
2021-03-19 08:13:30 +00:00
|
|
|
/// <summary>
|
|
|
|
/// A custom step value for each key press which actuates a change on this control.
|
|
|
|
/// </summary>
|
|
|
|
public float KeyboardStep
|
|
|
|
{
|
|
|
|
get => slider.KeyboardStep;
|
|
|
|
set => slider.KeyboardStep = value;
|
|
|
|
}
|
|
|
|
|
2020-09-30 08:52:12 +00:00
|
|
|
public Bindable<T> Current
|
|
|
|
{
|
2020-10-06 08:18:41 +00:00
|
|
|
get => slider.Current;
|
|
|
|
set => slider.Current = value;
|
2020-09-30 08:52:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|