mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-25 22:18:34 +01:00
added the option to add a placeholder to the text field
This commit is contained in:
parent
d078f144fd
commit
3132074a39
2 changed files with 14 additions and 4 deletions
|
@ -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));
|
||||
|
|
|
@ -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 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, 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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue