mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 12:58:33 +01:00
added rules for text input
This commit is contained in:
parent
15b468bab4
commit
edf073dacb
2 changed files with 18 additions and 2 deletions
|
@ -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), "+") {
|
||||
|
|
|
@ -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);
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue