// 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 osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.UserInterface; using osu.Game.Graphics.UserInterfaceV2; using osu.Game.Overlays.Settings; namespace osu.Game.Screens.Edit.Timing { internal class SliderWithTextBoxInput : CompositeDrawable, IHasCurrentValue where T : struct, IEquatable, IComparable, IConvertible { private readonly SettingsSlider slider; public SliderWithTextBoxInput(string labelText) { LabelledTextBox textbox; RelativeSizeAxes = Axes.X; AutoSizeAxes = Axes.Y; InternalChildren = new Drawable[] { new FillFlowContainer { RelativeSizeAxes = Axes.X, AutoSizeAxes = Axes.Y, Direction = FillDirection.Vertical, Children = new Drawable[] { textbox = new LabelledTextBox { Label = labelText, }, slider = new SettingsSlider { RelativeSizeAxes = Axes.X, } } }, }; textbox.OnCommit += (t, isNew) => { if (!isNew) return; try { slider.Bindable.Parse(t.Text); } catch { // TriggerChange below will restore the previous text value on failure. } // 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(); }; Current.BindValueChanged(val => { textbox.Text = val.NewValue.ToString(); }, true); } public Bindable Current { get => slider.Bindable; set => slider.Bindable = value; } } }