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

fixed SplitString not respecting long words

This commit is contained in:
Ellpeck 2020-02-06 02:27:21 +01:00
parent 31e2b72197
commit 5bf111d05a
2 changed files with 24 additions and 7 deletions

View file

@ -209,6 +209,7 @@ namespace Demos {
root.AddChild(ElementHelper.ImageButton(Anchor.AutoLeft, new Vector2(1, 10), tree, "Button with image")).PositionOffset = new Vector2(0, 1); root.AddChild(ElementHelper.ImageButton(Anchor.AutoLeft, new Vector2(1, 10), tree, "Button with image")).PositionOffset = new Vector2(0, 1);
root.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Disabled button") {IsDisabled = true}).PositionOffset = new Vector2(0, 1); root.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Disabled button") {IsDisabled = true}).PositionOffset = new Vector2(0, 1);
//root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "This_is_a_really_long_line_to_see_if_splitting_without_spaces_works_properly._I_also_want_to_see_if_it_works_across_multiple_lines_or_just_on_the_first_one. But after this, I want the text to continue normally before changing_back_to_being_really_long_oh_yes"));
// Below are some querying examples that help you find certain elements easily // Below are some querying examples that help you find certain elements easily

View file

@ -29,13 +29,28 @@ namespace MLEM.Extensions {
return total.ToString(); return total.ToString();
} }
public static string SplitString(Func<StringBuilder, float> widthFunc, string text, float width, float scale) { public static string SplitString(Func<string, 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')) {
var curr = new StringBuilder(); var curr = new StringBuilder();
foreach (var word in line.Split(' ')) { foreach (var word in line.Split(' ')) {
if (widthFunc(word) * scale >= width) {
if (curr.Length > 0) {
total.Append(curr.ToString(0, curr.Length - 1)).Append('\n');
curr.Clear();
}
var wordBuilder = new StringBuilder();
for (var i = 0; i < word.Length; i++) {
wordBuilder.Append(word[i]);
if (widthFunc(wordBuilder.ToString()) * scale >= width) {
total.Append(wordBuilder.ToString(0, wordBuilder.Length - 1)).Append('\n');
wordBuilder.Remove(0, wordBuilder.Length - 1);
}
}
curr.Append(wordBuilder).Append(' ');
} else {
curr.Append(word).Append(' '); curr.Append(word).Append(' ');
if (widthFunc(curr) * scale >= width) { if (widthFunc(curr.ToString()) * scale >= width) {
var len = curr.Length - word.Length - 1; var len = curr.Length - word.Length - 1;
if (len > 0) { if (len > 0) {
total.Append(curr.ToString(0, len - 1)).Append('\n'); total.Append(curr.ToString(0, len - 1)).Append('\n');
@ -43,6 +58,7 @@ namespace MLEM.Extensions {
} }
} }
} }
}
total.Append(curr.ToString(0, curr.Length - 1)).Append('\n'); total.Append(curr.ToString(0, curr.Length - 1)).Append('\n');
} }
return total.ToString(0, total.Length - 1); return total.ToString(0, total.Length - 1);