2019-08-10 19:23:08 +02:00
|
|
|
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;
|
2019-08-10 21:37:10 +02:00
|
|
|
using MLEM.Ui.Style;
|
2019-08-10 19:23:08 +02:00
|
|
|
|
|
|
|
namespace MLEM.Ui.Elements {
|
|
|
|
public class TextField : Element {
|
|
|
|
|
2019-08-10 21:37:10 +02:00
|
|
|
public NinePatch Texture;
|
|
|
|
public NinePatch HoveredTexture;
|
|
|
|
public Color HoveredColor;
|
2019-08-10 19:23:08 +02:00
|
|
|
public float TextScale;
|
|
|
|
public readonly StringBuilder Text = new StringBuilder();
|
|
|
|
public TextChanged OnTextChange;
|
|
|
|
public int MaxTextLength = int.MaxValue;
|
|
|
|
public float TextOffsetX = 4;
|
2019-08-10 21:37:10 +02:00
|
|
|
private IGenericFont font;
|
2019-08-10 19:23:08 +02:00
|
|
|
private double caretBlinkTimer;
|
|
|
|
|
|
|
|
public TextField(Anchor anchor, Vector2 size, IGenericFont font = null) : base(anchor, size) {
|
2019-08-10 21:37:10 +02:00
|
|
|
this.font = font;
|
2019-08-10 19:23:08 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-08-10 21:37:10 +02:00
|
|
|
protected override void InitStyle(UiStyle style) {
|
|
|
|
base.InitStyle(style);
|
|
|
|
this.TextScale = style.TextScale;
|
|
|
|
this.font = style.Font;
|
|
|
|
this.Texture = style.TextFieldTexture;
|
|
|
|
this.HoveredTexture = style.TextFieldHoveredTexture;
|
|
|
|
this.HoveredColor = style.TextFieldHoveredColor;
|
|
|
|
}
|
|
|
|
|
2019-08-10 19:23:08 +02:00
|
|
|
public delegate void TextChanged(TextField field, string text);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|