Skip to content

Commit d3acd17

Browse files
authored
Merge pull request #6498 from frenzibyte/add-decimal-type
Add input type for decimal numbers
2 parents 6bbcfd8 + e1b844f commit d3acd17

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

osu.Framework/Graphics/UserInterface/TextBox.cs

+23-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System;
77
using System.Collections.Generic;
88
using System.Diagnostics;
9+
using System.Globalization;
910
using System.Linq;
1011
using System.Text;
1112
using JetBrains.Annotations;
@@ -94,9 +95,28 @@ public abstract partial class TextBox : TabbableContainer, IHasCurrentValue<stri
9495
/// </summary>
9596
private bool canAddCharacter(char character)
9697
{
97-
return !char.IsControl(character)
98-
&& (!InputProperties.Type.IsNumerical() || char.IsAsciiDigit(character))
99-
&& CanAddCharacter(character);
98+
if (!CanAddCharacter(character))
99+
// discard characters explicitly overriden by custom implementation.
100+
return false;
101+
102+
if (char.IsControl(character))
103+
// discard control/special characters.
104+
return false;
105+
106+
var currentNumberFormat = CultureInfo.CurrentCulture.NumberFormat;
107+
108+
switch (InputProperties.Type)
109+
{
110+
case TextInputType.Decimal:
111+
return char.IsAsciiDigit(character) || currentNumberFormat.NumberDecimalSeparator.Contains(character);
112+
113+
case TextInputType.Number:
114+
case TextInputType.NumericalPassword:
115+
return char.IsAsciiDigit(character);
116+
117+
default:
118+
return true;
119+
}
100120
}
101121

102122
private bool readOnly;

osu.Framework/Input/TextInputType.cs

+6-14
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ public enum TextInputType
2121
Code,
2222

2323
/// <summary>
24-
/// The text input is numerical.
24+
/// The text input is a whole number.
2525
/// </summary>
2626
Number,
2727

28+
/// <summary>
29+
/// The text input is numerical with decimal point.
30+
/// </summary>
31+
Decimal,
32+
2833
/// <summary>
2934
/// The text input is an email address.
3035
/// </summary>
@@ -61,19 +66,6 @@ public static bool IsPassword(this TextInputType type)
6166
}
6267
}
6368

64-
public static bool IsNumerical(this TextInputType type)
65-
{
66-
switch (type)
67-
{
68-
case TextInputType.Number:
69-
case TextInputType.NumericalPassword:
70-
return true;
71-
72-
default:
73-
return false;
74-
}
75-
}
76-
7769
public static bool SupportsIme(this TextInputType type) => type == TextInputType.Name || type == TextInputType.Text;
7870
}
7971
}

osu.Framework/Platform/SDL3/SDL3Extensions.cs

+1
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,7 @@ public static SDL_TextInputType ToSDLTextInputType(this TextInputType type)
10291029
return SDL_TextInputType.SDL_TEXTINPUT_TYPE_TEXT_USERNAME;
10301030

10311031
case TextInputType.Number:
1032+
case TextInputType.Decimal:
10321033
return SDL_TextInputType.SDL_TEXTINPUT_TYPE_NUMBER;
10331034

10341035
case TextInputType.Password:

0 commit comments

Comments
 (0)