Browse Source

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

GitOrigin-RevId: 47f9115767
master
Colin McMillen 4 years ago
parent
commit
c213009134
  1. 1
      Shared/Camera.cs
  2. 16
      Shared/ExtensionMethods.cs
  3. 19
      Shared/Scene.cs
  4. 1
      Shared/Shared.projitems
  5. 21
      Shared/Text.cs
  6. 2
      Shared/Textures.cs

1
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);

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

19
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();
}

1
Shared/Shared.projitems

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

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

2
Shared/Textures.cs

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

Loading…
Cancel
Save