-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedactorForm.cs
More file actions
871 lines (829 loc) · 37.1 KB
/
RedactorForm.cs
File metadata and controls
871 lines (829 loc) · 37.1 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
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;
using ExtraSharp;
using ExtraForms;
using Resource_Redactor.Descriptions;
using Resource_Redactor.Descriptions.Redactors;
using System.Collections.Specialized;
using Resource_Redactor.Compiler;
namespace Resource_Redactor
{
public partial class RedactorForm : Form
{
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
DirectoryInfo[] dirs = dir.GetDirectories();
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, false);
}
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}
private IResourceControl SelectedRedactor
{
get
{
if (RedactorsTabControl.TabCount < 1) return null;
var tab = RedactorsTabControl.SelectedTab;
if (tab == null) return null;
if (tab.Controls.Count != 1) return null;
var control = tab.Controls[0];
return control as IResourceControl;
}
}
private void LoadRedactor(string path, string name)
{
try
{
foreach (TabPage tab in RedactorsTabControl.TabPages)
{
if (tab.Controls.Count == 1)
{
var redactor = tab.Controls[0] as IResourceControl;
if (redactor != null)
{
if (redactor.ResourcePath == path)
{
RedactorsTabControl.SelectedTab = tab;
return;
}
}
}
}
var type = Resource.GetType(path);
Control control = null;
switch (type)
{
case ResourceType.Texture: control = new TextureControl(path); break;
case ResourceType.Sprite: control = new SpriteControl(path); break;
case ResourceType.Ragdoll: control = new RagdollControl(path); break;
case ResourceType.Animation: control = new AnimationControl(path); break;
case ResourceType.Tool: control = new ToolControl(path); break;
case ResourceType.Entity: control = new EntityControl(path); break;
case ResourceType.Tile: control = new TileControl(path); break;
case ResourceType.Event: control = new EventControl(path); break;
case ResourceType.Outfit: control = new OutfitControl(path); break;
default:
MessageBox.Show(this, "Resource [" + Resource.TypeToString(type) +
"] redactor does not implemented!", "Warning!",
MessageBoxButtons.OK, MessageBoxIcon.Warning); break;
}
if (control == null) return;
control.Dock = DockStyle.Fill;
var page = new TabPage(name);
page.ImageIndex = Resource.TypeToIcon(type);
bool refresh = RedactorsTabControl.TabPages.Count == 0;
RedactorsTabControl.TabPages.Add(page);
page.Controls.Add(control);
var iresource = control as IResourceControl;
iresource.StateChanged += Redactor_StateChanged;
RedactorsTabControl.SelectedTab = page;
if (RedactorsTabControl.TabPages.Count == 1)
{
RedactorsTabControl.SelectedTab.Text = iresource.ResourceName + (iresource.Saved ? "" : "*");
UndoToolStripMenuItem.Enabled = iresource.UndoEnabled;
RedoToolStripMenuItem.Enabled = iresource.RedoEnabled;
CloseToolStripMenuItem.Enabled = true;
SaveToolStripMenuItem.Enabled = !iresource.Saved;
SaveAsToolStripMenuItem.Enabled = true;
CloseOthersToolStripMenuItem.Enabled = true;
ResourceToolStripMenuItem.DropDownItems.Clear();
ResourceToolStripMenuItem.DropDownItems.AddRange(iresource.MenuTabs);
ResourceToolStripMenuItem.Enabled = true;
}
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString(), "Error: Can not load resource redactor [" + name + "].",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void RedactorsTabControl_Selected(object sender, TabControlEventArgs e)
{
try
{
var redactor = SelectedRedactor;
if (redactor == null)
{
UndoToolStripMenuItem.Enabled = false;
RedoToolStripMenuItem.Enabled = false;
CloseToolStripMenuItem.Enabled = false;
SaveToolStripMenuItem.Enabled = false;
SaveAsToolStripMenuItem.Enabled = false;
CloseOthersToolStripMenuItem.Enabled = false;
ResourceToolStripMenuItem.DropDownItems.Clear();
ResourceToolStripMenuItem.Enabled = false;
}
else
{
RedactorsTabControl.SelectedTab.Text = redactor.ResourceName + (redactor.Saved ? "" : "*");
UndoToolStripMenuItem.Enabled = redactor.UndoEnabled;
RedoToolStripMenuItem.Enabled = redactor.RedoEnabled;
CloseToolStripMenuItem.Enabled = true;
SaveToolStripMenuItem.Enabled = !redactor.Saved;
SaveAsToolStripMenuItem.Enabled = true;
CloseOthersToolStripMenuItem.Enabled = true;
ResourceToolStripMenuItem.DropDownItems.Clear();
ResourceToolStripMenuItem.DropDownItems.AddRange(redactor.MenuTabs);
ResourceToolStripMenuItem.Enabled = true;
}
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString(), "Error: Can not select redactor.",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void Redactor_StateChanged(object sender, EventArgs e)
{
try
{
var control = sender as Control;
var redactor = sender as IResourceControl;
if (control == null || redactor == null) return;
var tab = control.Parent as TabPage;
if (tab == null) return;
var name = redactor.ResourceName + (redactor.Saved ? "" : "*");
if (tab.Text != name) tab.Text = name;
if (tab == RedactorsTabControl.SelectedTab)
{
UndoToolStripMenuItem.Enabled = redactor.UndoEnabled;
RedoToolStripMenuItem.Enabled = redactor.RedoEnabled;
SaveToolStripMenuItem.Enabled = !redactor.Saved;
}
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString(), "Error: Can not handle redactor state change.",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ResourceExplorer_ItemLoaded(object sender, ExplorerEventArgs e)
{
try
{
LoadRedactor(e.Path, e.Name);
}
catch (Exception ex)
{
var name = "";
if (e != null) name = e.Name;
MessageBox.Show(this, ex.ToString(), "Error: Can not load item [" + name + "].",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ResourceExplorer_StateChanged(object sender, ExplorerEventArgs e)
{
try
{
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString(), "Error: Can not update redactor interface.",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void OpenDescription(string path)
{
try
{
var version = Description.CheckVersion(path);
if (version != Description.CurrentVersion)
{
var result = MessageBox.Show(this, "Would you like to convert it?",
"Description is outdated.", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result != DialogResult.Yes) return;
Converter.ConvertDescription(path);
}
path = Path.GetFullPath(path);
var desc = new Description(path);
path = Path.Combine(Path.GetDirectoryName(path), "Resources");
Directory.SetCurrentDirectory(path);
ResourceExplorer.LoadLocation(path);
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString(), "Error: Can not open description.",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public RedactorForm(string initial_file = null)
{
InitializeComponent();
RedactorsTabControl.ImageList = ResourceExplorer.SmallIconList;
Text = "Clockwork engine resource redactor V" + Description.RedactorVersion;
if (initial_file != null) OpenDescription(initial_file);
}
private void CreateToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
ToolStripMenuItem item = sender as ToolStripMenuItem;
if (item == null) return;
switch (item.Text)
{
case "Description":
try
{
var Subform = new NewDescriptionForm();
if (Subform.ShowDialog(this) != DialogResult.OK) return;
Description.Create(Subform.Root, Subform.Name);
OpenDescription(Path.Combine(Subform.Root, Subform.Name, Subform.Name + "." + Description.Extension));
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString(), "Can not create description.",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
break;
default: ResourceExplorer.CreateResource(Resource.StringToType(item.Text)); break;
}
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString(), "Error: Can not create resource.",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void DescriptionOpenToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
OpenDescriptionDialog.DefaultExt = Description.Extension;
if (OpenDescriptionDialog.ShowDialog(this) != DialogResult.OK) return;
OpenDescription(OpenDescriptionDialog.FileName);
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString(), "Error: Can not open description.",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ResourceOpenToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (var path in ResourceExplorer.SelectedItems)
{
try
{
LoadRedactor(path, Path.GetFileName(path));
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString(), "Error: Can not open resource.",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
SelectedRedactor?.Save(SelectedRedactor.ResourcePath);
}
catch (Exception ex)
{
var name = "";
if (RedactorsTabControl.SelectedTab != null)
name = RedactorsTabControl.SelectedTab.Text;
if (SelectedRedactor != null) name = SelectedRedactor.ResourceName;
MessageBox.Show(this, ex.ToString(), "Error: Can not save resource [" + name + "].",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
// TODO: Add resource explorer
}
catch (Exception ex)
{
var name = "";
if (RedactorsTabControl.SelectedTab != null)
name = RedactorsTabControl.SelectedTab.Text;
if (SelectedRedactor != null) name = SelectedRedactor.ResourceName;
MessageBox.Show(this, ex.ToString(), "Error: Can not save resource [" + name + "].",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void SaveAllToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (TabPage tab in RedactorsTabControl.TabPages)
{
if (tab.Controls.Count != 1) continue;
var rcontrol = tab.Controls[0] as IResourceControl;
if (rcontrol == null) continue;
try
{
rcontrol.Save(rcontrol.ResourcePath);
tab.Text = rcontrol.ResourceName;
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString(), "Error: Can not save resource [" +
rcontrol.ResourceName + "].", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ExportToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
foreach (var resource in ResourceExplorer.SelectedItems)
{
var path = Path.Combine("../Exports", resource + ".json");
Directory.CreateDirectory(Path.GetDirectoryName(path));
Resource.Export(resource, path);
}
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString(), "Error: Can not export selected resources.",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ImportToolStripMenuItem_Click(object sender, EventArgs e)
{
ImportFileDialog.ShowDialog(this);
string path = ImportFileDialog.FileName;
try
{
string import_path = ResourceExplorer.CurrentDirectory;
string name = Path.GetFileNameWithoutExtension(path);
int i = 0;
if (File.Exists(Path.Combine(import_path, name)) ||
Directory.Exists(Path.Combine(import_path, name)))
while (File.Exists(Path.Combine(import_path, name + " " + ++i)) ||
Directory.Exists(Path.Combine(import_path, name + " " + i))) ;
if (i != 0) name += " " + i;
Resource.Import(path, Path.Combine(import_path, name));
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString(), "Error: Can not import [" + path + "].",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
private void ToSpriteToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (var res in ResourceExplorer.SelectedItems)
{
try
{
Resource.Factory(ResourceExplorer.CurrentDirectory,
ResourceType.Sprite, new string[] { res });
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString(), "Error: Can convert resource to sprite.",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ToRagdollToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
Resource.Factory(ResourceExplorer.CurrentDirectory,
ResourceType.Ragdoll, ResourceExplorer.SelectedItems.ToArray());
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString(), "Error: Can convert resources to ragdoll.",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void DeleteToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
if (MessageBox.Show(this, "Are you sure you want to delete " + ResourceExplorer.SelectedCount +
" selected items?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button2) != DialogResult.Yes) return;
var recycle = Path.Combine(Path.GetDirectoryName(Directory.GetCurrentDirectory()), "Recycle");
foreach (var path in ResourceExplorer.SelectedNames)
{
try
{
var dest = Path.Combine(Path.GetDirectoryName(Path.Combine(recycle, path)),
ResourceSignature.FileTimeStamp + Path.GetFileName(path));
if (!Directory.Exists(dest)) Directory.CreateDirectory(Path.GetDirectoryName(dest));
Directory.Move(path, dest);
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString(), "Error: Can not delete resource.",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString(), "Error: Can not delete resource.",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void RenameToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
ResourceExplorer.RenameSelected();
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString(), "Error: Can not rename resource.",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
StringCollection paths = new StringCollection();
if (ResourceExplorer.SelectedPaths.Length < 1) return;
paths.AddRange(ResourceExplorer.SelectedPaths);
Clipboard.SetFileDropList(paths);
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString(), "Error: Can not copy resource.",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void CutToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
StringCollection paths = new StringCollection();
if (ResourceExplorer.SelectedPaths.Length < 1) return;
paths.AddRange(ResourceExplorer.SelectedPaths);
DataObject data = new DataObject();
data.SetFileDropList(paths);
data.SetData("Preferred DropEffect", new MemoryStream(new byte[] { (byte)DragDropEffects.Move, 0, 0, 0 }));
Clipboard.SetDataObject(data, true);
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString(), "Error: Can not cut resource.",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
if (Clipboard.ContainsFileDropList())
{
DragDropEffects effect = DragDropEffects.Copy;
if (Clipboard.ContainsData("Preferred DropEffect"))
effect = (DragDropEffects)((MemoryStream)Clipboard.
GetData("Preferred DropEffect")).ReadByte();
var paths = Clipboard.GetFileDropList();
foreach (var path in paths)
{
var dest = Path.Combine(ResourceExplorer.CurrentDirectory, Path.GetFileName(path));
int p = 0;
string s = "";
while (File.Exists(dest + s) || Directory.Exists(dest + s)) s = " - copy " + (p++);
if (Directory.Exists(path))
{
if (effect.HasFlag(DragDropEffects.Move)) Directory.Move(path, dest + s);
else DirectoryCopy(path, dest + s, true);
}
else if (File.Exists(path))
{
if (effect.HasFlag(DragDropEffects.Move)) File.Move(path, dest + s);
else File.Copy(path, dest + s);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString(), "Error: Can not paste resource.",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void UndoToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
SelectedRedactor?.Undo();
}
catch (Exception ex)
{
var name = "";
if (RedactorsTabControl.SelectedTab != null)
name = RedactorsTabControl.SelectedTab.Text;
if (SelectedRedactor != null) name = SelectedRedactor.ResourceName;
MessageBox.Show(this, ex.ToString(), "Error: Can not undo resource [" + name + "].",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void RedoToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
SelectedRedactor?.Redo();
}
catch (Exception ex)
{
var name = "";
if (RedactorsTabControl.SelectedTab != null)
name = RedactorsTabControl.SelectedTab.Text;
if (SelectedRedactor != null) name = SelectedRedactor.ResourceName;
MessageBox.Show(this, ex.ToString(), "Error: Can not redo resource [" + name + "].",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void CloseToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
var redactor = SelectedRedactor;
if (redactor == null) return;
if (redactor.Saved) RedactorsTabControl.TabPages.
Remove(RedactorsTabControl.SelectedTab);
else
{
var result = MessageBox.Show(this, "Save changes before closing?",
"Warning: You have unsaved changes in [" + redactor.ResourceName +
"]!", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
if (result == DialogResult.Yes) redactor.Save(redactor.ResourcePath);
if (result != DialogResult.Cancel) RedactorsTabControl.TabPages.
Remove(RedactorsTabControl.SelectedTab);
}
}
catch (Exception ex)
{
var name = RedactorsTabControl.SelectedTab?.Text ?? "{INVALID_NAME}";
if (SelectedRedactor != null) name = SelectedRedactor.ResourceName;
MessageBox.Show(this, ex.ToString(), "Error: Can not close resource [" + name + "].",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (TabPage tab in RedactorsTabControl.TabPages)
{
try
{
var redactor = tab.Controls.Count == 1 ? tab.Controls[0] as IResourceControl : null;
if (redactor == null) return;
if (redactor.Saved) RedactorsTabControl.TabPages.Remove(tab);
else
{
var result = MessageBox.Show(this, "Save changes before closing?",
"Warning: You have unsaved changes in [" + redactor.ResourceName +
"]!", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
if (result == DialogResult.Yes) redactor.Save(redactor.ResourcePath);
if (result != DialogResult.Cancel) RedactorsTabControl.TabPages.Remove(tab);
}
}
catch (Exception ex)
{
var name = "";
if (RedactorsTabControl.SelectedTab != null)
name = RedactorsTabControl.SelectedTab.Text;
if (SelectedRedactor != null) name = SelectedRedactor.ResourceName;
MessageBox.Show(this, ex.ToString(), "Error: Can not close resource [" + name + "].",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void CloseOthersToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (TabPage tab in RedactorsTabControl.TabPages)
{
try
{
if (tab == RedactorsTabControl.SelectedTab) continue;
var redactor = tab.Controls.Count == 1 ? tab.Controls[0] as IResourceControl : null;
if (redactor == null) return;
if (redactor.Saved) RedactorsTabControl.TabPages.Remove(tab);
else
{
var result = MessageBox.Show(this, "Save changes before closing?",
"Warning: You have unsaved changes in [" + redactor.ResourceName +
"]!", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
if (result == DialogResult.Yes) redactor.Save(redactor.ResourcePath);
if (result != DialogResult.Cancel) RedactorsTabControl.TabPages.Remove(tab);
}
}
catch (Exception ex)
{
var name = "";
if (RedactorsTabControl.SelectedTab != null)
name = RedactorsTabControl.SelectedTab.Text;
if (SelectedRedactor != null) name = SelectedRedactor.ResourceName;
MessageBox.Show(this, ex.ToString(), "Error: Can not close resource [" + name + "].",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ToggleExplorerToolStripMenuItem_Click(object sender, EventArgs e)
{
ExplorerSplitContainer.Panel1Collapsed =
!ExplorerSplitContainer.Panel1Collapsed;
}
private void SwitchViewToolStripMenuItem_Click(object sender, EventArgs e)
{
var mode = ResourceExplorer.ViewMode;
switch (mode)
{
case ListViewMode.LargeIcon: ResourceExplorer.ViewMode = ListViewMode.MediumIcon; break;
case ListViewMode.MediumIcon: ResourceExplorer.ViewMode = ListViewMode.SmallIcon; break;
case ListViewMode.SmallIcon: ResourceExplorer.ViewMode = ListViewMode.List; break;
case ListViewMode.List: ResourceExplorer.ViewMode = ListViewMode.LargeIcon; break;
}
}
private void ExplorerForm_FormClosing(object sender, FormClosingEventArgs e)
{
foreach (TabPage tab in RedactorsTabControl.TabPages)
{
try
{
if (tab.Controls.Count != 1) continue;
var rcontrol = tab.Controls[0] as IResourceControl;
if (rcontrol == null) continue;
if (!rcontrol.Saved)
{
var result = MessageBox.Show(this, "Save changes before closing?",
"Warning: You have unsaved changes in [" + rcontrol.ResourceName +
"]!", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
if (result == DialogResult.Yes) rcontrol.Save(rcontrol.ResourcePath);
if (result == DialogResult.Cancel)
{
e.Cancel = true;
return;
}
}
RedactorsTabControl.TabPages.Remove(tab);
}
catch (Exception ex)
{
e.Cancel = true;
MessageBox.Show(this, ex.ToString(), "Error: Can not close redactor.",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
static readonly Image CloseImage = (Image)(new ComponentResourceManager(typeof(RedactorForm)).GetObject("CloseToolStripMenuItem.Image"));
private void RedactorsTabControl_DrawItem(object sender, DrawItemEventArgs e)
{
var tc = sender as TabControl;
var tab = tc.TabPages[e.Index];
if (tc.SelectedIndex != e.Index) e.Graphics.FillRectangle(Brushes.LightGray, e.Bounds.Left, e.Bounds.Top + 2, e.Bounds.Width, e.Bounds.Height);
else e.Graphics.FillRectangle(Brushes.White, e.Bounds.Left, e.Bounds.Top + 2, e.Bounds.Width, e.Bounds.Height);
e.Graphics.DrawImage(tc.ImageList.Images[tab.ImageIndex], e.Bounds.Left + 3, e.Bounds.Top + 3, 16, 16);
e.Graphics.DrawString(tab.Text, e.Font, Brushes.Black, e.Bounds.Left + 20, e.Bounds.Top + 4);
e.Graphics.DrawImage(CloseImage, e.Bounds.Right - 19, e.Bounds.Top + 3, 16, 16);
}
private void RedactorsTabControl_MouseDown(object sender, MouseEventArgs e)
{
try
{
var tc = sender as TabControl;
if (tc == null) return;
for (int i = 0; i < tc.TabPages.Count; i++)
{
Rectangle r = tc.GetTabRect(i);
Rectangle closeButton = new Rectangle(r.Right - 16, r.Top + 4, 14, 14);
if (closeButton.Contains(e.Location) && false)
{
if (tc.TabPages[i].Controls.Count != 1) continue;
var redactor = tc.TabPages[i].Controls[0] as IResourceControl;
if (redactor == null) continue;
if (redactor.Saved) RedactorsTabControl.TabPages.RemoveAt(i);
else
{
var result = MessageBox.Show(this, "Save changes before closing?",
"Warning: You have unsaved changes in [" + redactor.ResourceName +
"]!", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
if (result == DialogResult.Yes) redactor.Save(redactor.ResourcePath);
if (result != DialogResult.Cancel) RedactorsTabControl.TabPages.RemoveAt(i);
}
}
if (r.Contains(e.Location)) tc.Tag = tc.TabPages[i];
}
}
catch (Exception ex)
{
var name = "";
if (RedactorsTabControl.SelectedTab != null)
name = RedactorsTabControl.SelectedTab.Text;
if (SelectedRedactor != null) name = SelectedRedactor.ResourceName;
MessageBox.Show(this, ex.ToString(), "Error: Can not close resource [" + name + "].",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void RedactorsTabControl_MouseMove(object sender, MouseEventArgs e)
{
TabControl tc = sender as TabControl;
if ((e.Button != MouseButtons.Left) || (tc.Tag == null)) return;
tc.DoDragDrop(tc.Tag as TabPage, DragDropEffects.All);
}
private void RedactorsTabControl_MouseUp(object sender, MouseEventArgs e)
{
var tc = sender as TabControl;
if (tc != null) tc.Tag = null;
}
private void RedactorsTabControl_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(TabPage))) return;
}
private void RedactorsTabControl_DragOver(object sender, DragEventArgs e)
{
var tc = sender as TabControl;
if (e.Data.GetData(typeof(TabPage)) == null) return;
TabPage dragTab = (TabPage)e.Data.GetData(typeof(TabPage));
int dragTab_index = tc.TabPages.IndexOf(dragTab);
int hoverTab_index = -1;
for (int i = 0; i < tc.TabPages.Count; i++)
{
Rectangle r = tc.GetTabRect(i);
if (r.Contains(tc.PointToClient(new Point(e.X, e.Y)))) hoverTab_index = i;
}
if (hoverTab_index < 0) { e.Effect = DragDropEffects.None; return; }
var hoverTab = tc.TabPages[hoverTab_index];
e.Effect = DragDropEffects.Move;
if (dragTab == hoverTab) return;
Rectangle dragTabRect = tc.GetTabRect(dragTab_index);
Rectangle hoverTabRect = tc.GetTabRect(hoverTab_index);
if (dragTabRect.Width < hoverTabRect.Width)
{
Point tcLocation = tc.PointToScreen(tc.Location);
if (dragTab_index < hoverTab_index)
{
if ((e.X - tcLocation.X) > ((hoverTabRect.X + hoverTabRect.Width) - dragTabRect.Width))
SwapTabPages(tc, dragTab, hoverTab);
}
else if (dragTab_index > hoverTab_index)
{
if ((e.X - tcLocation.X) < (hoverTabRect.X + dragTabRect.Width))
SwapTabPages(tc, dragTab, hoverTab);
}
}
else SwapTabPages(tc, dragTab, hoverTab);
tc.SelectedIndex = tc.TabPages.IndexOf(dragTab);
}
private void SwapTabPages(TabControl tc, TabPage src, TabPage dst)
{
int index_src = tc.TabPages.IndexOf(src);
int index_dst = tc.TabPages.IndexOf(dst);
tc.TabPages[index_dst] = src;
tc.TabPages[index_src] = dst;
tc.Refresh();
}
private void CompilerToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
Visible = false;
using (var Compiler = new CompilerForm("../IDTable.txt"))
Compiler.ShowDialog(this);
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString(), "Error: Can not handle compiler.",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
Visible = true;
}
}
}
}