From 15081a8fe0e7d314ae2d228bfae5a2e6773fb5ff Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 10 Aug 2019 19:23:08 +0200 Subject: [PATCH] added a text field --- MLEM.Ui/Elements/Paragraph.cs | 1 - MLEM.Ui/Elements/TextField.cs | 75 +++++++++++++++++++++++++++++++++++ Tests/GameImpl.cs | 2 + 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 MLEM.Ui/Elements/TextField.cs diff --git a/MLEM.Ui/Elements/Paragraph.cs b/MLEM.Ui/Elements/Paragraph.cs index a13fcab..b504faf 100644 --- a/MLEM.Ui/Elements/Paragraph.cs +++ b/MLEM.Ui/Elements/Paragraph.cs @@ -25,7 +25,6 @@ namespace MLEM.Ui.Elements { this.text = value; this.SetDirty(); } - } public Paragraph(Anchor anchor, float width, string text, bool centerText = false, IGenericFont font = null) : base(anchor, new Vector2(width, 0)) { diff --git a/MLEM.Ui/Elements/TextField.cs b/MLEM.Ui/Elements/TextField.cs new file mode 100644 index 0000000..bd75a0b --- /dev/null +++ b/MLEM.Ui/Elements/TextField.cs @@ -0,0 +1,75 @@ +using System; +using System.Text; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; +using MLEM.Font; +using MLEM.Textures; + +namespace MLEM.Ui.Elements { + public class TextField : Element { + + public static NinePatch DefaultTexture; + public static NinePatch DefaultHoveredTexture; + public static Color DefaultHoveredColor = Color.LightGray; + + public NinePatch Texture = DefaultTexture; + public NinePatch HoveredTexture = DefaultHoveredTexture; + public Color HoveredColor = DefaultHoveredColor; + public float TextScale; + public readonly StringBuilder Text = new StringBuilder(); + public TextChanged OnTextChange; + public int MaxTextLength = int.MaxValue; + public float TextOffsetX = 4; + private readonly IGenericFont font; + private double caretBlinkTimer; + + public TextField(Anchor anchor, Vector2 size, IGenericFont font = null) : base(anchor, size) { + this.font = font ?? Paragraph.DefaultFont; + this.TextScale = Paragraph.DefaultTextScale; + this.OnTextInput += (element, key, character) => { + if (!this.IsSelected) + return; + var textChanged = false; + if (key == Keys.Back) { + if (this.Text.Length > 0) { + this.Text.Remove(this.Text.Length - 1, 1); + textChanged = true; + } + } else if (!char.IsControl(character)) { + if (this.Text.Length < this.MaxTextLength) { + this.Text.Append(character); + textChanged = true; + } + } + if (textChanged) + this.OnTextChange?.Invoke(this, this.Text.ToString()); + }; + } + + public override void Update(GameTime time) { + base.Update(time); + + this.caretBlinkTimer += time.ElapsedGameTime.TotalSeconds; + if (this.caretBlinkTimer >= 1) + this.caretBlinkTimer = 0; + } + + public override void Draw(GameTime time, SpriteBatch batch, float alpha) { + var tex = this.Texture; + var color = Color.White * alpha; + if (this.IsMouseOver) { + if (this.HoveredTexture != null) + tex = this.HoveredTexture; + color = this.HoveredColor * alpha; + } + batch.Draw(tex, this.DisplayArea, color); + var caret = this.IsSelected && this.caretBlinkTimer >= 0.5F ? "|" : ""; + this.font.DrawCenteredString(batch, this.Text + caret, this.DisplayArea.Location.ToVector2() + new Vector2(this.TextOffsetX, this.DisplayArea.Height / 2), this.TextScale, Color.White * alpha, false, true); + base.Draw(time, batch, alpha); + } + + public delegate void TextChanged(TextField field, string text); + + } +} \ No newline at end of file diff --git a/Tests/GameImpl.cs b/Tests/GameImpl.cs index 1300702..de44b4a 100644 --- a/Tests/GameImpl.cs +++ b/Tests/GameImpl.cs @@ -28,6 +28,7 @@ namespace Tests { Paragraph.DefaultFont = new GenericSpriteFont(LoadContent("Fonts/TestFont")); Paragraph.DefaultTextScale = 0.2F; Button.DefaultTexture = new NinePatch(new TextureRegion(this.testTexture, 24, 8, 16, 16), 4); + TextField.DefaultTexture = Button.DefaultTexture; this.UiSystem.GlobalScale = 5; var root = new Panel(Anchor.BottomLeft, new Vector2(100, 100), new Point(5, 5), this.testPatch); @@ -39,6 +40,7 @@ namespace Tests { image.IsHidden = !image.IsHidden; } }); + root.AddChild(new TextField(Anchor.AutoLeft, new Vector2(1, 15))); this.UiSystem.Add("Test", root); }