2019-06-21 13:04:34 +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.
|
|
|
|
|
2021-06-28 03:00:07 +00:00
|
|
|
using osu.Framework.Bindables;
|
2019-06-21 13:04:34 +00:00
|
|
|
using osu.Framework.Graphics;
|
2021-06-28 03:00:07 +00:00
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Graphics.UserInterface;
|
2019-06-21 13:04:34 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Settings
|
|
|
|
{
|
2021-06-28 03:00:07 +00:00
|
|
|
public class SettingsNumberBox : SettingsItem<int?>
|
2019-06-21 13:04:34 +00:00
|
|
|
{
|
2021-06-28 03:00:07 +00:00
|
|
|
protected override Drawable CreateControl() => new NumberControl
|
2019-06-21 13:04:34 +00:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
2021-06-28 03:00:07 +00:00
|
|
|
Margin = new MarginPadding { Top = 5 }
|
2019-06-21 13:04:34 +00:00
|
|
|
};
|
2021-06-28 03:00:07 +00:00
|
|
|
|
|
|
|
private sealed class NumberControl : CompositeDrawable, IHasCurrentValue<int?>
|
|
|
|
{
|
|
|
|
private readonly BindableWithCurrent<int?> current = new BindableWithCurrent<int?>();
|
|
|
|
|
|
|
|
public Bindable<int?> Current
|
|
|
|
{
|
2021-06-28 03:20:00 +00:00
|
|
|
get => current.Current;
|
|
|
|
set => current.Current = value;
|
2021-06-28 03:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public NumberControl()
|
|
|
|
{
|
|
|
|
AutoSizeAxes = Axes.Y;
|
|
|
|
|
2021-06-28 03:20:00 +00:00
|
|
|
OutlinedNumberBox numberBox;
|
|
|
|
|
2021-06-28 03:00:07 +00:00
|
|
|
InternalChildren = new[]
|
|
|
|
{
|
|
|
|
numberBox = new OutlinedNumberBox
|
|
|
|
{
|
|
|
|
Margin = new MarginPadding { Top = 5 },
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
CommitOnFocusLost = true
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
numberBox.Current.BindValueChanged(e =>
|
|
|
|
{
|
|
|
|
int? value = null;
|
|
|
|
|
|
|
|
if (int.TryParse(e.NewValue, out var intVal))
|
|
|
|
value = intVal;
|
|
|
|
|
|
|
|
current.Value = value;
|
|
|
|
});
|
|
|
|
|
2021-06-28 03:20:00 +00:00
|
|
|
Current.BindValueChanged(e =>
|
|
|
|
{
|
|
|
|
numberBox.Current.Value = e.NewValue?.ToString();
|
|
|
|
});
|
2021-06-28 03:00:07 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-28 06:24:55 +00:00
|
|
|
|
|
|
|
private class OutlinedNumberBox : OutlinedTextBox
|
|
|
|
{
|
|
|
|
protected override bool CanAddCharacter(char character) => char.IsNumber(character);
|
|
|
|
}
|
2019-06-21 13:04:34 +00:00
|
|
|
}
|
|
|
|
}
|