diff --git a/MLEM/Font/GenericFont.cs b/MLEM/Font/GenericFont.cs index 1799a80..0f01fea 100644 --- a/MLEM/Font/GenericFont.cs +++ b/MLEM/Font/GenericFont.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Text; using Microsoft.Xna.Framework; @@ -24,6 +25,30 @@ 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); + 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, 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, 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); + position.X -= w / 2; + if (align == TextAlign.CenterBothAxes) + position.Y -= h / 2; + break; + case TextAlign.Right: + position.X -= this.MeasureString(text).X; + break; + } + this.DrawString(batch, text, position, color, rotation, origin, scale, effects, layerDepth); + } + public string TruncateString(string text, float width, float scale, bool fromBack = false) { var total = new StringBuilder(); for (var i = 0; i < text.Length; i++) { @@ -82,4 +107,13 @@ namespace MLEM.Font { } } + + public enum TextAlign { + + Left, + Center, + Right, + CenterBothAxes + + } } \ No newline at end of file diff --git a/Sandbox/GameImpl.cs b/Sandbox/GameImpl.cs index 00df559..f374f8f 100644 --- a/Sandbox/GameImpl.cs +++ b/Sandbox/GameImpl.cs @@ -51,8 +51,9 @@ namespace Sandbox { }; var tex = LoadContent("Textures/Test"); + var font = new GenericSpriteFont(LoadContent("Fonts/TestFont")); this.UiSystem.Style = new UntexturedStyle(this.SpriteBatch) { - Font = new GenericSpriteFont(LoadContent("Fonts/TestFont")), + Font = font, TextScale = 0.1F, PanelTexture = new NinePatch(new TextureRegion(tex, 0, 8, 24, 24), 8), ButtonTexture = new NinePatch(new TextureRegion(tex, 24, 8, 16, 16), 4) @@ -87,6 +88,16 @@ namespace Sandbox { var res = this.Content.LoadJson("Test"); Console.WriteLine(res); + + this.OnDraw += (game, time) => { + this.SpriteBatch.Begin(); + 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.End(); + }; } protected override void DoUpdate(GameTime gameTime) {