Add Text.DrawOutlined() function & other text helpers.

GitOrigin-RevId: 47f9115767c9acac31edaed13ba0ac62732a27f1
This commit is contained in:
Colin McMillen 2020-02-27 15:46:16 -05:00
parent 830fc1ee9a
commit c213009134
6 changed files with 39 additions and 21 deletions

View File

@ -11,6 +11,7 @@ namespace SemiColinGames {
public int Height { get => bbox.Height; } public int Height { get => bbox.Height; }
public int Left { get => bbox.Left; } public int Left { get => bbox.Left; }
public int Top { get => bbox.Top; } public int Top { get => bbox.Top; }
public Point HalfSize { get => new Point(Width / 2, Height / 2); }
public Matrix Projection { public Matrix Projection {
get => Matrix.CreateOrthographicOffCenter(Left, Left + Width, Height, 0, -1, 1); get => Matrix.CreateOrthographicOffCenter(Left, Left + Width, Height, 0, -1, 1);

View File

@ -1,9 +1,21 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
// All extension methods on built-in types (of C# or MonoGame) go in this file. // All extension methods on built-in types (of C# or MonoGame) go in this file.
// Methods are ordered alphabetically by type name.
namespace SemiColinGames { namespace SemiColinGames {
static class ExtensionMethods { 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 // Vector2
public static Vector2 Rotate(this Vector2 point, float angle) { public static Vector2 Rotate(this Vector2 point, float angle) {
float cos = FMath.Cos(angle); float cos = FMath.Cos(angle);
@ -12,9 +24,5 @@ namespace SemiColinGames {
point.X * cos - point.Y * sin, point.X * cos - point.Y * sin,
point.Y * cos + point.X * 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);
} }
} }

View File

@ -95,26 +95,13 @@ namespace SemiColinGames {
Debug.Draw(graphics, basicEffect); Debug.Draw(graphics, basicEffect);
// Draw in-world UI on top of everything. // Draw in-world UI on top of everything.
// TODO: make a Text.cs file and move things like "black outlined text" there.
if (paused) { 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( spriteBatch.Begin(
SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, null); SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, null);
for (int i = -1; i <= 1; i++) { string text = "Paused";
for (int j = -1; j <= 1; j++) { Vector2 position = Textures.BannerFont.CenteredOn(text, camera.HalfSize);
if (i == 0 && j == 0) { Text.DrawOutlined(spriteBatch, Textures.BannerFont, text, position, Color.White);
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);
spriteBatch.End(); spriteBatch.End();
} }

View File

@ -11,6 +11,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Camera.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Camera.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ExtensionMethods.cs" /> <Compile Include="$(MSBuildThisFileDirectory)ExtensionMethods.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Text.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Textures.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Textures.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Clock.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Clock.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Geometry.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Geometry.cs" />

21
Shared/Text.cs Normal file
View File

@ -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);
}
}
}

View File

@ -28,7 +28,7 @@ namespace SemiColinGames {
} }
} }
class Textures { static class Textures {
public static SpriteFont DebugFont; public static SpriteFont DebugFont;
public static SpriteFont BannerFont; public static SpriteFont BannerFont;