2020-09-30 16:52:12 +08: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;
|
|
|
|
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
|
|
|
|
{
|
2020-10-06 16:18:41 +08:00
|
|
|
public class SliderWithTextBoxInput<T> : CompositeDrawable, IHasCurrentValue<T>
|
2020-09-30 16:52:12 +08:00
|
|
|
where T : struct, IEquatable<T>, IComparable<T>, IConvertible
|
|
|
|
{
|
|
|
|
private readonly SettingsSlider<T> 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<T>
|
|
|
|
{
|
2020-10-02 16:59:57 +08:00
|
|
|
TransferValueOnCommit = true,
|
2020-09-30 16:52:12 +08:00
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
textbox.OnCommit += (t, isNew) =>
|
|
|
|
{
|
|
|
|
if (!isNew) return;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-10-06 16:18:41 +08:00
|
|
|
slider.Current.Parse(t.Text);
|
2020-09-30 16:52:12 +08:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
2020-10-01 12:06:24 +08:00
|
|
|
// TriggerChange below will restore the previous text value on failure.
|
2020-09-30 16:52:12 +08:00
|
|
|
}
|
2020-10-01 12:06:24 +08: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 16:52:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Current.BindValueChanged(val =>
|
|
|
|
{
|
|
|
|
textbox.Text = val.NewValue.ToString();
|
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
|
2021-03-19 16:13:30 +08: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 16:52:12 +08:00
|
|
|
public Bindable<T> Current
|
|
|
|
{
|
2020-10-06 16:18:41 +08:00
|
|
|
get => slider.Current;
|
|
|
|
set => slider.Current = value;
|
2020-09-30 16:52:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|