Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add input type for decimal numbers #6498

Merged
merged 6 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions osu.Framework/Graphics/UserInterface/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using JetBrains.Annotations;
Expand Down Expand Up @@ -94,9 +95,28 @@ public abstract partial class TextBox : TabbableContainer, IHasCurrentValue<stri
/// </summary>
private bool canAddCharacter(char character)
{
return !char.IsControl(character)
&& (!InputProperties.Type.IsNumerical() || char.IsAsciiDigit(character))
&& CanAddCharacter(character);
if (!CanAddCharacter(character))
// discard characters explicitly overriden by custom implementation.
return false;

if (char.IsControl(character))
// discard control/special characters.
return false;

var currentNumberFormat = CultureInfo.CurrentCulture.NumberFormat;

switch (InputProperties.Type)
{
case TextInputType.Decimal:
return char.IsAsciiDigit(character) || currentNumberFormat.NumberDecimalSeparator.Contains(character);

case TextInputType.Number:
case TextInputType.NumericalPassword:
return char.IsAsciiDigit(character);

default:
return true;
}
}

private bool readOnly;
Expand Down
20 changes: 6 additions & 14 deletions osu.Framework/Input/TextInputType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ public enum TextInputType
Code,

/// <summary>
/// The text input is numerical.
/// The text input is a whole number.
/// </summary>
Number,

/// <summary>
/// The text input is numerical with decimal point.
/// </summary>
Decimal,
Comment on lines +28 to +31
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only thing that worries me with having this separate is the potential of needing to switch between non-decimal and decimal input at some stage. But maybe that's not that frequent of a use case...


/// <summary>
/// The text input is an email address.
/// </summary>
Expand Down Expand Up @@ -61,19 +66,6 @@ public static bool IsPassword(this TextInputType type)
}
}

public static bool IsNumerical(this TextInputType type)
{
switch (type)
{
case TextInputType.Number:
case TextInputType.NumericalPassword:
return true;

default:
return false;
}
}

public static bool SupportsIme(this TextInputType type) => type == TextInputType.Name || type == TextInputType.Text;
}
}
1 change: 1 addition & 0 deletions osu.Framework/Platform/SDL3/SDL3Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,7 @@ public static SDL_TextInputType ToSDLTextInputType(this TextInputType type)
return SDL_TextInputType.SDL_TEXTINPUT_TYPE_TEXT_USERNAME;

case TextInputType.Number:
case TextInputType.Decimal:
return SDL_TextInputType.SDL_TEXTINPUT_TYPE_NUMBER;

case TextInputType.Password:
Expand Down
Loading