// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.Globalization; using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.UserInterface; using osu.Framework.Localisation; using osu.Game.Overlays.Settings; using osu.Game.Utils; using osuTK; namespace osu.Game.Graphics.UserInterfaceV2 { public partial class SliderWithTextBoxInput : CompositeDrawable, IHasCurrentValue where T : struct, IEquatable, IComparable, IConvertible { /// /// A custom step value for each key press which actuates a change on this control. /// public float KeyboardStep { get => slider.KeyboardStep; set => slider.KeyboardStep = value; } public Bindable Current { get => slider.Current; set => slider.Current = value; } private bool instantaneous; /// /// Whether changes to the slider should instantaneously transfer to the text box (and vice versa). /// If , the transfer will happen on text box commit (explicit, or implicit via focus loss), or on slider drag end. /// public bool Instantaneous { get => instantaneous; set { instantaneous = value; slider.TransferValueOnCommit = !instantaneous; } } private readonly SettingsSlider slider; private readonly LabelledTextBox textBox; public SliderWithTextBoxInput(LocalisableString labelText) { RelativeSizeAxes = Axes.X; AutoSizeAxes = Axes.Y; InternalChildren = new Drawable[] { new FillFlowContainer { RelativeSizeAxes = Axes.X, AutoSizeAxes = Axes.Y, Direction = FillDirection.Vertical, Spacing = new Vector2(20), Children = new Drawable[] { textBox = new LabelledTextBox { Label = labelText, }, slider = new SettingsSlider { TransferValueOnCommit = true, RelativeSizeAxes = Axes.X, } } }, }; textBox.OnCommit += textCommitted; textBox.Current.BindValueChanged(textChanged); Current.BindValueChanged(updateTextBoxFromSlider, true); } public bool TakeFocus() => GetContainingInputManager().ChangeFocus(textBox); private bool updatingFromTextBox; private void textChanged(ValueChangedEvent change) { if (!instantaneous) return; tryUpdateSliderFromTextBox(); } private void textCommitted(TextBox t, bool isNew) { tryUpdateSliderFromTextBox(); // If the attempted update above failed, restore text box to match the slider. Current.TriggerChange(); } private void tryUpdateSliderFromTextBox() { updatingFromTextBox = true; try { switch (slider.Current) { case Bindable bindableInt: bindableInt.Value = int.Parse(textBox.Current.Value); break; case Bindable bindableDouble: bindableDouble.Value = double.Parse(textBox.Current.Value); break; default: slider.Current.Parse(textBox.Current.Value); 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 _) { if (updatingFromTextBox) return; decimal decimalValue = slider.Current.Value.ToDecimal(NumberFormatInfo.InvariantInfo); textBox.Text = decimalValue.ToString($@"N{FormatUtils.FindPrecision(decimalValue)}"); } } }