-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathSpriteBatchExtensions.cs
212 lines (177 loc) · 10.9 KB
/
SpriteBatchExtensions.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using System.IO;
using Blish_HUD.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended.BitmapFonts;
using MonoGame.Extended.TextureAtlases;
namespace Blish_HUD {
public class SpriteBatchParameters {
public SpriteSortMode SortMode { get; set; }
public BlendState BlendState { get; set; }
public SamplerState SamplerState { get; set; }
public DepthStencilState DepthStencilState { get; set; }
public RasterizerState RasterizerState { get; set; }
public Effect Effect { get; set; }
public Matrix? TransformMatrix { get; set; }
public SpriteBatchParameters(
SpriteSortMode sortMode = SpriteSortMode.Deferred,
BlendState blendState = null,
SamplerState samplerState = null,
DepthStencilState depthStencilState = null,
RasterizerState rasterizerState = null,
Effect effect = null,
Matrix? transformMatrix = null
) {
this.SortMode = sortMode;
this.BlendState = blendState;
this.SamplerState = samplerState;
this.DepthStencilState = depthStencilState;
this.RasterizerState = rasterizerState ?? BlishHud.Instance.UiRasterizer;
this.Effect = effect;
this.TransformMatrix = transformMatrix;
}
public static bool ParamsEqual(SpriteBatchParameters leftSpriteBatchParams, SpriteBatchParameters rightSpriteBatchParams) {
return Equals(leftSpriteBatchParams, rightSpriteBatchParams)
|| (Equals(leftSpriteBatchParams.SortMode, rightSpriteBatchParams.SortMode)
&& Equals(leftSpriteBatchParams.BlendState, rightSpriteBatchParams.BlendState)
&& Equals(leftSpriteBatchParams.DepthStencilState, rightSpriteBatchParams.DepthStencilState)
&& Equals(leftSpriteBatchParams.RasterizerState, rightSpriteBatchParams.RasterizerState)
&& Equals(leftSpriteBatchParams.Effect, rightSpriteBatchParams.Effect)
&& Equals(leftSpriteBatchParams.TransformMatrix, rightSpriteBatchParams.TransformMatrix));
}
}
public static class SpriteBatchExtensions {
public static void Begin(this SpriteBatch spriteBatch, SpriteBatchParameters parameters) {
spriteBatch.Begin(parameters.SortMode,
parameters.BlendState,
parameters.SamplerState,
parameters.DepthStencilState,
parameters.RasterizerState ?? BlishHud.Instance.UiRasterizer,
parameters.Effect,
parameters.TransformMatrix ?? GameService.Graphics.UIScaleTransform);
}
public static void DrawOnCtrl(this SpriteBatch spriteBatch, Control ctrl, Texture2D texture, Rectangle destinationRectangle) {
spriteBatch.Draw(texture,
destinationRectangle.ToBounds(ctrl.AbsoluteBounds),
Color.White * ctrl.AbsoluteOpacity());
}
public static void DrawOnCtrl(this SpriteBatch spriteBatch, Control ctrl, Texture2D texture, Rectangle destinationRectangle, Color color) {
spriteBatch.Draw(texture,
destinationRectangle.ToBounds(ctrl.AbsoluteBounds),
color * ctrl.AbsoluteOpacity());
}
public static void DrawOnCtrl(this SpriteBatch spriteBatch, Control ctrl, TextureRegion2D texture, Rectangle destinationRectangle) {
spriteBatch.Draw(texture,
destinationRectangle.ToBounds(ctrl.AbsoluteBounds),
Color.White * ctrl.AbsoluteOpacity());
}
public static void DrawOnCtrl(this SpriteBatch spriteBatch, Control ctrl, TextureRegion2D texture, Rectangle destinationRectangle, Color color) {
spriteBatch.Draw(texture,
destinationRectangle.ToBounds(ctrl.AbsoluteBounds),
color * ctrl.AbsoluteOpacity());
}
public static void DrawOnCtrl(this SpriteBatch spriteBatch, Control ctrl, Texture2D texture, Rectangle destinationRectangle, Rectangle? sourceRectangle) {
spriteBatch.Draw(texture,
destinationRectangle.ToBounds(ctrl.AbsoluteBounds),
sourceRectangle,
Color.White * ctrl.AbsoluteOpacity());
}
public static void DrawOnCtrl(this SpriteBatch spriteBatch, Control ctrl, Texture2D texture, Rectangle destinationRectangle, Rectangle? sourceRectangle, Color color) {
spriteBatch.Draw(texture,
destinationRectangle.ToBounds(ctrl.AbsoluteBounds),
sourceRectangle,
color * ctrl.AbsoluteOpacity());
}
public static void DrawOnCtrl(this SpriteBatch spriteBatch, Control ctrl, Texture2D texture, Rectangle destinationRectangle, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects = SpriteEffects.None) {
spriteBatch.Draw(texture,
destinationRectangle.ToBounds(ctrl.AbsoluteBounds),
sourceRectangle,
color * ctrl.AbsoluteOpacity(),
rotation,
origin,
effects,
0);
}
public static void DrawStringOnCtrl(this SpriteBatch spriteBatch,
Control ctrl,
string text,
BitmapFont font,
Rectangle destinationRectangle,
Color color,
bool wrap = false,
HorizontalAlignment horizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment verticalAlignment = VerticalAlignment.Middle) {
DrawStringOnCtrl(spriteBatch,
ctrl,
text,
font,
destinationRectangle,
color,
wrap,
false,
1,
horizontalAlignment,
verticalAlignment);
}
public static void DrawStringOnCtrl(this SpriteBatch spriteBatch,
Control ctrl,
string text,
BitmapFont font,
Rectangle destinationRectangle,
Color color,
bool wrap,
bool stroke,
int strokeDistance = 1,
HorizontalAlignment horizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment verticalAlignment = VerticalAlignment.Middle) {
if (string.IsNullOrEmpty(text)) return;
text = wrap ? DrawUtil.WrapText(font, text, destinationRectangle.Width) : text;
// TODO: This does not account for vertical alignment
if (horizontalAlignment != HorizontalAlignment.Left && (wrap || text.Contains("\n"))) {
using (StringReader reader = new StringReader(text)) {
string line;
int lineHeightDiff = 0;
while (destinationRectangle.Height - lineHeightDiff > 0 && (line = reader.ReadLine()) != null) {
DrawStringOnCtrl(spriteBatch, ctrl, line, font, destinationRectangle.Add(0, lineHeightDiff, 0, -0), color, wrap, stroke, strokeDistance, horizontalAlignment, verticalAlignment);
lineHeightDiff += font.LineHeight;
}
}
return;
}
Vector2 textSize = font.MeasureString(text);
destinationRectangle = destinationRectangle.ToBounds(ctrl.AbsoluteBounds);
int xPos = destinationRectangle.X;
int yPos = destinationRectangle.Y;
switch (horizontalAlignment) {
case HorizontalAlignment.Center:
xPos += destinationRectangle.Width / 2 - (int)textSize.X / 2;
break;
case HorizontalAlignment.Right:
xPos += destinationRectangle.Width - (int)textSize.X;
break;
}
switch (verticalAlignment) {
case VerticalAlignment.Middle:
yPos += destinationRectangle.Height / 2 - (int)textSize.Y / 2;
break;
case VerticalAlignment.Bottom:
yPos += destinationRectangle.Height - (int)textSize.Y;
break;
}
var textPos = new Vector2(xPos, yPos);
float absoluteOpacity = ctrl.AbsoluteOpacity();
if (stroke) {
var strokePreMultiplied = Color.Black * absoluteOpacity;
spriteBatch.DrawString(font, text, textPos.OffsetBy(0, -strokeDistance), strokePreMultiplied);
spriteBatch.DrawString(font, text, textPos.OffsetBy(strokeDistance, -strokeDistance), strokePreMultiplied);
spriteBatch.DrawString(font, text, textPos.OffsetBy(strokeDistance, 0), strokePreMultiplied);
spriteBatch.DrawString(font, text, textPos.OffsetBy(strokeDistance, strokeDistance), strokePreMultiplied);
spriteBatch.DrawString(font, text, textPos.OffsetBy(0, strokeDistance), strokePreMultiplied);
spriteBatch.DrawString(font, text, textPos.OffsetBy(-strokeDistance, strokeDistance), strokePreMultiplied);
spriteBatch.DrawString(font, text, textPos.OffsetBy(-strokeDistance, 0), strokePreMultiplied);
spriteBatch.DrawString(font, text, textPos.OffsetBy(-strokeDistance, -strokeDistance), strokePreMultiplied);
}
spriteBatch.DrawString(font, text, textPos, color * absoluteOpacity);
}
}
}