-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
CraftUI.cs
290 lines (256 loc) · 9.97 KB
/
CraftUI.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
using Microsoft.Xna.Framework;
using RecipeBrowser.UIElements;
using System.Collections.Generic;
using System.Threading;
using Terraria;
using Terraria.GameContent.UI.Elements;
using Terraria.UI;
namespace RecipeBrowser
{
class CraftUI
{
internal static string RBText(string key, string category = "CraftUI") => RecipeBrowser.RBText(category, key);
internal static CraftUI instance;
internal static Color color = new Color(90, 158, 57);// Color.YellowGreen;
internal UIPanel mainPanel;
internal UIPanel craftPanel;
internal UIList craftPathList;
internal UICraftQueryItemSlot recipeResultItemSlot;
internal List<UIElement> additionalDragTargets;
internal List<int> selectedIndexes;
internal bool craftPathsUpToDate = false;
public CraftUI()
{
instance = this;
additionalDragTargets = new List<UIElement>();
selectedIndexes = new List<int>();
}
internal UIElement CreateCraftPanel()
{
mainPanel = new UIPanel();
mainPanel.SetPadding(6);
mainPanel.BackgroundColor = color;
mainPanel.Top.Set(20, 0f);
mainPanel.Height.Set(-20, 1f);
mainPanel.Width.Set(0, 1f);
recipeResultItemSlot = new UICraftQueryItemSlot(new Item());
recipeResultItemSlot.emptyHintText = RBText("EmptyQuerySlotHint");
// Not used, UICraftQueryItemSlot calls SetItem on click.
// SetItem -> ReplaceWithFake -> OnItemChanged -> SetItem loop issue.
//recipeResultItemSlot.OnItemChanged += RecipeResultItemSlot_OnItemChanged;
mainPanel.Append(recipeResultItemSlot);
UICheckbox extendedCraft = new UICheckbox(RBText("Calc"), RBText("CalcTooltip"));
extendedCraft.Top.Set(42, 0f);
extendedCraft.Left.Set(0, 0f);
extendedCraft.SetText(" " + RBText("Calc"));
extendedCraft.Selected = RecipePath.extendedCraft;
extendedCraft.OnSelectedChanged += (s, e) =>
{
RecipeCatalogueUI.instance.InvalidateExtendedCraft();
RecipePath.extendedCraft = extendedCraft.Selected;
};
mainPanel.Append(extendedCraft);
int top = 2;
int left = 50;
// 1.3 used NPC.killCount/Banners, 1.4 now uses bestiary unlock state, handles killed, seen, talked to npcs. Tracked per world, synced in multiplayer.
UICheckbox lootables = new UICheckbox(RBText("Loot"), RBText("LootTooltip"));
lootables.Top.Set(top, 0f);
lootables.Left.Set(left, 0f);
lootables.Selected = RecipePath.allowLoots;
lootables.OnSelectedChanged += (s, e) =>
{
RecipeCatalogueUI.instance.InvalidateExtendedCraft();
RecipePath.allowLoots = lootables.Selected;
};
mainPanel.Append(lootables);
left += (int)lootables.MinWidth.Pixels + 6;
UICheckbox mineables = new UICheckbox(RBText("Mineable"), RBText("MineableTooltip"));
mineables.Top.Set(top, 0f);
mineables.Left.Set(left, 0f);
mineables.Selected = RecipePath.allowMineables;
mineables.OnSelectedChanged += (s, e) => {
RecipeCatalogueUI.instance.InvalidateExtendedCraft();
RecipePath.allowMineables = mineables.Selected;
};
mainPanel.Append(mineables);
left += (int)mineables.MinWidth.Pixels + 6;
UICheckbox bugnetables = new UICheckbox(RBText("Bugnet"), RBText("BugnetTooltip"));
bugnetables.Top.Set(top, 0f);
bugnetables.Left.Set(left, 0f);
bugnetables.Selected = RecipePath.allowBugNetables;
bugnetables.OnSelectedChanged += (s, e) => {
RecipeCatalogueUI.instance.InvalidateExtendedCraft();
RecipePath.allowBugNetables = bugnetables.Selected;
};
mainPanel.Append(bugnetables);
left += (int)bugnetables.MinWidth.Pixels + 6;
UICheckbox npcShopsCheckbox = new UICheckbox(RBText("Shop"), RBText("ShopTooltip"));
npcShopsCheckbox.Top.Set(top, 0f);
npcShopsCheckbox.Left.Set(left, 0f);
npcShopsCheckbox.Selected = RecipePath.allowPurchasable;
npcShopsCheckbox.OnSelectedChanged += (s, e) =>
{
RecipeCatalogueUI.instance.InvalidateExtendedCraft();
RecipePath.allowPurchasable = npcShopsCheckbox.Selected;
};
mainPanel.Append(npcShopsCheckbox);
left += (int)npcShopsCheckbox.MinWidth.Pixels + 6;
// Adjacent, Seen, Unseen. This allows Unseen.
UICheckbox missingStationsCheckbox = new UICheckbox(RBText("MissingStations"), RBText("MissingStationsTooltip"));
missingStationsCheckbox.Top.Set(top, 0f);
missingStationsCheckbox.Left.Set(left, 0f);
missingStationsCheckbox.Selected = RecipePath.allowMissingStations;
missingStationsCheckbox.OnSelectedChanged += (s, e) =>
{
RecipeCatalogueUI.instance.InvalidateExtendedCraft();
RecipePath.allowMissingStations = missingStationsCheckbox.Selected;
};
mainPanel.Append(missingStationsCheckbox);
left += (int)missingStationsCheckbox.MinWidth.Pixels + 6;
// Checkbox for RecipeAvailable?
// TODO: Handle other invalidations. Button here for forced refresh?
top += 25;
left = 50;
// Option to skip some items? Hotbar? Favorited?
UICheckbox sourceInventoryCheckbox = new UICheckbox(RBText("Inventory"), "");
sourceInventoryCheckbox.SetDisabled();
sourceInventoryCheckbox.Top.Set(top, 0f);
sourceInventoryCheckbox.Left.Set(left, 0f);
sourceInventoryCheckbox.Selected = RecipePath.sourceInventory;
sourceInventoryCheckbox.OnSelectedChanged += (s, e) =>
{
RecipeCatalogueUI.instance.InvalidateExtendedCraft();
RecipePath.sourceInventory = sourceInventoryCheckbox.Selected;
};
mainPanel.Append(sourceInventoryCheckbox);
left += (int)sourceInventoryCheckbox.MinWidth.Pixels + 6;
UICheckbox sourceBanksCheckbox = new UICheckbox(RBText("Banks"), RBText("BanksTooltip"));
sourceBanksCheckbox.SetDisabled();
sourceBanksCheckbox.Top.Set(top, 0f);
sourceBanksCheckbox.Left.Set(left, 0f);
sourceBanksCheckbox.Selected = RecipePath.sourceBanks;
sourceBanksCheckbox.OnSelectedChanged += (s, e) =>
{
RecipeCatalogueUI.instance.InvalidateExtendedCraft();
RecipePath.sourceBanks = sourceBanksCheckbox.Selected;
};
mainPanel.Append(sourceBanksCheckbox);
left += (int)sourceBanksCheckbox.MinWidth.Pixels + 6;
// Need to point to chest on hovering over ingredient, or actually implement taking from chest. Nearby, or siphon remotely?
UICheckbox sourceChestsCheckbox = new UICheckbox(RBText("Chests"), RBText("ChestsTooltip"));
sourceChestsCheckbox.SetDisabled();
sourceChestsCheckbox.Top.Set(top, 0f);
sourceChestsCheckbox.Left.Set(left, 0f);
sourceChestsCheckbox.Selected = RecipePath.sourceChests;
sourceChestsCheckbox.OnSelectedChanged += (s, e) =>
{
RecipeCatalogueUI.instance.InvalidateExtendedCraft();
RecipePath.sourceChests = sourceChestsCheckbox.Selected;
};
mainPanel.Append(sourceChestsCheckbox);
left += (int)sourceChestsCheckbox.MinWidth.Pixels + 6;
UICheckbox sourceMagicStorageCheckbox = new UICheckbox(RBText("MagicStorage"), RBText("MagicStorageTooltip"));
sourceMagicStorageCheckbox.SetDisabled();
sourceMagicStorageCheckbox.Top.Set(top, 0f);
sourceMagicStorageCheckbox.Left.Set(left, 0f);
sourceMagicStorageCheckbox.Selected = RecipePath.sourceMagicStorage;
sourceMagicStorageCheckbox.OnSelectedChanged += (s, e) =>
{
RecipeCatalogueUI.instance.InvalidateExtendedCraft();
RecipePath.sourceMagicStorage = sourceMagicStorageCheckbox.Selected;
};
mainPanel.Append(sourceMagicStorageCheckbox);
left += (int)sourceMagicStorageCheckbox.MinWidth.Pixels + 6;
top += 37;
craftPanel = new UIPanel();
craftPanel.SetPadding(6);
craftPanel.Top.Pixels = top;
craftPanel.Left.Set(0, 0f);
craftPanel.Width.Set(0, 1f);
craftPanel.Height.Set(-top - 16, 1f);
craftPanel.BackgroundColor = Color.DarkCyan;
// TODO: Fix vanilla UIList to properly capture scrollwheel
craftPathList = new UIList();
craftPathList.Width.Set(-24f, 1f);
craftPathList.Height.Set(0, 1f);
craftPathList.ListPadding = 6f;
craftPanel.Append(craftPathList);
var craftPathListScrollbar = new FixedUIScrollbar(RecipeBrowserUI.instance.userInterface);
craftPathListScrollbar.SetView(100f, 1000f);
craftPathListScrollbar.Height.Set(0, 1f);
craftPathListScrollbar.Left.Set(-20, 1f);
craftPanel.Append(craftPathListScrollbar);
craftPathList.SetScrollbar(craftPathListScrollbar);
mainPanel.Append(craftPanel);
UIText text = new UIText(RBText("BottomInstructions"), 0.85f);
text.Top.Set(-14, 1f);
text.HAlign = 0.5f;
mainPanel.Append(text);
additionalDragTargets.Add(text);
return mainPanel;
}
private void PopulateList(List<UIRecipeSlot> slots)
{
List<UIRecipePath> recipePaths = new List<UIRecipePath>();
craftPathList.Clear();
foreach (var slot in slots)
{
foreach (var path in slot.craftPaths)
{
recipePaths.Add(new UIRecipePath(path));
}
}
foreach (var item in recipePaths)
{
craftPathList.Add(item);
}
if (recipePaths.Count == 0)
{
var message = new UIText(RBText("NoPathsFound"));
craftPathList.Add(message);
}
}
internal void SetRecipe(int index)
{
// SetRecipe is more accessible than SetItem through the UI, but might not match the intended player usage. TODO: Option to not limit search to single recipe.
selectedIndexes.Clear();
selectedIndexes.Add(index);
Recipe recipe = Main.recipe[index];
recipeResultItemSlot.ReplaceWithFake(recipe.createItem.type);
craftPathsUpToDate = false;
}
internal void SetItem(int itemID)
{
List<int> indexes = new List<int>();
for (int i = 0; i < Recipe.numRecipes; i++)
{
Recipe r = Main.recipe[i];
if (r.createItem.type == itemID)
{
indexes.Add(i);
}
}
selectedIndexes.Clear();
selectedIndexes.AddRange(indexes);
recipeResultItemSlot.ReplaceWithFake(itemID);
craftPathsUpToDate = false;
}
internal void Update()
{
//Main.playerInventory = true;
if (craftPathsUpToDate)
return;
if (!RecipeBrowserUI.instance.ShowRecipeBrowser || RecipeBrowserUI.instance.CurrentPanel != RecipeBrowserUI.Craft)
return;
craftPathsUpToDate = true;
var slots = new List<UIRecipeSlot>();
foreach (var selectedIndex in selectedIndexes)
{
var slot = RecipeCatalogueUI.instance.recipeSlots[selectedIndex];
slot.CraftPathsImmediatelyNeeded();
slots.Add(slot);
}
PopulateList(slots);
}
}
}