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

made text fields auto-scroll once they're too full

This commit is contained in:
Ellpeck 2019-08-11 00:39:40 +02:00
parent c030d075dd
commit 2e8b311b00
2 changed files with 21 additions and 4 deletions

View file

@ -20,6 +20,7 @@ namespace MLEM.Ui.Elements {
public float TextOffsetX = 4; public float TextOffsetX = 4;
private IGenericFont font; private IGenericFont font;
private double caretBlinkTimer; private double caretBlinkTimer;
private int textStartIndex;
public TextField(Anchor anchor, Vector2 size, IGenericFont font = null) : base(anchor, size) { public TextField(Anchor anchor, Vector2 size, IGenericFont font = null) : base(anchor, size) {
this.font = font; this.font = font;
@ -38,8 +39,23 @@ namespace MLEM.Ui.Elements {
textChanged = true; textChanged = true;
} }
} }
if (textChanged) if (textChanged) {
var length = this.font.MeasureString(this.Text).X * this.TextScale;
var maxWidth = this.DisplayArea.Width - this.TextOffsetX * 2;
if (length > maxWidth) {
for (var i = Math.Max(0, this.textStartIndex - 1); i < this.Text.Length; i++) {
var substring = this.Text.ToString(i, this.Text.Length - i);
if (this.font.MeasureString(substring).X * this.TextScale <= maxWidth) {
this.textStartIndex = i;
break;
}
}
} else {
this.textStartIndex = 0;
}
this.OnTextChange?.Invoke(this, this.Text.ToString()); this.OnTextChange?.Invoke(this, this.Text.ToString());
}
}; };
} }
@ -61,7 +77,8 @@ namespace MLEM.Ui.Elements {
} }
batch.Draw(tex, this.DisplayArea, color); batch.Draw(tex, this.DisplayArea, color);
var caret = this.IsSelected && this.caretBlinkTimer >= 0.5F ? "|" : ""; var caret = this.IsSelected && this.caretBlinkTimer >= 0.5F ? "|" : "";
this.font.DrawCenteredString(batch, this.Text + caret, this.DisplayArea.Location.ToVector2() + new Vector2(this.TextOffsetX, this.DisplayArea.Height / 2), this.TextScale, Color.White * alpha, false, true); var text = this.Text.ToString(this.textStartIndex, this.Text.Length - this.textStartIndex) + caret;
this.font.DrawCenteredString(batch, text, this.DisplayArea.Location.ToVector2() + new Vector2(this.TextOffsetX, this.DisplayArea.Height / 2), this.TextScale, Color.White * alpha, false, true);
base.Draw(time, batch, alpha); base.Draw(time, batch, alpha);
} }

View file

@ -11,7 +11,7 @@ with.
<!-- <!--
Modify this string to change the font that will be imported. Modify this string to change the font that will be imported.
--> -->
<FontName>BitPotion</FontName> <FontName>BitPotionExt</FontName>
<!-- <!--
Size is a float value, measured in points. Modify this value to change Size is a float value, measured in points. Modify this value to change
@ -41,7 +41,7 @@ with.
If you uncomment this line, the default character will be substituted if you draw If you uncomment this line, the default character will be substituted if you draw
or measure text that contains characters which were not included in the font. or measure text that contains characters which were not included in the font.
--> -->
<!-- <DefaultCharacter>*</DefaultCharacter> --> <DefaultCharacter>*</DefaultCharacter>
<!-- <!--
CharacterRegions control what letters are available in the font. Every CharacterRegions control what letters are available in the font. Every