/
NPCInfoUI.cs
329 lines (289 loc) · 10.9 KB
/
NPCInfoUI.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
using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.ID;
using Terraria.UI;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
using Terraria.GameContent.UI.Elements;
using NPCInfo.UIElements;
namespace NPCInfo
{
class UINPCInfoMainPanel : UIDragablePanel
{
public UINPCInfoMainPanel(bool dragable = true, bool resizeableX = false, bool resizeableY = false) : base(dragable, resizeableX, resizeableY)
{
}
public override bool IsLock()
{
bool result = ModContent.GetInstance<NPCInfoConfig>().isUiLock;
return result;
}
}
class NPCInfoUI : UIModState
{
static internal NPCInfoUI instance;
internal UINPCInfoMainPanel panelMain;
internal UIHoverImageButton closeButton;
internal UIPanel inlaidPanel;
internal UIGrid gridNPC;
internal UIImageListButton btnViewMode;
internal UIImageListButton btnSpawnNPCFilter;
internal bool updateNeeded;
internal string caption = $"NPC Info v{NPCInfo.instance.Version} Count:0";
static internal int menuIconSize = 28;
static internal int menuMargin = 4;
private Dictionary<int, int> dicSpawnNPC = new Dictionary<int, int>();
private Dictionary<int, int> dicDropItem = new Dictionary<int, int>();
private bool showNPCInfo;
public bool ShowNPCInfo
{
get { return showNPCInfo; }
set
{
if (value)
{
Append(panelMain);
}
else
{
RemoveChild(panelMain);
}
showNPCInfo = value;
NPCInfo.instance.npcInfoTool.visible = value;
}
}
public NPCInfoUI(UserInterface ui) : base(ui)
{
instance = this;
}
public void InitializeUI()
{
RemoveAllChildren();
panelMain = new UINPCInfoMainPanel(true, true, true);
panelMain.caption = caption;
panelMain.SetPadding(6);
panelMain.Left.Set(400f, 0f);
panelMain.Top.Set(400f, 0f);
panelMain.Width.Set(290f, 0f);
panelMain.MinWidth.Set(290f, 0f);
panelMain.MaxWidth.Set(Main.screenWidth, 0f);
panelMain.Height.Set(298, 0f);
panelMain.MinHeight.Set(124, 0f);
panelMain.MaxHeight.Set(Main.screenHeight, 0f);
Texture2D texture = ModLoader.GetMod("NPCInfo").GetTexture("UIElements/closeButton");
closeButton = new UIHoverImageButton(texture, "Close");
closeButton.OnClick += (a, b) => ShowNPCInfo = false;
closeButton.Left.Set(-20f, 1f);
closeButton.Top.Set(6f, 0f);
panelMain.Append(closeButton);
inlaidPanel = new UIPanel();
inlaidPanel.SetPadding(5);
inlaidPanel.Top.Pixels = 32;
inlaidPanel.Width.Set(0, 1f);
inlaidPanel.Height.Set(-50, 1f);
panelMain.Append(inlaidPanel);
gridNPC = new UIGrid();
gridNPC.Width.Set(-20f, 1f);
gridNPC.Height.Set(0, 1f);
gridNPC.ListPadding = 2f;
inlaidPanel.Append(gridNPC);
var lootItemsScrollbar = new FixedUIScrollbar(userInterface);
lootItemsScrollbar.SetView(100f, 1000f);
lootItemsScrollbar.Height.Set(0, 1f);
lootItemsScrollbar.Left.Set(-20, 1f);
inlaidPanel.Append(lootItemsScrollbar);
gridNPC.SetScrollbar(lootItemsScrollbar);
int leftPos = menuMargin;
btnViewMode = new UIImageListButton(
new List<Texture2D>() {
Main.itemTexture[ItemID.AlphabetStatue1].Resize(menuIconSize),
Main.itemTexture[ItemID.AlphabetStatue2].Resize(menuIconSize),
Main.itemTexture[ItemID.AlphabetStatue3].Resize(menuIconSize)},
new List<object>() { ViewMode.CombatNPC, ViewMode.SpawnNPC, ViewMode.DropItem },
new List<string>() { "Combat NPC", "Spawn NPC", "Drop Item" });
btnViewMode.OnClick += (a, b) =>
{
btnViewMode.NextIamge();
ChangeViewMode();
};
btnViewMode.OnRightClick += (a, b) =>
{
btnViewMode.PrevIamge();
ChangeViewMode();
};
btnViewMode.Left.Set(leftPos, 0f);
btnViewMode.Top.Set(0f, 0f);
panelMain.Append(btnViewMode);
btnSpawnNPCFilter = new UIImageListButton(
new List<Texture2D>() {
Main.itemTexture[ItemID.AlphabetStatue1].Resize(menuIconSize),
Main.itemTexture[ItemID.AlphabetStatue2].Resize(menuIconSize)},
new List<object>() { SpawnNPCFilter.All, SpawnNPCFilter.HideTownNPC },
new List<string>() { "Spawn NPC: All", "Spawn NPC: Hide twon NPC" });
btnSpawnNPCFilter.OnClick += (a, b) =>
{
btnSpawnNPCFilter.NextIamge();
ChangeSpawnNPCFilter();
};
btnSpawnNPCFilter.OnRightClick += (a, b) =>
{
btnSpawnNPCFilter.PrevIamge();
ChangeSpawnNPCFilter();
};
leftPos += menuIconSize + menuMargin;
btnSpawnNPCFilter.Left.Set(leftPos, 0f);
btnSpawnNPCFilter.Top.Set(0f, 0f);
updateNeeded = true;
}
private void ChangeViewMode()
{
gridNPC.Clear();
updateNeeded = true;
InfoCombat.isUpdate = true;
if (ViewMode == ViewMode.SpawnNPC)
panelMain.Append(btnSpawnNPCFilter);
else
panelMain.RemoveChild(btnSpawnNPCFilter);
}
private void ChangeSpawnNPCFilter()
{
gridNPC.Clear();
dicSpawnNPC.Clear();
updateNeeded = true;
}
internal void UpdateGrid()
{
if (!updateNeeded) { return; }
updateNeeded = false;
InfoCombat.isUpdate = false;
gridNPC.Clear();
if (ViewMode == ViewMode.CombatNPC)
{
int sortOrder = InfoCombat.sortedInfo.Values.Count;
foreach (var type in InfoCombat.sortedInfo.Values)
{
var info = InfoCombat.combatNPC[type];
info.slot.sortOrder = sortOrder--;
gridNPC._items.Add(info.slot);
gridNPC._innerList.Append(info.slot);
}
}
else if (ViewMode == ViewMode.SpawnNPC)
{
foreach (var key in dicSpawnNPC.Keys)
{
var slot = new UISpawnNPCSlot(key, dicSpawnNPC[key]);
gridNPC._items.Add(slot);
gridNPC._innerList.Append(slot);
}
}
else if (ViewMode == ViewMode.DropItem)
{
foreach (var key in dicDropItem.Keys)
{
var slot = new UIItemSlot(key, dicDropItem[key]);
gridNPC._items.Add(slot);
gridNPC._innerList.Append(slot);
}
}
try
{
gridNPC.UpdateOrder();
gridNPC._innerList.Recalculate();
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write(ex.Message);
}
string replaceCaption = "Count:0";
switch (ViewMode)
{
case ViewMode.CombatNPC:
replaceCaption = $"Count: {gridNPC._items.Count}";
break;
case ViewMode.SpawnNPC:
replaceCaption = $"Count: {gridNPC._items.Count} ({Main.npc.Count(x => x.active)})";
break;
case ViewMode.DropItem:
replaceCaption = $"Count: {gridNPC._items.Count} ({Main.item.Count(x => x.active)})";
break;
}
panelMain.caption = caption.Replace("Count:0", replaceCaption);
}
public ViewMode ViewMode
{
get
{
return btnViewMode.GetValue<ViewMode>();
}
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
switch (ViewMode)
{
case ViewMode.CombatNPC:
InfoCombat.Update();
updateNeeded = InfoCombat.isUpdate;
break;
case ViewMode.SpawnNPC:
var bFilterTown = btnSpawnNPCFilter.GetValue<SpawnNPCFilter>() == SpawnNPCFilter.HideTownNPC;
var npcList = Main.npc.Where(x => x.active && x.netID != 0 && (bFilterTown ? !x.townNPC : true) && x.whoAmI < 200).GroupBy(x => x.netID);
if (dicSpawnNPC.Count != npcList.Count() || dicSpawnNPC.Sum(x => x.Value) != npcList.Sum(x => x.Count()))
{
dicSpawnNPC.Clear();
foreach (var npc in npcList)
dicSpawnNPC.Add(npc.ToArray()[0].netID, npc.Count());
updateNeeded = true;
}
break;
case ViewMode.DropItem:
var itemList = Main.item.Where(x => x.active && x.netID != 0).GroupBy(x => x.netID);
if (dicDropItem.Count != itemList.Count() || dicDropItem.Sum(x => x.Value) != itemList.Sum(x => x.Sum(y => y.stack)))
{
dicDropItem.Clear();
foreach (var item in itemList)
dicDropItem.Add(item.ToArray()[0].netID, item.Sum(x => x.stack));
updateNeeded = true;
}
break;
}
if (ViewMode == ViewMode.CombatNPC)
{
}
UpdateGrid();
}
public override TagCompound Save()
{
TagCompound result = base.Save();
if (panelMain != null)
{
result.Add("position", panelMain.SavePositionJsonString());
}
return result;
}
public override void Load(TagCompound tag)
{
base.Load(tag);
if (tag.ContainsKey("position"))
{
panelMain.LoadPositionJsonString(tag.GetString("position"));
}
}
}
enum ViewMode
{
CombatNPC,
SpawnNPC,
DropItem,
}
enum SpawnNPCFilter
{
All,
HideTownNPC,
}
}