1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-12-24 17:29:23 +01:00

finished better text editing my dudes

This commit is contained in:
Ellpeck 2019-09-05 18:15:51 +02:00
parent b812bbe677
commit e39394bce3
7 changed files with 64 additions and 13 deletions

View file

@ -11,5 +11,9 @@ namespace MLEM.Extended.Extensions {
return SpriteFontExtensions.SplitString(s => font.MeasureString(s).Width, text, width, scale);
}
public static string TruncateString(this BitmapFont font, string text, float width, float scale, bool fromBack = false) {
return SpriteFontExtensions.TruncateString(s => font.MeasureString(s).Width, text, width, scale, fromBack);
}
}
}

View file

@ -56,5 +56,9 @@ namespace MLEM.Extended.Font {
return this.Font.SplitString(text, width, scale);
}
public string TruncateString(string text, float width, float scale, bool fromBack = false) {
return this.Font.TruncateString(text, width, scale, fromBack);
}
}
}

View file

@ -29,8 +29,8 @@ namespace MLEM.Ui.Elements {
public float TextOffsetX = 4;
private IGenericFont font;
private double caretBlinkTimer;
private int textStartIndex;
private int textDisplayLength;
private string displayedText;
private int textOffset;
public Rule InputRule;
public string MobileTitle;
public string MobileDescription;
@ -57,8 +57,10 @@ namespace MLEM.Ui.Elements {
if (!this.IsSelected)
return;
if (key == Keys.Back) {
this.CaretPos--;
this.RemoveText(this.CaretPos, 1);
if (this.CaretPos > 0) {
this.CaretPos--;
this.RemoveText(this.CaretPos, 1);
}
} else if (key == Keys.Delete) {
this.RemoveText(this.CaretPos, 1);
} else {
@ -81,19 +83,27 @@ namespace MLEM.Ui.Elements {
private void HandleTextChange(bool textChanged = true) {
// not initialized yet
if (this.font == null) {
this.textDisplayLength = this.text.Length;
if (this.font == null)
return;
}
var length = this.font.MeasureString(this.text).X * this.TextScale;
var maxWidth = this.DisplayArea.Width / this.Scale - this.TextOffsetX * 2;
if (length > maxWidth) {
// TODO
this.textStartIndex = 0;
this.textDisplayLength = this.text.Length;
// if we're moving the caret to the left
if (this.textOffset > this.CaretPos) {
this.textOffset = this.CaretPos;
} else {
// if we're moving the caret to the right
var importantArea = this.text.ToString(this.textOffset, Math.Min(this.CaretPos, this.text.Length) - this.textOffset);
var bound = this.CaretPos - this.font.TruncateString(importantArea, maxWidth, this.TextScale, true).Length;
if (this.textOffset < bound) {
this.textOffset = bound;
}
}
var visible = this.text.ToString(this.textOffset, this.text.Length - this.textOffset);
this.displayedText = this.font.TruncateString(visible, maxWidth, this.TextScale);
} else {
this.textStartIndex = 0;
this.textDisplayLength = this.text.Length;
this.displayedText = this.Text;
this.textOffset = 0;
}
if (textChanged)
@ -103,6 +113,10 @@ namespace MLEM.Ui.Elements {
public override void Update(GameTime time) {
base.Update(time);
// handle first initialization if not done
if (this.displayedText == null)
this.HandleTextChange(false);
if (this.Input.IsKeyPressed(Keys.Left)) {
this.CaretPos--;
} else if (this.Input.IsKeyPressed(Keys.Right)) {
@ -131,7 +145,7 @@ namespace MLEM.Ui.Elements {
var textPos = this.DisplayArea.Location.ToVector2() + 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.text.ToString(this.textStartIndex, this.textDisplayLength).Insert(this.CaretPos - this.textStartIndex, caret);
var display = this.displayedText.Insert(this.CaretPos - this.textOffset, caret);
this.font.DrawCenteredString(batch, display, textPos, this.TextScale * this.Scale, Color.White * alpha, false, true);
} else if (this.PlaceholderText != null) {
this.font.DrawCenteredString(batch, this.PlaceholderText, textPos, this.TextScale * this.Scale, Color.Gray * alpha, false, true);

View file

@ -82,6 +82,10 @@ namespace MLEM.Ui.Style {
return text;
}
public string TruncateString(string text, float width, float scale, bool fromBack = false) {
return text;
}
}
}

View file

@ -10,6 +10,25 @@ namespace MLEM.Extensions {
return SplitString(s => font.MeasureString(s).X, text, width, scale);
}
public static string TruncateString(this SpriteFont font, string text, float width, float scale, bool fromBack = false) {
return TruncateString(s => font.MeasureString(s).X, text, width, scale, fromBack);
}
public static string TruncateString(Func<StringBuilder, float> widthFunc, string text, float width, float scale, bool fromBack = false) {
var total = new StringBuilder();
for (var i = 0; i < text.Length; i++) {
if (fromBack) {
total.Insert(0, text[text.Length - 1 - i]);
} else {
total.Append(text[i]);
}
if (widthFunc(total) * scale >= width)
return total.ToString(fromBack ? 1 : 0, total.Length - 1);
}
return total.ToString();
}
public static string SplitString(Func<StringBuilder, float> widthFunc, string text, float width, float scale) {
var total = new StringBuilder();
foreach (var line in text.Split('\n')) {

View file

@ -54,5 +54,9 @@ namespace MLEM.Font {
return this.Font.SplitString(text, width, scale);
}
public string TruncateString(string text, float width, float scale, bool fromBack = false) {
return this.Font.TruncateString(text, width, scale, fromBack);
}
}
}

View file

@ -28,5 +28,7 @@ namespace MLEM.Font {
string SplitString(string text, float width, float scale);
string TruncateString(string text, float width, float scale, bool fromBack = false);
}
}