-
-
Notifications
You must be signed in to change notification settings - Fork 460
/
FoldersManagerForm.cs
990 lines (935 loc) · 42.7 KB
/
FoldersManagerForm.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
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
using com.clusterrr.hakchi_gui.Properties;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
namespace com.clusterrr.hakchi_gui
{
public partial class FoldersManagerForm : Form
{
public static string FoldersXmlPath
{
get
{
switch (ConfigIni.ConsoleType)
{
default:
case MainForm.ConsoleType.NES:
return Path.Combine(Path.Combine(Program.BaseDirectoryExternal, ConfigIni.ConfigDir), "folders.xml");
case MainForm.ConsoleType.Famicom:
return Path.Combine(Path.Combine(Program.BaseDirectoryExternal, ConfigIni.ConfigDir), "folders_famicom.xml");
case MainForm.ConsoleType.SNES:
return Path.Combine(Path.Combine(Program.BaseDirectoryExternal, ConfigIni.ConfigDir), "folders_snes.xml");
case MainForm.ConsoleType.SuperFamicom:
return Path.Combine(Path.Combine(Program.BaseDirectoryExternal, ConfigIni.ConfigDir), "folders_super_famicom.xml");
}
}
}
List<TreeNode> cuttedNodes = new List<TreeNode>();
List<INesMenuElement> deletedGames = new List<INesMenuElement>();
NesMenuCollection gamesCollection = new NesMenuCollection();
MainForm mainForm;
private class NodeSorter : IComparer
{
public int Compare(object o1, object o2)
{
if (o1 is ListViewItem)
o1 = (o1 as ListViewItem).Tag;
if (o2 is ListViewItem)
o2 = (o2 as ListViewItem).Tag;
if ((o1 as TreeNode).Tag is NesMenuCollection) return -1; // Root is always first
if ((o2 as TreeNode).Tag is NesMenuCollection) return 1;
INesMenuElement el1 = (o1 as TreeNode).Tag as INesMenuElement;
INesMenuElement el2 = (o2 as TreeNode).Tag as INesMenuElement;
var pos1 = 2;
var pos2 = 2;
if (el1 is NesMenuFolder) pos1 = (int)(el1 as NesMenuFolder).Position;
if (el2 is NesMenuFolder) pos2 = (int)(el2 as NesMenuFolder).Position;
if (pos1 != pos2) return pos1.CompareTo(pos2);
return el1.Name.CompareTo(el2.Name);
}
}
public FoldersManagerForm(NesMenuCollection nesMenuCollection, MainForm mainForm = null)
{
try
{
InitializeComponent();
gamesCollection = nesMenuCollection;
this.mainForm = mainForm;
if (File.Exists(FoldersXmlPath))
{
try
{
XmlToTree(File.ReadAllText(FoldersXmlPath));
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message + ex.StackTrace);
File.Delete(FoldersXmlPath);
throw ex;
}
}
else DrawTree();
splitContainer.Panel2MinSize = 485;
comboBoxPosition.Left = labelPosition.Left + labelPosition.Width;
treeView.TreeViewNodeSorter = new NodeSorter();
listViewContent.ListViewItemSorter = new NodeSorter();
}
catch (Exception ex)
{
var message = ex.Message;
#if DEBUG
message += ex.StackTrace;
#endif
Debug.WriteLine(ex.Message + ex.StackTrace);
MessageBox.Show(this, message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
void DrawTree()
{
cuttedNodes.Clear();
treeView.Nodes.Clear();
var folderImageIndex = getImageIndex(null);
var rootNode = new TreeNode(Resources.MainMenu, folderImageIndex, folderImageIndex);
treeView.Nodes.Add(rootNode);
rootNode.Tag = gamesCollection;
AddNodes(rootNode.Nodes, gamesCollection);
rootNode.Expand();
treeView.SelectedNode = rootNode;
}
void DrawSplitTree(NesMenuCollection.SplitStyle splitStyle = NesMenuCollection.SplitStyle.NoSplit)
{
var node = treeView.SelectedNode;
NesMenuCollection collection;
if (node.Tag is NesMenuFolder)
collection = (node.Tag as NesMenuFolder).ChildMenuCollection;
else if (node.Tag is NesMenuCollection)
collection = node.Tag as NesMenuCollection;
else return;
// Collide and resplit collection
collection.Unsplit();
collection.Split(splitStyle, ConfigIni.MaxGamesPerFolder);
// Refill nodes with new collection
node.Nodes.Clear();
AddNodes(node.Nodes, collection);
node.Expand();
treeView.SelectedNode = node;
ShowSelected();
}
static int getImageIndex(INesMenuElement nesElement)
{
if (nesElement == null || nesElement is NesMenuFolder || nesElement is NesMenuCollection)
return 12;
if (nesElement is Sega32XGame)
return 0;
if (nesElement is Atari2600Game)
return 2;
if (nesElement is ArcadeGame)
return 6;
if (nesElement is NesUGame)
return 8;
if (nesElement is FdsGame)
return 10;
if (nesElement is GbGame)
return 14;
if (nesElement is GbaGame)
return 16;
if (nesElement is GbcGame)
return 18;
if (nesElement is GenesisGame)
return 20;
if (nesElement is GameGearGame)
return 22;
if (nesElement is N64Game)
return 24;
if (nesElement is NesGame)
return 28;
if (nesElement is NesDefaultGame)
return 30;
if (nesElement is PceGame)
return 32;
if (nesElement is SmsGame)
return 34;
if (nesElement is SnesGame)
return 36;
return 4;
}
void AddNodes(TreeNodeCollection treeNodeCollection, NesMenuCollection nesMenuCollection, List<NesMenuCollection> usedFolders = null)
{
if (usedFolders == null)
usedFolders = new List<NesMenuCollection>();
if (usedFolders.Contains(nesMenuCollection))
return;
usedFolders.Add(nesMenuCollection);
var sorted = nesMenuCollection.OrderBy(o => o.Name).OrderBy(o => (o is NesMenuFolder) ? (byte)(o as NesMenuFolder).Position : 2);
foreach (var nesElement in sorted)
{
var newNode = new TreeNode();
if (nesElement is NesMenuFolder)
{
if (usedFolders.Contains((nesElement as NesMenuFolder).ChildMenuCollection))
{
nesMenuCollection.Remove(nesElement); // We don't need any "back" folders
continue;
}
}
newNode.SelectedImageIndex = newNode.ImageIndex = getImageIndex(nesElement as INesMenuElement);
newNode.Text = nesElement.Name;
newNode.Tag = nesElement;
treeNodeCollection.Add(newNode);
if (nesElement is NesMenuFolder)
{
AddNodes(newNode.Nodes, (nesElement as NesMenuFolder).ChildMenuCollection, usedFolders);
}
}
}
private void buttonNoFolders_Click(object sender, EventArgs e)
{
DrawSplitTree(NesMenuCollection.SplitStyle.NoSplit);
}
private void buttonNoFoldersOriginal_Click(object sender, EventArgs e)
{
DrawSplitTree(NesMenuCollection.SplitStyle.Original_NoSplit);
}
private void buttonFoldersEqually_Click(object sender, EventArgs e)
{
DrawSplitTree(NesMenuCollection.SplitStyle.FoldersEqual);
}
private void buttonFoldersEquallyOriginal_Click(object sender, EventArgs e)
{
DrawSplitTree(NesMenuCollection.SplitStyle.Original_FoldersEqual);
}
private void buttonFoldersLetters_Click(object sender, EventArgs e)
{
DrawSplitTree(NesMenuCollection.SplitStyle.FoldersAlphabetic_FoldersEqual);
}
private void buttonFoldersLettersOriginal_Click(object sender, EventArgs e)
{
DrawSplitTree(NesMenuCollection.SplitStyle.Original_FoldersAlphabetic_FoldersEqual);
}
private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
{
ShowSelected();
}
private void ShowSelected()
{
var node = treeView.SelectedNode;
listViewContent.Clear();
if (node != null && (node.Nodes.Count > 0 || node.Tag is NesMenuFolder)) // Folder or root
{
pictureBoxArt.Image = (node.Tag is NesMenuFolder) ? (node.Tag as NesMenuFolder).Image : null;
groupBoxArt.Enabled = (node.Tag is NesMenuFolder);
groupBoxSplitModes.Enabled = true;
pictureBoxArt.Cursor = Cursors.Hand;
listViewContent.Enabled = true;
foreach (TreeNode n in node.Nodes)
{
var element = (INesMenuElement)n.Tag;
var item = new ListViewItem();
item.Text = element.Name;
var transparency = cuttedNodes.Contains(n) ? 1 : 0;
item.ImageIndex = getImageIndex(element) + transparency;
item.Tag = n;
listViewContent.Items.Add(item);
}
}
else
{
if (node != null && node.Tag is NesMiniApplication)
{
var game = node.Tag as NesMiniApplication;
pictureBoxArt.Image = NesMiniApplication.LoadBitmap(game.IconPath);
groupBoxArt.Enabled = true;
listViewContent.Enabled = false;
}
else //if (e.Node.Tag is NesDefaultGame)
{
pictureBoxArt.Image = null;
groupBoxArt.Enabled = false;
}
listViewContent.Enabled = false;
groupBoxSplitModes.Enabled = false;
pictureBoxArt.Cursor = Cursors.Default;
}
ShowFolderStats();
}
void ShowFolderStats()
{
var node = treeView.SelectedNode;
if (node != null && (node.Tag is NesMenuCollection || node.Tag is NesMenuFolder)) // Folder or root
{
labelElementCount.Text = string.Format(Resources.FolderStatistics, node.Text, node.Nodes.Count);
if (node.Nodes.Count >= MainForm.MaxGamesPerFolder)
labelElementCount.Text += " " + Resources.TooManyPerFolder;
buttonNewFolder.Enabled = true;
}
else
{
labelElementCount.Text = "";
buttonNewFolder.Enabled = false;
}
if (node != null && node.Tag is NesMenuFolder) // Folder
{
labelPosition.Enabled = comboBoxPosition.Enabled = true;
var folder = node.Tag as NesMenuFolder;
var position = (int)folder.Position;
if (position > 1) position--;
comboBoxPosition.SelectedIndex = position;
}
else
{
labelPosition.Enabled = comboBoxPosition.Enabled = false;
comboBoxPosition.SelectedIndex = -1;
}
}
private void listViewContent_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left && listViewContent.SelectedItems.Count == 1)
{
var node = (listViewContent.SelectedItems[0].Tag as TreeNode);
treeView.SelectedNode = node;
//node.Expand();
}
}
private void treeView_BeforeLabelEdit(object sender, NodeLabelEditEventArgs e)
{
if (!(e.Node.Tag is NesMenuFolder))
e.CancelEdit = true;
}
private void treeView_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
if (!(e.Node.Tag is NesMenuFolder) ||
string.IsNullOrEmpty(e.Label) || string.IsNullOrEmpty(e.Label.Trim()))
e.CancelEdit = true;
else
{
e.Node.Text = e.Label;
(e.Node.Tag as INesMenuElement).Name = e.Label;
var parent = e.Node.Parent;
FixSort(e.Node);
}
}
private void listViewContent_BeforeLabelEdit(object sender, LabelEditEventArgs e)
{
if (!((listViewContent.Items[e.Item].Tag as TreeNode).Tag is NesMenuFolder))
e.CancelEdit = true;
}
private void listViewContent_AfterLabelEdit(object sender, LabelEditEventArgs e)
{
if (string.IsNullOrEmpty(e.Label) || string.IsNullOrEmpty(e.Label.Trim()))
e.CancelEdit = true;
else
{
var item = listViewContent.Items[e.Item];
var node = item.Tag as TreeNode;
node.Text = e.Label;
(node.Tag as INesMenuElement).Name = e.Label;
FixSort(item);
}
}
private void FixSort(object o)
{
// This is most simple to resort node without resorting the whole tree/list
new Thread(KostilKostilevich).Start(o);
}
private void KostilKostilevich(object o)
{
// This is stupid workaround for resort after renaming item, lol
if (InvokeRequired)
{
Invoke(new Action<object>(KostilKostilevich), new object[] { o });
return;
}
if (o is TreeNode)
{
var node = o as TreeNode;
var parent = node.Parent;
parent.Nodes.Remove(node);
parent.Nodes.Add(node);
treeView.SelectedNode = node;
ShowFolderStats();
}
if (o is ListViewItem)
{
var item = o as ListViewItem;
listViewContent.Items.Remove(item);
listViewContent.Items.Add(item);
var node = item.Tag as TreeNode;
var parent = node.Parent;
parent.Nodes.Remove(node);
parent.Nodes.Add(node);
}
}
private void treeView_ItemDrag(object sender, ItemDragEventArgs e)
{
if ((e.Button == MouseButtons.Left) && !((e.Item as TreeNode).Tag is NesMenuCollection)) // We can't drag root
DoDragDrop(new TreeNode[] { (TreeNode)e.Item }, DragDropEffects.Move);
}
private void listViewContent_ItemDrag(object sender, ItemDragEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
var nodes = new List<TreeNode>();
foreach (ListViewItem i in listViewContent.SelectedItems)
nodes.Add(i.Tag as TreeNode);
DoDragDrop(nodes.ToArray(), DragDropEffects.Move);
}
private void treeView_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode[]", false))
e.Effect = DragDropEffects.Move;
}
private void treeView_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode[]", false))
{
TreeNode destinationNode;
if (sender is TreeView)
{
Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
destinationNode = ((TreeView)sender).GetNodeAt(pt);
}
else
{
Point pt = ((ListView)sender).PointToClient(new Point(e.X, e.Y));
var item = ((ListView)sender).GetItemAt(pt.X, pt.Y);
if (item == null)
destinationNode = treeView.SelectedNode;
else
destinationNode = (TreeNode)item.Tag;
}
var newNodes = (TreeNode[])e.Data.GetData("System.Windows.Forms.TreeNode[]");
MoveToFolder(newNodes, destinationNode, false);
if (sender is TreeView)
treeView.Select();
else
listViewContent.Select();
}
}
private void buttonNewFolder_Click(object sender, EventArgs e)
{
newFolder(null);
}
private void comboBoxPosition_SelectionChangeCommitted(object sender, EventArgs e)
{
if (!(treeView.SelectedNode.Tag is NesMenuFolder)) return;
int value = comboBoxPosition.SelectedIndex;
if (value >= 2) value++;
var node = treeView.SelectedNode;
var folder = (node.Tag as NesMenuFolder);
folder.Position = (NesMenuFolder.Priority)value;
FixSort(node);
}
private void renameToolStripMenuItem_Click(object sender, EventArgs e)
{
renameFolder((sender as ToolStripMenuItem).Tag);
}
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
if ((sender as ToolStripMenuItem).Tag is TreeNode)
deleteElements(new TreeNode[] { treeView.SelectedNode });
else
if ((sender as ToolStripMenuItem).Tag is ListView)
deleteElements(from i in listViewContent.SelectedItems.Cast<ListViewItem>().ToArray() select i.Tag as TreeNode);
}
private void cutToolStripMenuItem_Click(object sender, EventArgs e)
{
if ((sender as ToolStripMenuItem).Tag is TreeNode)
cutElements(new TreeNode[] { treeView.SelectedNode });
else
if ((sender as ToolStripMenuItem).Tag is ListView)
cutElements(from i in listViewContent.SelectedItems.Cast<ListViewItem>().ToArray() select i.Tag as TreeNode);
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
pasteElements(treeView.SelectedNode);
}
private void treeView_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Right) return;
if (sender is TreeView)
{
var destinationNode = ((TreeView)sender).GetNodeAt(e.X, e.Y);
if (destinationNode == null) destinationNode = treeView.Nodes[0]; // Root
newFolderToolStripMenuItem.Tag = deleteToolStripMenuItem.Tag = renameToolStripMenuItem.Tag =
cutToolStripMenuItem.Tag = pasteToolStripMenuItem.Tag = destinationNode;
newFolderToolStripMenuItem.Enabled = true;
if ((destinationNode.Tag is NesMenuFolder || destinationNode.Tag is NesMenuCollection)) // Folder
treeView.SelectedNode = destinationNode;
else
newFolderToolStripMenuItem.Tag = destinationNode.Parent;
renameToolStripMenuItem.Enabled = destinationNode.Tag is NesMenuFolder; // Folder
cutToolStripMenuItem.Enabled = deleteToolStripMenuItem.Enabled = !(destinationNode.Tag is NesMenuCollection); // Not root
pasteToolStripMenuItem.Enabled = cuttedNodes.Count > 0;
}
else
{
var item = ((ListView)sender).GetItemAt(e.X, e.Y);
if (listViewContent.SelectedItems.Count == 0 && item != null) item.Selected = true;
renameToolStripMenuItem.Tag = item;
newFolderToolStripMenuItem.Tag = deleteToolStripMenuItem.Tag = cutToolStripMenuItem.Tag = pasteToolStripMenuItem.Tag = listViewContent;
newFolderToolStripMenuItem.Enabled = treeView.SelectedNode != null && (treeView.SelectedNode.Tag is NesMenuFolder || treeView.SelectedNode.Tag is NesMenuCollection); // Folder
renameToolStripMenuItem.Enabled = (item != null) && (item.Tag as TreeNode).Tag is NesMenuFolder;
cutToolStripMenuItem.Enabled = deleteToolStripMenuItem.Enabled = listViewContent.SelectedItems.Count > 0;
pasteToolStripMenuItem.Enabled = cuttedNodes.Count > 0;
}
contextMenuStrip.Show(sender as Control, e.X, e.Y);
}
private void newFolderToolStripMenuItem_Click(object sender, EventArgs e)
{
if ((sender as ToolStripMenuItem).Tag is TreeNode)
newFolder((sender as ToolStripMenuItem).Tag as TreeNode);
else
newFolder(null);
}
bool MoveToFolder(IEnumerable<TreeNode> newNodes, TreeNode destinationNode, bool showDest = true)
{
if (destinationNode == null)
destinationNode = treeView.Nodes[0]; // Root
if (destinationNode.Tag is NesMiniApplication || destinationNode.Tag is NesDefaultGame)
destinationNode = destinationNode.Parent;
foreach (var newNode in newNodes)
{
if (!destinationNode.FullPath.StartsWith(newNode.FullPath + '\\') && (destinationNode != newNode.Parent))
{
Debug.WriteLine(string.Format("Drag: {0}->{1}", newNode, destinationNode));
if (newNode.Parent.Tag is NesMenuFolder)
(newNode.Parent.Tag as NesMenuFolder).ChildMenuCollection.Remove(newNode.Tag as INesMenuElement);
else if (newNode.Parent.Tag is NesMenuCollection)
(newNode.Parent.Tag as NesMenuCollection).Remove(newNode.Tag as INesMenuElement);
newNode.Parent.Nodes.Remove(newNode);
destinationNode.Nodes.Add(newNode);
if (destinationNode.Tag is NesMenuFolder)
(destinationNode.Tag as NesMenuFolder).ChildMenuCollection.Add(newNode.Tag as INesMenuElement);
else if (destinationNode.Tag is NesMenuCollection)
(destinationNode.Tag as NesMenuCollection).Add(newNode.Tag as INesMenuElement);
}
else
{
System.Media.SystemSounds.Hand.Play();
return false;
}
}
if (showDest)
treeView.SelectedNode = destinationNode;
if (treeView.SelectedNode == destinationNode)
ShowSelected();
else
foreach (var i in (from n in listViewContent.Items.Cast<ListViewItem>().ToArray() where newNodes.Contains(n.Tag as TreeNode) select n))
listViewContent.Items.Remove(i);
foreach (ListViewItem item in listViewContent.Items)
item.Selected = newNodes.Contains(item.Tag as TreeNode) || item.Tag == destinationNode;
ShowFolderStats();
return true;
}
void newFolder(TreeNode parent = null)
{
var newFolder = new NesMenuFolder(Resources.FolderNameNewFolder);
var folderImageIndex = getImageIndex(newFolder);
var newnode = new TreeNode(Resources.FolderNameNewFolder, folderImageIndex, folderImageIndex);
newnode.Tag = newFolder;
if (parent != null)
{
parent.Nodes.Add(newnode);
treeView.SelectedNode = newnode;
ShowSelected();
newnode.BeginEdit();
}
else if (treeView.SelectedNode != null)
{
parent = treeView.SelectedNode;
parent.Nodes.Add(newnode);
ShowFolderStats();
var item = new ListViewItem(newnode.Text, folderImageIndex);
item.Tag = newnode;
listViewContent.SelectedItems.Clear();
listViewContent.Items.Add(item);
item.BeginEdit();
}
if (parent != null)
{
if (parent.Tag is NesMenuFolder)
(parent.Tag as NesMenuFolder).ChildMenuCollection.Add(newFolder);
else if (parent.Tag is NesMenuCollection)
(parent.Tag as NesMenuCollection).Add(newFolder);
}
}
TreeNode getFolder(string name)
{
var root = treeView.Nodes[0];
foreach (TreeNode el in root.Nodes)
{
if (el.Text == name && el.Tag is NesMenuFolder)
return el;
}
var newFolder = new NesMenuFolder(name);
var folderImageIndex = getImageIndex(newFolder);
var newNode = new TreeNode(name, folderImageIndex, folderImageIndex);
newFolder.Position = NesMenuFolder.Priority.Leftmost;
newNode.Tag = newFolder;
(root.Tag as NesMenuCollection).Add(newFolder);
root.Nodes.Add(newNode);
return newNode;
}
void deleteElements(IEnumerable<TreeNode> nodes)
{
if (nodes.Count() == 1)
{
if (MessageBox.Show(this, string.Format(Resources.DeleteElement, nodes.First().Text),
Resources.AreYouSure, MessageBoxButtons.YesNo,
MessageBoxIcon.Warning) != DialogResult.Yes)
return;
}
else
{
if (MessageBox.Show(this, string.Format(Resources.DeleteElements, nodes.Count()),
Resources.AreYouSure, MessageBoxButtons.YesNo,
MessageBoxIcon.Warning) != DialogResult.Yes)
return;
}
bool needWarn = false;
TreeNode parent = null;
foreach (var node in nodes)
{
parent = node.Parent;
if (node.Tag is NesMenuFolder) // Folder
{
if (node.Nodes.Count > 0)
{
var unsortedFolder = getFolder(Resources.FolderNameTrashBin);
if (node.FullPath.StartsWith(unsortedFolder.FullPath)) // It's already in trash bin
{
(node.Tag as NesMenuFolder).ChildMenuCollection.Unsplit();
deletedGames.AddRange((node.Tag as NesMenuFolder).ChildMenuCollection);
(node.Tag as NesMenuFolder).ChildMenuCollection.Clear();
}
else
{
MoveToFolder(node.Nodes.Cast<TreeNode>().ToArray(), unsortedFolder, false);
needWarn = true;
}
}
if (parent.Tag is NesMenuFolder)
(parent.Tag as NesMenuFolder).ChildMenuCollection.Remove(node.Tag as INesMenuElement);
else if (parent.Tag is NesMenuCollection)
(parent.Tag as NesMenuCollection).Remove(node.Tag as INesMenuElement);
parent.Nodes.Remove(node);
}
else
{ // Game
var unsortedFolder = getFolder(Resources.FolderNameTrashBin);
if (node.FullPath.StartsWith(unsortedFolder.FullPath)) // It's already in trash bin
{
deletedGames.Add(node.Tag as INesMenuElement);
if (parent.Tag is NesMenuFolder)
(parent.Tag as NesMenuFolder).ChildMenuCollection.Remove(node.Tag as INesMenuElement);
else if (parent.Tag is NesMenuCollection)
(parent.Tag as NesMenuCollection).Remove(node.Tag as INesMenuElement);
parent.Nodes.Remove(node);
}
else
{
MoveToFolder(new TreeNode[] { node }, unsortedFolder, false);
needWarn = true;
}
}
foreach (var i in from i in listViewContent.Items.Cast<ListViewItem>().ToArray() where i.Tag == node select i)
listViewContent.Items.Remove(i);
cuttedNodes.Remove(node);
}
if (parent != null)
treeView.SelectedNode = parent;
if (needWarn)
MessageBox.Show(this, Resources.FolderContent, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
buttonOk.Enabled = treeView.Nodes[0].Nodes.Count > 0;
}
void cutElements(IEnumerable<TreeNode> nodes)
{
foreach (var node in cuttedNodes)
node.SelectedImageIndex = node.ImageIndex = node.ImageIndex / 2 * 2;
cuttedNodes.Clear();
foreach (var node in nodes)
{
cuttedNodes.Add(node);
node.SelectedImageIndex = node.ImageIndex = node.ImageIndex / 2 * 2 + 1;
}
foreach (ListViewItem item in listViewContent.Items)
{
item.ImageIndex = item.ImageIndex / 2 * 2 +
((cuttedNodes.Contains(item.Tag as TreeNode)) ? 1 : 0);
}
}
void pasteElements(TreeNode node)
{
if ((cuttedNodes.Count > 0) && MoveToFolder(cuttedNodes, node))
{
cutElements(new TreeNode[0]);
}
}
private void renameFolder(object folder)
{
if (folder is TreeNode)
(folder as TreeNode).BeginEdit();
else if (folder is ListViewItem)
(folder as ListViewItem).BeginEdit();
}
private void treeView_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete && e.Modifiers == Keys.None)
{
if ((treeView.SelectedNode != null) && !(treeView.SelectedNode.Tag is NesMenuCollection))
deleteElements(new TreeNode[] { treeView.SelectedNode });
}
else if (e.KeyCode == Keys.F2 && e.Modifiers == Keys.None)
{
if ((treeView.SelectedNode != null) && (treeView.SelectedNode.Tag is NesMenuFolder))
renameFolder(treeView.SelectedNode);
}
else if (e.KeyCode == Keys.X && e.Modifiers == Keys.Control)
{
if ((treeView.SelectedNode != null) && !(treeView.SelectedNode.Tag is NesMenuCollection))
cutElements(new TreeNode[] { treeView.SelectedNode });
}
else if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control)
{
if (treeView.SelectedNode != null)
{
if ((treeView.SelectedNode.Tag is NesMenuFolder || treeView.SelectedNode.Tag is NesMenuCollection))
pasteElements(treeView.SelectedNode);
else
pasteElements(treeView.SelectedNode.Parent);
}
}
else if (e.KeyCode == Keys.N && e.Modifiers == Keys.Control)
{
if (treeView.SelectedNode != null)
{
if (treeView.SelectedNode.Tag is NesMenuFolder || treeView.SelectedNode.Tag is NesMenuCollection)
newFolder(treeView.SelectedNode);
else
newFolder(treeView.SelectedNode.Parent);
}
}
}
private void listViewContent_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
deleteElements(from i in listViewContent.SelectedItems.Cast<ListViewItem>().ToArray() select i.Tag as TreeNode);
}
else if (e.KeyCode == Keys.F2 && e.Modifiers == Keys.None)
{
if (listViewContent.SelectedItems.Count != 1) return;
var item = listViewContent.SelectedItems[0];
renameFolder(item);
}
else if (e.KeyCode == Keys.A && e.Modifiers == Keys.Control)
{
foreach (ListViewItem item in listViewContent.Items)
item.Selected = true;
}
else if (e.KeyCode == Keys.X && e.Modifiers == Keys.Control)
{
cutElements(from i in listViewContent.SelectedItems.Cast<ListViewItem>().ToArray() select i.Tag as TreeNode);
}
else if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control)
{
if (treeView.SelectedNode != null)
{
if ((treeView.SelectedNode.Tag is NesMenuFolder || treeView.SelectedNode.Tag is NesMenuCollection))
pasteElements(treeView.SelectedNode);
else
pasteElements(treeView.SelectedNode.Parent);
}
}
else if (e.KeyCode == Keys.N && e.Modifiers == Keys.Control)
{
if (treeView.SelectedNode != null && (treeView.SelectedNode.Tag is NesMenuFolder || treeView.SelectedNode.Tag is NesMenuCollection))
newFolder();
}
}
private void listViewContent_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
if (listViewContent.SelectedItems.Count != 1) return;
var item = listViewContent.SelectedItems[0];
if (!((item.Tag as TreeNode).Tag is NesMenuFolder))
return;
treeView.SelectedNode = item.Tag as TreeNode;
}
else if (e.KeyChar == (char)8)
{
if (treeView.SelectedNode != null && treeView.SelectedNode.Parent != null)
{
treeView.SelectedNode = treeView.SelectedNode.Parent;
}
}
}
private void buttonOk_Click(object sender, EventArgs e)
{
SaveTree();
DialogResult = DialogResult.OK;
}
private void buttonCancel_Click(object sender, EventArgs e)
{
Close();
}
private void pictureBoxArt_Click(object sender, EventArgs e)
{
if (treeView.SelectedNode != null && treeView.SelectedNode.Tag is NesMenuFolder)
{
var folder = treeView.SelectedNode.Tag as NesMenuFolder;
var form = new SelectIconForm(folder.ImageId);
if (form.ShowDialog() == DialogResult.OK)
{
folder.ImageId = form.listBox.SelectedItem.ToString();
pictureBoxArt.Image = folder.Image;
}
}
}
private void TreeContructorForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason != CloseReason.UserClosing || DialogResult == DialogResult.OK) return;
var a = MessageBox.Show(this, Resources.FoldersSaveQ, this.Text, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (a == DialogResult.Cancel)
{
e.Cancel = true;
return;
}
if (a == DialogResult.Yes)
SaveTree();
DialogResult = DialogResult.Cancel;
}
void SaveTree()
{
Directory.CreateDirectory(Path.GetDirectoryName(FoldersXmlPath));
File.WriteAllText(FoldersXmlPath, TreeToXml());
if (mainForm != null)
{
for (int i = 1; i < mainForm.listViewGames.Items.Count; i++)
{
if (deletedGames.Contains(mainForm.listViewGames.Items[i].Tag as NesMiniApplication))
mainForm.listViewGames.Items[i].Checked = false;
}
for (int i = 0; i < mainForm.checkedListBoxDefaultGames.Items.Count; i++)
{
if (deletedGames.Contains(mainForm.checkedListBoxDefaultGames.Items[i] as NesDefaultGame))
mainForm.checkedListBoxDefaultGames.SetItemChecked(i, false);
}
ConfigIni.Save();
}
}
private string TreeToXml()
{
var root = treeView.Nodes[0];
var xml = new XmlDocument();
var treeNode = xml.CreateElement("Tree");
xml.AppendChild(treeNode);
NodeToXml(xml, treeNode, root);
using (var stringWriter = new StringWriter())
using (var xmlTextWriter = new XmlTextWriter(stringWriter))
{
xmlTextWriter.Formatting = Formatting.Indented;
xmlTextWriter.WriteStartDocument();
xml.WriteTo(xmlTextWriter);
xmlTextWriter.WriteEndDocument();
xmlTextWriter.Flush();
return stringWriter.GetStringBuilder().ToString();
}
}
private void NodeToXml(XmlDocument xml, XmlElement element, TreeNode node)
{
foreach (TreeNode child in node.Nodes)
{
if (child.Tag is NesMenuFolder)
{
var subElement = xml.CreateElement("Folder");
var folder = child.Tag as NesMenuFolder;
subElement.SetAttribute("name", folder.Name);
subElement.SetAttribute("icon", folder.ImageId);
subElement.SetAttribute("position", ((byte)folder.Position).ToString());
element.AppendChild(subElement);
NodeToXml(xml, subElement, child);
}
else if (child.Tag is NesMiniApplication)
{
var subElement = xml.CreateElement("Game");
var game = child.Tag as NesMiniApplication;
subElement.SetAttribute("code", game.Code);
subElement.SetAttribute("name", game.Name);
element.AppendChild(subElement);
}
else if (child.Tag is NesDefaultGame)
{
var subElement = xml.CreateElement("OriginalGame");
var game = child.Tag as NesDefaultGame;
subElement.SetAttribute("code", game.Code);
subElement.SetAttribute("name", game.Name);
element.AppendChild(subElement);
}
}
}
void XmlToTree(string xmlString)
{
gamesCollection.Unsplit();
var oldCollection = new NesMenuCollection();
oldCollection.AddRange(gamesCollection);
var xml = new XmlDocument();
xml.LoadXml(xmlString);
gamesCollection.Clear();
XmlToNode(xml, xml.SelectSingleNode("/Tree").ChildNodes, oldCollection, gamesCollection);
// oldCollection has only unsorted (new) games
if (oldCollection.Count > 0)
{
NesMenuFolder unsorted;
var unsorteds = from f in gamesCollection where f is NesMenuFolder && f.Name == Resources.FolderNameUnsorted select f;
if (unsorteds.Count() > 0)
unsorted = unsorteds.First() as NesMenuFolder;
else
{
unsorted = new NesMenuFolder(Resources.FolderNameUnsorted);
unsorted.Position = NesMenuFolder.Priority.Leftmost;
gamesCollection.Add(unsorted);
}
foreach (var game in oldCollection)
unsorted.ChildMenuCollection.Add(game);
MessageBox.Show(this, Resources.NewGamesUnsorted, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
DrawTree();
}
void XmlToNode(XmlDocument xml, XmlNodeList elements, NesMenuCollection rootMenuCollection, NesMenuCollection nesMenuCollection = null)
{
if (nesMenuCollection == null)
nesMenuCollection = rootMenuCollection;
foreach (XmlNode element in elements)
{
switch (element.Name)
{
case "Folder":
var folder = new NesMenuFolder(element.Attributes["name"].Value, element.Attributes["icon"].Value);
folder.Position = (NesMenuFolder.Priority)byte.Parse(element.Attributes["position"].Value);
nesMenuCollection.Add(folder);
XmlToNode(xml, element.ChildNodes, rootMenuCollection, folder.ChildMenuCollection);
break;
case "Game":
case "OriginalGame":
var code = element.Attributes["code"].Value;
var games = from n in rootMenuCollection where ((n is NesMiniApplication || n is NesDefaultGame) && (n.Code == code)) select n;
if (games.Count() > 0)
{
var game = games.First();
nesMenuCollection.Add(game);
rootMenuCollection.Remove(game);
}
break;
}
}
}
}
}