1
0
Fork 0
mirror of https://github.com/ppy/osu-tools.git synced 2025-06-08 07:17:01 +09:00
osu-tools/PerformanceCalculatorGUI/Components/TextBoxes/LimitedLabelledNumberBox.cs
2025-01-31 20:58:38 +05:00

88 lines
2.5 KiB
C#

// 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.Game.Graphics.UserInterface;
using osu.Game.Graphics.UserInterfaceV2;
namespace PerformanceCalculatorGUI.Components.TextBoxes
{
public partial class LimitedLabelledNumberBox : LabelledNumberBox
{
private partial class LimitedNumberBox : OsuNumberBox
{
protected override void OnUserTextAdded(string added)
{
base.OnUserTextAdded(added);
string textToParse = Text;
if (string.IsNullOrEmpty(Text))
{
textToParse = PlaceholderText.ToString();
}
if (int.TryParse(textToParse, out int parsed))
{
if (parsed >= (MinValue ?? int.MinValue) && parsed <= (MaxValue ?? int.MaxValue))
{
Value.Value = parsed;
return;
}
}
DeleteBy(-1);
NotifyInputError();
}
protected override void OnUserTextRemoved(string removed)
{
string textToParse = Text;
if (string.IsNullOrEmpty(Text))
{
textToParse = PlaceholderText.ToString();
}
if (int.TryParse(textToParse, out int parsed))
{
Value.Value = parsed;
return;
}
Value.Value = default;
}
public int? MaxValue { get; set; }
public int? MinValue { get; set; }
public Bindable<int> Value { get; } = new Bindable<int>();
}
protected override OsuTextBox CreateTextBox() => new LimitedNumberBox();
public int? MaxValue
{
set => ((LimitedNumberBox)Component).MaxValue = value;
}
public int? MinValue
{
set => ((LimitedNumberBox)Component).MinValue = value;
}
public Bindable<int> Value => ((LimitedNumberBox)Component).Value;
public bool CommitOnFocusLoss
{
get => Component.CommitOnFocusLost;
set => Component.CommitOnFocusLost = value;
}
public LimitedLabelledNumberBox()
{
CornerRadius = ExtendedLabelledTextBox.CORNER_RADIUS;
}
}
}