forked from EtherianDR/InventoryView
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInventoryViewForm.cs
More file actions
1670 lines (1505 loc) · 66.6 KB
/
InventoryViewForm.cs
File metadata and controls
1670 lines (1505 loc) · 66.6 KB
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Xml;
namespace InventoryView
{
public class InventoryViewForm : Form
{
private readonly List<TreeNode> searchMatches = new();
private IContainer components;
private TreeView tv;
private ContextMenuStrip listBox_Menu;
private ToolStripMenuItem copyToolStripMenuItem;
private ToolStripMenuItem wikiToolStripMenuItem;
private ToolStripMenuItem copyAllToolStripMenuItem;
private bool clickSearch = false;
private ToolStripMenuItem copySelectedToolStripMenuItem;
// Create a new list to store the search matches for each TreeView control
private readonly List<InventoryViewForm.TreeViewSearchMatches> treeViewSearchMatchesList = new();
// Create a list to store the hidden tab pages and their original positions
private readonly List<(TabPage tabPage, int index)> hiddenTabPages = new();
private TabControl tabControl1;
private ListBox lblMatches;
private TableLayoutPanel tableLayoutPanel1;
private Panel panel1;
private Button btnFindPrev;
private TextBox txtSearch;
private Label lblFound;
private Label lblSearch;
private Button btnSearch;
private Button btnExport;
private Button btnExpand;
private Button btnReload;
private Button btnCollapse;
private Button btnScan;
private Button btnWiki;
private Button btnFindNext;
private Button btnReset;
private SplitContainer splitContainer1;
private ComboBox cboCharacters;
private Button btnRemoveCharacter;
private Label infolabel;
internal CheckBox chkMultilineTabs;
internal CheckBox chkDarkMode;
internal CheckBox chkFamily;
private static string basePath = Application.StartupPath;
internal CheckBox chkAlwaysTop;
private Button button1;
private readonly Dictionary<string, List<MatchedItemInfo>> matchedItemsDictionary = new();
public InventoryViewForm()
{
InitializeComponent();
AutoScaleMode = AutoScaleMode.Dpi;
}
private void InventoryViewForm_Load(object sender, EventArgs e)
{
BindData();
basePath = Class1.Host.get_Variable("PluginPath");
// Load the character data
LoadSave.LoadSettings();
// Get a list of distinct character names from the characterData list
List<string> characterNames = Class1.CharacterData.Select(c => c.name).Distinct().ToList();
// Sort the character names
characterNames.Sort();
// Add the character names to the cboCharacters control
cboCharacters.Items.Clear();
cboCharacters.Items.AddRange(characterNames.ToArray());
lblMatches.MouseDoubleClick += LblMatches_MouseDoubleClick;
InitializeTooltipTimer();
chkAlwaysTop.CheckedChanged += ChkAlwaysTop_CheckedChanged;
if (tabControl1.SelectedTab?.Controls.Count > 0 && tabControl1.SelectedTab.Controls[0] is TreeView tv)
{
// Expand root nodes for the selected tab
foreach (TreeNode rootNode in tv.Nodes)
{
rootNode.Expand();
}
}
}
private void BindData()
{
// Clear existing data
tabControl1.TabPages.Clear();
// Get a distinct character list
var characters = GetDistinctCharacters();
// Create a new tab page for each character
foreach (var character in characters)
{
// Create a new tab page
var tabPage = new TabPage(character);
tabControl1.TabPages.Add(tabPage);
// Create a new TreeView control
var tv = new TreeView
{
Dock = DockStyle.Fill
};
if (chkDarkMode.Checked)
{
tv.ForeColor = Color.White;
tv.BackColor = Color.Black;
}
else
{
tv.ForeColor = Color.Black;
tv.BackColor = Color.White;
}
tabPage.Controls.Add(tv);
// Clear existing nodes
tv.Nodes.Clear();
// Create a new ContextMenuStrip for the TreeView control
var contextMenuStrip = CreateTreeViewContextMenuStrip(tv);
tv.ContextMenuStrip = contextMenuStrip;
// Add the character's data to the TreeView and get the total item count
int totalCount = AddCharacterDataToTreeView(tv, character);
// Update the tab page text with the total item count
tabPage.Text = $"{character} (T: {totalCount})";
foreach (TreeNode rootNode in tv.Nodes)
{
rootNode.Expand();
}
}
}
private static List<string> GetDistinctCharacters()
{
var characters = Class1.CharacterData.Select(tbl => tbl.name).Distinct().ToList();
characters.Sort();
return characters;
}
private static int AddCharacterDataToTreeView(TreeView tv, string character)
{
int totalCount = 0;
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;
totalCount += PopulateTree(sourceNode, source.items);
}
return totalCount;
}
private static int PopulateTree(TreeNode treeNode, List<ItemData> itemList)
{
int totalCount = 0;
foreach (ItemData itemData in itemList)
{
TreeNode treeNode1 = treeNode.Nodes.Add(itemData.tap);
treeNode1.ToolTipText = treeNode1.FullPath;
if (!itemData.tap.EndsWith("."))
{
totalCount++;
}
if (itemData.items.Count > 0)
totalCount += PopulateTree(treeNode1, itemData.items);
}
return totalCount;
}
private static ContextMenuStrip CreateTreeViewContextMenuStrip(TreeView tv)
{
var contextMenuStrip = new ContextMenuStrip();
var wikiLookupToolStripMenuItem = new ToolStripMenuItem("Wiki Lookup");
wikiLookupToolStripMenuItem.Click += (sender, e) =>
{
if (tv.SelectedNode == null)
{
int num = (int)MessageBox.Show("Select an item to lookup.");
}
else
OpenWikiPage(tv.SelectedNode.Text);
};
contextMenuStrip.Items.Add(wikiLookupToolStripMenuItem);
// Add the "Copy Text" option
var copyTextToolStripMenuItem = new ToolStripMenuItem("Copy Text");
copyTextToolStripMenuItem.Click += (sender, e) =>
{
if (tv.SelectedNode != null)
Clipboard.SetText(Regex.Replace(tv.SelectedNode.Text, @"\(\d+\)\s|^(an?|some|several)\s", ""));
};
contextMenuStrip.Items.Add(copyTextToolStripMenuItem);
// Add the "Copy Branch" option
var copyBranchToolStripMenuItem = new ToolStripMenuItem("Copy Branch");
copyBranchToolStripMenuItem.Click += (sender, e) =>
{
if (tv.SelectedNode != null)
{
List<string> branchText = new()
{
Regex.Replace(tv.SelectedNode.Text, @"\(\d+\)\s|^(an?|some|several)\s", "")
};
CopyBranchText(tv.SelectedNode.Nodes, branchText, 1);
Clipboard.SetText(string.Join("\r\n", branchText.ToArray()));
}
};
contextMenuStrip.Items.Add(copyBranchToolStripMenuItem);
return contextMenuStrip;
}
private void BtnSearch_Click(object sender, EventArgs e)
{
if (customTooltip != null && !customTooltip.IsDisposed)
{
customTooltip.Close();
tooltipTimer.Stop(); // Stop the timer
}
// Reset the found count value
lblFound.Text = "Found: 0";
ClearMatchedItemPaths();
// Clear existing search matches
searchMatches.Clear();
treeViewSearchMatchesList.Clear();
lblMatches.Items.Clear();
if (!string.IsNullOrEmpty(txtSearch.Text))
{
// Clear hiddenTabPages
hiddenTabPages.Clear();
// Call BindData to make sure all tabs are visible
BindData();
// Save the currently selected tab page
var selectedTab = tabControl1.SelectedTab;
// Iterate over each visible and hidden tab page
var allTabPages = tabControl1.TabPages.Cast<TabPage>().Concat(hiddenTabPages.Select(x => x.tabPage)).ToList();
// Clear the treeViewSearchMatchesList
treeViewSearchMatchesList.Clear();
foreach (var tabPage in allTabPages)
{
// Reset the search count for the tab page
tabPage.Text = tabPage.Text.Split(' ')[0];
// Select the tab page if it's visible
if (tabControl1.TabPages.Contains(tabPage))
tabControl1.SelectedTab = tabPage;
// Get the TreeView control on the tab page
var tv = tabPage.Controls[0] as TreeView;
// Create a new TreeViewSearchMatches object for the TreeView control
var treeViewSearchMatches = new TreeViewSearchMatches() { TreeView = tv };
treeViewSearchMatchesList.Add(treeViewSearchMatches);
// Reset the search count and total count
int searchCount = 0;
int totalCount = 0;
// Search the TreeView
tv.CollapseAll();
SearchTree(tv, tv.Nodes, treeViewSearchMatches.SearchMatches, ref searchCount, ref totalCount);
// Update the tab page text with the search count if greater than zero
if (searchCount > 0)
tabPage.Text = new StringBuilder(tabPage.Text.Split(' ')[0]).Append($" (M: {searchCount})").ToString();
else
{
tabPage.Text = new StringBuilder(tabPage.Text.Split(' ')[0]).Append($" (T: {totalCount})").ToString();
// Hide the tab page if no matching items were found and it's not already hidden
if (tabControl1.TabPages.Contains(tabPage) && !hiddenTabPages.Any(x => x.tabPage == tabPage))
{
hiddenTabPages.Add((tabPage, tabControl1.TabPages.IndexOf(tabPage)));
tabControl1.TabPages.Remove(tabPage);
}
}
}
// Restore the originally selected tab page
if (tabControl1.TabPages.Contains(selectedTab))
tabControl1.SelectedTab = selectedTab;
else if (tabControl1.TabPages.Count > 0)
tabControl1.SelectedIndex = 0;
btnFindNext.Visible = btnFindPrev.Visible = btnReset.Visible = treeViewSearchMatchesList.Any(x => x.SearchMatches.Count > 0);
lblFound.Text = "Found: " + treeViewSearchMatchesList.Sum(x => x.SearchMatches.Count).ToString();
if (!treeViewSearchMatchesList.Any(x => x.SearchMatches.Count > 0))
{
BindData();
// Set focus back to the txtSearch control
txtSearch.Focus();
return;
}
}
// Set clickSearch to true to indicate that a search has been performed
clickSearch = true;
// Set focus back to the txtSearch control
txtSearch.Focus();
}
private void ClearMatchedItemPaths()
{
foreach (var matchedItem in matchedItemsDictionary.Values)
{
matchedItem.Clear();
}
}
private bool SearchTree(TreeView treeView, TreeNodeCollection nodes, List<TreeNode> searchMatches, ref int searchCount, ref int totalCount)
{
bool isMatchFound = false;
foreach (TreeNode node in nodes)
{
totalCount++;
// Reset the node's background color and foreground color based on the dark mode
node.BackColor = chkDarkMode.Checked ? Color.Black : Color.White;
node.ForeColor = chkDarkMode.Checked ? Color.White : Color.Black;
// Search the node's child nodes and update the match status
if (SearchTree(treeView, node.Nodes, searchMatches, ref searchCount, ref totalCount))
{
// Expand the node if a match was found in its child nodes
node.Expand();
isMatchFound = true;
}
else
{
// Collapse the node if no match was found in its child nodes
node.Collapse();
}
// Check if the node's text contains the search text
if (node.Text.Contains(txtSearch.Text, StringComparison.OrdinalIgnoreCase))
{
// Highlight the node and add it to the list of search matches
node.BackColor = chkDarkMode.Checked ? Color.LightBlue : Color.Yellow;
node.ForeColor = Color.Black;
searchMatches.Add(node);
searchCount++;
isMatchFound = true;
// Add the node's text to the lblMatches ListBox control
if (tabControl1.SelectedTab.Controls[0] == treeView)
{
// Get the character name from the tab page's Text property
string characterName = tabControl1.SelectedTab.Text;
if (!lblMatches.Items.Contains(" --- " + characterName + " --- "))
{
// Add a space after the last item if this is not the first character
if (lblMatches.Items.Count > 0 && lblMatches.Items[^1].ToString() != " ")
{
lblMatches.Items.Add(" ");
}
// Add the character name to the ListBox
lblMatches.Items.Add(" --- " + characterName + " --- ");
}
// Add the node's text to the ListBox
string nodeText = Regex.Replace(node.Text.TrimEnd('.'), @"\(\d+\)\s|^(an?|some|several)\s", "");
lblMatches.Items.Add(nodeText);
if (!matchedItemsDictionary.ContainsKey(nodeText))
{
matchedItemsDictionary[nodeText] = new List<MatchedItemInfo>();
}
matchedItemsDictionary[nodeText].Add(new MatchedItemInfo
{
FullPath = Regex.Replace(node.FullPath.TrimEnd('.'), @"\(\d+\)\s|^(an?|some|several)\s", "")
});
}
}
else
{
// Change the color of non-matching nodes
node.ForeColor = Color.LightGray;
// Check if any child node is a matching node and update its color
foreach (TreeNode childNode in node.Nodes)
{
if (childNode.BackColor == Color.LightBlue || childNode.BackColor == Color.Yellow)
{
childNode.ForeColor = Color.Black;
break;
}
}
}
}
return isMatchFound;
}
private void LblMatches_MouseDown(object sender, MouseEventArgs e)
{
// Close the custom tooltip if it's open
if (customTooltip != null && !customTooltip.IsDisposed)
{
customTooltip.Close();
tooltipTimer.Stop();
}
}
private void InventoryViewForm_FormClosed(object sender, FormClosedEventArgs e)
{
if (customTooltip != null && !customTooltip.IsDisposed)
{
customTooltip.Close();
tooltipTimer.Stop();
}
}
private Form customTooltip = null;
private readonly Timer tooltipTimer = new();
private void InitializeTooltipTimer()
{
// Set the interval for the timer
tooltipTimer.Interval = 5000;
// Tick event of the timer
tooltipTimer.Tick += (sender, e) =>
{
if (customTooltip != null && !customTooltip.IsDisposed)
{
customTooltip.Close();
}
tooltipTimer.Stop(); // Stop the timer
};
}
private void LblMatches_MouseDoubleClick(object sender, MouseEventArgs e)
{
ListBox listBox = (ListBox)sender;
int selectedIndex = listBox.SelectedIndex;
if (selectedIndex >= 0 && selectedIndex < listBox.Items.Count)
{
string selectedItemText = listBox.Items[selectedIndex].ToString();
if (matchedItemsDictionary.ContainsKey(selectedItemText))
{
List<MatchedItemInfo> matchedItems = matchedItemsDictionary[selectedItemText];
// Close the old tooltip if it's open
if (customTooltip != null && !customTooltip.IsDisposed)
{
customTooltip.Close();
tooltipTimer.Stop();
}
// Create a new custom tooltip form
customTooltip = new Form
{
// Set the form's properties
FormBorderStyle = FormBorderStyle.FixedSingle,
MaximizeBox = false,
MinimizeBox = false,
ControlBox = false,
AutoScroll = true,
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
//MaximumSize = new Size(int.MaxValue, 420),
TopMost = true // Keep the form on top of all other windows
};
// Create a label to display the tooltip text
#pragma warning disable CA1416 // Validate platform compatibility
Label label = new()
{
AutoSize = true,
Text = string.Join(Environment.NewLine + Environment.NewLine, matchedItems.Select(item => FormatPath(item.FullPath))),
ForeColor = Color.Black,
BackColor = Color.Beige,
Font = new Font("System", 10, FontStyle.Bold), // Set the desired font and size
};
#pragma warning restore CA1416 // Validate platform compatibility
// Add the label to the form
customTooltip.Controls.Add(label);
// Show the custom tooltip
tooltipTimer.Start(); // Start or restart the timer
tooltipTimer.Tick += (s, args) =>
{
customTooltip.Close();
tooltipTimer.Stop(); // Stop the timer
};
// Calculate the tooltip position near the click location
Point screenClickLocation = listBox.PointToScreen(e.Location);
customTooltip.StartPosition = FormStartPosition.CenterParent;
customTooltip.Location = new Point(screenClickLocation.X + 10, screenClickLocation.Y + 10);
customTooltip.Show();
}
}
}
// Add this method to format the path with hyphen indentation
private static string FormatPath(string fullPath)
{
string[] parts = fullPath.Split('\\');
if (parts.Length == 0)
{
return fullPath;
}
string itemName = parts[^1];
string indentation = new('-', parts.Length - 1);
if (parts.Length > 1)
{
string parentPath = FormatPath(string.Join("\\", parts.Take(parts.Length - 1)));
return $"{parentPath}{Environment.NewLine}{indentation} {itemName}";
}
return $"{indentation} {itemName}";
}
private void BtnExpand_Click(object sender, EventArgs e)
{
// Expand all nodes in all TreeView controls
SetTreeViewNodeState(true);
}
private void BtnCollapse_Click(object sender, EventArgs e)
{
// Collapse all nodes in all TreeView controls
SetTreeViewNodeState(false);
}
private void SetTreeViewNodeState(bool isExpanded)
{
// Iterate over each tab page
foreach (TabPage tabPage in tabControl1.TabPages)
{
// Get the TreeView control on the tab page
var tv = tabPage.Controls[0] as TreeView;
// Expand or collapse all nodes in the TreeView
if (isExpanded)
tv.ExpandAll();
else
tv.CollapseAll();
}
}
private void BtnWiki_Click(object sender, EventArgs e)
{
// Get the TreeView control on the currently selected tab page
var tv = tabControl1.SelectedTab.Controls[0] as TreeView;
if (tv.SelectedNode == null)
{
MessageBox.Show("Select an item to lookup.");
}
else
{
try
{
OpenWikiPage(tv.SelectedNode.Text);
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred while opening the wiki page: {ex.Message}");
}
}
}
private void Listbox_Wiki_Click(object sender, EventArgs e)
{
if (lblMatches.SelectedItem == null)
{
MessageBox.Show("Select an item to lookup.");
}
else
{
string selectedItem = (string)lblMatches.SelectedItem;
if (selectedItem == " " || selectedItem.StartsWith(" --- "))
{
MessageBox.Show("Select a valid item to lookup.");
}
else
{
OpenWikiPage(selectedItem);
}
}
}
private static void OpenWikiPage(string text)
{
if (Class1.Host.InterfaceVersion == 4)
Class1.Host.SendText(string.Format("#browser https://elanthipedia.play.net/index.php?search={0}", Uri.EscapeDataString(Regex.Replace(text, @"\(\d+\)\s|\s\(closed\)|^(an?|some|several)\s", ""))));
else
Process.Start(new ProcessStartInfo(string.Format("https://elanthipedia.play.net/index.php?search={0}", Regex.Replace(text, @"\(\d+\)\s|\s\(closed\)|(^an?|some|several)\s", ""))) { UseShellExecute = true });
}
private void BtnFindNext_Click(object sender, EventArgs e)
{
// Find the next search match
FindSearchMatch(true);
}
private void BtnFindPrev_Click(object sender, EventArgs e)
{
// Find the previous search match
FindSearchMatch(false);
}
private void FindSearchMatch(bool isNext)
{
// Get the TreeView control on the currently selected tab page
var tv = tabControl1.SelectedTab.Controls[0] as TreeView;
// Get the TreeViewSearchMatches object for the TreeView control
var treeViewSearchMatches = treeViewSearchMatchesList.FirstOrDefault(x => x.TreeView == tv);
if (treeViewSearchMatches == null)
return;
if (treeViewSearchMatches.CurrentMatch == null)
{
// Set the current match to the first or last match in the list
treeViewSearchMatches.CurrentMatch = isNext ? treeViewSearchMatches.SearchMatches.First<TreeNode>() : treeViewSearchMatches.SearchMatches.Last<TreeNode>();
}
else
{
// Reset the current match's background color
if (chkDarkMode.Checked)
treeViewSearchMatches.CurrentMatch.BackColor = Color.LightBlue;
else
treeViewSearchMatches.CurrentMatch.BackColor = Color.Yellow;
// Get the index of the current match
int index = treeViewSearchMatches.SearchMatches.IndexOf(treeViewSearchMatches.CurrentMatch) + (isNext ? 1 : -1);
if (index == treeViewSearchMatches.SearchMatches.Count || index == -1)
{
// Get the index of the currently selected tab page
int tabIndex = tabControl1.SelectedIndex;
// Find the next or previous tab page that has search matches
while (true)
{
tabIndex += isNext ? 1 : -1;
if (tabIndex < 0 || tabIndex >= tabControl1.TabPages.Count)
{
// Wrap around to the first or last tab page
tabIndex = isNext ? 0 : tabControl1.TabPages.Count - 1;
}
// Select the next or previous tab page
tabControl1.SelectedIndex = tabIndex;
// Get the TreeView control on the next or previous tab page
tv = tabControl1.SelectedTab.Controls[0] as TreeView;
// Get the TreeViewSearchMatches object for the TreeView control
treeViewSearchMatches = treeViewSearchMatchesList.FirstOrDefault(x => x.TreeView == tv);
if (treeViewSearchMatches != null && treeViewSearchMatches.SearchMatches.Count > 0)
{
// Set the current match to the first or last match in the list
index = isNext ? 0 : treeViewSearchMatches.SearchMatches.Count - 1;
break;
}
}
}
if (treeViewSearchMatches != null)
treeViewSearchMatches.CurrentMatch = treeViewSearchMatches.SearchMatches[index];
}
if (treeViewSearchMatches != null)
{
// Ensure that the current match is visible and highlight it
treeViewSearchMatches.CurrentMatch.EnsureVisible();
if (chkDarkMode.Checked)
treeViewSearchMatches.CurrentMatch.BackColor = Color.LightBlue;
else
treeViewSearchMatches.CurrentMatch.BackColor = Color.Yellow;
}
}
private void BtnScan_Click(object sender, EventArgs e)
{
Class1.Host.SendText("/InventoryView scan");
Close();
}
private void BtnRemoveCharacter_Click(object sender, EventArgs e)
{
string characterName = cboCharacters.Text; // The name of the selected character
if (!string.IsNullOrEmpty(characterName))
{
// Display a confirmation message
DialogResult result = MessageBox.Show($"Are you sure you want to remove the character '{characterName}'?", "Confirm Remove", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
try
{
// Load the XML document
XmlDocument doc = new();
string xmlPath = Path.Combine(basePath, "InventoryView.xml");
doc.Load(xmlPath);
// Find all CharacterData elements with the specified name element value
XmlNodeList characterNodes = doc.SelectNodes($"/Root/ArrayOfCharacterData/ArrayOfCharacterData/CharacterData[name='{characterName}']");
if (characterNodes.Count > 0)
{
// Remove all matching CharacterData elements from their parent
foreach (XmlNode characterNode in characterNodes)
{
characterNode.ParentNode.RemoveChild(characterNode);
}
// Save the modified XML document
doc.Save(xmlPath);
// Update the cboCharacters combobox
cboCharacters.Items.Remove(characterName);
cboCharacters.SelectedIndex = -1;
ReloadData();
}
else
{
Class1.Host.EchoText($"Could not find any CharacterData elements with a name element value of '{characterName}' in the XML file.");
}
}
catch (Exception ex)
{
// Handle the exception here
Class1.Host.EchoText($"An exception occurred: {ex.Message}");
}
}
}
else
{
Class1.Host.EchoText("Please select a character name.");
}
}
private void BtnReset_Click(object sender, EventArgs e)
{
if (customTooltip != null && !customTooltip.IsDisposed)
{
customTooltip.Close();
tooltipTimer.Stop();
}
ClearMatchedItemPaths();
// Reload the data
ReloadData();
// Reset the search controls
ResetSearchControls();
}
private void BtnReload_Click(object sender, EventArgs e)
{
// Reload the data
ReloadData();
}
private void ReloadData()
{
LoadSave.LoadSettings();
//Class1._host.EchoText("Inventory reloaded.");
BindData();
UpdateCboCharacters();
}
private void UpdateCboCharacters()
{
// Clear the cboCharacters combobox
cboCharacters.Items.Clear();
try
{
// Load the XML document
XmlDocument doc = new();
string xmlPath = Path.Combine(basePath, "InventoryView.xml");
doc.Load(xmlPath);
// Find all CharacterData elements
XmlNodeList characterNodes = doc.SelectNodes("/Root/ArrayOfCharacterData/ArrayOfCharacterData/CharacterData");
// Create a list to store the character names
List<string> characterNames = new();
// Add the character names to the list
foreach (XmlNode characterNode in characterNodes)
{
string characterName = characterNode["name"].InnerText;
if (!characterNames.Contains(characterName))
{
characterNames.Add(characterName);
}
}
// Sort the character names in alphabetical order
characterNames.Sort();
// Add the sorted character names to the cboCharacters combobox
foreach (string characterName in characterNames)
{
cboCharacters.Items.Add(characterName);
}
// Select the first item in the cboCharacters combobox
if (cboCharacters.Items.Count > 0)
{
cboCharacters.SelectedIndex = -1;
}
if (cboCharacters.Items.Count == 0)
{
cboCharacters.Text = "";
}
}
catch (Exception ex)
{
// Handle the exception here
MessageBox.Show($"An exception occurred: {ex.Message}");
}
}
private void ResetSearchControls()
{
btnFindNext.Visible = btnFindPrev.Visible = btnReset.Visible = clickSearch = false;
lblMatches.Items.Clear();
lblFound.Text = "Found: 0";
searchMatches.Clear();
txtSearch.Text = "";
// Set clickSearch to true to indicate that a search has been performed
clickSearch = false;
// Set focus back to the txtSearch control
txtSearch.Focus();
}
private void ExportBranchToFileToolStripMenuItem_Click(object sender, EventArgs e)
{
List<string> branchText = new()
{
Regex.Replace(tv.SelectedNode.Text, @"\(\d+\)\s|(an?|some|several)\s", "")
};
CopyBranchText(tv.SelectedNode.Nodes, branchText, 1);
Clipboard.SetText(string.Join("\r\n", branchText.ToArray()));
}
private static void CopyBranchText(TreeNodeCollection nodes, List<string> branchText, int level)
{
foreach (TreeNode node in nodes)
{
branchText.Add(new string('\t', level) + Regex.Replace(node.Text, @"\(\d+\)\s|^(an?|some|several)\s", ""));
CopyBranchText(node.Nodes, branchText, level + 1);
}
}
private void ListBox_Copy_Click(object sender, EventArgs e)
{
if (lblMatches.SelectedItem == null)
{
_ = (int)MessageBox.Show("Select an item to copy.");
}
else
{
StringBuilder txt = new();
foreach (object row in lblMatches.SelectedItems)
{
txt.Append(row.ToString());
txt.AppendLine();
}
txt.Remove(txt.Length - 1, 1);
Clipboard.SetData(System.Windows.Forms.DataFormats.Text, txt.ToString());
}
}
public void ListBox_Copy_All_Click(Object sender, EventArgs e)
{
if (clickSearch == false)
{
_ = (int)MessageBox.Show("Must search first to copy all.");
}
else
{
StringBuilder buffer = new();
for (int i = 0; i < lblMatches.Items.Count; i++)
{
buffer.Append(lblMatches.Items[i].ToString());
buffer.Append('\n');
}
Clipboard.SetText(buffer.ToString());
}
}
public void ListBox_Copy_All_Selected_Click(Object sender, EventArgs e)
{
if (lblMatches.SelectedItem == null)
{
_ = (int)MessageBox.Show("Select items to copy.");
}
else
{
StringBuilder buffer = new();
for (int i = 0; i < lblMatches.SelectedItems.Count; i++)
{
buffer.Append(lblMatches.SelectedItems[i].ToString());
buffer.Append('\n');
}
Clipboard.SetText(buffer.ToString());
}
}
private void Tv_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Right)
return;
Point point = new(e.X, e.Y);
TreeNode nodeAt = tv.GetNodeAt(point);
if (nodeAt == null)
return;
tv.SelectedNode = nodeAt;
}
private void BtnExport_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new()
{
Filter = "CSV file|*.csv",
Title = "Save the CSV file"
};
_ = (int)saveFileDialog.ShowDialog();
if (!(saveFileDialog.FileName != ""))
return;
using (StreamWriter text = File.CreateText(saveFileDialog.FileName))
{
List<InventoryViewForm.ExportData> list = new();
// Get the TreeView control on the currently selected tab page
var tv = tabControl1.SelectedTab.Controls[0] as TreeView;
// Add the TreeView's data to the list
ExportBranch(tv.Nodes, list, 1);
text.WriteLine("Character,Tap,Path");
foreach (InventoryViewForm.ExportData exportData in list)
{
if (exportData.Path.Count >= 1)
{
if (exportData.Path.Count == 3)
{
if (((IEnumerable<string>)new string[2]
{
"Vault",
"Home"
}).Contains<string>(exportData.Path[1]))
continue;
}
text.WriteLine(string.Format("{0},{1},{2}", (object)CleanCSV(exportData.Character), (object)CleanCSV(exportData.Tap), (object)CleanCSV(string.Join("\\", (IEnumerable<string>)exportData.Path))));
}
}