1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-15 13:48:46 +02:00

clean up GenericFont changes

This commit is contained in:
Ellpeck 2020-06-20 12:12:34 +02:00
parent 73f2e1c565
commit 64b9246aa5
5 changed files with 18 additions and 17 deletions

View file

@ -33,7 +33,7 @@ namespace MLEM.Extended.Font {
}
/// <inheritdoc />
protected override Vector2 CalcCharSize(char c) {
protected override Vector2 MeasureChar(char c) {
var region = this.Font.GetCharacterRegion(c);
return region != null ? new Vector2(region.XAdvance, region.Height) : Vector2.Zero;
}

View file

@ -46,7 +46,7 @@ namespace MLEM.Ui.Style {
public override GenericFont Italic => this;
public override float LineHeight => 1;
protected override Vector2 CalcCharSize(char c) {
protected override Vector2 MeasureChar(char c) {
return Vector2.Zero;
}

View file

@ -49,7 +49,7 @@ namespace MLEM.Font {
public abstract void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth);
///<inheritdoc cref="SpriteFont.MeasureString(string)"/>
protected abstract Vector2 CalcCharSize(char c);
protected abstract Vector2 MeasureChar(char c);
/// <summary>
/// Draws a string with the given text alignment.
@ -85,23 +85,23 @@ namespace MLEM.Font {
this.DrawString(batch, text, position, color, rotation, origin, scale, effects, layerDepth);
}
///<inheritdoc cref="SpriteFont.MeasureString(string)"/>
public Vector2 MeasureChar(char c) {
return c == OneEmSpace ? new Vector2(this.LineHeight) : this.CalcCharSize(c);
}
///<inheritdoc cref="SpriteFont.MeasureString(string)"/>
public Vector2 MeasureString(string text) {
var size = Vector2.Zero;
var xOffset = 0F;
foreach (var c in text) {
if (c == '\n') {
xOffset = 0;
size.Y += this.LineHeight;
continue;
switch (c) {
case '\n':
xOffset = 0;
size.Y += this.LineHeight;
break;
case OneEmSpace:
xOffset += this.LineHeight;
break;
default:
xOffset += this.MeasureChar(c).X;
break;
}
xOffset += this.MeasureChar(c).X;
// increase x size if this line is the longest
if (xOffset > size.X)
size.X = xOffset;

View file

@ -31,7 +31,7 @@ namespace MLEM.Font {
}
/// <inheritdoc />
protected override Vector2 CalcCharSize(char c) {
protected override Vector2 MeasureChar(char c) {
return this.Font.MeasureString(c.ToString());
}

View file

@ -124,8 +124,9 @@ namespace MLEM.Formatting {
if (i == 0)
token.DrawSelf(time, batch, pos + innerOffset, font, color, scale, depth);
token.DrawCharacter(time, batch, c, c.ToString(), i, pos + innerOffset, drawFont, drawColor, scale, depth);
innerOffset.X += font.MeasureChar(c).X * scale;
var cString = c.ToString();
token.DrawCharacter(time, batch, c, cString, i, pos + innerOffset, drawFont, drawColor, scale, depth);
innerOffset.X += font.MeasureString(cString).X * scale;
}
}
}