mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-12-25 01:39:23 +01:00
made text fields auto-scroll once they're too full
This commit is contained in:
parent
c030d075dd
commit
2e8b311b00
2 changed files with 21 additions and 4 deletions
|
@ -20,6 +20,7 @@ namespace MLEM.Ui.Elements {
|
|||
public float TextOffsetX = 4;
|
||||
private IGenericFont font;
|
||||
private double caretBlinkTimer;
|
||||
private int textStartIndex;
|
||||
|
||||
public TextField(Anchor anchor, Vector2 size, IGenericFont font = null) : base(anchor, size) {
|
||||
this.font = font;
|
||||
|
@ -38,8 +39,23 @@ namespace MLEM.Ui.Elements {
|
|||
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());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -61,7 +77,8 @@ namespace MLEM.Ui.Elements {
|
|||
}
|
||||
batch.Draw(tex, this.DisplayArea, color);
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ with.
|
|||
<!--
|
||||
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
|
||||
|
@ -41,7 +41,7 @@ with.
|
|||
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.
|
||||
-->
|
||||
<!-- <DefaultCharacter>*</DefaultCharacter> -->
|
||||
<DefaultCharacter>*</DefaultCharacter>
|
||||
|
||||
<!--
|
||||
CharacterRegions control what letters are available in the font. Every
|
||||
|
|
Loading…
Reference in a new issue