2019-06-21 21:04:34 +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.
|
|
|
|
|
2021-06-28 11:00:07 +08:00
|
|
|
using osu.Framework.Bindables;
|
2019-06-21 21:04:34 +08:00
|
|
|
using osu.Framework.Graphics;
|
2021-06-28 11:00:07 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Graphics.UserInterface;
|
2019-06-21 21:04:34 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Settings
|
|
|
|
{
|
2021-06-28 11:00:07 +08:00
|
|
|
public class SettingsNumberBox : SettingsItem<int?>
|
2019-06-21 21:04:34 +08:00
|
|
|
{
|
2021-06-28 11:00:07 +08:00
|
|
|
protected override Drawable CreateControl() => new NumberControl
|
2019-06-21 21:04:34 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
};
|
2021-06-28 11:00:07 +08:00
|
|
|
|
|
|
|
private sealed class NumberControl : CompositeDrawable, IHasCurrentValue<int?>
|
|
|
|
{
|
|
|
|
private readonly BindableWithCurrent<int?> current = new BindableWithCurrent<int?>();
|
|
|
|
|
|
|
|
public Bindable<int?> Current
|
|
|
|
{
|
2021-06-28 11:20:00 +08:00
|
|
|
get => current.Current;
|
|
|
|
set => current.Current = value;
|
2021-06-28 11:00:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public NumberControl()
|
|
|
|
{
|
|
|
|
AutoSizeAxes = Axes.Y;
|
|
|
|
|
2021-06-28 11:20:00 +08:00
|
|
|
OutlinedNumberBox numberBox;
|
|
|
|
|
2021-06-28 11:00:07 +08: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-23 03:41:27 +08:00
|
|
|
if (string.IsNullOrEmpty(e.NewValue))
|
|
|
|
{
|
|
|
|
Current.Value = null;
|
|
|
|
return;
|
|
|
|
}
|
2021-06-28 11:00:07 +08:00
|
|
|
|
2021-10-27 12:04:41 +08: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-23 03:41:27 +08:00
|
|
|
Current.Value = intVal;
|
|
|
|
else
|
|
|
|
numberBox.NotifyInputError();
|
2021-06-28 11:00:07 +08: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-23 03:41:27 +08: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 11:00:07 +08:00
|
|
|
});
|
|
|
|
|
2021-06-28 11:20:00 +08:00
|
|
|
Current.BindValueChanged(e =>
|
|
|
|
{
|
|
|
|
numberBox.Current.Value = e.NewValue?.ToString();
|
|
|
|
});
|
2021-06-28 11:00:07 +08:00
|
|
|
}
|
|
|
|
}
|
2021-06-28 14:24:55 +08:00
|
|
|
|
|
|
|
private class OutlinedNumberBox : OutlinedTextBox
|
|
|
|
{
|
2021-12-08 03:06:22 +08:00
|
|
|
protected override bool CanAddCharacter(char character) => character >= '0' && character <= '9';
|
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-23 03:41:27 +08:00
|
|
|
|
|
|
|
public new void NotifyInputError() => base.NotifyInputError();
|
2021-06-28 14:24:55 +08:00
|
|
|
}
|
2019-06-21 21:04:34 +08:00
|
|
|
}
|
|
|
|
}
|