1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-16 18:53:12 +01:00
MLEM/MLEM.Extended/Font/GenericStashFont.cs

46 lines
1.8 KiB
C#
Raw Normal View History

using FontStashSharp;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Extensions;
using MLEM.Font;
namespace MLEM.Extended.Font {
/// <inheritdoc/>
public class GenericStashFont : GenericFont {
/// <summary>
/// The <see cref="SpriteFontBase"/> that is being wrapped by this generic font
/// </summary>
public readonly SpriteFontBase Font;
/// <inheritdoc />
public override GenericFont Bold { get; }
/// <inheritdoc />
public override GenericFont Italic { get; }
/// <inheritdoc />
public override float LineHeight => this.Font.LineHeight;
/// <summary>
/// Creates a new generic font using <see cref="SpriteFontBase"/>.
/// Optionally, a bold and italic version of the font can be supplied.
/// </summary>
/// <param name="font">The font to wrap</param>
/// <param name="bold">A bold version of the font</param>
/// <param name="italic">An italic version of the font</param>
public GenericStashFont(SpriteFontBase font, SpriteFontBase bold = null, SpriteFontBase italic = null) {
this.Font = font;
this.Bold = bold != null ? new GenericStashFont(bold) : this;
this.Italic = italic != null ? new GenericStashFont(italic) : this;
}
/// <inheritdoc />
2021-12-22 12:46:17 +01:00
protected override float MeasureChar(char c) {
return this.Font.MeasureString(c.ToCachedString()).X;
}
/// <inheritdoc />
2021-12-28 14:56:11 +01:00
protected override void DrawChar(SpriteBatch batch, string cString, Vector2 position, Color color, float rotation, Vector2 scale, SpriteEffects effects, float layerDepth) {
this.Font.DrawText(batch, cString, position, color, scale, rotation, Vector2.Zero, layerDepth);
}
}
}