-
Notifications
You must be signed in to change notification settings - Fork 4
/
InventoryViewForm.cs
277 lines (250 loc) · 9.69 KB
/
InventoryViewForm.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace InventoryView
{
public partial class InventoryViewForm : Form
{
public InventoryViewForm()
{
InitializeComponent();
}
List<TreeNode> searchMatches = new List<TreeNode>();
TreeNode currentMatch = null;
private void InventoryViewForm_Load(object sender, EventArgs e)
{
BindData();
}
private void BindData()
{
chkCharacters.Items.Clear();
tv.Nodes.Clear();
// Get a distinct character list and load them into the checked list box... which is currently not shown/used.
var characters = Class1.characterData.Select(tbl => tbl.name).Distinct().ToList();
characters.Sort();
// Recursively load all the items into the tree
foreach (var character in characters)
{
chkCharacters.Items.Add(character, true);
TreeNode charNode = tv.Nodes.Add(character);
foreach (var source in Class1.characterData.Where(tbl => tbl.name == character))
{
TreeNode sourceNode = charNode.Nodes.Add(source.source);
sourceNode.ToolTipText = sourceNode.FullPath;
PopulateTree(sourceNode, source.items);
}
}
}
private void PopulateTree(TreeNode treeNode, List<ItemData> itemList)
{
foreach (var item in itemList)
{
TreeNode newNode = treeNode.Nodes.Add(item.tap);
newNode.ToolTipText = newNode.FullPath;
if (item.items.Count() > 0)
PopulateTree(newNode, item.items);
}
}
private void btnSearch_Click(object sender, EventArgs e)
{
// Recursively search the tree.
if (!string.IsNullOrEmpty(txtSearch.Text))
{
searchMatches.Clear();
currentMatch = null;
tv.CollapseAll();
SearchTree(tv.Nodes);
//foreach (TreeNode treeNode in tv.Nodes)
//{
// if (chkCharacters.CheckedItems.Contains(treeNode.Text))
// if (SearchTree(treeNode.Nodes))
// treeNode.Expand();
//}
}
btnFindNext.Visible = btnFindPrev.Visible = btnReset.Visible = (searchMatches.Count() != 0);
if (searchMatches.Count() != 0)
{
btnFindNext.PerformClick();
}
}
private bool SearchTree(TreeNodeCollection nodes)
{
bool retValue = false;
foreach (TreeNode treeNode in nodes)
{
treeNode.BackColor = Color.White;
if (SearchTree(treeNode.Nodes) == true) // Recursively search child items. Expand the node if a child item is expanded.
{
treeNode.Expand();
retValue = true;
}
if (treeNode.Text.Contains(txtSearch.Text)) // If the current item is a match, change the color & add it to the matches list.
{
treeNode.Expand();
treeNode.BackColor = Color.Yellow;
retValue = true;
searchMatches.Add(treeNode);
}
}
return retValue;
}
private void btnExpand_Click(object sender, EventArgs e)
{
tv.ExpandAll();
}
private void btnCollapse_Click(object sender, EventArgs e)
{
tv.CollapseAll();
}
private void btnWiki_Click(object sender, EventArgs e)
{
// Sends the tap of the item to drservice.info which has been building a table linking taps to wiki pages from the Plaza scans.
if (tv.SelectedNode == null)
MessageBox.Show("Select an item to lookup.");
else
System.Diagnostics.Process.Start(string.Format("https://drservice.info/wiki.ashx?tap={0}", tv.SelectedNode.Text.Replace(" (closed)", "")));
}
private void btnReset_Click(object sender, EventArgs e)
{
txtSearch.Text = " ";
btnSearch.PerformClick();
txtSearch.Text = string.Empty;
}
private void btnFindPrev_Click(object sender, EventArgs e)
{
if (currentMatch == null)
currentMatch = searchMatches.Last();
else
{
currentMatch.BackColor = Color.Yellow;
int index = searchMatches.IndexOf(currentMatch) - 1;
if (index == -1) index = searchMatches.Count()-1;
currentMatch = searchMatches[index];
}
currentMatch.EnsureVisible();
currentMatch.BackColor = Color.LightBlue;
}
private void btnFindNext_Click(object sender, EventArgs e)
{
if (currentMatch == null)
currentMatch = searchMatches.First();
else
{
currentMatch.BackColor = Color.Yellow;
int index = searchMatches.IndexOf(currentMatch) + 1;
if (index == searchMatches.Count()) index = 0;
currentMatch = searchMatches[index];
}
currentMatch.EnsureVisible();
currentMatch.BackColor = Color.LightBlue;
}
private void btnScan_Click(object sender, EventArgs e)
{
Class1._host.SendText("/InventoryView scan");
this.Close();
}
private void btnReload_Click(object sender, EventArgs e)
{
Class1.LoadSettings();
Class1._host.EchoText("Inventory reloaded.");
BindData();
}
private void copyTapToolStripMenuItem_Click(object sender, EventArgs e)
{
Clipboard.SetText(tv.SelectedNode.Text);
}
private void exportBranchToFileToolStripMenuItem_Click(object sender, EventArgs e)
{
List<string> branchText = new List<string>();
branchText.Add(tv.SelectedNode.Text);
copyBranchText(tv.SelectedNode.Nodes, branchText, 1);
Clipboard.SetText(string.Join("\r\n", branchText.ToArray()));
}
private void copyBranchText(TreeNodeCollection nodes, List<string> branchText, int level)
{
foreach (TreeNode node in nodes)
{
branchText.Add(new string('\t', level) + node.Text);
copyBranchText(node.Nodes, branchText, level+1);
}
}
private void tv_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
Point p = new Point(e.X, e.Y);
TreeNode node = tv.GetNodeAt(p);
if (node != null)
{
tv.SelectedNode = node;
contextMenuStrip1.Show(tv, p);
}
}
}
private void btnExport_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "CSV file|*.csv";
saveFileDialog1.Title = "Save the CSV file";
saveFileDialog1.ShowDialog();
if (saveFileDialog1.FileName != "")
{
using (StreamWriter sw = File.CreateText(saveFileDialog1.FileName))
{
List<ExportData> list = new List<ExportData>();
exportBranch(tv.Nodes, list, 1);
sw.WriteLine("Character,Tap,Path");
foreach (ExportData item in list)
{
if (item.Path.Count < 1) { } // Skip
else if (item.Path.Count == 3 && new string[] { "Vault", "Home" }.Contains(item.Path[1])) { } // Skip
else
{
sw.WriteLine(string.Format("{0},{1},{2}", CleanCSV(item.Character), CleanCSV(item.Tap), CleanCSV(string.Join("\\", item.Path))));
}
}
}
MessageBox.Show("Export Complete.");
}
}
private string CleanCSV(string data)
{
if (!data.Contains(","))
return data;
else if (!data.Contains("\""))
return string.Format("\"{0}\"", data);
else
return string.Format("\"{0}\"", data.Replace("\"","\"\""));
}
private void exportBranch(TreeNodeCollection nodes, List<ExportData> list, int level)
{
foreach (TreeNode node in nodes)
{
ExportData exportData = new ExportData() { Tap = node.Text };
TreeNode tmpNode = node;
while (tmpNode.Parent != null)
{
tmpNode = tmpNode.Parent;
if (tmpNode.Parent != null)
exportData.Path.Insert(0, tmpNode.Text);
}
exportData.Character = tmpNode.Text;
list.Add(exportData);
exportBranch(node.Nodes, list, level + 1);
}
}
public class ExportData
{
public string Character { get; set; }
public string Tap { get; set; }
public List<string> Path { get; set; } = new List<string>();
}
}
}