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.
|
|
|
|
|
2024-05-22 08:29:39 +00:00
|
|
|
using System.Numerics;
|
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;
|
2024-05-22 08:29:39 +00:00
|
|
|
using Vector2 = osuTK.Vector2;
|
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>
|
2024-05-22 08:29:39 +00:00
|
|
|
where T : struct, INumber<T>, IMinMaxValue<T>
|
2020-09-30 08:52:12 +00:00
|
|
|
{
|
2023-07-31 22:51:29 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Bindable<T> Current
|
|
|
|
{
|
|
|
|
get => slider.Current;
|
|
|
|
set => slider.Current = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool instantaneous;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Whether changes to the slider should instantaneously transfer to the text box (and vice versa).
|
|
|
|
/// If <see langword="false"/>, the transfer will happen on text box commit (explicit, or implicit via focus loss), or on slider drag end.
|
|
|
|
/// </summary>
|
|
|
|
public bool Instantaneous
|
|
|
|
{
|
|
|
|
get => instantaneous;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
instantaneous = value;
|
|
|
|
slider.TransferValueOnCommit = !instantaneous;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 08:52:12 +00:00
|
|
|
private readonly SettingsSlider<T> slider;
|
2023-07-31 22:51:29 +00:00
|
|
|
private readonly LabelledTextBox textBox;
|
2020-09-30 08:52:12 +00:00
|
|
|
|
2021-10-30 16:50:13 +00:00
|
|
|
public SliderWithTextBoxInput(LocalisableString labelText)
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-07-31 22:51:29 +00:00
|
|
|
textBox.OnCommit += textCommitted;
|
|
|
|
textBox.Current.BindValueChanged(textChanged);
|
2022-12-01 07:08:14 +00:00
|
|
|
|
2023-07-31 22:51:29 +00:00
|
|
|
Current.BindValueChanged(updateTextBoxFromSlider, true);
|
|
|
|
}
|
2022-12-01 07:08:14 +00:00
|
|
|
|
2024-05-27 09:02:51 +00:00
|
|
|
public bool TakeFocus() => GetContainingFocusManager()?.ChangeFocus(textBox) == true;
|
2023-07-09 19:00:35 +00:00
|
|
|
|
2024-05-25 16:41:31 +00:00
|
|
|
public bool SelectAll() => textBox.SelectAll();
|
|
|
|
|
2023-07-31 22:51:29 +00:00
|
|
|
private bool updatingFromTextBox;
|
2020-10-01 04:06:24 +00:00
|
|
|
|
2023-07-31 22:51:29 +00:00
|
|
|
private void textChanged(ValueChangedEvent<string> change)
|
|
|
|
{
|
|
|
|
if (!instantaneous) return;
|
2020-09-30 08:52:12 +00:00
|
|
|
|
2023-07-31 22:51:29 +00:00
|
|
|
tryUpdateSliderFromTextBox();
|
2020-09-30 08:52:12 +00:00
|
|
|
}
|
|
|
|
|
2023-07-31 22:51:29 +00:00
|
|
|
private void textCommitted(TextBox t, bool isNew)
|
2021-03-19 08:13:30 +00:00
|
|
|
{
|
2023-07-31 22:51:29 +00:00
|
|
|
tryUpdateSliderFromTextBox();
|
|
|
|
|
|
|
|
// If the attempted update above failed, restore text box to match the slider.
|
|
|
|
Current.TriggerChange();
|
2021-03-19 08:13:30 +00:00
|
|
|
}
|
|
|
|
|
2023-07-31 22:51:29 +00:00
|
|
|
private void tryUpdateSliderFromTextBox()
|
2020-09-30 08:52:12 +00:00
|
|
|
{
|
2023-07-31 22:51:29 +00:00
|
|
|
updatingFromTextBox = true;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
switch (slider.Current)
|
|
|
|
{
|
|
|
|
case Bindable<int> bindableInt:
|
|
|
|
bindableInt.Value = int.Parse(textBox.Current.Value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Bindable<double> bindableDouble:
|
|
|
|
bindableDouble.Value = double.Parse(textBox.Current.Value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-12-13 05:07:38 +00:00
|
|
|
slider.Current.Parse(textBox.Current.Value, CultureInfo.CurrentCulture);
|
2023-07-31 22:51:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
// ignore parsing failures.
|
|
|
|
// sane state will eventually be restored by a commit (either explicit, or implicit via focus loss).
|
|
|
|
}
|
|
|
|
|
|
|
|
updatingFromTextBox = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateTextBoxFromSlider(ValueChangedEvent<T> _)
|
|
|
|
{
|
|
|
|
if (updatingFromTextBox) return;
|
|
|
|
|
2024-05-22 08:29:39 +00:00
|
|
|
decimal decimalValue = decimal.CreateTruncating(slider.Current.Value);
|
2023-07-31 22:51:29 +00:00
|
|
|
textBox.Text = decimalValue.ToString($@"N{FormatUtils.FindPrecision(decimalValue)}");
|
2020-09-30 08:52:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|