From edf073dacb7cdf6e4dfae9974e33d7889b911629 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sun, 18 Aug 2019 17:59:14 +0200 Subject: [PATCH] added rules for text input --- Demos/UiDemo.cs | 6 ++++++ MLEM.Ui/Elements/TextField.cs | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Demos/UiDemo.cs b/Demos/UiDemo.cs index 7f7fbc3..badb355 100644 --- a/Demos/UiDemo.cs +++ b/Demos/UiDemo.cs @@ -64,6 +64,8 @@ namespace Demos { root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "This is a small demo for MLEM.Ui, a user interface library that is part of (M)LEM (L)ibrary by (E)llpeck for (M)onoGame.")); var image = root.AddChild(new Image(Anchor.AutoCenter, new Vector2(50, 50), new TextureRegion(this.testTexture, 0, 0, 8, 8)) {IsHidden = true, Padding = new Point(3)}); + // Setting their the x or y coordinate to 1 or a lower number causes the width or height to be a percentage of the parent's width or height + // (for example, setting the size's x to 0.75 would make the element's width be 0.75*parentWidth) root.AddChild(new Button(Anchor.AutoCenter, new Vector2(1, 10), "Toggle Test Image", "This button shows a grass tile as a test image to show the automatic anchoring of objects.") { OnClicked = (element, button) => { if (button == MouseButton.Left) @@ -89,6 +91,10 @@ namespace Demos { root.AddChild(new Paragraph(Anchor.AutoCenter, 1, "Text input:", true)); root.AddChild(new TextField(Anchor.AutoLeft, new Vector2(1, 10)) {PositionOffset = new Vector2(0, 1)}); + root.AddChild(new VerticalSpace(3)); + root.AddChild(new Paragraph(Anchor.AutoCenter, 1, "Numbers-only input:", true)); + root.AddChild(new TextField(Anchor.AutoLeft, new Vector2(1, 10), TextField.OnlyNumbers) {PositionOffset = new Vector2(0, 1)}); + root.AddChild(new VerticalSpace(3)); root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Zoom in or out:")); root.AddChild(new Button(Anchor.AutoLeft, new Vector2(10), "+") { diff --git a/MLEM.Ui/Elements/TextField.cs b/MLEM.Ui/Elements/TextField.cs index a0b3a67..89082a7 100644 --- a/MLEM.Ui/Elements/TextField.cs +++ b/MLEM.Ui/Elements/TextField.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; @@ -11,6 +12,11 @@ using MLEM.Ui.Style; namespace MLEM.Ui.Elements { public class TextField : Element { + public static readonly Rule DefaultRule = (field, add) => !add.Any(char.IsControl); + public static readonly Rule OnlyLetters = (field, add) => add.All(char.IsLetter); + public static readonly Rule OnlyNumbers = (field, add) => add.All(char.IsNumber); + public static readonly Rule LettersNumbers = (field, add) => add.All(c => char.IsLetter(c) || char.IsNumber(c)); + public NinePatch Texture; public NinePatch HoveredTexture; public Color HoveredColor; @@ -22,8 +28,10 @@ namespace MLEM.Ui.Elements { private IGenericFont font; private double caretBlinkTimer; private int textStartIndex; + public Rule InputRule; - public TextField(Anchor anchor, Vector2 size, IGenericFont font = null) : base(anchor, size) { + public TextField(Anchor anchor, Vector2 size, Rule rule = null, IGenericFont font = null) : base(anchor, size) { + this.InputRule = rule ?? DefaultRule; this.font = font; this.OnTextInput += (element, key, character) => { if (!this.IsSelected) @@ -34,7 +42,7 @@ namespace MLEM.Ui.Elements { this.Text.Remove(this.Text.Length - 1, 1); textChanged = true; } - } else if (!char.IsControl(character)) { + } else if (this.InputRule(this, character.ToString())) { if (this.Text.Length < this.MaxTextLength) { this.Text.Append(character); textChanged = true; @@ -94,5 +102,7 @@ namespace MLEM.Ui.Elements { public delegate void TextChanged(TextField field, string text); + public delegate bool Rule(TextField field, string textToAdd); + } } \ No newline at end of file