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

fixed the new TextInput not updating correctly if the Font is set late

This commit is contained in:
Ell 2022-06-29 14:34:13 +02:00
parent 6f05263980
commit d58c5d8b33

View file

@ -95,7 +95,7 @@ namespace MLEM.Input {
if (this.caretPos != val) { if (this.caretPos != val) {
this.caretPos = val; this.caretPos = val;
this.caretBlinkTimer = 0; this.caretBlinkTimer = 0;
this.UpdateTextData(false); this.SetTextDataDirty(false);
} }
} }
} }
@ -103,12 +103,22 @@ namespace MLEM.Input {
/// The line of text that the caret is currently on. /// The line of text that the caret is currently on.
/// This can only be only non-0 if <see cref="Multiline"/> is true. /// This can only be only non-0 if <see cref="Multiline"/> is true.
/// </summary> /// </summary>
public int CaretLine { get; private set; } public int CaretLine {
get {
this.UpdateTextDataIfDirty();
return this.caretLine;
}
}
/// <summary> /// <summary>
/// The position in the current <see cref="CaretLine"/> that the caret is currently on. /// The position in the current <see cref="CaretLine"/> that the caret is currently on.
/// If <see cref="Multiline"/> is false, this value is always equal to <see cref="CaretPos"/>. /// If <see cref="Multiline"/> is false, this value is always equal to <see cref="CaretPos"/>.
/// </summary> /// </summary>
public int CaretPosInLine { get; private set; } public int CaretPosInLine {
get {
this.UpdateTextDataIfDirty();
return this.caretPosInLine;
}
}
/// <summary> /// <summary>
/// A character that should be displayed instead of this text input's <see cref="Text"/> content. /// A character that should be displayed instead of this text input's <see cref="Text"/> content.
/// The amount of masking characters displayed will be equal to the <see cref="Text"/>'s length. /// The amount of masking characters displayed will be equal to the <see cref="Text"/>'s length.
@ -119,7 +129,7 @@ namespace MLEM.Input {
set { set {
if (this.maskingCharacter != value) { if (this.maskingCharacter != value) {
this.maskingCharacter = value; this.maskingCharacter = value;
this.UpdateTextData(false); this.SetTextDataDirty(false);
} }
} }
} }
@ -138,7 +148,7 @@ namespace MLEM.Input {
set { set {
if (this.multiline != value) { if (this.multiline != value) {
this.multiline = value; this.multiline = value;
this.UpdateTextData(false); this.SetTextDataDirty(false);
} }
} }
} }
@ -150,7 +160,7 @@ namespace MLEM.Input {
set { set {
if (this.font != value) { if (this.font != value) {
this.font = value; this.font = value;
this.UpdateTextData(false); this.SetTextDataDirty(false);
} }
} }
} }
@ -163,7 +173,7 @@ namespace MLEM.Input {
set { set {
if (this.textScale != value) { if (this.textScale != value) {
this.textScale = value; this.textScale = value;
this.UpdateTextData(false); this.SetTextDataDirty(false);
} }
} }
} }
@ -176,7 +186,7 @@ namespace MLEM.Input {
set { set {
if (this.size != value) { if (this.size != value) {
this.size = value; this.size = value;
this.UpdateTextData(false); this.SetTextDataDirty(false);
} }
} }
} }
@ -200,11 +210,14 @@ namespace MLEM.Input {
private int textOffset; private int textOffset;
private int lineOffset; private int lineOffset;
private int caretPos; private int caretPos;
private int caretLine;
private int caretPosInLine;
private float caretDrawOffset; private float caretDrawOffset;
private bool multiline; private bool multiline;
private GenericFont font; private GenericFont font;
private float textScale; private float textScale;
private Vector2 size; private Vector2 size;
private bool textDataDirty;
/// <summary> /// <summary>
/// Creates a new text input with the given settings. /// Creates a new text input with the given settings.
@ -259,6 +272,8 @@ namespace MLEM.Input {
/// <param name="time">The current game time.</param> /// <param name="time">The current game time.</param>
/// <param name="input">The input handler to use for input querying.</param> /// <param name="input">The input handler to use for input querying.</param>
public void Update(GameTime time, InputHandler input) { public void Update(GameTime time, InputHandler input) {
this.UpdateTextDataIfDirty();
// FNA's text input event doesn't supply keys, so we handle this here // FNA's text input event doesn't supply keys, so we handle this here
#if FNA #if FNA
if (this.CaretPos > 0 && input.TryConsumePressed(Keys.Back)) { if (this.CaretPos > 0 && input.TryConsumePressed(Keys.Back)) {
@ -309,9 +324,7 @@ namespace MLEM.Input {
/// <param name="caretWidth">The width that the caret should have, which is multiplied with <paramref name="drawScale"/> before drawing.</param> /// <param name="caretWidth">The width that the caret should have, which is multiplied with <paramref name="drawScale"/> before drawing.</param>
/// <param name="textColor">The color to draw the text and caret with.</param> /// <param name="textColor">The color to draw the text and caret with.</param>
public void Draw(SpriteBatch batch, Vector2 textPos, float drawScale, float caretWidth, Color textColor) { public void Draw(SpriteBatch batch, Vector2 textPos, float drawScale, float caretWidth, Color textColor) {
// handle first initialization if not done this.UpdateTextDataIfDirty();
if (this.displayedText == null)
this.UpdateTextData(false);
var scale = this.TextScale * drawScale; var scale = this.TextScale * drawScale;
this.Font.DrawString(batch, this.displayedText, textPos, textColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0); this.Font.DrawString(batch, this.displayedText, textPos, textColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
@ -339,7 +352,7 @@ namespace MLEM.Input {
this.text.Clear(); this.text.Clear();
this.text.Append(strg); this.text.Append(strg);
this.CaretPos = this.text.Length; this.CaretPos = this.text.Length;
this.UpdateTextData(); this.SetTextDataDirty();
} }
/// <summary> /// <summary>
@ -356,7 +369,7 @@ namespace MLEM.Input {
strg = strg.Substring(0, this.MaximumCharacters.Value - this.text.Length); strg = strg.Substring(0, this.MaximumCharacters.Value - this.text.Length);
this.text.Insert(this.CaretPos, strg); this.text.Insert(this.CaretPos, strg);
this.CaretPos += strg.Length; this.CaretPos += strg.Length;
this.UpdateTextData(); this.SetTextDataDirty();
return true; return true;
} }
@ -371,7 +384,7 @@ namespace MLEM.Input {
this.text.Remove(index, length); this.text.Remove(index, length);
// ensure that caret pos is still in bounds // ensure that caret pos is still in bounds
this.CaretPos = this.CaretPos; this.CaretPos = this.CaretPos;
this.UpdateTextData(); this.SetTextDataDirty();
return true; return true;
} }
@ -382,6 +395,7 @@ namespace MLEM.Input {
/// <param name="line">The line to move the caret to</param> /// <param name="line">The line to move the caret to</param>
/// <returns>True if the caret was moved, false if it was not (which indicates that the line with the given <paramref name="line"/> index does not exist)</returns> /// <returns>True if the caret was moved, false if it was not (which indicates that the line with the given <paramref name="line"/> index does not exist)</returns>
public bool MoveCaretToLine(int line) { public bool MoveCaretToLine(int line) {
this.UpdateTextDataIfDirty();
var (destStart, destEnd) = this.GetLineBounds(line); var (destStart, destEnd) = this.GetLineBounds(line);
if (destEnd > 0) { if (destEnd > 0) {
// find the position whose distance from the start is closest to the current distance from the start // find the position whose distance from the start is closest to the current distance from the start
@ -413,9 +427,16 @@ namespace MLEM.Input {
return true; return true;
} }
private void UpdateTextData(bool textChanged = true) { private void SetTextDataDirty(bool textChanged = true) {
if (this.Font == null) this.textDataDirty = true;
if (textChanged)
this.OnTextChange?.Invoke(this, this.Text);
}
private void UpdateTextDataIfDirty() {
if (!this.textDataDirty || this.Font == null)
return; return;
this.textDataDirty = false;
if (this.Multiline) { if (this.Multiline) {
// soft wrap if we're multiline // soft wrap if we're multiline
this.splitText = this.Font.SplitStringSeparate(this.text, this.Size.X, this.TextScale).ToArray(); this.splitText = this.Font.SplitStringSeparate(this.text, this.Size.X, this.TextScale).ToArray();
@ -482,9 +503,6 @@ namespace MLEM.Input {
if (this.MaskingCharacter != null) if (this.MaskingCharacter != null)
this.displayedText = new string(this.MaskingCharacter.Value, this.displayedText.Length); this.displayedText = new string(this.MaskingCharacter.Value, this.displayedText.Length);
if (textChanged)
this.OnTextChange?.Invoke(this, this.Text);
} }
private void UpdateCaretData() { private void UpdateCaretData() {
@ -496,8 +514,8 @@ namespace MLEM.Input {
var split = this.splitText[d]; var split = this.splitText[d];
for (var i = 0; i <= split.Length; i++) { for (var i = 0; i <= split.Length; i++) {
if (index == this.CaretPos) { if (index == this.CaretPos) {
this.CaretLine = line; this.caretLine = line;
this.CaretPosInLine = i - startOfLine; this.caretPosInLine = i - startOfLine;
this.caretDrawOffset = this.Font.MeasureString(split.Substring(startOfLine, this.CaretPosInLine)).X; this.caretDrawOffset = this.Font.MeasureString(split.Substring(startOfLine, this.CaretPosInLine)).X;
return; return;
} }
@ -514,8 +532,8 @@ namespace MLEM.Input {
line++; line++;
} }
} else if (this.displayedText != null) { } else if (this.displayedText != null) {
this.CaretLine = 0; this.caretLine = 0;
this.CaretPosInLine = this.CaretPos; this.caretPosInLine = this.CaretPos;
this.caretDrawOffset = this.Font.MeasureString(this.displayedText.Substring(0, this.CaretPos - this.textOffset)).X; this.caretDrawOffset = this.Font.MeasureString(this.displayedText.Substring(0, this.CaretPos - this.textOffset)).X;
} }
} }