From 8078d417242522587a1588399af833eed6ec6e72 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Mon, 19 Apr 2021 14:02:28 +0200 Subject: [PATCH] simplified GenericFont implementation --- MLEM.Extended/Font/GenericBitmapFont.cs | 20 ------ MLEM.Extended/Font/GenericStashFont.cs | 20 ------ MLEM.Ui/Style/UntexturedStyle.cs | 12 ---- MLEM/Font/GenericFont.cs | 82 +++++-------------------- MLEM/Font/GenericSpriteFont.cs | 20 ------ Sandbox/GameImpl.cs | 5 -- 6 files changed, 16 insertions(+), 143 deletions(-) diff --git a/MLEM.Extended/Font/GenericBitmapFont.cs b/MLEM.Extended/Font/GenericBitmapFont.cs index d61bff7..7ac84cd 100644 --- a/MLEM.Extended/Font/GenericBitmapFont.cs +++ b/MLEM.Extended/Font/GenericBitmapFont.cs @@ -38,31 +38,11 @@ namespace MLEM.Extended.Font { return region != null ? new Vector2(region.XAdvance, region.Height) : Vector2.Zero; } - /// - public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color) { - batch.DrawString(this.Font, text, position, color); - } - - /// - 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); - } - /// 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); } - /// - public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color) { - batch.DrawString(this.Font, text, position, color); - } - - /// - 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); - } - /// 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); diff --git a/MLEM.Extended/Font/GenericStashFont.cs b/MLEM.Extended/Font/GenericStashFont.cs index 2ba376d..f4c3bc0 100644 --- a/MLEM.Extended/Font/GenericStashFont.cs +++ b/MLEM.Extended/Font/GenericStashFont.cs @@ -36,31 +36,11 @@ namespace MLEM.Extended.Font { this.Italic = italic != null ? new GenericStashFont(italic) : this; } - /// - public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color) { - this.Font.DrawText(batch, text, position, color); - } - - /// - 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); - } - /// 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); } - /// - public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color) { - this.Font.DrawText(batch, text, position, color); - } - - /// - 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); - } - /// 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); diff --git a/MLEM.Ui/Style/UntexturedStyle.cs b/MLEM.Ui/Style/UntexturedStyle.cs index 24c93d6..bc58fd6 100644 --- a/MLEM.Ui/Style/UntexturedStyle.cs +++ b/MLEM.Ui/Style/UntexturedStyle.cs @@ -50,21 +50,9 @@ namespace MLEM.Ui.Style { 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, 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) { } diff --git a/MLEM/Font/GenericFont.cs b/MLEM/Font/GenericFont.cs index bf59eea..42a3ce9 100644 --- a/MLEM/Font/GenericFont.cs +++ b/MLEM/Font/GenericFont.cs @@ -41,59 +41,33 @@ namespace MLEM.Font { /// public abstract float LineHeight { get; } - /// - public abstract void DrawString(SpriteBatch batch, string text, Vector2 position, Color color); - - /// - public abstract void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth); + /// + protected abstract Vector2 MeasureChar(char c); /// 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, StringBuilder text, Vector2 position, Color color); - - /// - public abstract void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, float 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); - /// - protected abstract Vector2 MeasureChar(char c); - - /// - /// Draws a string with the given text alignment. - /// - /// The sprite batch to use - /// The string to draw - /// The position of the top left corner of the string - /// The alignment to use - /// The color to use - 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); + /// + 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); } - /// - public void DrawString(SpriteBatch batch, string text, Vector2 position, TextAlign align, 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); + /// + 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, color, rotation, origin, new Vector2(scale), effects, layerDepth); } - /// - public void DrawString(SpriteBatch batch, string text, Vector2 position, TextAlign align, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) { - switch (align) { - case TextAlign.Center: - case TextAlign.CenterBothAxes: - var (w, h) = this.MeasureString(text) * scale; - position.X -= w / 2; - if (align == TextAlign.CenterBothAxes) - 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); + /// + public void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color) { + this.DrawString(batch, text, position, color, 0, Vector2.Zero, Vector2.One, SpriteEffects.None, 0); + } + + /// + public void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) { + this.DrawString(batch, text, position, color, rotation, origin, new Vector2(scale), effects, layerDepth); } /// @@ -214,28 +188,4 @@ namespace MLEM.Font { } } - - /// - /// An enum that represents the text alignment options for - /// - public enum TextAlign { - - /// - /// The text is aligned as normal - /// - Left, - /// - /// The position passed represents the center of the resulting string in the x axis - /// - Center, - /// - /// The position passed represents the right edge of the resulting string - /// - Right, - /// - /// The position passed represents the center of the resulting string, both in the x and y axes - /// - CenterBothAxes - - } } \ No newline at end of file diff --git a/MLEM/Font/GenericSpriteFont.cs b/MLEM/Font/GenericSpriteFont.cs index baac7eb..d7b1a7f 100644 --- a/MLEM/Font/GenericSpriteFont.cs +++ b/MLEM/Font/GenericSpriteFont.cs @@ -37,31 +37,11 @@ namespace MLEM.Font { return this.Font.MeasureString(c.ToCachedString()); } - /// - public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color) { - batch.DrawString(this.Font, text, position, color); - } - - /// - 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); - } - /// 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); } - /// - public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color) { - batch.DrawString(this.Font, text, position, color); - } - - /// - 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); - } - /// 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); diff --git a/Sandbox/GameImpl.cs b/Sandbox/GameImpl.cs index 0ee42db..589dfa6 100644 --- a/Sandbox/GameImpl.cs +++ b/Sandbox/GameImpl.cs @@ -131,11 +131,6 @@ namespace Sandbox { this.OnDraw += (game, time) => { 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(200, 400, 200, 400), Color.Green);