From c213009134fe90f39e19534e7536beb341698771 Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Thu, 27 Feb 2020 15:46:16 -0500 Subject: [PATCH] Add Text.DrawOutlined() function & other text helpers. GitOrigin-RevId: 47f9115767c9acac31edaed13ba0ac62732a27f1 --- Shared/Camera.cs | 1 + Shared/ExtensionMethods.cs | 16 ++++++++++++---- Shared/Scene.cs | 19 +++---------------- Shared/Shared.projitems | 1 + Shared/Text.cs | 21 +++++++++++++++++++++ Shared/Textures.cs | 2 +- 6 files changed, 39 insertions(+), 21 deletions(-) create mode 100644 Shared/Text.cs diff --git a/Shared/Camera.cs b/Shared/Camera.cs index 7c41c24..db7bb43 100644 --- a/Shared/Camera.cs +++ b/Shared/Camera.cs @@ -11,6 +11,7 @@ namespace SemiColinGames { public int Height { get => bbox.Height; } public int Left { get => bbox.Left; } public int Top { get => bbox.Top; } + public Point HalfSize { get => new Point(Width / 2, Height / 2); } public Matrix Projection { get => Matrix.CreateOrthographicOffCenter(Left, Left + Width, Height, 0, -1, 1); diff --git a/Shared/ExtensionMethods.cs b/Shared/ExtensionMethods.cs index aeecc8e..b781156 100644 --- a/Shared/ExtensionMethods.cs +++ b/Shared/ExtensionMethods.cs @@ -1,9 +1,21 @@ using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; // All extension methods on built-in types (of C# or MonoGame) go in this file. +// Methods are ordered alphabetically by type name. namespace SemiColinGames { static class ExtensionMethods { + // Point + public static void Deconstruct(this Point point, out int x, out int y) => + (x, y) = (point.X, point.Y); + + // SpriteFont + public static Vector2 CenteredOn(this SpriteFont font, string text, Point position) { + Vector2 size = font.MeasureString(text); + return new Vector2(position.X - (int) size.X / 2, position.Y - (int) size.Y / 2); + } + // Vector2 public static Vector2 Rotate(this Vector2 point, float angle) { float cos = FMath.Cos(angle); @@ -12,9 +24,5 @@ namespace SemiColinGames { point.X * cos - point.Y * sin, point.Y * cos + point.X * sin); } - - // Point - public static void Deconstruct(this Point point, out int x, out int y) => - (x, y) = (point.X, point.Y); } } diff --git a/Shared/Scene.cs b/Shared/Scene.cs index c665d7f..53a8227 100644 --- a/Shared/Scene.cs +++ b/Shared/Scene.cs @@ -95,26 +95,13 @@ namespace SemiColinGames { Debug.Draw(graphics, basicEffect); // Draw in-world UI on top of everything. - // TODO: make a Text.cs file and move things like "black outlined text" there. if (paused) { - string text = "Paused"; - Vector2 size = Textures.BannerFont.MeasureString(text); - Vector2 position = new Vector2((camera.Width - size.X) / 2, (camera.Height - size.Y) / 2); - spriteBatch.Begin( SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, null); - for (int i = -1; i <= 1; i++) { - for (int j = -1; j <= 1; j++) { - if (i == 0 && j == 0) { - continue; - } - Vector2 stencilPos = new Vector2(position.X + i, position.Y + j); - spriteBatch.DrawString(Textures.BannerFont, text, stencilPos, Color.Black); - } - } - - spriteBatch.DrawString(Textures.BannerFont, text, position, Color.White); + string text = "Paused"; + Vector2 position = Textures.BannerFont.CenteredOn(text, camera.HalfSize); + Text.DrawOutlined(spriteBatch, Textures.BannerFont, text, position, Color.White); spriteBatch.End(); } diff --git a/Shared/Shared.projitems b/Shared/Shared.projitems index 832e155..0e40fc4 100644 --- a/Shared/Shared.projitems +++ b/Shared/Shared.projitems @@ -11,6 +11,7 @@ + diff --git a/Shared/Text.cs b/Shared/Text.cs new file mode 100644 index 0000000..a62c0de --- /dev/null +++ b/Shared/Text.cs @@ -0,0 +1,21 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; + +namespace SemiColinGames { + static class Text { + // Outlined text in black. + public static void DrawOutlined( + SpriteBatch spriteBatch, SpriteFont font, string text, Vector2 position, Color color) { + for (int i = -1; i <= 1; i++) { + for (int j = -1; j <= 1; j++) { + if (i == 0 && j == 0) { + continue; + } + Vector2 stencilPos = new Vector2(position.X + i, position.Y + j); + spriteBatch.DrawString(font, text, stencilPos, Color.Black); + } + } + spriteBatch.DrawString(font, text, position, color); + } + } +} diff --git a/Shared/Textures.cs b/Shared/Textures.cs index 77806d4..d23b3cf 100644 --- a/Shared/Textures.cs +++ b/Shared/Textures.cs @@ -28,7 +28,7 @@ namespace SemiColinGames { } } - class Textures { + static class Textures { public static SpriteFont DebugFont; public static SpriteFont BannerFont;