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

simplified GenericFont implementation

This commit is contained in:
Ell 2021-04-19 14:02:28 +02:00
parent e7ab8fefe8
commit 8078d41724
6 changed files with 16 additions and 143 deletions

View file

@ -38,31 +38,11 @@ namespace MLEM.Extended.Font {
return region != null ? new Vector2(region.XAdvance, region.Height) : Vector2.Zero; return region != null ? new Vector2(region.XAdvance, region.Height) : Vector2.Zero;
} }
/// <inheritdoc/>
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color) {
batch.DrawString(this.Font, text, position, color);
}
/// <inheritdoc/>
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
}
/// <inheritdoc/> /// <inheritdoc/>
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) { public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth); batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
} }
/// <inheritdoc/>
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color) {
batch.DrawString(this.Font, text, position, color);
}
/// <inheritdoc/>
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
}
/// <inheritdoc/> /// <inheritdoc/>
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) { public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth); batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);

View file

@ -36,31 +36,11 @@ namespace MLEM.Extended.Font {
this.Italic = italic != null ? new GenericStashFont(italic) : this; this.Italic = italic != null ? new GenericStashFont(italic) : this;
} }
/// <inheritdoc />
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color) {
this.Font.DrawText(batch, text, position, color);
}
/// <inheritdoc />
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
this.Font.DrawText(batch, text, position, color, new Vector2(scale), rotation, origin, layerDepth);
}
/// <inheritdoc /> /// <inheritdoc />
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) { public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
this.Font.DrawText(batch, text, position, color, scale, rotation, origin, layerDepth); this.Font.DrawText(batch, text, position, color, scale, rotation, origin, layerDepth);
} }
/// <inheritdoc />
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color) {
this.Font.DrawText(batch, text, position, color);
}
/// <inheritdoc />
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
this.Font.DrawText(batch, text, position, color, new Vector2(scale), rotation, origin, layerDepth);
}
/// <inheritdoc /> /// <inheritdoc />
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) { public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
this.Font.DrawText(batch, text, position, color, scale, rotation, origin, layerDepth); this.Font.DrawText(batch, text, position, color, scale, rotation, origin, layerDepth);

View file

@ -50,21 +50,9 @@ namespace MLEM.Ui.Style {
return Vector2.Zero; return Vector2.Zero;
} }
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color) {
}
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
}
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) { public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
} }
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color) {
}
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
}
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) { public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
} }

View file

