Merge branch 'text-box-allow-char' of https://github.com/Tryibion/FlaxEngine into Tryibion-text-box-allow-char

This commit is contained in:
Wojtek Figat
2026-03-11 09:39:10 +01:00

View File

@@ -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
'<',
};
/// <summary>
/// The allowable characters to use for the text.
/// </summary>
[Flags]
public enum AllowableCharacters
{
/// <summary>
/// Wether to not allow any character in the text.
/// </summary>
None = 0,
/// <summary>
/// Whether to use letters in the text.
/// </summary>
Letters = 1 << 0,
/// <summary>
/// Whether to use numbers in the text.
/// </summary>
Numbers = 1 << 1,
/// <summary>
/// Whether to use symbols in the text.
/// </summary>
Symbols = 1 << 2,
/// <summary>
/// Whether to use all characters in the text.
/// </summary>
All = Letters | Numbers | Symbols,
}
/// <summary>
/// Default height of the text box
/// </summary>
@@ -86,6 +120,11 @@ namespace FlaxEngine.GUI
/// Flag used to indicate whenever text can contain multiple lines.
/// </summary>
protected bool _isMultiline;
/// <summary>
/// The characters to allow in the text.
/// </summary>
protected AllowableCharacters _charactersToAllow = AllowableCharacters.All;
/// <summary>
/// Flag used to indicate whenever text is read-only and cannot be modified by the user.
@@ -188,6 +227,16 @@ namespace FlaxEngine.GUI
}
}
/// <summary>
/// The character to allow in the text.
/// </summary>
[EditorOrder(41), Tooltip("The character to allow in the text.")]
public AllowableCharacters CharactersToAllow
{
get => _charactersToAllow;
set => _charactersToAllow = value;
}
/// <summary>
/// Gets or sets the maximum number of characters the user can type into the text box control.
/// </summary>
@@ -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();
}
/// <summary>