-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRenderer.cs
521 lines (468 loc) · 17.2 KB
/
Renderer.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace GUI
{
public class Renderer
{
public Effect GUIEffect;
public Texture2D WindowSkin;
public Texture2D InventoryPartsMap;
public Texture2D AbilityMap;
public Texture2D SmallIconMap;
public SpriteFont UIFont;
public SpriteFont FloatFont;
private SpriteBatch batch;
public Renderer(GraphicsDevice device)
{
this.batch = new SpriteBatch(device);
}
/// <summary>
/// Sets texture for the GUIEffect. If the texture being set is the same one, nothing happens.
/// </summary>
/// <param name="texture">Any Texture2D.</param>
public void SetTexture2(Texture2D texture)
{
// if (GUIEffect.Texture != texture)
{
// GUIEffect.Texture = texture;
// GUIEffect.DiffuseColor = new Vector3(0, 1, 0);
// GUIEffect.
foreach (EffectPass p in GUIEffect.CurrentTechnique.Passes)
{
p.Apply();
}
}
}
public void SetTexture(Texture2D texture)
{
// if (GUIEffect.Texture != texture)
{
GUIEffect.Parameters["IconMap"].SetValue(texture);
// GUIEffect.
foreach (EffectPass p in GUIEffect.CurrentTechnique.Passes)
{
p.Apply();
}
}
}
public void SetColour(Color Colour, int Alpha = 127)
{
// if (GUIEffect.Texture != texture)
Colour.A = (byte)Alpha;
{
GUIEffect.Parameters["Colour"].SetValue(Colour.ToVector4());
// GUIEffect.
foreach (EffectPass p in GUIEffect.CurrentTechnique.Passes)
{
p.Apply();
}
}
}
public struct Rect
{
public int Left { get; set; }
public int Right { get; set; }
public int Top { get; set; }
public int Bottom { get; set; }
public Rect(int X, int Y, int Width, int Height) : this()
{
this.Left = X;
this.Right = X + Width;
this.Top = Y;
this.Bottom = Y + Height;
}
}
public struct Slice9
{
public Rect Outer { get; set; }
public Rect Inner { get; set; }
public Slice9(int X, int Y, int Width, int Height, int Left, int Right, int Top, int Bottom) : this()
{
this.Outer = new Rect(X, Y, Width, Height);
this.Inner = new Rect(X + Left, Y + Top, Width - (Right + Left), Height - (Bottom + Top));
}
public Slice9(int X, int Y, int Width, int Height, int Margin) : this()
{
this.Outer = new Rect(X, Y, Width, Height);
this.Inner = new Rect(X + Margin, Y + Margin, Width - Margin * 2, Height - Margin * 2);
}
#region Matrix
#region A
public Vector2 AA
{
get
{
return new Vector2(this.Outer.Left, this.Outer.Top);
}
}
public Vector2 AB
{
get
{
return new Vector2(this.Inner.Left, this.Outer.Top);
}
}
public Vector2 AC
{
get
{
return new Vector2(this.Inner.Right, this.Outer.Top);
}
}
public Vector2 AD
{
get
{
return new Vector2(this.Outer.Right, this.Outer.Top);
}
}
#endregion
#region B
public Vector2 BA
{
get
{
return new Vector2(this.Outer.Left, this.Inner.Top);
}
}
public Vector2 BB
{
get
{
return new Vector2(this.Inner.Left, this.Inner.Top);
}
}
public Vector2 BC
{
get
{
return new Vector2(this.Inner.Right, this.Inner.Top);
}
}
public Vector2 BD
{
get
{
return new Vector2(this.Outer.Right, this.Inner.Top);
}
}
#endregion
#region C
public Vector2 CA
{
get
{
return new Vector2(this.Outer.Left, this.Inner.Bottom);
}
}
public Vector2 CB
{
get
{
return new Vector2(this.Inner.Left, this.Inner.Bottom);
}
}
public Vector2 CC
{
get
{
return new Vector2(this.Inner.Right, this.Inner.Bottom);
}
}
public Vector2 CD
{
get
{
return new Vector2(this.Outer.Right, this.Inner.Bottom);
}
}
#endregion
#region D
public Vector2 DA
{
get
{
return new Vector2(this.Outer.Left, this.Outer.Bottom);
}
}
public Vector2 DB
{
get
{
return new Vector2(this.Inner.Left, this.Outer.Bottom);
}
}
public Vector2 DC
{
get
{
return new Vector2(this.Inner.Right, this.Outer.Bottom);
}
}
public Vector2 DD
{
get
{
return new Vector2(this.Outer.Right, this.Outer.Bottom);
}
}
#endregion
#endregion
public int Top
{
get
{
return Inner.Top - Outer.Top;
}
}
public int Bottom
{
get
{
return Outer.Bottom - Inner.Bottom;
}
}
public int Left
{
get
{
return Inner.Left - Outer.Left;
}
}
public int Right
{
get
{
return Outer.Right - Inner.Right;
}
}
}
public static Vector2 TexelToCoord(int X, int Y, Texture2D Texture)
{
Vector2 output;
float TextureX = (float)X / (float)Texture.Width;
float TextureY = (float)Y / (float)Texture.Height;
output = new Vector2(TextureX, TextureY);
return output;
}
public static Vector2 TexelToCoord(Vector2 Coord, Texture2D Texture)
{
// Coord.X += 0.5f;
// Coord.Y += 0.5f;
Coord.X /= Texture.Width;
Coord.Y /= Texture.Height;
return Coord;
}
#region Colours
// publstatic ic Color ColourGreen = new Color(0, 255, 100);
public static Color ColourBlue = new Color(0, 189, 255);
public static Color ColourGreen = new Color(0, 206, 0);
public static Color ColourGold = new Color(255, 200, 12);
public static Color ColourViolet = new Color(255, 12, 200);
public static Color ColourOrange = new Color(255, 100, 0);
public static Color ColourGray = new Color(200, 200, 200);
public static Color ColourYellow = new Color(255, 255, 0);
public static Color ColourRed = new Color(255, 20, 50);
public static Color ColourNeon = new Color(0, 240, 255);
public static string ColourToCode(Color Colour)
{
string s = "";
int r = Colour.R;
int g = Colour.G;
int b = Colour.B;
s =HexByte(r) +HexByte(g) +HexByte(b);
s = "^" + s + " ";
return s;
}
#endregion
public void RenderTexturedQuad(GraphicsDevice device, float X, float Y, float W, float H, Texture2D Texture)
{
SetTexture(Texture);
VertexPositionTexture[] c = GFXUtility.Quad(device, X, Y, W, H);
device.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, c, 0, c.Length / 3);
}
public void RenderClock(GraphicsDevice device, float X, float Y, float Progress, int Size = 32)
{
// GUIEffect.DiffuseColor = new Vector3(0.0f, 0.0f, 0.0f);
// GUIEffect.Alpha = 0.7f;
SetTexture(WindowSkin);
SetColour(new Color(0, 0, 0), 200);
VertexPositionTexture[] c = GFXUtility.Clock(device, X, Y, Progress, Size);
device.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, c, 0, c.Length / 3);
SetColour(Color.Gray);
// GUIEffect.DiffuseColor = new Vector3(1,1,1);
// GUIEffect.Alpha = 2.0f;
}
/// <summary>
/// Renders a progress bar
/// </summary>
/// <param name="device">Device to render to</param>
/// <param name="X">Location X</param>
/// <param name="Y">Location Y</param>
/// <param name="Width">Width of the bar</param>
/// <param name="Height">Height of the bar</param>
/// <param name="Progress">Amount of progress, floating point number between 0.0f (empty) and 1.0f (full)</param>
/// <param name="skin">Skin used for the bar</param>
public void RenderBar(GraphicsDevice device, float X, float Y, float Width, float Height, float Progress, int skin)
{
//SetTexture(BarTexture);
SetTexture(WindowSkin);
VertexPositionTexture[] bar = GFXUtility.Bar(device, X, Y, Width, Height, Progress, skin);
device.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, bar, 0, 4);
}
public void RenderSmallText(GraphicsDevice device, float X, float Y, string Text, Color Colour, bool Centered = false, bool outlined = false)
{
if (Text == null)
return;
if (Centered)
{
Vector2 sd = UIFont.MeasureString(Text);
X -= sd.X / 2;
X = (float)Math.Round(X);
}
batch.Begin();
if (outlined)
{
batch.DrawString(UIFont, Text, new Microsoft.Xna.Framework.Vector2(X, Y), Colour);
}
else
{
batch.DrawString(UIFont, Text, new Microsoft.Xna.Framework.Vector2(X, Y), Colour);
}
batch.End();
}
public void RenderBigText(GraphicsDevice device, float X, float Y, string Text, Color Colour, bool Centered = false)
{
if (Text == null)
return;
if (Centered)
{
Vector2 sd = FloatFont.MeasureString(Text);
X -= sd.X / 2;
X = (float)Math.Round(X);
}
batch.Begin();
batch.DrawString(FloatFont, Text, new Microsoft.Xna.Framework.Vector2(X, Y), Colour);
batch.End();
}
string ReadToSymbol(string Text, string Symbol, int Offset, out int NewOffset)
{
//NewOffset = 0;
int off = Text.IndexOf(Symbol, Offset);
if (off == -1)
{
NewOffset = Text.Length;
return Text.Substring(Offset);
}
NewOffset = off + 1;
return Text.Substring(Offset, off - Offset);
}
public void RenderRichText(GraphicsDevice device, float X, float Y, string Text, bool Outline = false)
{
if (Text == null)
return;
Color Colour = Color.White;
int offset = 0;
float dX = 0;
string s = "";
string cstring;
batch.Begin();
while (offset < Text.Length)
{
int o2 = 0;
s = ReadToSymbol(Text, "^", offset, out o2);
offset = o2;
cstring = ReadToSymbol(Text, " ", offset, out offset);
batch.DrawString(UIFont, s, new Vector2(X + dX, Y), Colour);
if (cstring.Length == 6)
{
int R, G, B;
R = int.Parse(cstring.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
G = int.Parse(cstring.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
B = int.Parse(cstring.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
Colour = new Color(R, G, B);
}
dX += UIFont.MeasureString(s).X;
}
batch.End();
}
public void RenderFrame(GraphicsDevice device, float X, float Y, float Width, float Height, Slice9? slice=null)
{
SetTexture(WindowSkin);
SetColour(Color.LightGray);
if(!slice.HasValue)
slice = new Slice9(0, 0, 48, 48, 5, 5, 17, 5);
VertexPositionTexture[] frame = GFXUtility.SlicedQuad(device, X, Y, Width, Height, slice.GetValueOrDefault(), WindowSkin);// GFXUtility.Frame(device, X, Y, Width, Height);
device.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, frame, 0, 18);
SetColour(Color.Gray);
}
public void RenderButton(GraphicsDevice device, float X, float Y, float Width, float Height, bool Hot = false)
{
SetTexture(WindowSkin);
Slice9 slice = new Slice9(80, Hot ? 24 : 0, 24, 24, 4);
VertexPositionTexture[] frame = GFXUtility.SlicedQuad(device, X, Y, Width, Height, slice, WindowSkin);
device.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, frame, 0, 18);
}
public void RenderCloseButton(GraphicsDevice device, float X, float Y, bool Hot = false)
{
SetTexture(WindowSkin);
Rect r = new Rect(48, Hot ? 16 : 0, 16, 16);
VertexPositionTexture[] butt = GFXUtility.Quad(device, X, Y, 16, 16, r,WindowSkin); // GFXUtility.CloseButton(device, X, Y, Hot);
device.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, butt, 0, 2);
}
public void RenderQuad(GraphicsDevice device, float X, float Y, float Width, float Height, Renderer.Rect TexMap)
{
SetTexture(WindowSkin);
VertexPositionTexture[] butt = GFXUtility.Quad(device, X, Y, Width, Height, TexMap, WindowSkin);
device.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, butt, 0, 2);
}
public void RenderBigIcon(GraphicsDevice device, float X, float Y, int Icon,Texture2D Texture)
{
SetTexture(Texture);
VertexPositionTexture[] i = GFXUtility.Icon(device, X, Y, Icon);
device.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, i, 0, 2);
}
public void RenderIconEx(GraphicsDevice device, float X, float Y, int Icon)
{
// SetTexture(bagIcons);
VertexPositionTexture[] i = GFXUtility.Icon(device, X, Y, Icon);
device.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, i, 0, 2);
}
public void RenderSmallIcon(GraphicsDevice device, float X, float Y, int Icon)
{
SetTexture(SmallIconMap);
VertexPositionTexture[] j = GFXUtility.Icon(device, X, Y, Icon, 16);
device.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, j, 0, 2);
}
public void RenderBox(GraphicsDevice device, float X, float Y, float Width, float Height)
{
SetTexture(WindowSkin);
VertexPositionTexture[] b = GFXUtility.Box(device, X, Y, Width, Height);
device.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, b, 0, 6);
}
public static string Pad(string String, int Length, string PadChr = "0")
{
string tstr = "";
for (int i = 1; i <= Length - String.Length; i++)
{
tstr += PadChr;
}
return tstr + String;
}
public static string Hex(int Number)
{
return Number.ToString("X");
}
public static string HexByte(int Number)
{
string num = Hex(Number);
num = Pad(num, 2);
return num;
}
}
}