/
Text.cs
132 lines (107 loc) · 3.56 KB
/
Text.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using Microsoft.Xna.Framework;
using Monofoxe.Engine.Utils;
namespace Monofoxe.Engine.Drawing
{
public class Text : IDrawable
{
public Vector2 Position {get; set;}
public Vector2 Scale;
public Vector2 Origin;
public Angle Rotation;
public Color Color;
public string String;
public Text(string str, Vector2 position, Vector2 scale, Vector2 origin, Angle rotation)
{
String = str;
Position = position;
Scale = scale;
Origin = origin;
Rotation = rotation;
Color = GraphicsMgr.CurrentColor;
}
// Text.
public static IFont CurrentFont;
public static TextAlign HorAlign = TextAlign.Left;
public static TextAlign VerAlign = TextAlign.Top;
// Text.
public void Draw() =>
Draw(String, Position, Scale, Origin, Rotation);
/// <summary>
/// Draws text in specified coordinates.
/// </summary>
public static void Draw(string text, float x, float y) =>
Draw(text, new Vector2(x, y));
/// <summary>
/// Draws text in specified coordinates.
/// </summary>
public static void Draw(string text, Vector2 position)
{
if (CurrentFont == null)
{
throw new NullReferenceException("CurrentFont is null! Did you forgot to set a font?");
}
/*
* Font is a wrapper for MG's SpriteFont, which uses non-premultiplied alpha.
* Using GraphicsMode.Sprites will result in black pixels everywhere.
* TextureFont, on the other hand, is just a bunch of regular sprites,
* so it's fine to draw with sprite mode.
*/
if (CurrentFont is Font)
{
GraphicsMgr.SwitchGraphicsMode(GraphicsMode.SpritesNonPremultiplied);
}
else
{
GraphicsMgr.SwitchGraphicsMode(GraphicsMode.Sprites);
}
CurrentFont.Draw(GraphicsMgr._batch, text, position, HorAlign, VerAlign);
}
/// <summary>
/// Draws text in specified coordinates with rotation, scale and origin.
/// </summary>
public static void Draw(string text, Vector2 position, Vector2 scale, Vector2 origin, Angle rotation) =>
Draw(text, position.X, position.Y, scale.X, scale.Y, origin.X, origin.Y, rotation);
/// <summary>
/// Draws text in specified coordinates with rotation, scale and origin.
/// </summary>
public static void Draw(
string text,
float x,
float y,
float scaleX,
float scaleY,
float originX,
float originY,
Angle rotation
)
{
if (CurrentFont == null)
{
throw new NullReferenceException("CurrentFont is null! Did you forgot to set a font?");
}
var transformMatrix =
Matrix.CreateTranslation(new Vector3(-originX, -originY, 0)) * // Origin.
Matrix.CreateRotationZ(-rotation.RadiansF) * // Rotation.
Matrix.CreateScale(new Vector3(scaleX, scaleY, 1)) * // Scale.
Matrix.CreateTranslation(new Vector3(x, y, 0)); // Position.
GraphicsMgr.AddTransformMatrix(transformMatrix);
/*
* Font is a wrapper for MG's SpriteFont, which uses non-premultiplied alpha.
* Using GraphicsMode.Sprites will result in black pixels everywhere.
* TextureFont, on the other hand, is just regular sprites, so it's fine to
* draw with sprite mode.
*/
if (CurrentFont is Font)
{
GraphicsMgr.SwitchGraphicsMode(GraphicsMode.SpritesNonPremultiplied);
}
else
{
GraphicsMgr.SwitchGraphicsMode(GraphicsMode.Sprites);
}
CurrentFont.Draw(GraphicsMgr._batch, text, Vector2.Zero, HorAlign, VerAlign);
GraphicsMgr.ResetTransformMatrix();
}
}
}