-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathGumpControl.cs
More file actions
1173 lines (996 loc) · 37.3 KB
/
Copy pathGumpControl.cs
File metadata and controls
1173 lines (996 loc) · 37.3 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
/***************************************************************************
*
* $Author: Turley
*
* "THE BEER-WARE LICENSE"
* As long as you retain this notice you can do whatever you want with
* this stuff. If we meet some day, and you think this stuff is worth it,
* you can buy me a beer in return.
*
***************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using Ultima;
using UoFiddler.Controls.Classes;
using UoFiddler.Controls.Forms;
using UoFiddler.Controls.Helpers;
namespace UoFiddler.Controls.UserControls
{
public partial class GumpControl : UserControl
{
public GumpControl()
{
InitializeComponent();
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint,
true);
if (!Files.CacheData)
{
Preload.Visible = false;
}
ProgressBar.Visible = false;
_refMarker = this;
pictureBox.BackColor = Options.PreviewBackgroundColor;
}
private sealed record GumpEntry(string Name, string[] Tags);
private static GumpControl _refMarker;
private bool _loaded;
private bool _showFreeSlots;
private Dictionary<int, GumpEntry> _gumpEntries = new();
private string _activeNameFilter = string.Empty;
private readonly HashSet<string> _activeTagFilters = new(StringComparer.OrdinalIgnoreCase);
private static readonly string[] _layerTags =
{
"", // 0x00
"one-hand", // 0x01
"two-hand", // 0x02
"boots", // 0x03
"pants", // 0x04
"shirt", // 0x05
"helmet", // 0x06
"gloves", // 0x07
"ring", // 0x08
"talisman", // 0x09
"gorget", // 0x0A
"hair", // 0x0B
"waist", // 0x0C
"chest-armor", // 0x0D
"bracelet", // 0x0E
"", // 0x0F
"facial-hair", // 0x10
"tunic", // 0x11
"earring", // 0x12
"sleeves", // 0x13
"cloak", // 0x14
"backpack", // 0x15
"robe", // 0x16
"skirt", // 0x17
"leg-armor", // 0x18
};
/// <summary>
/// Reload when loaded (file changed)
/// </summary>
private void Reload()
{
if (!_loaded)
{
return;
}
_loaded = false;
OnLoad(EventArgs.Empty);
}
protected override void OnLoad(EventArgs e)
{
if (IsAncestorSiteInDesignMode || FormsDesignerHelper.IsInDesignMode())
{
return;
}
if (_loaded)
{
return;
}
Cursor.Current = Cursors.WaitCursor;
Options.LoadedUltimaClass["Gumps"] = true;
_showFreeSlots = false;
showFreeSlotsToolStripMenuItem.Checked = false;
PopulateListBox(true);
LoadGumpXml();
if (!_loaded)
{
ControlEvents.FilePathChangeEvent += OnFilePathChangeEvent;
ControlEvents.GumpChangeEvent += OnGumpChangeEvent;
ControlEvents.PreviewBackgroundColorChangeEvent += OnPreviewBackgroundColorChanged;
}
_loaded = true;
Cursor.Current = Cursors.Default;
}
private void PopulateListBox(bool showOnlyValid)
{
listBox.BeginUpdate();
listBox.Items.Clear();
bool hasNameFilter = _activeNameFilter.Length > 0;
bool hasTagFilter = _activeTagFilters.Count > 0;
List<object> cache = new List<object>();
for (int i = 0; i < Gumps.GetCount(); ++i)
{
if (showOnlyValid && !Gumps.IsValidIndex(i))
{
continue;
}
if (hasNameFilter || hasTagFilter)
{
// Gumps with no XML entry are hidden only while a filter is active.
// When all filters are cleared every gump reappears as normal.
if (!_gumpEntries.TryGetValue(i, out GumpEntry entry))
{
continue;
}
if (hasNameFilter && !entry.Name.ContainsCaseInsensitive(_activeNameFilter))
{
continue;
}
// AND logic: gump must carry every checked tag
if (hasTagFilter && !_activeTagFilters.All(t => entry.Tags.Contains(t, StringComparer.OrdinalIgnoreCase)))
{
continue;
}
}
cache.Add(i);
}
listBox.Items.AddRange(cache.ToArray());
listBox.EndUpdate();
if (listBox.Items.Count > 0)
{
listBox.SelectedIndex = 0;
}
}
private void LoadGumpXml()
{
_gumpEntries.Clear();
string path = Path.Combine(Options.AppDataPath, "Gumplist.xml");
if (!File.Exists(path))
{
return;
}
try
{
var doc = new XmlDocument();
doc.Load(path);
XmlElement root = doc["Gumps"];
if (root == null)
{
return;
}
int maxId = Gumps.GetCount();
foreach (XmlElement elem in root.SelectNodes("Gump"))
{
string idAttr = elem.GetAttribute("id");
if (!Utils.ConvertStringToInt(idAttr, out int id, 0, maxId) || id >= maxId)
{
continue;
}
string name = elem.GetAttribute("name");
string tagsAttr = elem.GetAttribute("tags");
string[] tags = string.IsNullOrWhiteSpace(tagsAttr)
? Array.Empty<string>()
: tagsAttr.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
_gumpEntries[id] = new GumpEntry(name, tags);
}
}
catch
{
_gumpEntries.Clear();
}
RebuildTagDropdown();
PopulateListBox(!_showFreeSlots);
}
private void RebuildTagDropdown()
{
tagFilterDropDownButton.DropDownItems.Clear();
_activeTagFilters.Clear();
var allTags = new SortedSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (GumpEntry entry in _gumpEntries.Values)
{
foreach (string tag in entry.Tags)
{
if (!string.IsNullOrWhiteSpace(tag))
{
allTags.Add(tag);
}
}
}
tagFilterDropDownButton.Enabled = allTags.Count > 0;
if (allTags.Count == 0)
{
return;
}
var clearItem = new ToolStripMenuItem("Clear All");
clearItem.Click += OnClearTagFilters;
tagFilterDropDownButton.DropDownItems.Add(clearItem);
tagFilterDropDownButton.DropDownItems.Add(new ToolStripSeparator());
foreach (string tag in allTags)
{
var item = new ToolStripMenuItem(tag) { CheckOnClick = true };
item.CheckedChanged += OnTagFilterChanged;
tagFilterDropDownButton.DropDownItems.Add(item);
}
// Keep dropdown open while the user checks/unchecks items
tagFilterDropDownButton.DropDown.Closing -= OnTagDropDownClosing;
tagFilterDropDownButton.DropDown.Closing += OnTagDropDownClosing;
}
private void OnTagDropDownClosing(object sender, ToolStripDropDownClosingEventArgs e)
{
if (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked)
{
e.Cancel = true;
}
}
private void OnClearTagFilters(object sender, EventArgs e)
{
foreach (ToolStripItem item in tagFilterDropDownButton.DropDownItems)
{
if (item is ToolStripMenuItem mi)
{
mi.Checked = false;
}
}
_activeTagFilters.Clear();
PopulateListBox(!_showFreeSlots);
}
private void OnTagFilterChanged(object sender, EventArgs e)
{
_activeTagFilters.Clear();
foreach (ToolStripItem item in tagFilterDropDownButton.DropDownItems)
{
if (item is ToolStripMenuItem { Checked: true } mi)
{
_activeTagFilters.Add(mi.Text);
}
}
PopulateListBox(!_showFreeSlots);
}
private void OnFilePathChangeEvent()
{
Reload();
}
private void OnPreviewBackgroundColorChanged()
{
pictureBox.BackColor = Options.PreviewBackgroundColor;
}
private void OnGumpChangeEvent(object sender, int index)
{
if (!_loaded)
{
return;
}
if (sender.Equals(this))
{
return;
}
if (Gumps.IsValidIndex(index))
{
bool done = false;
for (int i = 0; i < listBox.Items.Count; ++i)
{
int j = int.Parse(listBox.Items[i].ToString());
if (j > index)
{
listBox.Items.Insert(i, index);
listBox.SelectedIndex = i;
done = true;
break;
}
if (j == index)
{
done = true;
break;
}
}
if (!done)
{
listBox.Items.Add(index);
}
}
else
{
for (int i = 0; i < listBox.Items.Count; ++i)
{
int j = int.Parse(listBox.Items[i].ToString());
if (j == index)
{
listBox.Items.RemoveAt(i);
break;
}
}
listBox.Invalidate();
}
}
private void ChangeBackgroundColorToolStripMenuItem_Click(object sender, EventArgs e)
{
if (colorDialog.ShowDialog() != DialogResult.OK)
{
return;
}
Options.PreviewBackgroundColor = colorDialog.Color;
ControlEvents.FirePreviewBackgroundColorChangeEvent();
}
private void ListBox_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index < 0)
{
return;
}
Brush fontBrush = Brushes.Gray;
int i = int.Parse(listBox.Items[e.Index].ToString());
bool hasEntry = _gumpEntries.TryGetValue(i, out GumpEntry entry);
if (Gumps.IsValidIndex(i))
{
Bitmap bmp = Gumps.GetGump(i, out bool patched);
if (bmp != null)
{
int thumbMaxH = e.Bounds.Height - 6;
int width = bmp.Width > 100 ? 100 : bmp.Width;
int height = bmp.Height > thumbMaxH ? thumbMaxH : bmp.Height;
if (listBox.SelectedIndex == e.Index)
{
e.Graphics.FillRectangle(Brushes.LightSteelBlue, e.Bounds.X, e.Bounds.Y, 105, e.Bounds.Height);
}
else if (patched)
{
e.Graphics.FillRectangle(Brushes.LightCoral, e.Bounds.X, e.Bounds.Y, 105, e.Bounds.Height);
}
e.Graphics.DrawImage(bmp, new Rectangle(e.Bounds.X + 3, e.Bounds.Y + 3, width, height));
}
else
{
fontBrush = Brushes.Red;
}
}
else
{
if (listBox.SelectedIndex == e.Index)
{
e.Graphics.FillRectangle(Brushes.LightSteelBlue, e.Bounds.X, e.Bounds.Y, 105, e.Bounds.Height);
}
fontBrush = Brushes.Red;
}
string idText = $"0x{i:X} ({i})";
float idY = hasEntry
? e.Bounds.Y + 4
: e.Bounds.Y + ((e.Bounds.Height / 2f) - (e.Graphics.MeasureString(idText, Font).Height / 2f));
e.Graphics.DrawString(idText, Font, fontBrush, new PointF(105, idY));
if (hasEntry)
{
if (!string.IsNullOrEmpty(entry.Name))
{
e.Graphics.DrawString(entry.Name, Font, fontBrush, new PointF(105, e.Bounds.Y + 22));
}
if (entry.Tags.Length > 0)
{
string tagLine = string.Join(" ", Array.ConvertAll(entry.Tags, t => "#" + t));
using Font smallFont = new Font(Font.FontFamily, Font.Size - 1f);
e.Graphics.DrawString(tagLine, smallFont, Brushes.Gray, new PointF(105, e.Bounds.Y + 42));
}
}
}
private void ListBox_MeasureItem(object sender, MeasureItemEventArgs e)
{
e.ItemHeight = 75;
}
private void ListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox.SelectedIndex == -1)
{
return;
}
int i = int.Parse(listBox.Items[listBox.SelectedIndex].ToString());
pictureBox.BackColor = Options.PreviewBackgroundColor;
if (Gumps.IsValidIndex(i))
{
Bitmap bmp = Gumps.GetGump(i);
if (bmp != null)
{
pictureBox.BackgroundImage = bmp;
IDLabel.Text = $"ID: 0x{i:X} ({i})";
SizeLabel.Text = $"Size: {bmp.Width},{bmp.Height}";
}
else
{
pictureBox.BackgroundImage = null;
}
}
else
{
pictureBox.BackgroundImage = null;
}
listBox.Invalidate();
JumpToMaleFemaleInvalidate();
}
private void JumpToMaleFemaleInvalidate()
{
if (listBox.SelectedIndex == -1)
{
return;
}
int gumpId = (int)listBox.SelectedItem;
if (gumpId >= 50000)
{
if (gumpId >= 60000)
{
jumpToMaleFemale.Text = "Jump to Male";
jumpToMaleFemale.Enabled = HasGumpId(gumpId - 10000);
}
else
{
jumpToMaleFemale.Text = "Jump to Female";
jumpToMaleFemale.Enabled = HasGumpId(gumpId + 10000);
}
}
else
{
jumpToMaleFemale.Enabled = false;
jumpToMaleFemale.Text = "Jump to Male/Female";
}
}
private void OnClickReplace(object sender, EventArgs e)
{
if (listBox.SelectedItems.Count != 1)
{
return;
}
using (OpenFileDialog dialog = new OpenFileDialog())
{
dialog.Multiselect = false;
dialog.Title = "Choose image file to replace";
dialog.CheckFileExists = true;
dialog.Filter = "Image files (*.tif;*.tiff;*.bmp;*.png)|*.tif;*.tiff;*.bmp;*.png";
if (dialog.ShowDialog() != DialogResult.OK)
{
return;
}
using (var bmpTemp = new Bitmap(dialog.FileName))
{
Bitmap bitmap = new Bitmap(bmpTemp);
if (dialog.FileName.Contains(".bmp"))
{
bitmap = Utils.ConvertBmp(bitmap);
}
int i = int.Parse(listBox.Items[listBox.SelectedIndex].ToString());
Gumps.ReplaceGump(i, bitmap);
ControlEvents.FireGumpChangeEvent(this, i);
listBox.Invalidate();
ListBox_SelectedIndexChanged(this, EventArgs.Empty);
Options.ChangedUltimaClass["Gumps"] = true;
}
}
}
private void OnClickSave(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure? Will take a while", "Save", MessageBoxButtons.YesNo,
MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
if (result != DialogResult.Yes)
{
return;
}
Cursor.Current = Cursors.WaitCursor;
ProgressBarDialog barDialog = new ProgressBarDialog(Gumps.GetCount(), "Save");
Gumps.Save(Options.OutputPath);
barDialog.Dispose();
Cursor.Current = Cursors.Default;
Options.ChangedUltimaClass["Gumps"] = false;
FileSavedDialog.Show(FindForm(), Options.OutputPath, "Files saved successfully.");
}
private void OnClickRemove(object sender, EventArgs e)
{
int i = int.Parse(listBox.Items[listBox.SelectedIndex].ToString());
DialogResult result = MessageBox.Show($"Are you sure to remove {i}", "Remove", MessageBoxButtons.YesNo,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (result != DialogResult.Yes)
{
return;
}
Gumps.RemoveGump(i);
ControlEvents.FireGumpChangeEvent(this, i);
if (!_showFreeSlots)
{
listBox.Items.RemoveAt(listBox.SelectedIndex);
}
pictureBox.BackgroundImage = null;
listBox.Invalidate();
Options.ChangedUltimaClass["Gumps"] = true;
}
private void OnClickFindFree(object sender, EventArgs e)
{
int id = int.Parse(listBox.Items[listBox.SelectedIndex].ToString());
++id;
for (int i = listBox.SelectedIndex + 1; i < listBox.Items.Count; ++i, ++id)
{
if (id < int.Parse(listBox.Items[i].ToString()))
{
listBox.SelectedIndex = i;
break;
}
if (!_showFreeSlots)
{
continue;
}
if (!Gumps.IsValidIndex(int.Parse(listBox.Items[i].ToString())))
{
listBox.SelectedIndex = i;
break;
}
}
}
private void OnTextChanged_InsertAt(object sender, EventArgs e)
{
if (Utils.ConvertStringToInt(InsertText.Text, out int index, 0, Gumps.GetCount()))
{
InsertText.ForeColor = Gumps.IsValidIndex(index) ? Color.Red : Color.Black;
}
else
{
InsertText.ForeColor = Color.Red;
}
}
private void OnKeydown_InsertText(object sender, KeyEventArgs e)
{
if (e.KeyCode != Keys.Enter)
{
return;
}
if (!Utils.ConvertStringToInt(InsertText.Text, out int index, 0, Gumps.GetCount()))
{
return;
}
if (Gumps.IsValidIndex(index))
{
return;
}
contextMenuStrip.Close();
using (OpenFileDialog dialog = new OpenFileDialog())
{
dialog.Multiselect = false;
dialog.Title = $"Choose image file to insert at 0x{index:X}";
dialog.CheckFileExists = true;
dialog.Filter = "Image files (*.tif;*.tiff;*.bmp;*.png)|*.tif;*.tiff;*.bmp;*.png";
if (dialog.ShowDialog() != DialogResult.OK)
{
return;
}
using (var bmpTemp = new Bitmap(dialog.FileName))
{
Bitmap bitmap = new Bitmap(bmpTemp);
if (dialog.FileName.Contains(".bmp"))
{
bitmap = Utils.ConvertBmp(bitmap);
}
Gumps.ReplaceGump(index, bitmap);
ControlEvents.FireGumpChangeEvent(this, index);
bool done = false;
for (int i = 0; i < listBox.Items.Count; ++i)
{
int j = int.Parse(listBox.Items[i].ToString());
if (j > index)
{
listBox.Items.Insert(i, index);
listBox.SelectedIndex = i;
done = true;
break;
}
if (!_showFreeSlots)
{
continue;
}
if (j != i)
{
continue;
}
Search(index);
done = true;
break;
}
if (!done)
{
listBox.Items.Add(index);
listBox.SelectedIndex = listBox.Items.Count - 1;
}
Options.ChangedUltimaClass["Gumps"] = true;
}
}
}
private void Extract_Image_ClickBmp(object sender, EventArgs e)
{
int i = int.Parse(listBox.Items[listBox.SelectedIndex].ToString());
ExportGumpImage(i, ImageFormat.Bmp);
}
private void Extract_Image_ClickTiff(object sender, EventArgs e)
{
int i = int.Parse(listBox.Items[listBox.SelectedIndex].ToString());
ExportGumpImage(i, ImageFormat.Tiff);
}
private void Extract_Image_ClickJpg(object sender, EventArgs e)
{
int i = int.Parse(listBox.Items[listBox.SelectedIndex].ToString());
ExportGumpImage(i, ImageFormat.Jpeg);
}
private void Extract_Image_ClickPng(object sender, EventArgs e)
{
int i = int.Parse(listBox.Items[listBox.SelectedIndex].ToString());
ExportGumpImage(i, ImageFormat.Png);
}
private static void ExportGumpImage(int index, ImageFormat imageFormat)
{
string fileExtension = Utils.GetFileExtensionFor(imageFormat);
string fileName = Path.Combine(Options.OutputPath, $"Gump {Utils.FormatExportId(index)}.{fileExtension}");
using (Bitmap bit = new Bitmap(Gumps.GetGump(index)))
{
bit.Save(fileName, imageFormat);
}
MessageBox.Show(
$"Gump saved to {fileName}",
"Saved",
MessageBoxButtons.OK,
MessageBoxIcon.Information,
MessageBoxDefaultButton.Button1);
}
private void OnClick_SaveAllBmp(object sender, EventArgs e)
{
ExportAllGumps(ImageFormat.Bmp);
}
private void OnClick_SaveAllTiff(object sender, EventArgs e)
{
ExportAllGumps(ImageFormat.Tiff);
}
private void OnClick_SaveAllJpg(object sender, EventArgs e)
{
ExportAllGumps(ImageFormat.Jpeg);
}
private void OnClick_SaveAllPng(object sender, EventArgs e)
{
ExportAllGumps(ImageFormat.Png);
}
private void ExportAllGumps(ImageFormat imageFormat)
{
string fileExtension = Utils.GetFileExtensionFor(imageFormat);
using (FolderBrowserDialog dialog = new FolderBrowserDialog())
{
dialog.Description = "Select directory";
dialog.ShowNewFolderButton = true;
if (dialog.ShowDialog() != DialogResult.OK)
{
return;
}
Cursor.Current = Cursors.WaitCursor;
for (int i = 0; i < listBox.Items.Count; ++i)
{
int index = int.Parse(listBox.Items[i].ToString());
if (index < 0)
{
continue;
}
string fileName = Path.Combine(dialog.SelectedPath, $"Gump {Utils.FormatExportId(index)}.{fileExtension}");
var gump = Gumps.GetGump(index);
if (gump is null)
{
continue;
}
using (Bitmap bit = new Bitmap(gump))
{
bit.Save(fileName, imageFormat);
}
}
Cursor.Current = Cursors.Default;
FileSavedDialog.Show(FindForm(), dialog.SelectedPath, "All Gumps saved successfully.");
}
}
private void OnClickShowFreeSlots(object sender, EventArgs e)
{
_showFreeSlots = !_showFreeSlots;
PopulateListBox(!_showFreeSlots);
}
private void OnClickPreLoad(object sender, EventArgs e)
{
if (PreLoader.IsBusy)
{
return;
}
ProgressBar.Minimum = 1;
ProgressBar.Maximum = Gumps.GetCount();
ProgressBar.Step = 1;
ProgressBar.Value = 1;
ProgressBar.Visible = true;
PreLoader.RunWorkerAsync();
}
private void PreLoaderDoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < Gumps.GetCount(); ++i)
{
Gumps.GetGump(i);
PreLoader.ReportProgress(1);
}
}
private void PreLoaderProgressChanged(object sender, ProgressChangedEventArgs e)
{
ProgressBar.PerformStep();
}
private void PreLoaderCompleted(object sender, RunWorkerCompletedEventArgs e)
{
ProgressBar.Visible = false;
}
internal static void Select(int gumpId)
{
if (!_refMarker._loaded)
{
_refMarker.OnLoad(EventArgs.Empty);
}
Search(gumpId);
}
public static bool HasGumpId(int gumpId)
{
if (!_refMarker._loaded)
{
_refMarker.OnLoad(EventArgs.Empty);
}
return _refMarker.listBox.Items.Cast<object>().Any(id => (int)id == gumpId);
}
private void JumpToMaleFemale_Click(object sender, EventArgs e)
{
if (listBox.SelectedIndex == -1)
{
return;
}
int gumpId = (int)listBox.SelectedItem;
gumpId = gumpId < 60000 ? (gumpId % 10000) + 60000 : (gumpId % 10000) + 50000;
Select(gumpId);
}
public static bool Search(int graphic)
{
if (!_refMarker._loaded)
{
_refMarker.OnLoad(EventArgs.Empty);
}
for (int i = 0; i < _refMarker.listBox.Items.Count; ++i)
{
object id = _refMarker.listBox.Items[i];
if ((int)id != graphic)
{
continue;
}
_refMarker.listBox.SelectedIndex = i;
_refMarker.listBox.TopIndex = i;
return true;
}
return false;
}
private void Gump_KeyUp(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.F)
{
searchByIdToolStripTextBox.Focus();
e.SuppressKeyPress = true;
e.Handled = true;
return;
}
if (e.Control && e.KeyCode == Keys.G)
{
searchByNameToolStripTextBox.Focus();
e.SuppressKeyPress = true;
e.Handled = true;
}
}
private void SearchByNameToolStripTextBox_KeyUp(object sender, KeyEventArgs e)
{
_activeNameFilter = searchByNameToolStripTextBox.Text.Trim();
PopulateListBox(!_showFreeSlots);
}
private void InsertStartingFromTb_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode != Keys.Enter)
{
return;
}
if (!Utils.ConvertStringToInt(InsertStartingFromTb.Text, out int index, 0, Gumps.GetCount()))
{
return;
}
contextMenuStrip.Close();
using (OpenFileDialog dialog = new OpenFileDialog())
{
dialog.Multiselect = true;
dialog.Title = $"Choose image file to insert at 0x{index:X}";
dialog.CheckFileExists = true;
dialog.Filter = "Image files (*.tif;*.tiff;*.bmp;*.png)|*.tif;*.tiff;*.bmp;*.png";
if (dialog.ShowDialog() != DialogResult.OK)
{
return;
}
var fileCount = dialog.FileNames.Length;
if (CheckForIndexes(index, fileCount))
{
for (int i = 0; i < fileCount; i++)
{
var currentIdx = index + i;
AddSingleGump(dialog.FileNames[i], currentIdx);
}
Search(index + (fileCount - 1));
}
}
Options.ChangedUltimaClass["Gumps"] = true;
}
/// <summary>
/// Check if all the indexes from baseIndex to baseIndex + count are valid
/// </summary>
/// <param name="baseIndex">Starting Index</param>
/// <param name="count">Number of the indexes to check.</param>
/// <returns></returns>
private static bool CheckForIndexes(int baseIndex, int count)
{
for (int i = baseIndex; i < baseIndex + count; i++)
{
if (i >= Gumps.GetCount() || Gumps.IsValidIndex(i))
{
return false;
}
}
return true;
}
/// <summary>
/// Adds a single Gump.
/// </summary>
/// <param name="fileName">Filename of the gump to add</param>
/// <param name="index">Index where the gump shall be added.</param>
private void AddSingleGump(string fileName, int index)
{