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

added a text field

This commit is contained in:
Ellpeck 2019-08-10 19:23:08 +02:00
parent f96511d17d
commit 15081a8fe0
3 changed files with 77 additions and 1 deletions

View file

@ -25,7 +25,6 @@ namespace MLEM.Ui.Elements {
this.text = value; this.text = value;
this.SetDirty(); this.SetDirty();
} }
} }
public Paragraph(Anchor anchor, float width, string text, bool centerText = false, IGenericFont font = null) : base(anchor, new Vector2(width, 0)) { public Paragraph(Anchor anchor, float width, string text, bool centerText = false, IGenericFont font = null) : base(anchor, new Vector2(width, 0)) {

View file

@ -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);
}
}

View file

@ -28,6 +28,7 @@ namespace Tests {
Paragraph.DefaultFont = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont")); Paragraph.DefaultFont = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont"));
Paragraph.DefaultTextScale = 0.2F; Paragraph.DefaultTextScale = 0.2F;
Button.DefaultTexture = new NinePatch(new TextureRegion(this.testTexture, 24, 8, 16, 16), 4); Button.DefaultTexture = new NinePatch(new TextureRegion(this.testTexture, 24, 8, 16, 16), 4);
TextField.DefaultTexture = Button.DefaultTexture;
this.UiSystem.GlobalScale = 5; this.UiSystem.GlobalScale = 5;
var root = new Panel(Anchor.BottomLeft, new Vector2(100, 100), new Point(5, 5), this.testPatch); 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; image.IsHidden = !image.IsHidden;
} }
}); });
root.AddChild(new TextField(Anchor.AutoLeft, new Vector2(1, 15)));
this.UiSystem.Add("Test", root); this.UiSystem.Add("Test", root);
} }