1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 14:38:34 +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); 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); 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; public float TextOffsetX = 4;
private IGenericFont font; private IGenericFont font;
private double caretBlinkTimer; private double caretBlinkTimer;
private int textStartIndex; private string displayedText;
private int textDisplayLength; private int textOffset;
public Rule InputRule; public Rule InputRule;
public string MobileTitle; public string MobileTitle;
public string MobileDescription; public string MobileDescription;
@ -57,8 +57,10 @@ namespace MLEM.Ui.Elements {
if (!this.IsSelected) if (!this.IsSelected)
return; return;
if (key == Keys.Back) { if (key == Keys.Back) {
if (this.CaretPos > 0) {
this.CaretPos--; this.CaretPos--;
this.RemoveText(this.CaretPos, 1); this.RemoveText(this.CaretPos, 1);
}
} else if (key == Keys.Delete) { } else if (key == Keys.Delete) {
this.RemoveText(this.CaretPos, 1); this.RemoveText(this.CaretPos, 1);
} else { } else {
@ -81,19 +83,27 @@ namespace MLEM.Ui.Elements {
private void HandleTextChange(bool textChanged = true) { private void HandleTextChange(bool textChanged = true) {
// not initialized yet // not initialized yet
if (this.font == null) { if (this.font == null)
this.textDisplayLength = this.text.Length;
return; return;
}
var length = this.font.MeasureString(this.text).X * this.TextScale; var length = this.font.MeasureString(this.text).X * this.TextScale;
var maxWidth = this.DisplayArea.Width / this.Scale - this.TextOffsetX * 2; var maxWidth = this.DisplayArea.Width / this.Scale - this.TextOffsetX * 2;
if (length > maxWidth) { if (length > maxWidth) {
// TODO // if we're moving the caret to the left
this.textStartIndex = 0; if (this.textOffset > this.CaretPos) {
this.textDisplayLength = this.text.Length; this.textOffset = this.CaretPos;
} else { } else {
this.textStartIndex = 0; // if we're moving the caret to the right
this.textDisplayLength = this.text.Length; 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.displayedText = this.Text;
this.textOffset = 0;
} }
if (textChanged) if (textChanged)
@ -103,6 +113,10 @@ namespace MLEM.Ui.Elements {
public override void Update(GameTime time) { public override void Update(GameTime time) {
base.Update(time); base.Update(time);
// handle first initialization if not done
if (this.displayedText == null)
this.HandleTextChange(false);
if (this.Input.IsKeyPressed(Keys.Left)) { if (this.Input.IsKeyPressed(Keys.Left)) {
this.CaretPos--; this.CaretPos--;
} else if (this.Input.IsKeyPressed(Keys.Right)) { } 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); var textPos = this.DisplayArea.Location.ToVector2() + 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 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); this.font.DrawCenteredString(batch, display, textPos, this.TextScale * this.Scale, Color.White * alpha, false, true);
} else if (this.PlaceholderText != null) { } else if (this.PlaceholderText != null) {
this.font.DrawCenteredString(batch, this.PlaceholderText, textPos, this.TextScale * this.Scale, Color.Gray * alpha, false, true); 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; 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); 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) { public static string SplitString(Func<StringBuilder, float> widthFunc, string text, float width, float scale) {
var total = new StringBuilder(); var total = new StringBuilder();
foreach (var line in text.Split('\n')) { foreach (var line in text.Split('\n')) {

View file

@ -54,5 +54,9 @@ namespace MLEM.Font {
return this.Font.SplitString(text, width, scale); 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 SplitString(string text, float width, float scale);
string TruncateString(string text, float width, float scale, bool fromBack = false);
} }
} }