diff --git a/Demos/UiDemo.cs b/Demos/UiDemo.cs index bed85a4..5f7028e 100644 --- a/Demos/UiDemo.cs +++ b/Demos/UiDemo.cs @@ -90,7 +90,10 @@ namespace Demos { root.AddChild(new VerticalSpace(3)); 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 Paragraph(Anchor.AutoCenter, 1, "Numbers-only input:", true)); diff --git a/MLEM.Ui/Elements/TextField.cs b/MLEM.Ui/Elements/TextField.cs index 89082a7..62e6b7f 100644 --- a/MLEM.Ui/Elements/TextField.cs +++ b/MLEM.Ui/Elements/TextField.cs @@ -22,6 +22,7 @@ namespace MLEM.Ui.Elements { public Color HoveredColor; public float TextScale; public readonly StringBuilder Text = new StringBuilder(); + public string PlaceholderText; public TextChanged OnTextChange; public int MaxTextLength = int.MaxValue; public float TextOffsetX = 4; @@ -85,9 +86,15 @@ namespace MLEM.Ui.Elements { color = this.HoveredColor * alpha; } batch.Draw(tex, this.DisplayArea.OffsetCopy(offset), color, this.Scale); - var caret = this.IsSelected && this.caretBlinkTimer >= 0.5F ? "|" : ""; - 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); + + 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 text = this.Text.ToString(this.textStartIndex, this.Text.Length - this.textStartIndex) + caret; + 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); }