1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 12:58:33 +01:00

Added a masking character to text fields

This commit is contained in:
Ell 2021-07-08 18:17:39 +02:00
parent 8d92131630
commit bb189261d7
3 changed files with 36 additions and 6 deletions

View file

@ -18,6 +18,9 @@ Improvements
- Moved sound-related classes into Sound namespace - Moved sound-related classes into Sound namespace
### MLEM.Ui ### MLEM.Ui
Additions
- Added a masking character to TextField to allow for password-style text fields
Fixes Fixes
- Fixed a crash if a paragraph has a link formatting code, but no font - Fixed a crash if a paragraph has a link formatting code, but no font

View file

@ -101,6 +101,13 @@ namespace Demos {
this.root.AddChild(new Paragraph(Anchor.AutoCenter, 1, "Numbers-only input:", true)); this.root.AddChild(new Paragraph(Anchor.AutoCenter, 1, "Numbers-only input:", true));
this.root.AddChild(new TextField(Anchor.AutoLeft, new Vector2(1, 10), TextField.OnlyNumbers) {PositionOffset = new Vector2(0, 1)}); this.root.AddChild(new TextField(Anchor.AutoLeft, new Vector2(1, 10), TextField.OnlyNumbers) {PositionOffset = new Vector2(0, 1)});
this.root.AddChild(new VerticalSpace(3));
this.root.AddChild(new Paragraph(Anchor.AutoCenter, 1, "Password-style input:", true));
this.root.AddChild(new TextField(Anchor.AutoLeft, new Vector2(1, 10), text: "secret") {
PositionOffset = new Vector2(0, 1),
MaskingCharacter = '*'
});
this.root.AddChild(new VerticalSpace(3)); this.root.AddChild(new VerticalSpace(3));
this.root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Zoom in or out:")); this.root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Zoom in or out:"));
this.root.AddChild(new Button(Anchor.AutoLeft, new Vector2(10), "+") { this.root.AddChild(new Button(Anchor.AutoLeft, new Vector2(10), "+") {

View file

@ -74,7 +74,6 @@ namespace MLEM.Ui.Elements {
/// The font that this text field should display text with /// The font that this text field should display text with
/// </summary> /// </summary>
public StyleProp<GenericFont> Font; public StyleProp<GenericFont> Font;
private readonly StringBuilder text = new StringBuilder();
/// <summary> /// <summary>
/// This text field's current text /// This text field's current text
/// </summary> /// </summary>
@ -95,9 +94,6 @@ namespace MLEM.Ui.Elements {
/// The width that the caret should render with. /// The width that the caret should render with.
/// </summary> /// </summary>
public float CaretWidth = 0.5F; public float CaretWidth = 0.5F;
private double caretBlinkTimer;
private string displayedText;
private int textOffset;
/// <summary> /// <summary>
/// The rule used for text input. /// The rule used for text input.
/// Rules allow only certain characters to be allowed inside of a text field. /// Rules allow only certain characters to be allowed inside of a text field.
@ -111,7 +107,6 @@ namespace MLEM.Ui.Elements {
/// The description of the <c>KeyboardInput</c> field on mobile devices and consoles /// The description of the <c>KeyboardInput</c> field on mobile devices and consoles
/// </summary> /// </summary>
public string MobileDescription; public string MobileDescription;
private int caretPos;
/// <summary> /// <summary>
/// The position of the caret within the text. /// The position of the caret within the text.
/// This is always between 0 and the <see cref="string.Length"/> of <see cref="Text"/> /// This is always between 0 and the <see cref="string.Length"/> of <see cref="Text"/>
@ -128,6 +123,26 @@ namespace MLEM.Ui.Elements {
} }
} }
} }
/// <summary>
/// A character that should be displayed instead of this text field's <see cref="Text"/> content.
/// The amount of masking characters displayed will be equal to the <see cref="Text"/>'s length.
/// This behavior is useful for password fields or similar.
/// </summary>
public char? MaskingCharacter {
get => this.maskingCharacter;
set {
this.maskingCharacter = value;
this.HandleTextChange(false);
}
}
private readonly StringBuilder text = new StringBuilder();
private char? maskingCharacter;
private double caretBlinkTimer;
private string displayedText;
private int textOffset;
private int caretPos;
/// <summary> /// <summary>
/// Creates a new text field with the given settings /// Creates a new text field with the given settings
@ -136,10 +151,13 @@ namespace MLEM.Ui.Elements {
/// <param name="size">The text field's size</param> /// <param name="size">The text field's size</param>
/// <param name="rule">The text field's input rule</param> /// <param name="rule">The text field's input rule</param>
/// <param name="font">The font to use for drawing text</param> /// <param name="font">The font to use for drawing text</param>
public TextField(Anchor anchor, Vector2 size, Rule rule = null, GenericFont font = null) : base(anchor, size) { /// <param name="text">The text that the text field should contain by default</param>
public TextField(Anchor anchor, Vector2 size, Rule rule = null, GenericFont font = null, string text = null) : base(anchor, size) {
this.InputRule = rule ?? DefaultRule; this.InputRule = rule ?? DefaultRule;
if (font != null) if (font != null)
this.Font.Set(font); this.Font.Set(font);
if (text != null)
this.SetText(text, true);
MlemPlatform.EnsureExists(); MlemPlatform.EnsureExists();
this.OnPressed += async e => { this.OnPressed += async e => {
@ -190,6 +208,8 @@ namespace MLEM.Ui.Elements {
this.displayedText = this.Text; this.displayedText = this.Text;
this.textOffset = 0; this.textOffset = 0;
} }
if (this.MaskingCharacter != null)
this.displayedText = new string(this.MaskingCharacter.Value, this.displayedText.Length);
if (textChanged) if (textChanged)
this.OnTextChange?.Invoke(this, this.Text); this.OnTextChange?.Invoke(this, this.Text);