From df418c2087044794b4f41aac0f9bd9b3fa26fd77 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 19 Mar 2020 03:27:21 +0100 Subject: [PATCH] made the text field caret an actual caret finally --- MLEM.Ui/Elements/TextField.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/MLEM.Ui/Elements/TextField.cs b/MLEM.Ui/Elements/TextField.cs index 4714c56..e72272b 100644 --- a/MLEM.Ui/Elements/TextField.cs +++ b/MLEM.Ui/Elements/TextField.cs @@ -20,6 +20,8 @@ namespace MLEM.Ui.Elements { public static readonly Rule OnlyNumbers = (field, add) => add.All(char.IsNumber); public static readonly Rule LettersNumbers = (field, add) => add.All(c => char.IsLetter(c) || char.IsNumber(c)); + public StyleProp TextColor; + public StyleProp PlaceholderColor; public StyleProp Texture; public StyleProp HoveredTexture; public StyleProp HoveredColor; @@ -30,6 +32,7 @@ namespace MLEM.Ui.Elements { public string PlaceholderText; public TextChanged OnTextChange; public float TextOffsetX = 4; + public float CaretWidth = 0.5F; private double caretBlinkTimer; private string displayedText; private int textOffset; @@ -158,11 +161,15 @@ namespace MLEM.Ui.Elements { if (this.displayedText != null) { var textPos = this.DisplayArea.Location + new Vector2(this.TextOffsetX * this.Scale, this.DisplayArea.Height / 2); if (this.text.Length > 0 || this.IsSelected) { - var caret = this.IsSelected ? this.caretBlinkTimer >= 0.5F ? "|" : " " : ""; - var display = this.displayedText.Insert(this.CaretPos - this.textOffset, caret); - this.Font.Value.DrawCenteredString(batch, display, textPos, this.TextScale * this.Scale, Color.White * alpha, false, true); + var textColor = this.TextColor.OrDefault(Color.White); + this.Font.Value.DrawCenteredString(batch, this.displayedText, textPos, this.TextScale * this.Scale, textColor * alpha, false, true); + if (this.IsSelected && this.caretBlinkTimer >= 0.5F) { + var textSize = this.Font.Value.MeasureString(this.displayedText.Substring(0, this.CaretPos - this.textOffset)) * this.TextScale * this.Scale; + var caretHeight = this.Font.Value.LineHeight * this.TextScale * this.Scale; + batch.Draw(batch.GetBlankTexture(), new RectangleF(textPos.X + textSize.X, textPos.Y - caretHeight / 2, this.CaretWidth * this.Scale, caretHeight), null, textColor * alpha); + } } else if (this.PlaceholderText != null) { - this.Font.Value.DrawCenteredString(batch, this.PlaceholderText, textPos, this.TextScale * this.Scale, Color.Gray * alpha, false, true); + this.Font.Value.DrawCenteredString(batch, this.PlaceholderText, textPos, this.TextScale * this.Scale, this.PlaceholderColor.OrDefault(Color.Gray) * alpha, false, true); } } base.Draw(time, batch, alpha, blendState, samplerState, matrix);