forked from JavidPack/RecipeBrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecipeBrowserUI.cs
563 lines (483 loc) · 16.7 KB
/
RecipeBrowserUI.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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RecipeBrowser.UIElements;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Terraria;
using Terraria.GameContent.UI.Elements;
using Terraria.ModLoader;
using Terraria.UI;
namespace RecipeBrowser
{
/*
* ItemSlot FilterName CloseButton
* FilterDesc
*
*
*
*
*
*/
internal class RecipeBrowserUI : UIModState
{
internal static RecipeBrowserUI instance;
internal const int RecipeCatalogue = 0;
internal const int Craft = 1;
internal const int ItemCatalogue = 2;
internal const int Bestiary = 3;
internal const int Help = 4;
internal TabController tabController;
internal UIDragableElement mainPanel;
internal UIDragablePanel favoritePanel;
internal UIHoverImageButton closeButton;
internal SharedUI sharedUI;
internal RecipeCatalogueUI recipeCatalogueUI;
internal CraftUI craftUI;
internal ItemCatalogueUI itemCatalogueUI;
internal BestiaryUI bestiaryUI;
internal HelpUI helpUI;
internal List<int> localPlayerFavoritedRecipes => Main.LocalPlayer.GetModPlayer<RecipeBrowserPlayer>().favoritedRecipes;
internal bool[] foundItems;
internal string[] mods;
private bool showFavoritePanel;
public bool ShowFavoritePanel
{
get { return showFavoritePanel; }
set
{
if (value)
{
Append(favoritePanel);
}
else
{
RemoveChild(favoritePanel);
}
showFavoritePanel = value;
}
}
private bool showRecipeBrowser;
public bool ShowRecipeBrowser
{
get { return showRecipeBrowser; }
set
{
if (value)
{
Recipe.FindRecipes();
Append(mainPanel);
}
else
{
RemoveChild(mainPanel);
}
showRecipeBrowser = value;
}
}
public int CurrentPanel => tabController.currentPanel;
internal static string RBText(string key, string category = "RecipeBrowserUI") => RecipeBrowser.RBText(category, key);
public RecipeBrowserUI(UserInterface ui) : base(ui)
{
instance = this;
mods = new string[] { "ModLoader" };
}
// Technically since RecipeBrowserUI ctor happens in Mod.Load, we could miss mods that add during Load that happen after me.
public void PostSetupContent()
{
var type = Assembly.GetAssembly(typeof(Mod)).GetType("Terraria.ModLoader.Mod");
FieldInfo loadModsField = type.GetField("items", BindingFlags.Instance | BindingFlags.NonPublic);
mods = ModLoader.GetLoadedMods().Where(x => ((Dictionary<string, ModItem>)loadModsField.GetValue(ModLoader.GetMod(x))).Count > 0).ToArray();
modIndex = mods.Length - 1;
}
public override void OnInitialize()
{
mainPanel = new UIDragableElement(true, true, true);
//mainPanel.SetPadding(0);
//mainPanel.PaddingTop = 4;
mainPanel.Left.Set(400f, 0f);
mainPanel.Top.Set(400f, 0f);
mainPanel.Width.Set(475f, 0f); // + 30
mainPanel.MinWidth.Set(415f, 0f);
mainPanel.MaxWidth.Set(784f, 0f);
mainPanel.Height.Set(350, 0f);
mainPanel.MinHeight.Set(263, 0f);
mainPanel.MaxHeight.Set(1000, 0f);
//mainPanel.BackgroundColor = Color.LightBlue;
sharedUI = new SharedUI();
recipeCatalogueUI = new RecipeCatalogueUI();
craftUI = new CraftUI();
itemCatalogueUI = new ItemCatalogueUI();
bestiaryUI = new BestiaryUI();
helpUI = new HelpUI();
sharedUI.Initialize();
var recipePanel = recipeCatalogueUI.CreateRecipeCataloguePanel();
mainPanel.Append(recipePanel);
var craftPanel = craftUI.CreateCraftPanel();
mainPanel.Append(craftPanel);
var cataloguePanel = itemCatalogueUI.CreateItemCataloguePanel();
mainPanel.Append(cataloguePanel);
var bestiaryPanel = bestiaryUI.CreateBestiaryPanel();
mainPanel.Append(bestiaryPanel);
var helpPanel = helpUI.CreateHelpPanel();
//mainPanel.Append(helpPanel); // does this do anything?
tabController = new TabController(mainPanel);
tabController.AddPanel(recipePanel);
tabController.AddPanel(craftPanel);
tabController.AddPanel(cataloguePanel);
tabController.AddPanel(bestiaryPanel);
tabController.AddPanel(helpPanel);
mainPanel.AddDragTarget(recipePanel);
mainPanel.AddDragTarget(recipeCatalogueUI.recipeInfo);
mainPanel.AddDragTarget(recipeCatalogueUI.RadioButtonGroup);
mainPanel.AddDragTarget(craftPanel);
craftUI.additionalDragTargets.ForEach(x => mainPanel.AddDragTarget(x));
mainPanel.AddDragTarget(cataloguePanel);
itemCatalogueUI.additionalDragTargets.ForEach(x => mainPanel.AddDragTarget(x));
mainPanel.AddDragTarget(bestiaryPanel);
mainPanel.AddDragTarget(helpPanel);
mainPanel.AddDragTarget(helpUI.message);
UIPanel button = new UIBottomlessPanel();
button.SetPadding(0);
button.Left.Set(10, 0);
button.Width.Set(80, 0);
button.Height.Set(22, 0);
button.OnClick += (a, b) => tabController.SetPanel(RecipeCatalogue);
button.BackgroundColor = RecipeCatalogueUI.color;
UIText text = new UIText(RBText("Recipes"), 0.85f);
text.HAlign = 0.5f;
text.VAlign = 0.5f;
button.Append(text);
mainPanel.Append(button);
tabController.AddButton(button);
button = new UIBottomlessPanel();
button.SetPadding(0);
button.Left.Set(85, 0);
button.Width.Set(80, 0);
button.Height.Set(22, 0);
button.OnClick += (a, b) => { tabController.SetPanel(Craft); };
button.BackgroundColor = CraftUI.color;
text = new UIText(RBText("Craft"), 0.85f);
text.HAlign = 0.5f;
text.VAlign = 0.5f;
button.Append(text);
mainPanel.Append(button);
tabController.AddButton(button);
button = new UIBottomlessPanel();
button.SetPadding(0);
button.Left.Set(160, 0);
button.Width.Set(80, 0);
button.Height.Set(22, 0);
button.OnClick += (a, b) => { tabController.SetPanel(ItemCatalogue); itemCatalogueUI.updateNeeded = true; };
button.BackgroundColor = ItemCatalogueUI.color;
text = new UIText(RBText("Items"), 0.85f);
text.HAlign = 0.5f;
text.VAlign = 0.5f;
button.Append(text);
mainPanel.Append(button);
tabController.AddButton(button);
button = new UIBottomlessPanel();
button.SetPadding(0);
button.Left.Set(235, 0);
button.Width.Set(80, 0);
button.Height.Set(22, 0);
button.OnClick += (a, b) => tabController.SetPanel(Bestiary);
button.BackgroundColor = BestiaryUI.color;
text = new UIText(RBText("Bestiary"), 0.85f);
text.HAlign = 0.5f;
text.VAlign = 0.5f;
button.Append(text);
mainPanel.Append(button);
tabController.AddButton(button);
button = new UIBottomlessPanel();
button.SetPadding(0);
button.Left.Set(-155, 1);
button.Width.Set(80, 0);
button.Height.Set(22, 0);
button.OnClick += (a, b) => tabController.SetPanel(Help);
button.BackgroundColor = HelpUI.color;
text = new UIText("Help", 0.85f);
text.HAlign = 0.5f;
text.VAlign = 0.5f;
button.Append(text);
mainPanel.Append(button);
tabController.AddButton(button);
// TODO: Help panel with expandable help topics.
button = new UIBottomlessPanel();
button.SetPadding(0);
button.Left.Set(-80, 1);
button.Width.Set(70, 0);
button.Height.Set(22, 0);
button.BackgroundColor = Color.DarkRed;
var modFilterButton = new UIHoverImageButtonMod(RecipeBrowser.instance.GetTexture("Images/filterMod"), RBText("ModFilter") + ": " + RBText("All"));
modFilterButton.Left.Set(-60, 1f);
modFilterButton.Top.Set(-0, 0f);
modFilterButton.OnClick += ModFilterButton_OnClick;
modFilterButton.OnRightClick += ModFilterButton_OnRightClick;
modFilterButton.OnMiddleClick += ModFilterButton_OnMiddleClick;
button.Append(modFilterButton);
Texture2D texture = RecipeBrowser.instance.GetTexture("UIElements/closeButton");
closeButton = new UIHoverImageButton(texture, RBText("Close"));
closeButton.OnClick += CloseButtonClicked;
closeButton.Left.Set(-26, 1f);
closeButton.VAlign = 0.5f;
button.Append(closeButton);
mainPanel.Append(button);
tabController.SetPanel(0);
//favoritedRecipes = new List<int>();
favoritePanel = new UIDragablePanel();
favoritePanel.SetPadding(6);
favoritePanel.Left.Set(-310f, 0f);
favoritePanel.HAlign = 1f;
favoritePanel.Top.Set(90f, 0f);
favoritePanel.Width.Set(415f, 0f);
favoritePanel.MinWidth.Set(50f, 0f);
favoritePanel.MaxWidth.Set(600f, 0f);
favoritePanel.Height.Set(350, 0f);
favoritePanel.MinHeight.Set(50, 0f);
favoritePanel.MaxHeight.Set(300, 0f);
//favoritePanel.BackgroundColor = new Color(73, 94, 171);
favoritePanel.BackgroundColor = Color.Transparent;
//Append(favoritePanel);
}
//private void ItemChecklistRadioButton_OnRightClick(UIMouseEvent evt, UIElement listeningElement)
//{
// // Switch modes.
// ItemChecklistRadioButton.SetHoverText("Mode: Only All Results");
//}
// Vanilla ModLoader mod will act as "all"
internal static int modIndex;
private void ModFilterButton_OnClick(UIMouseEvent evt, UIElement listeningElement)
{
UIHoverImageButtonMod button = (evt.Target as UIHoverImageButtonMod);
button.hoverText = RBText("ModFilter") + ": " + GetModFilterTooltip(true);
UpdateModHoverImage(button);
AllUpdateNeeded();
}
private void ModFilterButton_OnRightClick(UIMouseEvent evt, UIElement listeningElement)
{
UIHoverImageButtonMod button = (evt.Target as UIHoverImageButtonMod);
button.hoverText = RBText("ModFilter") + ": " + GetModFilterTooltip(false);
UpdateModHoverImage(button);
AllUpdateNeeded();
}
private void ModFilterButton_OnMiddleClick(UIMouseEvent evt, UIElement listeningElement)
{
UIHoverImageButtonMod button = (evt.Target as UIHoverImageButtonMod);
modIndex = mods.Length - 1;
button.hoverText = RBText("ModFilter") + ": " + RBText("All");
UpdateModHoverImage(button);
AllUpdateNeeded();
}
private void UpdateModHoverImage(UIHoverImageButtonMod button)
{
button.texture = null;
Mod otherMod = ModLoader.GetMod(mods[modIndex]);
if (otherMod != null && otherMod.FileExists("icon.png"))
{
var modIconTexture = Texture2D.FromStream(Main.instance.GraphicsDevice, new MemoryStream(otherMod.GetFileBytes("icon.png")));
if (modIconTexture.Width == 80 && modIconTexture.Height == 80)
{
button.texture = modIconTexture;
}
}
}
private string GetModFilterTooltip(bool increment)
{
modIndex = increment ? (modIndex + 1) % mods.Length : (mods.Length + modIndex - 1) % mods.Length;
return modIndex == mods.Length - 1 ? RBText("All") : mods[modIndex];
}
internal void AllUpdateNeeded()
{
recipeCatalogueUI.updateNeeded = true;
itemCatalogueUI.updateNeeded = true;
bestiaryUI.updateNeeded = true;
}
public void NewItemFound(int type)
{
recipeCatalogueUI.newestItem = type;
recipeCatalogueUI.updateNeeded = true;
}
private void CloseButtonClicked(UIMouseEvent evt, UIElement listeningElement)
{
RecipeBrowserUI.instance.ShowRecipeBrowser = !RecipeBrowserUI.instance.ShowRecipeBrowser;
recipeCatalogueUI.CloseButtonClicked();
bestiaryUI.CloseButtonClicked();
}
internal void FavoriteChange(int index, bool favorite)
{
RecipeCatalogueUI.instance.recipeSlots[index].favorited = favorite;
localPlayerFavoritedRecipes.RemoveAll(x => x == index);
if (favorite)
localPlayerFavoritedRecipes.Add(index);
favoritePanelUpdateNeeded = true;
RecipeCatalogueUI.instance.updateNeeded = true;
}
internal bool favoritePanelUpdateNeeded;
internal void UpdateFavoritedPanel()
{
if (!favoritePanelUpdateNeeded)
return;
favoritePanelUpdateNeeded = false;
// Reset All
foreach (var recipeSlot in RecipeCatalogueUI.instance.recipeSlots)
{
recipeSlot.favorited = false;
}
foreach (var recipeIndex in localPlayerFavoritedRecipes)
{
RecipeCatalogueUI.instance.recipeSlots[recipeIndex].favorited = true;
}
ShowFavoritePanel = localPlayerFavoritedRecipes.Count > 0; // TODO: checkbox for force this.
favoritePanel.RemoveAllChildren();
UIGrid list = new UIGrid();
list.Width.Set(0, 1f);
list.Height.Set(0, 1f);
list.ListPadding = 5f;
list.OnScrollWheel += RecipeBrowserUI.OnScrollWheel_FixHotbarScroll;
favoritePanel.Append(list);
favoritePanel.AddDragTarget(list);
favoritePanel.AddDragTarget(list._innerList);
int width = 1;
int height = 0;
int order = 1;
for (int i = 0; i < Main.maxPlayers; i++)
{
if (i != Main.myPlayer && Main.player[i].active)
{
foreach (var recipeIndex in Main.player[i].GetModPlayer<RecipeBrowserPlayer>().favoritedRecipes) // Collection was modified potential with receiving other player starred recipes?
{
Recipe r = Main.recipe[recipeIndex];
UIRecipeProgress s = new UIRecipeProgress(recipeIndex, r, order, i);
order++;
s.Recalculate();
var a = s.GetInnerDimensions();
s.Width.Precent = 1;
list.Add(s);
height += (int)(a.Height + list.ListPadding);
width = Math.Max(width, (int)a.Width);
favoritePanel.AddDragTarget(s);
}
}
}
foreach (var recipeIndex in localPlayerFavoritedRecipes)
{
Recipe r = Main.recipe[recipeIndex];
UIRecipeProgress s = new UIRecipeProgress(recipeIndex, r, order, Main.myPlayer);
order++;
s.Recalculate();
var a = s.GetInnerDimensions();
s.Width.Precent = 1;
list.Add(s);
height += (int)(a.Height + list.ListPadding);
width = Math.Max(width, (int)a.Width);
favoritePanel.AddDragTarget(s);
}
favoritePanel.Height.Pixels = height + favoritePanel.PaddingBottom + favoritePanel.PaddingTop - list.ListPadding;
favoritePanel.Width.Pixels = width;
favoritePanel.Recalculate();
var scrollbar = new InvisibleFixedUIScrollbar(userInterface);
scrollbar.SetView(100f, 1000f);
scrollbar.Height.Set(0, 1f);
scrollbar.Left.Set(-20, 1f);
favoritePanel.Append(scrollbar);
list.SetScrollbar(scrollbar);
Recipe.FindRecipes();
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
// additional updates.
if (!mainPanel.GetDimensions().ToRectangle().Intersects(GetDimensions().ToRectangle()))
{
var parentSpace = GetDimensions().ToRectangle();
mainPanel.Left.Pixels = Utils.Clamp(mainPanel.Left.Pixels, 0, parentSpace.Right - mainPanel.Width.Pixels);
mainPanel.Top.Pixels = Utils.Clamp(mainPanel.Top.Pixels, 0, parentSpace.Bottom - mainPanel.Height.Pixels);
mainPanel.Recalculate();
}
sharedUI.Update();
recipeCatalogueUI.Update();
craftUI.Update();
itemCatalogueUI.Update();
bestiaryUI.Update();
UpdateFavoritedPanel();
}
internal void ItemReceived(Item item)
{
var removes = localPlayerFavoritedRecipes.Where(x => Main.recipe[x].createItem.type == item.type && Main.recipe[x].createItem.maxStack == 1).ToList();
foreach (var recipeIndex in removes)
{
FavoriteChange(recipeIndex, false);
}
}
internal void QueryItemChecklist()
{
object result = RecipeBrowser.itemChecklistInstance.Call("RequestFoundItems");
if (result is string)
{
Main.NewText("Error, ItemChecklist said: " + result);
}
else if (result is bool[])
{
RecipeBrowserUI.instance.foundItems = (result as bool[]);
}
}
internal static void OnScrollWheel_FixHotbarScroll(UIScrollWheelEvent evt, UIElement listeningElement)
{
Main.LocalPlayer.ScrollHotbar(Terraria.GameInput.PlayerInput.ScrollWheelDelta / 120);
}
}
internal class TabController
{
private UIElement parent;
private List<UIElement> panels;
private List<UIElement> buttons;
internal int currentPanel;
public TabController(UIElement parent)
{
this.parent = parent;
panels = new List<UIElement>();
buttons = new List<UIElement>();
}
public void AddPanel(UIElement element)
{
panels.Add(element);
}
public void AddButton(UIElement element)
{
buttons.Add(element);
}
public void SetPanel(int panelIndex)
{
if (panelIndex >= 0 && panelIndex < panels.Count)
{
currentPanel = panelIndex;
panels.ForEach(panel => { if (parent.HasChild(panel)) parent.RemoveChild(panel); });
for (int i = buttons.Count - 1; i >= 0; i--)
{
var button = buttons[i];
parent.RemoveChild(button); parent.Append(button);
}
parent.RemoveChild(buttons[panelIndex]);
parent.Append(panels[panelIndex]);
parent.Append(buttons[panelIndex]);
if(panelIndex == RecipeBrowserUI.ItemCatalogue)
{
SharedUI.instance.sortsAndFiltersPanel.Top.Set(0, 0f);
SharedUI.instance.sortsAndFiltersPanel.Width.Set(-272, 1);
SharedUI.instance.sortsAndFiltersPanel.Height.Set(60, 0f);
ItemCatalogueUI.instance.mainPanel.Append(SharedUI.instance.sortsAndFiltersPanel);
}
else if (panelIndex == RecipeBrowserUI.RecipeCatalogue)
{
SharedUI.instance.sortsAndFiltersPanel.Top.Set(60, 0f);
SharedUI.instance.sortsAndFiltersPanel.Width.Set(-52, 1);
SharedUI.instance.sortsAndFiltersPanel.Height.Set(60, 0f);
RecipeCatalogueUI.instance.mainPanel.Append(SharedUI.instance.sortsAndFiltersPanel);
}
}
}
}
}