1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 17:57:29 +08:00
osu-lazer/osu.Game/Overlays/Settings/SettingsNumberBox.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

77 lines
2.4 KiB
C#
Raw Normal View History

// 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 osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.UserInterface;
namespace osu.Game.Overlays.Settings
{
public partial class SettingsNumberBox : SettingsItem<int?>
{
protected override Drawable CreateControl() => new NumberControl
{
RelativeSizeAxes = Axes.X,
};
private sealed partial class NumberControl : CompositeDrawable, IHasCurrentValue<int?>
{
private readonly BindableWithCurrent<int?> current = new BindableWithCurrent<int?>();
public Bindable<int?> Current
{
get => current.Current;
set => current.Current = value;
}
public NumberControl()
{
AutoSizeAxes = Axes.Y;
OutlinedNumberBox numberBox;
InternalChildren = new[]
{
numberBox = new OutlinedNumberBox
{
RelativeSizeAxes = Axes.X,
CommitOnFocusLost = true
}
};
numberBox.Current.BindValueChanged(e =>
{
if (string.IsNullOrEmpty(e.NewValue))
{
Current.Value = null;
return;
}
if (int.TryParse(e.NewValue, out int intVal))
Current.Value = intVal;
else
numberBox.NotifyInputError();
// trigger Current again to either restore the previous text box value, or to reformat the new value via .ToString().
Current.TriggerChange();
});
Current.BindValueChanged(e =>
{
numberBox.Current.Value = e.NewValue?.ToString();
});
}
}
private partial class OutlinedNumberBox : OutlinedTextBox
{
protected override bool AllowIme => false;
2024-03-06 10:42:20 +08:00
protected override bool CanAddCharacter(char character) => char.IsAsciiDigit(character);
public new void NotifyInputError() => base.NotifyInputError();
}
}
}