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

made text formatting and line splitting preserve spaces properly

This commit is contained in:
Ellpeck 2020-02-08 18:24:14 +01:00
parent f19cacf739
commit 72eff17d5f
3 changed files with 8 additions and 6 deletions

View file

@ -103,7 +103,7 @@ namespace Demos {
root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Additionally, you can create custom formatting codes that contain [Grass] images or [Walk] sprite animations! Note that these images have to be square, or [Tree] bad things happen.")); root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Additionally, you can create custom formatting codes that contain [Grass] images or [Walk] sprite animations! Note that these images have to be square, or [Tree] bad things happen."));
var animatedPar = root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Defining text animations as formatting codes is also possible, including [Wobbly]wobbly text[Unanimated] as well as a [Typing]dialogue-esc typing effect by default. Of course, more animations can be added though.")); var animatedPar = root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Defining text animations as formatting codes is also possible, including [Wobbly]wobbly text[Unanimated] as well as a [Typing]dialogue-esque typing effect by default. Of course, more animations can be added though."));
root.AddChild(new Button(Anchor.AutoCenter, new Vector2(1, 10), "Reset Typing Animation") { root.AddChild(new Button(Anchor.AutoCenter, new Vector2(1, 10), "Reset Typing Animation") {
// to reset any animation, simply change the paragraph's TimeIntoAnimation // to reset any animation, simply change the paragraph's TimeIntoAnimation
OnPressed = e => animatedPar.TimeIntoAnimation = TimeSpan.Zero OnPressed = e => animatedPar.TimeIntoAnimation = TimeSpan.Zero
@ -209,7 +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")); root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "This_is_a_really_long_[Blue]line[White]_to_see_if_splitting_without_spaces_works_properly._I_also_want_to_see_if_it_works_[Blue]across[White]_multiple_lines_or_just_on_the_first_one. But after this, I want the text to [Blue]continue[White] normally before changing_back_to_being_really_long_[Blue]oh[White]_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

@ -36,7 +36,7 @@ namespace MLEM.Extensions {
foreach (var word in line.Split(' ')) { foreach (var word in line.Split(' ')) {
if (widthFunc(word) * scale >= width) { if (widthFunc(word) * scale >= width) {
if (curr.Length > 0) { if (curr.Length > 0) {
total.Append(curr.ToString(0, curr.Length - 1)).Append('\n'); total.Append(curr.ToString(0, curr.Length)).Append('\n');
curr.Clear(); curr.Clear();
} }
var wordBuilder = new StringBuilder(); var wordBuilder = new StringBuilder();
@ -53,13 +53,13 @@ namespace MLEM.Extensions {
if (widthFunc(curr.ToString()) * 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)).Append('\n');
curr.Remove(0, len); curr.Remove(0, len);
} }
} }
} }
} }
total.Append(curr.ToString(0, curr.Length - 1)).Append('\n'); total.Append(curr).Append('\n');
} }
return total.ToString(0, total.Length - 1); return total.ToString(0, total.Length - 1);
} }

View file

@ -89,9 +89,10 @@ namespace MLEM.Formatting {
var animStart = 0; var animStart = 0;
var innerOffset = new Vector2(); var innerOffset = new Vector2();
var formatIndex = 0;
for (var i = 0; i < unformattedText.Length; i++) { for (var i = 0; i < unformattedText.Length; i++) {
// check if the current character's index has a formatting code // check if the current character's index has a formatting code
if (formatting.TryGetValue(i, out var codes)) { if (formatting.TryGetValue(formatIndex, out var codes)) {
foreach (var code in codes) { foreach (var code in codes) {
// if so, apply it // if so, apply it
switch (code.CodeType) { switch (code.CodeType) {
@ -134,6 +135,7 @@ namespace MLEM.Formatting {
currAnim(settings, currFont, batch, unformattedText, i, animStart, cSt, pos + innerOffset + settings.DropShadowOffset * scale, settings.DropShadowColor, scale, depth, timeIntoAnimation); currAnim(settings, currFont, batch, unformattedText, i, animStart, cSt, pos + innerOffset + settings.DropShadowOffset * scale, settings.DropShadowColor, scale, depth, timeIntoAnimation);
currAnim(settings, currFont, batch, unformattedText, i, animStart, cSt, pos + innerOffset, currColor, scale, depth, timeIntoAnimation); currAnim(settings, currFont, batch, unformattedText, i, animStart, cSt, pos + innerOffset, currColor, scale, depth, timeIntoAnimation);
innerOffset.X += regularFont.MeasureString(cSt).X * scale; innerOffset.X += regularFont.MeasureString(cSt).X * scale;
formatIndex++;
} }
} }
} }