1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 06:28:35 +01:00

added the option to add a placeholder to the text field

This commit is contained in:
Ellpeck 2019-08-23 18:56:39 +02:00
parent d078f144fd
commit 3132074a39
2 changed files with 14 additions and 4 deletions

View file

@ -90,7 +90,10 @@ namespace Demos {
root.AddChild(new VerticalSpace(3)); root.AddChild(new VerticalSpace(3));
root.AddChild(new Paragraph(Anchor.AutoCenter, 1, "Text input:", true)); 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 TextField(Anchor.AutoLeft, new Vector2(1, 10)) {
PositionOffset = new Vector2(0, 1),
PlaceholderText = "Click here to input text"
});
root.AddChild(new VerticalSpace(3)); root.AddChild(new VerticalSpace(3));
root.AddChild(new Paragraph(Anchor.AutoCenter, 1, "Numbers-only input:", true)); root.AddChild(new Paragraph(Anchor.AutoCenter, 1, "Numbers-only input:", true));

View file

@ -22,6 +22,7 @@ namespace MLEM.Ui.Elements {
public Color HoveredColor; public Color HoveredColor;
public float TextScale; public float TextScale;
public readonly StringBuilder Text = new StringBuilder(); public readonly StringBuilder Text = new StringBuilder();
public string PlaceholderText;
public TextChanged OnTextChange; public TextChanged OnTextChange;
public int MaxTextLength = int.MaxValue; public int MaxTextLength = int.MaxValue;
public float TextOffsetX = 4; public float TextOffsetX = 4;
@ -85,9 +86,15 @@ namespace MLEM.Ui.Elements {
color = this.HoveredColor * alpha; color = this.HoveredColor * alpha;
} }
batch.Draw(tex, this.DisplayArea.OffsetCopy(offset), color, this.Scale); batch.Draw(tex, this.DisplayArea.OffsetCopy(offset), color, this.Scale);
var textPos = this.DisplayArea.Location.ToVector2() + new Vector2(offset.X + this.TextOffsetX * this.Scale, offset.Y + this.DisplayArea.Height / 2);
if (this.Text.Length > 0 || this.IsSelected) {
var caret = this.IsSelected && this.caretBlinkTimer >= 0.5F ? "|" : ""; var caret = this.IsSelected && this.caretBlinkTimer >= 0.5F ? "|" : "";
var text = this.Text.ToString(this.textStartIndex, this.Text.Length - this.textStartIndex) + caret; var text = this.Text.ToString(this.textStartIndex, this.Text.Length - this.textStartIndex) + caret;
this.font.DrawCenteredString(batch, text, this.DisplayArea.Location.ToVector2() + new Vector2(offset.X + this.TextOffsetX * this.Scale, offset.Y + this.DisplayArea.Height / 2), this.TextScale * this.Scale, Color.White * alpha, false, true); this.font.DrawCenteredString(batch, text, textPos, this.TextScale * this.Scale, Color.White * alpha, false, true);
} else if (this.PlaceholderText != null) {
this.font.DrawCenteredString(batch, this.PlaceholderText, textPos, this.TextScale * this.Scale, Color.Gray * alpha, false, true);
}
base.Draw(time, batch, alpha, offset); base.Draw(time, batch, alpha, offset);
} }