1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-06-16 18:54:31 +02:00

Fixed some end-of-line inconsistencies when using the Right text alignment

This commit is contained in:
Ell 2021-11-22 18:52:52 +01:00
parent c9c9e566b1
commit 84a6e5a29a
3 changed files with 17 additions and 11 deletions

View file

@ -22,6 +22,9 @@ Improvements
- Added Padding.Empty - Added Padding.Empty
- Throw an exception when text formatter macros resolve recursively too many times - 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 ### MLEM.Ui
Additions Additions
- Allow specifying a maximum amount of characters for a TextField - Allow specifying a maximum amount of characters for a TextField

View file

@ -140,10 +140,10 @@ namespace MLEM.Ui.Elements {
/// <inheritdoc /> /// <inheritdoc />
public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, Effect effect, Matrix matrix) { 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 sc = this.TextScale * this.TextScaleMultiplier * this.Scale;
var color = this.TextColor.OrDefault(Color.White) * alpha; 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); base.Draw(time, batch, alpha, blendState, samplerState, depthStencilState, effect, matrix);
} }

View file

@ -111,7 +111,7 @@ namespace MLEM.Formatting {
} }
/// <inheritdoc cref="GenericFont.DrawString(SpriteBatch,string,Vector2,Color,float,Vector2,float,SpriteEffects,float)"/> /// <inheritdoc cref="GenericFont.DrawString(SpriteBatch,string,Vector2,Color,float,Vector2,float,SpriteEffects,float)"/>
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); var innerOffset = new Vector2(this.initialInnerOffset * scale, 0);
for (var t = 0; t < this.Tokens.Length; t++) { for (var t = 0; t < this.Tokens.Length; t++) {
var token = this.Tokens[t]; var token = this.Tokens[t];
@ -178,7 +178,7 @@ namespace MLEM.Formatting {
foreach (var token in this.Tokens) foreach (var token in this.Tokens)
token.SplitDisplayString = token.DisplayString.Split('\n'); token.SplitDisplayString = token.DisplayString.Split('\n');
// token areas // token areas and inner offsets
this.initialInnerOffset = this.GetInnerOffsetX(font, 0, 0, alignment); this.initialInnerOffset = this.GetInnerOffsetX(font, 0, 0, alignment);
var innerOffset = new Vector2(this.initialInnerOffset, 0); var innerOffset = new Vector2(this.initialInnerOffset, 0);
for (var t = 0; t < this.Tokens.Length; t++) { 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) { private float GetInnerOffsetX(GenericFont font, int tokenIndex, int lineIndex, TextAlignment alignment) {
if (alignment > TextAlignment.Left) { if (alignment > TextAlignment.Left) {
var token = this.Tokens[tokenIndex]; var token = this.Tokens[tokenIndex];
var restOfLine = font.MeasureString(token.SplitDisplayString[lineIndex], true).X; // 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
if (lineIndex >= token.SplitDisplayString.Length - 1) { var endsLater = lineIndex >= token.SplitDisplayString.Length - 1;
// the line ends somewhere in or after the next token // 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++) { for (var i = tokenIndex + 1; i < this.Tokens.Length; i++) {
var other = this.Tokens[i]; var other = this.Tokens[i];
if (other.SplitDisplayString.Length > 1) { if (other.SplitDisplayString.Length > 1) {
// the line ends in this token // the line ends in this token (so we also ignore trailing whitespaces)
restOfLine += font.MeasureString(other.SplitDisplayString[0]).X; restOfLine += font.MeasureString(other.SplitDisplayString[0], true).X;
break; break;
} else { } else {
// the line doesn't end in this token, so add it fully // the line doesn't end in this token (or it's the last token), so add it fully
restOfLine += font.MeasureString(other.DisplayString).X; var lastToken = i >= this.Tokens.Length - 1;
restOfLine += font.MeasureString(other.DisplayString, lastToken).X;
} }
} }
} }