@ -41,59 +41,33 @@ namespace MLEM.Font {
///<inheritdoc cref="SpriteFont.LineSpacing"/> ///<inheritdoc cref="SpriteFont.LineSpacing"/>
public abstract float LineHeight { get; } public abstract float LineHeight { get; }
///<inheritdoc cref="SpriteBatch.DrawString(SpriteFont,string,Vector2,Color,float,Vector2,float,SpriteEffects,float)"/> ///<inheritdoc cref="SpriteFont.MeasureString(string)"/>
public abstract void DrawString(SpriteBatch batch, string text, Vector2 position, Color color); protected abstract Vector2 MeasureChar(char c);
///<inheritdoc cref="SpriteBatch.DrawString(SpriteFont,string,Vector2,Color,float,Vector2,float,SpriteEffects,float)"/>
public abstract void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth);
///<inheritdoc cref="SpriteBatch.DrawString(SpriteFont,string,Vector2,Color,float,Vector2,float,SpriteEffects,float)"/> ///<inheritdoc cref="SpriteBatch.DrawString(SpriteFont,string,Vector2,Color,float,Vector2,float,SpriteEffects,float)"/>
public abstract void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth); public abstract void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth);
///<inheritdoc cref="SpriteBatch.DrawString(SpriteFont,string,Vector2,Color,float,Vector2,float,SpriteEffects,float)"/>
public abstract void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color);
///<inheritdoc cref="SpriteBatch.DrawString(SpriteFont,string,Vector2,Color,float,Vector2,float,SpriteEffects,float)"/>
public abstract void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth);
///<inheritdoc cref="SpriteBatch.DrawString(SpriteFont,string,Vector2,Color,float,Vector2,float,SpriteEffects,float)"/> ///<inheritdoc cref="SpriteBatch.DrawString(SpriteFont,string,Vector2,Color,float,Vector2,float,SpriteEffects,float)"/>
public abstract void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth); 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)"/> ///<inheritdoc cref="SpriteBatch.DrawString(SpriteFont,string,Vector2,Color,float,Vector2,float,SpriteEffects,float)"/>
protected abstract Vector2 MeasureChar(char c); public void DrawString(SpriteBatch batch, string text, Vector2 position, Color color) {
this.DrawString(batch, text, position, color, 0, Vector2.Zero, Vector2.One, SpriteEffects.None, 0);
/// <summary>
/// Draws a string with the given text alignment.
/// </summary>
/// <param name="batch">The sprite batch to use</param>
/// <param name="text">The string to draw</param>
/// <param name="position">The position of the top left corner of the string</param>
/// <param name="align">The alignment to use</param>
/// <param name="color">The color to use</param>
public void DrawString(SpriteBatch batch, string text, Vector2 position, TextAlign align, Color color) {
this.DrawString(batch, text, position, align, color, 0, Vector2.Zero, Vector2.One, SpriteEffects.None, 0);
} }
///<inheritdoc cref="DrawString(SpriteBatch,string,Vector2,TextAlign,Color)"/> ///<inheritdoc cref="SpriteBatch.DrawString(SpriteFont,string,Vector2,Color,float,Vector2,float,SpriteEffects,float)"/>
public void DrawString(SpriteBatch batch, string text, Vector2 position, TextAlign align, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) { public void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
this.DrawString(batch, text, position, align, color, rotation, origin, new Vector2(scale), effects, layerDepth); this.DrawString(batch, text, position, color, rotation, origin, new Vector2(scale), effects, layerDepth);
} }
///<inheritdoc cref="DrawString(SpriteBatch,string,Vector2,TextAlign,Color)"/> ///<inheritdoc cref="SpriteBatch.DrawString(SpriteFont,string,Vector2,Color,float,Vector2,float,SpriteEffects,float)"/>
public void DrawString(SpriteBatch batch, string text, Vector2 position, TextAlign align, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) { public void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color) {
switch (align) { this.DrawString(batch, text, position, color, 0, Vector2.Zero, Vector2.One, SpriteEffects.None, 0);
case TextAlign.Center: }
case TextAlign.CenterBothAxes:
var (w, h) = this.MeasureString(text) * scale; ///<inheritdoc cref="SpriteBatch.DrawString(SpriteFont,string,Vector2,Color,float,Vector2,float,SpriteEffects,float)"/>
position.X -= w / 2; public void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
if (align == TextAlign.CenterBothAxes) this.DrawString(batch, text, position, color, rotation, origin, new Vector2(scale), effects, layerDepth);
position.Y -= h / 2;
break;
case TextAlign.Right:
position.X -= this.MeasureString(text).X * scale.X;
break;
}
this.DrawString(batch, text, position, color, rotation, origin, scale, effects, layerDepth);
} }
///<inheritdoc cref="SpriteFont.MeasureString(string)"/> ///<inheritdoc cref="SpriteFont.MeasureString(string)"/>
@ -214,28 +188,4 @@ namespace MLEM.Font {
} }
} }
/// <summary>
/// An enum that represents the text alignment options for <see cref="GenericFont.DrawString(SpriteBatch,string,Vector2,TextAlign,Color)"/>
/// </summary>
public enum TextAlign {
/// <summary>
/// The text is aligned as normal
/// </summary>
Left,
/// <summary>
/// The position passed represents the center of the resulting string in the x axis
/// </summary>
Center,
/// <summary>
/// The position passed represents the right edge of the resulting string
/// </summary>
Right,
/// <summary>
/// The position passed represents the center of the resulting string, both in the x and y axes
/// </summary>
CenterBothAxes
}
} }

View file

@ -37,31 +37,11 @@ namespace MLEM.Font {
return this.Font.MeasureString(c.ToCachedString()); return this.Font.MeasureString(c.ToCachedString());
} }
/// <inheritdoc/>
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color) {
batch.DrawString(this.Font, text, position, color);
}
/// <inheritdoc/>
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
}
/// <inheritdoc/> /// <inheritdoc/>
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) { public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth); batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
} }
/// <inheritdoc/>
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color) {
batch.DrawString(this.Font, text, position, color);
}
/// <inheritdoc/>
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
}
/// <inheritdoc/> /// <inheritdoc/>
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) { public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth); batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);

View file

@ -131,11 +131,6 @@ namespace Sandbox {
this.OnDraw += (game, time) => { this.OnDraw += (game, time) => {
this.SpriteBatch.Begin(); this.SpriteBatch.Begin();
system.GetFont(32).DrawText(this.SpriteBatch, "Left Aligned\nover multiple lines", new Vector2(640, 0), Color.White);
font.DrawString(this.SpriteBatch, "Left Aligned\nover multiple lines", new Vector2(640, 0), TextAlign.Left, Color.White);
font.DrawString(this.SpriteBatch, "Center Aligned\nover multiple lines", new Vector2(640, 100), TextAlign.Center, Color.White);
font.DrawString(this.SpriteBatch, "Right Aligned\nover multiple lines", new Vector2(640, 200), TextAlign.Right, Color.White);
font.DrawString(this.SpriteBatch, "Center Aligned on both axes", new Vector2(640, 360), TextAlign.CenterBothAxes, Color.White);
this.SpriteBatch.Draw(this.SpriteBatch.GetBlankTexture(), new Rectangle(640 - 4, 360 - 4, 8, 8), Color.Green); this.SpriteBatch.Draw(this.SpriteBatch.GetBlankTexture(), new Rectangle(640 - 4, 360 - 4, 8, 8), Color.Green);
this.SpriteBatch.Draw(this.SpriteBatch.GetBlankTexture(), new Rectangle(200, 400, 200, 400), Color.Green); this.SpriteBatch.Draw(this.SpriteBatch.GetBlankTexture(), new Rectangle(200, 400, 200, 400), Color.Green);