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.
|
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2021-06-28 03:00:07 +00:00
|
|
|
using osu.Framework.Bindables;
|
2021-12-15 06:13:24 +00:00
|
|
|
using osu.Framework.Extensions;
|
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
|
|
|
|
|
|
|
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 =>
|
|
|
|
{
|
Accept full range of `int` in `SettingsNumberBox`
This fixes stack overflow exceptions that would arise when a
`Current.Value` of 1 billion or more was set on a `SettingsNumberBox`.
The stack overflow was caused by the "maximum 9 digits" spec. If a value
technically within `int` bounds, but larger than 1 billion (in the range
[1,000,000,000; 2,147,483,647], to be more precise), a feedback loop
between the setting control's `Current` and its inner text box's
`Current` would occur, wherein the last digit would be trimmed and then
re-appended again forevermore.
To resolve, remove the offending spec and rely on `int.TryParse`
entirely to be able to discern overflow range. Additionally, UX of the
text box is slightly changed to notify when the `int` range is exceeded
with a red flash.
This behaviour would not have been possible to implement without recent
framework-side fixes to text box (removal of text set scheduling).
2021-11-22 19:41:27 +00:00
|
|
|
if (string.IsNullOrEmpty(e.NewValue))
|
|
|
|
{
|
|
|
|
Current.Value = null;
|
|
|
|
return;
|
|
|
|
}
|
2021-06-28 03:00:07 +00:00
|
|
|
|
2021-10-27 04:04:41 +00:00
|
|
|
if (int.TryParse(e.NewValue, out int intVal))
|
Accept full range of `int` in `SettingsNumberBox`
This fixes stack overflow exceptions that would arise when a
`Current.Value` of 1 billion or more was set on a `SettingsNumberBox`.
The stack overflow was caused by the "maximum 9 digits" spec. If a value
technically within `int` bounds, but larger than 1 billion (in the range
[1,000,000,000; 2,147,483,647], to be more precise), a feedback loop
between the setting control's `Current` and its inner text box's
`Current` would occur, wherein the last digit would be trimmed and then
re-appended again forevermore.
To resolve, remove the offending spec and rely on `int.TryParse`
entirely to be able to discern overflow range. Additionally, UX of the
text box is slightly changed to notify when the `int` range is exceeded
with a red flash.
This behaviour would not have been possible to implement without recent
framework-side fixes to text box (removal of text set scheduling).
2021-11-22 19:41:27 +00:00
|
|
|
Current.Value = intVal;
|
|
|
|
else
|
|
|
|
numberBox.NotifyInputError();
|
2021-06-28 03:00:07 +00:00
|
|
|
|
Accept full range of `int` in `SettingsNumberBox`
This fixes stack overflow exceptions that would arise when a
`Current.Value` of 1 billion or more was set on a `SettingsNumberBox`.
The stack overflow was caused by the "maximum 9 digits" spec. If a value
technically within `int` bounds, but larger than 1 billion (in the range
[1,000,000,000; 2,147,483,647], to be more precise), a feedback loop
between the setting control's `Current` and its inner text box's
`Current` would occur, wherein the last digit would be trimmed and then
re-appended again forevermore.
To resolve, remove the offending spec and rely on `int.TryParse`
entirely to be able to discern overflow range. Additionally, UX of the
text box is slightly changed to notify when the `int` range is exceeded
with a red flash.
This behaviour would not have been possible to implement without recent
framework-side fixes to text box (removal of text set scheduling).
2021-11-22 19:41:27 +00:00
|
|
|
// trigger Current again to either restore the previous text box value, or to reformat the new value via .ToString().
|
|
|
|
Current.TriggerChange();
|
2021-06-28 03:00:07 +00:00
|
|
|
});
|
|
|
|
|
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
|
|
|
|
{
|
2022-01-11 23:15:17 +00:00
|
|
|
protected override bool AllowIme => false;
|
|
|
|
|
2021-12-15 06:13:24 +00:00
|
|
|
protected override bool CanAddCharacter(char character) => character.IsAsciiDigit();
|
Accept full range of `int` in `SettingsNumberBox`
This fixes stack overflow exceptions that would arise when a
`Current.Value` of 1 billion or more was set on a `SettingsNumberBox`.
The stack overflow was caused by the "maximum 9 digits" spec. If a value
technically within `int` bounds, but larger than 1 billion (in the range
[1,000,000,000; 2,147,483,647], to be more precise), a feedback loop
between the setting control's `Current` and its inner text box's
`Current` would occur, wherein the last digit would be trimmed and then
re-appended again forevermore.
To resolve, remove the offending spec and rely on `int.TryParse`
entirely to be able to discern overflow range. Additionally, UX of the
text box is slightly changed to notify when the `int` range is exceeded
with a red flash.
This behaviour would not have been possible to implement without recent
framework-side fixes to text box (removal of text set scheduling).
2021-11-22 19:41:27 +00:00
|
|
|
|
|
|
|
public new void NotifyInputError() => base.NotifyInputError();
|
2021-06-28 06:24:55 +00:00
|
|
|
}
|
2019-06-21 13:04:34 +00:00
|
|
|
}
|
|
|
|
}
|