-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUI.cs
507 lines (420 loc) · 18.3 KB
/
GUI.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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Realms.Effects;
using System;
using System.Collections.Generic;
using System.IO;
using Terraria;
using Terraria.Audio;
using Terraria.DataStructures;
using Terraria.Enums;
using Terraria.GameContent;
using Terraria.Graphics.Effects;
using Terraria.ID;
using Terraria.ModLoader;
using ReLogic.Graphics;
using ReLogic.Localization.IME;
using ReLogic.OS;
using ReLogic.Utilities;
using Terraria.ModLoader.IO;
using Terraria.ObjectData;
using static Realms.ContentHandler;
using Microsoft.Xna.Framework.Input;
namespace Realms
{
public static class UIHandler
{
public static List<IUIElement> ActiveUIList = new List<IUIElement>();//this gets cleared in the realm modworld on world load
public static List<IUIElement> ActivationQueue = new List<IUIElement>();
public static List<IUIElement> DeactivationQueue = new List<IUIElement>();
public static readonly IUIElement EmptyElement = new Empty();
public static IUIElement MouseElement;
public static Vector2 MouseElementOffset = Vector2.Zero;
public static bool LastMouseLeft;
public static void Update()
{
if (MouseElement != null)
MouseElement.Offset = new Vector2(Main.mouseX, Main.mouseY) + MouseElementOffset;
MouseElement = null;
foreach (GUI ui in ActivationQueue)
if (!ActiveUIList.Contains(ui))
{
ActiveUIList.Add(ui);
if (ui is GUI)
(ui as GUI).OnActivate();
}
ActivationQueue.Clear();
foreach (GUI ui in ActiveUIList)
{
if (ui.CheckHasInteracted(HasLeftClicked))
{
ActiveUIList.Remove(ui);
ActiveUIList.Insert(0, ui);//they are drawn in reverse order, so this moves it to the start of the list so its on top and checked first
break;
}
}
foreach (GUI ui in DeactivationQueue)
if (ActiveUIList.Contains(ui))
{
ActiveUIList.Remove(ui);
if (ui is GUI)
(ui as GUI).OnDeactivate();
}
DeactivationQueue.Clear();
LastMouseLeft = Main.mouseLeft;
}
public static void Draw(SpriteBatch spriteBatch)
{
for(int i = ActiveUIList.Count; i > 0; i--)//draws each ui in reverse order
{
ActiveUIList[i - 1].Draw(spriteBatch);
}
}
public static bool HasLeftClicked => Main.mouseLeft && !LastMouseLeft;
public static Rectangle GetRect(this IUIElement element) =>
new Rectangle((int)(element.RealPosition.X), (int)(element.RealPosition.Y), (int)element.Size.X, (int)element.Size.Y);
public static bool MouseOver(this IUIElement element) =>
element.GetRect().Contains(new Point(Main.mouseX, Main.mouseY));
public static void BlockItemUse()
{
Main.LocalPlayer.delayUseItem = true;
Main.mouseLeftRelease = false;
Main.LocalPlayer.releaseUseItem = false;
Main.LocalPlayer.mouseInterface = true;
}
public static void Activate(this IUIElement element) =>
ActivationQueue.Add(element);
public static void Deactivate(this IUIElement element) =>
DeactivationQueue.Add(element);
public static bool HasFocus(this IUIElement element) =>
ActiveUIList[0] == element || (element != EmptyElement && element.ParentUI.HasFocus());
}
//TODO
//figure out Ui scale (might be possible using RT and Main.UiScaleMatrix)
public interface IUIElement
{
IUIElement ParentUI { get; set; }
Vector2 Size { get; set; }
Vector2 Offset { get; set; }
Vector2 RealPosition { get; }
void Draw(SpriteBatch spriteBatch);
bool CheckHasInteracted(bool ClickReceived);
}
public class CloseButton : IUIElement
{
public IUIElement ParentUI { get; set; }
public Vector2 Size { get; set; } = new Vector2(16, 16);
public Vector2 Offset { get; set; } = Vector2.Zero;
public Vector2 RealPosition { get => ParentUI.RealPosition + Offset; }
public Texture2D texture = Main.cdTexture;
public Color unfocusedColor = Color.Gray;
public Color baseColor = Color.LightGray;
public Color highlightedColor = Color.White;
public CloseButton(IUIElement parentUI)
{
ParentUI = parentUI;
Offset = new Vector2(ParentUI.Size.X - Size.X, 0);
}
public virtual void Draw(SpriteBatch spriteBatch) => spriteBatch.Draw(texture, this.GetRect(), ParentUI.HasFocus() ? this.MouseOver() ? highlightedColor : baseColor : unfocusedColor);
public virtual bool CheckHasInteracted(bool ClickReceived)
{
if (this.MouseOver() && ClickReceived)
{
ParentUI.Deactivate();
return true;
}
return false;
}
}
public class DragBar : IUIElement
{
public IUIElement ParentUI { get; set; }
public Vector2 Size { get; set; } = new Vector2(20, 20);
public Vector2 Offset { get; set; } = Vector2.Zero;
public Vector2 RealPosition { get => ParentUI.RealPosition + Offset; }
public Texture2D texture = TextureAssets.BlackTile.Value;
//TODO unfocused color
public Color baseColor = Color.Transparent;
public Color highlightedColor = Color.LightGoldenrodYellow * 0.25f;
public DragBar() { }
public DragBar(IUIElement parentUI)
{
ParentUI = parentUI;
Size = new Vector2(parentUI.Size.X, 20);
}
bool drag = false;//remove
public virtual void Draw(SpriteBatch spriteBatch) => spriteBatch.Draw(texture, this.GetRect(), (this.MouseOver() && ParentUI.HasFocus()) ? highlightedColor : baseColor);
public virtual bool CheckHasInteracted(bool ClickReceived)//the window slipping can be fixed by removing this and having a Var in UiHandler for dragged element
{
if (this.MouseOver())
{
if (ClickReceived)
{
UIHandler.MouseElementOffset = ParentUI.Offset - new Vector2(Main.mouseX, Main.mouseY);
drag = true;
return true;
}
if (drag)
{
if (Main.mouseLeft && UIHandler.LastMouseLeft && ParentUI.HasFocus())
UIHandler.MouseElement = ParentUI;
else
drag = false;
}
}
//else //shouldn't be needed since you can move your mouse off this element without letting go of left mouse
// drag = false;
return false;
}
}
public class Button : IUIElement //for buttons to darken when the UI is out of focus, this needs a new color for
{
public IUIElement ParentUI { get; set; }
public Vector2 Size { get; set; } = new Vector2(32, 32);
public Vector2 Offset { get; set; } = Vector2.Zero;
public Vector2 RealPosition { get => ParentUI.RealPosition + Offset; }
public Color unfocusedColor = Color.DimGray;
public Texture2D texture = TextureAssets.BlackTile.Value;
public Color baseColor = Color.LightGray;
public Texture2D highlightedTexture = TextureAssets.BlackTile.Value;
public Color highlightedColor = Color.White;
public Action onClick;
public virtual void Draw(SpriteBatch spriteBatch)
{
bool hasFocus = ParentUI.HasFocus();
if (this.MouseOver() && !Main.mouseLeft && hasFocus)
spriteBatch.Draw(highlightedTexture, this.GetRect(), highlightedColor);
else
spriteBatch.Draw(texture, this.GetRect(), hasFocus ? baseColor : unfocusedColor);
}
public virtual bool CheckHasInteracted(bool ClickReceived)
{
if (this.MouseOver() && ClickReceived)
{
onClick?.Invoke();
return true;
}
return false;
}
}
public class ToggleButton : Button //simple toggle button, missing some features that may be needed
{
public Texture2D toggledTexture = TextureAssets.BlackTile.Value;
public Texture2D toggledhighlightedTexture = TextureAssets.BlackTile.Value;
public bool boolValue = false;
public override void Draw(SpriteBatch spriteBatch)
{
bool hasFocus = ParentUI.HasFocus();
if (this.MouseOver() && !Main.mouseLeft && hasFocus)
spriteBatch.Draw(boolValue ? toggledhighlightedTexture : highlightedTexture, this.GetRect(), highlightedColor);
else
spriteBatch.Draw(boolValue ? toggledTexture : texture, this.GetRect(), hasFocus ? baseColor : unfocusedColor);
}
public override bool CheckHasInteracted(bool ClickReceived)
{
if (this.MouseOver() && ClickReceived)
{
boolValue = !boolValue;
onClick?.Invoke();
return true;
}
return false;
}
}
public class UIText : IUIElement
{
public IUIElement ParentUI { get; set; }
public Vector2 Size { get; set; } = Vector2.One;
public Vector2 Offset { get; set; } = Vector2.Zero;
public Vector2 RealPosition { get => ParentUI.RealPosition + Offset; }
public virtual Color UnfocusedTextColor { get; set; } = Color.Gray;
public virtual Color TextColor { get; set; } = Color.White;
public virtual float TextScale { get; set; } = 1f;
public virtual string TextString { get; set; } = "Text Here";
public virtual void Draw(SpriteBatch spriteBatch) =>
Utils.DrawBorderString(spriteBatch, TextString, RealPosition, ParentUI.HasFocus() ? TextColor : UnfocusedTextColor, TextScale);
public virtual bool CheckHasInteracted(bool ClickReceived) => false;
}
public class UISprite : IUIElement
{
public IUIElement ParentUI { get; set; }
public Vector2 Size { get; set; } = Vector2.One;
public Vector2 Offset { get; set; } = Vector2.Zero;
public Vector2 RealPosition { get => ParentUI.RealPosition + Offset; }
public Texture2D texture = TextureAssets.BlackTile.Value;
public Color baseColor = Color.White;
public Color unfocusedColor = Color.Gray;
public virtual void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, this.GetRect(), ParentUI.HasFocus() ? baseColor : unfocusedColor);
}
public virtual bool CheckHasInteracted(bool ClickReceived) => false;
}
public class HoverText : IUIElement
{
public IUIElement ParentUI { get; set; }
public Vector2 Size { get; set; } = new Vector2(32, 32);
public Vector2 Offset { get; set; } = Vector2.Zero;
public Vector2 RealPosition { get => ParentUI.RealPosition + Offset; }
public virtual Color TextColor { get; set; } = Color.White;
public virtual float TextScale { get; set; } = 1f;
public virtual string TextString { get; set; } = "Text Here";
public virtual void Draw(SpriteBatch spriteBatch) {
if (this.MouseOver())
Main.instance.MouseTextHackZoom(TextString, Main.rare, 1);
}
public virtual bool CheckHasInteracted(bool ClickReceived) => false;
}
public class ItemSlot : IUIElement
{
public IUIElement ParentUI { get; set; }
public Vector2 Size { get; set; } = new Vector2(32, 32);
public Vector2 Offset { get; set; } = Vector2.Zero;
public Vector2 RealPosition { get => ParentUI.RealPosition + Offset; }
public Texture2D slotTexture = TextureAssets.InventoryBack.Value;
public Color slotColor = Color.White;
public Color slotUnfocusedColor = Color.Gray;
public Texture2D iconTexture = Main.EquipPageTexture[1];
public Color iconColor = Color.White;
public Color iconUnfocusedColor = Color.Gray;
public Color itemColor = Color.White;
public Color itemUnfocusedColor = Color.Gray;
public Func<Item, bool> itemAllowed;
public Item StoredItem = new Item();
private readonly Ref<Item> itemReference;
public ItemSlot(Ref<Item> itemRef = null)
{
if (itemRef != null)
{
itemReference = itemRef;
if (itemReference.Value == null)
{
itemReference.Value = new Item();
itemReference.Value.TurnToAir();
}
StoredItem = itemReference.Value;
}
else
StoredItem.TurnToAir();
}
public void TrySwapItem(ref Item item)
{
if(itemAllowed == null || itemAllowed(item))
{
if(!StoredItem.IsAir || !item.IsAir)//so sound doesn't play when clicking a empty slot
SoundEngine.PlaySound(SoundID.Grab);
Item temp = item;
item = StoredItem;
StoredItem = temp;
if (itemReference != null)
itemReference.Value = StoredItem;
}
}
public virtual void Draw(SpriteBatch spriteBatch)
{
Rectangle rect = this.GetRect();
bool hasFocus = ParentUI.HasFocus();
spriteBatch.Draw(slotTexture, rect, hasFocus ? slotColor : slotUnfocusedColor);
if (!StoredItem.IsAir)
{
Texture2D tex = TextureAssets.Item[StoredItem.type].Value;
Color texColor = hasFocus ? itemColor : itemUnfocusedColor;
Rectangle frame = Main.itemAnimations[StoredItem.type] != null ? Main.itemAnimations[StoredItem.type].GetFrame(tex) : tex.Frame();
Vector2 position = rect.TopLeft();
Vector2 origin = (frame.Size() - rect.Size()) / 2;
ModItem modItem = StoredItem.ModItem;
if (modItem != null)
{
if (modItem.PreDrawInInventory(spriteBatch, position, frame, texColor, texColor, origin, 1f))//vanilla item loader has a similar named method, keep in mind
spriteBatch.Draw(tex, position, frame, texColor, 0f, origin, 1f, default, default);
modItem.PostDrawInInventory(spriteBatch, position, frame, texColor, texColor, origin, 1f);
}
else
spriteBatch.Draw(tex, position, frame, texColor, 0f, origin, 1f, default, default);
if(StoredItem.stack > 1)
Utils.DrawBorderString(spriteBatch, StoredItem.stack.ToString(), rect.Center() - new Vector2(rect.Width / 3, 0), texColor);//TODO fix text offset
if (this.MouseOver())
{
Main.HoverItem = StoredItem;
Main.instance.MouseTextHackZoom("", Main.rare, 1);
}
}
else
{
Vector2 origin = (iconTexture.Size() - rect.Size()) / 2;
spriteBatch.Draw(iconTexture, rect.TopLeft(), null, hasFocus ? iconColor : iconUnfocusedColor, 0f, origin, 1f, default, default);
}
}
public virtual bool CheckHasInteracted(bool ClickReceived)
{
if (this.MouseOver() && ClickReceived)
{
TrySwapItem(ref Main.mouseItem);
return true;
}
return false;
}
}
public class Empty : IUIElement
{
public IUIElement ParentUI { get; set; }
public Vector2 Size { get; set; } = Vector2.Zero;
public Vector2 Offset { get; set; } = Vector2.Zero;
public Vector2 RealPosition { get => Offset; }
public void Draw(SpriteBatch spriteBatch) { }
public virtual bool CheckHasInteracted(bool ClickReceived) => false;
}
public class GUI : IUIElement//this may need to store the player that opened it for MP
{
public IUIElement ParentUI { get; set; } = UIHandler.EmptyElement;
public Vector2 Size { get; set; } = new Vector2(100, 100);
public Vector2 Offset { get; set; } = Vector2.Zero;
public virtual Vector2 RealPosition { get => ParentUI.RealPosition + Offset; }//virtual for anything that cant be set on creation
public Texture2D backTexture = TextureAssets.BlackTile.Value;
public Color focusedColor = Color.White;
public Color unfocusedColor = Color.Gray;
public List<IUIElement> elements = new List<IUIElement>();
public GUI()
{
Offset = (new Vector2(Main.screenWidth, Main.screenHeight) * 0.5f) - (Size * 0.5f);
OnCreate();
}
public virtual void OnCreate() { }
public virtual void OnActivate() { }
public virtual void OnDeactivate() { }
public void AddElement(IUIElement element)
{
if (!elements.Contains(element))
{
element.ParentUI = this;
elements.Add(element);
}
}
public void RemoveElement(IUIElement element)
{
if (elements.Contains(element))
elements.Remove(element);
}
public virtual void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(backTexture, this.GetRect(), this.HasFocus() ? focusedColor : unfocusedColor);
foreach (IUIElement element in elements)
{
element.Draw(spriteBatch);
}
}
public virtual bool CheckHasInteracted(bool ClickReceived)
{
bool mouseOver = this.MouseOver();
bool interacted = false;
foreach (IUIElement element in elements)
{
if (element.CheckHasInteracted(ClickReceived))
interacted = true;
}
if(mouseOver || interacted)
UIHandler.BlockItemUse();
return (mouseOver && ClickReceived) || interacted;
}
}
}