From c3aecc1a11eed5faef756685445654662e849a47 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Tue, 10 Mar 2026 19:47:09 -0500 Subject: [PATCH] Add allowable characters enum to text box --- Source/Engine/UI/GUI/Common/TextBoxBase.cs | 90 ++++++++++++++++++++-- 1 file changed, 83 insertions(+), 7 deletions(-) diff --git a/Source/Engine/UI/GUI/Common/TextBoxBase.cs b/Source/Engine/UI/GUI/Common/TextBoxBase.cs index 489e4c93d..201e91c61 100644 --- a/Source/Engine/UI/GUI/Common/TextBoxBase.cs +++ b/Source/Engine/UI/GUI/Common/TextBoxBase.cs @@ -1,6 +1,8 @@ // Copyright (c) Wojciech Figat. All rights reserved. using System; +using System.Collections.Generic; +using System.Linq; #if FLAX_EDITOR using FlaxEditor.Options; #endif @@ -42,6 +44,38 @@ namespace FlaxEngine.GUI '<', }; + /// + /// The allowable characters to use for the text. + /// + [Flags] + public enum AllowableCharacters + { + /// + /// Wether to not allow any character in the text. + /// + None = 0, + + /// + /// Whether to use letters in the text. + /// + Letters = 1 << 0, + + /// + /// Whether to use numbers in the text. + /// + Numbers = 1 << 1, + + /// + /// Whether to use symbols in the text. + /// + Symbols = 1 << 2, + + /// + /// Whether to use all characters in the text. + /// + All = Letters | Numbers | Symbols, + } + /// /// Default height of the text box /// @@ -86,6 +120,11 @@ namespace FlaxEngine.GUI /// Flag used to indicate whenever text can contain multiple lines. /// protected bool _isMultiline; + + /// + /// The characters to allow in the text. + /// + protected AllowableCharacters _charactersToAllow = AllowableCharacters.All; /// /// Flag used to indicate whenever text is read-only and cannot be modified by the user. @@ -188,6 +227,16 @@ namespace FlaxEngine.GUI } } + /// + /// The character to allow in the text. + /// + [EditorOrder(41), Tooltip("The character to allow in the text.")] + public AllowableCharacters CharactersToAllow + { + get => _charactersToAllow; + set => _charactersToAllow = value; + } + /// /// Gets or sets the maximum number of characters the user can type into the text box control. /// @@ -395,15 +444,42 @@ namespace FlaxEngine.GUI value = value.GetLines()[0]; } - if (_text != value) + if (_text.Equals(value, StringComparison.Ordinal)) + return; + + if (CharactersToAllow != AllowableCharacters.All) { - Deselect(); - ResetViewOffset(); - - _text = value; - - OnTextChanged(); + if (CharactersToAllow == AllowableCharacters.None) + { + value = string.Empty; + } + else + { + if (!CharactersToAllow.HasFlag(AllowableCharacters.Letters)) + { + if (value != null) + value = new string(value.Where(c => !char.IsLetter(c)).ToArray()); + } + if (!CharactersToAllow.HasFlag(AllowableCharacters.Numbers)) + { + if (value != null) + value = new string(value.Where(c => !char.IsNumber(c)).ToArray()); + } + if (!CharactersToAllow.HasFlag(AllowableCharacters.Symbols)) + { + if (value != null) + value = new string(value.Where(c => !char.IsSymbol(c)).ToArray()); + } + value ??= string.Empty; + } } + + Deselect(); + ResetViewOffset(); + + _text = value; + + OnTextChanged(); } ///