1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-25 22:18:34 +01:00

made the text field caret an actual caret finally

This commit is contained in:
Ellpeck 2020-03-19 03:27:21 +01:00
parent cb56319924
commit df418c2087

View file

@ -20,6 +20,8 @@ namespace MLEM.Ui.Elements {
public static readonly Rule OnlyNumbers = (field, add) => add.All(char.IsNumber); 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 static readonly Rule LettersNumbers = (field, add) => add.All(c => char.IsLetter(c) || char.IsNumber(c));
public StyleProp<Color> TextColor;
public StyleProp<Color> PlaceholderColor;
public StyleProp<NinePatch> Texture; public StyleProp<NinePatch> Texture;
public StyleProp<NinePatch> HoveredTexture; public StyleProp<NinePatch> HoveredTexture;
public StyleProp<Color> HoveredColor; public StyleProp<Color> HoveredColor;
@ -30,6 +32,7 @@ namespace MLEM.Ui.Elements {
public string PlaceholderText; public string PlaceholderText;
public TextChanged OnTextChange; public TextChanged OnTextChange;
public float TextOffsetX = 4; public float TextOffsetX = 4;
public float CaretWidth = 0.5F;
private double caretBlinkTimer; private double caretBlinkTimer;
private string displayedText; private string displayedText;
private int textOffset; private int textOffset;
@ -158,11 +161,15 @@ namespace MLEM.Ui.Elements {
if (this.displayedText != null) { if (this.displayedText != null) {
var textPos = this.DisplayArea.Location + new Vector2(this.TextOffsetX * this.Scale, this.DisplayArea.Height / 2); var textPos = this.DisplayArea.Location + new Vector2(this.TextOffsetX * this.Scale, this.DisplayArea.Height / 2);
if (this.text.Length > 0 || this.IsSelected) { if (this.text.Length > 0 || this.IsSelected) {
var caret = this.IsSelected ? this.caretBlinkTimer >= 0.5F ? "|" : " " : ""; var textColor = this.TextColor.OrDefault(Color.White);
var display = this.displayedText.Insert(this.CaretPos - this.textOffset, caret); this.Font.Value.DrawCenteredString(batch, this.displayedText, textPos, this.TextScale * this.Scale, textColor * alpha, false, true);
this.Font.Value.DrawCenteredString(batch, display, textPos, this.TextScale * this.Scale, Color.White * 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) { } 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); base.Draw(time, batch, alpha, blendState, samplerState, matrix);