From 84a6e5a29ab7c8858a6754c7d71d00ac1ff23835 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Mon, 22 Nov 2021 18:52:52 +0100 Subject: [PATCH] Fixed some end-of-line inconsistencies when using the Right text alignment --- CHANGELOG.md | 3 +++ MLEM.Ui/Elements/Paragraph.cs | 4 ++-- MLEM/Formatting/TokenizedString.cs | 21 ++++++++++++--------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97e5aa9..3fe2779 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,9 @@ Improvements - Added Padding.Empty - Throw an exception when text formatter macros resolve recursively too many times +Fixes +- Fixed some end-of-line inconsistencies when using the Right text alignment + ### MLEM.Ui Additions - Allow specifying a maximum amount of characters for a TextField diff --git a/MLEM.Ui/Elements/Paragraph.cs b/MLEM.Ui/Elements/Paragraph.cs index d3b2955..8d04ded 100644 --- a/MLEM.Ui/Elements/Paragraph.cs +++ b/MLEM.Ui/Elements/Paragraph.cs @@ -140,10 +140,10 @@ namespace MLEM.Ui.Elements { /// public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, Effect effect, Matrix matrix) { - var pos = this.DisplayArea.Location + new Vector2(GetAlignmentOffset(), 0); + var pos = this.DisplayArea.Location + new Vector2(this.GetAlignmentOffset(), 0); var sc = this.TextScale * this.TextScaleMultiplier * this.Scale; var color = this.TextColor.OrDefault(Color.White) * alpha; - this.TokenizedText.Draw(time, batch, pos, this.RegularFont, color, sc, 0, this.Alignment); + this.TokenizedText.Draw(time, batch, pos, this.RegularFont, color, sc, 0); base.Draw(time, batch, alpha, blendState, samplerState, depthStencilState, effect, matrix); } diff --git a/MLEM/Formatting/TokenizedString.cs b/MLEM/Formatting/TokenizedString.cs index bdc2fa5..70f5fba 100644 --- a/MLEM/Formatting/TokenizedString.cs +++ b/MLEM/Formatting/TokenizedString.cs @@ -111,7 +111,7 @@ namespace MLEM.Formatting { } /// - public void Draw(GameTime time, SpriteBatch batch, Vector2 pos, GenericFont font, Color color, float scale, float depth, TextAlignment alignment = TextAlignment.Left) { + public void Draw(GameTime time, SpriteBatch batch, Vector2 pos, GenericFont font, Color color, float scale, float depth) { var innerOffset = new Vector2(this.initialInnerOffset * scale, 0); for (var t = 0; t < this.Tokens.Length; t++) { var token = this.Tokens[t]; @@ -178,7 +178,7 @@ namespace MLEM.Formatting { foreach (var token in this.Tokens) token.SplitDisplayString = token.DisplayString.Split('\n'); - // token areas + // token areas and inner offsets this.initialInnerOffset = this.GetInnerOffsetX(font, 0, 0, alignment); var innerOffset = new Vector2(this.initialInnerOffset, 0); for (var t = 0; t < this.Tokens.Length; t++) { @@ -205,18 +205,21 @@ namespace MLEM.Formatting { private float GetInnerOffsetX(GenericFont font, int tokenIndex, int lineIndex, TextAlignment alignment) { if (alignment > TextAlignment.Left) { var token = this.Tokens[tokenIndex]; - var restOfLine = font.MeasureString(token.SplitDisplayString[lineIndex], true).X; - if (lineIndex >= token.SplitDisplayString.Length - 1) { - // the line ends somewhere in or after the next token + // if we're the last line in our line array, then we don't contain a line split, so the line ends in a later token + var endsLater = lineIndex >= token.SplitDisplayString.Length - 1; + // if the line ends in our token, we should ignore trailing white space + var restOfLine = font.MeasureString(token.SplitDisplayString[lineIndex], !endsLater).X; + if (endsLater) { for (var i = tokenIndex + 1; i < this.Tokens.Length; i++) { var other = this.Tokens[i]; if (other.SplitDisplayString.Length > 1) { - // the line ends in this token - restOfLine += font.MeasureString(other.SplitDisplayString[0]).X; + // the line ends in this token (so we also ignore trailing whitespaces) + restOfLine += font.MeasureString(other.SplitDisplayString[0], true).X; break; } else { - // the line doesn't end in this token, so add it fully - restOfLine += font.MeasureString(other.DisplayString).X; + // the line doesn't end in this token (or it's the last token), so add it fully + var lastToken = i >= this.Tokens.Length - 1; + restOfLine += font.MeasureString(other.DisplayString, lastToken).X; } } }