-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainForm.cs
More file actions
801 lines (694 loc) · 29.4 KB
/
MainForm.cs
File metadata and controls
801 lines (694 loc) · 29.4 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
using System.Formats.Tar;
using static MapsDatEditor.Structs;
namespace MapsDatEditor
{
public partial class MainForm : Form
{
// State
private MapsData? _data;
private byte[]? _originalBytes;
private MapEntry? _selectedMap;
private bool _dirty;
// Layout panels
private SplitContainer _splitMain = null!;
private Panel _topBar = null!;
// Left side - map list
private TextBox _searchBox = null!;
private ListBox _mapList = null!;
private Label _statusLabel = null!;
// Right side - detail tabs
private TabControl _tabs = null!;
// Properties tab
private NumericUpDown _nudId = null!, _nudSizeX = null!, _nudSizeY = null!;
private NumericUpDown _nudFlags = null!, _nudMusic = null!;
private TextBox _txtName = null!;
private Button _btnApplyProps = null!;
// Warps tab
private DataGridView _dgvWarps = null!;
private Button _btnAddWarp = null!, _btnDeleteWarp = null!;
// World Map Links tab
private DataGridView _dgvWMLinks = null!;
private Button _btnAddWML = null!, _btnDeleteWML = null!;
// World Maps tab
private ListBox _wmList = null!;
private DataGridView _dgvWMNodes = null!;
// Toolbar buttons
private Button _btnLoad = null!, _btnSave = null!, _btnVerify = null!;
private Button _btnAddMap = null!, _btnDuplicateMap = null!, _btnDeleteMap = null!;
public MainForm()
{
Text = "maps.dat Editor";
Size = new Size(1100, 720);
MinimumSize = new Size(860, 520);
StartPosition = FormStartPosition.CenterScreen;
Font = new Font("Segoe UI", 9f);
BackColor = Color.FromArgb(30, 30, 36);
ForeColor = Color.FromArgb(210, 214, 224);
BuildUI();
UpdateTitle();
}
// ?? UI Construction ??????????????????????????????????????
private void BuildUI()
{
// Top toolbar
_topBar = new Panel
{
Dock = DockStyle.Top,
Height = 44,
BackColor = Color.FromArgb(22, 22, 28),
Padding = new Padding(8, 6, 8, 6)
};
var toolFlow = new FlowLayoutPanel
{
Dock = DockStyle.Fill,
FlowDirection = FlowDirection.LeftToRight,
WrapContents = false,
AutoSize = false
};
_btnLoad = MakeButton("Load", Color.FromArgb(59, 130, 246));
_btnSave = MakeButton("Save", Color.FromArgb(34, 197, 94));
_btnVerify = MakeButton("Verify Roundtrip", Color.FromArgb(217, 119, 6));
_btnAddMap = MakeButton("+ Add Map", Color.FromArgb(34, 197, 94));
_btnDuplicateMap = MakeButton("Duplicate", Color.FromArgb(99, 102, 241));
_btnDeleteMap = MakeButton("Delete Map", Color.FromArgb(239, 68, 68));
_btnLoad.Click += (_, _) => DoLoad();
_btnSave.Click += (_, _) => DoSave();
_btnVerify.Click += (_, _) => DoVerify();
_btnAddMap.Click += (_, _) => DoAddMap();
_btnDuplicateMap.Click += (_, _) => DoDuplicateMap();
_btnDeleteMap.Click += (_, _) => DoDeleteMap();
var sep1 = new Label { Text = " ? ", AutoSize = true, ForeColor = Color.FromArgb(60, 65, 80), Margin = new Padding(2, 5, 2, 0) };
var sep2 = new Label { Text = " ? ", AutoSize = true, ForeColor = Color.FromArgb(60, 65, 80), Margin = new Padding(2, 5, 2, 0) };
toolFlow.Controls.AddRange(new Control[] { _btnLoad, _btnSave, _btnVerify, sep1, _btnAddMap, _btnDuplicateMap, _btnDeleteMap });
_topBar.Controls.Add(toolFlow);
Controls.Add(_topBar);
// Main split
_splitMain = new SplitContainer
{
Dock = DockStyle.Fill,
SplitterDistance = 280,
BackColor = Color.FromArgb(30, 30, 36),
SplitterWidth = 2
};
_splitMain.Panel1.BackColor = Color.FromArgb(24, 26, 33);
_splitMain.Panel2.BackColor = Color.FromArgb(30, 30, 36);
BuildLeftPanel();
BuildRightPanel();
Controls.Add(_splitMain);
_splitMain.BringToFront();
}
private void BuildLeftPanel()
{
var panel = _splitMain.Panel1;
panel.Padding = new Padding(0);
// Search
var searchPanel = new Panel { Dock = DockStyle.Top, Height = 36, Padding = new Padding(6, 6, 6, 4) };
_searchBox = new TextBox
{
Dock = DockStyle.Fill,
BackColor = Color.FromArgb(18, 20, 26),
ForeColor = Color.FromArgb(200, 205, 216),
BorderStyle = BorderStyle.FixedSingle,
Font = new Font("Segoe UI", 9.5f)
};
_searchBox.PlaceholderText = "Search by name or ID...";
_searchBox.TextChanged += (_, _) => FilterMapList();
searchPanel.Controls.Add(_searchBox);
panel.Controls.Add(searchPanel);
// Map list
_mapList = new ListBox
{
Dock = DockStyle.Fill,
BackColor = Color.FromArgb(18, 20, 26),
ForeColor = Color.FromArgb(200, 205, 216),
BorderStyle = BorderStyle.None,
Font = new Font("Consolas", 9f),
IntegralHeight = false
};
_mapList.SelectedIndexChanged += (_, _) => OnMapSelected();
panel.Controls.Add(_mapList);
_mapList.BringToFront();
// Status
_statusLabel = new Label
{
Dock = DockStyle.Bottom,
Height = 24,
BackColor = Color.FromArgb(18, 20, 26),
ForeColor = Color.FromArgb(90, 97, 120),
TextAlign = ContentAlignment.MiddleLeft,
Padding = new Padding(6, 0, 0, 0),
Font = new Font("Segoe UI", 8f)
};
panel.Controls.Add(_statusLabel);
}
private void BuildRightPanel()
{
var panel = _splitMain.Panel2;
_tabs = new TabControl
{
Dock = DockStyle.Fill,
Font = new Font("Segoe UI", 9f)
};
// Properties tab
var propsTab = new TabPage("Properties") { BackColor = Color.FromArgb(30, 30, 36) };
BuildPropertiesTab(propsTab);
_tabs.TabPages.Add(propsTab);
// Warps tab
var warpsTab = new TabPage("Warps") { BackColor = Color.FromArgb(30, 30, 36) };
BuildWarpsTab(warpsTab);
_tabs.TabPages.Add(warpsTab);
// World Map Links tab
var wmlTab = new TabPage("World Map Links") { BackColor = Color.FromArgb(30, 30, 36) };
BuildWMLinksTab(wmlTab);
_tabs.TabPages.Add(wmlTab);
// World Maps tab
var wmTab = new TabPage("World Maps") { BackColor = Color.FromArgb(30, 30, 36) };
BuildWorldMapsTab(wmTab);
_tabs.TabPages.Add(wmTab);
panel.Controls.Add(_tabs);
}
private void BuildPropertiesTab(TabPage tab)
{
var table = new TableLayoutPanel
{
Dock = DockStyle.Top,
AutoSize = true,
ColumnCount = 4,
Padding = new Padding(12)
};
table.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
table.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 160));
table.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
table.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 160));
int row = 0;
// Row 0: ID, Name
table.Controls.Add(MakeLabel("Map ID:"), 0, row);
_nudId = MakeNud(-32768, 32767, 0);
table.Controls.Add(_nudId, 1, row);
table.Controls.Add(MakeLabel("Name:"), 2, row);
_txtName = new TextBox
{
Width = 250,
BackColor = Color.FromArgb(18, 20, 26),
ForeColor = Color.FromArgb(210, 214, 224),
BorderStyle = BorderStyle.FixedSingle,
Font = new Font("Consolas", 9.5f)
};
table.Controls.Add(_txtName, 3, row);
row++;
// Row 1: SizeX, SizeY
table.Controls.Add(MakeLabel("Size X:"), 0, row);
_nudSizeX = MakeNud(0, 255, 0);
table.Controls.Add(_nudSizeX, 1, row);
table.Controls.Add(MakeLabel("Size Y:"), 2, row);
_nudSizeY = MakeNud(0, 255, 0);
table.Controls.Add(_nudSizeY, 3, row);
row++;
// Row 2: Flags, Music
table.Controls.Add(MakeLabel("Flags:"), 0, row);
_nudFlags = MakeNud(0, 255, 0);
table.Controls.Add(_nudFlags, 1, row);
table.Controls.Add(MakeLabel("Music:"), 2, row);
_nudMusic = MakeNud(-128, 127, -1);
table.Controls.Add(_nudMusic, 3, row);
row++;
// Apply button
_btnApplyProps = MakeButton("Apply Changes", Color.FromArgb(34, 197, 94));
_btnApplyProps.Margin = new Padding(0, 12, 0, 0);
_btnApplyProps.Click += (_, _) => ApplyProperties();
table.Controls.Add(_btnApplyProps, 0, row);
table.SetColumnSpan(_btnApplyProps, 2);
tab.Controls.Add(table);
}
private void BuildWarpsTab(TabPage tab)
{
_dgvWarps = MakeGrid();
_dgvWarps.Columns.AddRange(new DataGridViewColumn[]
{
new DataGridViewTextBoxColumn { HeaderText = "Source X", Width = 80, Name = "SrcX" },
new DataGridViewTextBoxColumn { HeaderText = "Source Y", Width = 80, Name = "SrcY" },
new DataGridViewTextBoxColumn { HeaderText = "Target Map", Width = 100, Name = "TgtMap" },
new DataGridViewTextBoxColumn { HeaderText = "Target X", Width = 80, Name = "TgtX" },
new DataGridViewTextBoxColumn { HeaderText = "Target Y", Width = 80, Name = "TgtY" },
});
_dgvWarps.CellEndEdit += DgvWarps_CellEndEdit;
var btnPanel = new FlowLayoutPanel
{
Dock = DockStyle.Bottom,
Height = 40,
FlowDirection = FlowDirection.LeftToRight,
Padding = new Padding(8, 6, 8, 6)
};
_btnAddWarp = MakeButton("+ Add Warp", Color.FromArgb(34, 197, 94));
_btnDeleteWarp = MakeButton("Delete Selected", Color.FromArgb(239, 68, 68));
_btnAddWarp.Click += (_, _) => DoAddWarp();
_btnDeleteWarp.Click += (_, _) => DoDeleteWarp();
btnPanel.Controls.AddRange(new Control[] { _btnAddWarp, _btnDeleteWarp });
tab.Controls.Add(_dgvWarps);
tab.Controls.Add(btnPanel);
}
private void BuildWMLinksTab(TabPage tab)
{
_dgvWMLinks = MakeGrid();
_dgvWMLinks.Columns.AddRange(new DataGridViewColumn[]
{
new DataGridViewTextBoxColumn { HeaderText = "X", Width = 80, Name = "X" },
new DataGridViewTextBoxColumn { HeaderText = "Y", Width = 80, Name = "Y" },
new DataGridViewTextBoxColumn { HeaderText = "Key (hex)", Width = 140, Name = "Key" },
});
_dgvWMLinks.CellEndEdit += DgvWMLinks_CellEndEdit;
var btnPanel = new FlowLayoutPanel
{
Dock = DockStyle.Bottom,
Height = 40,
FlowDirection = FlowDirection.LeftToRight,
Padding = new Padding(8, 6, 8, 6)
};
_btnAddWML = MakeButton("+ Add Link", Color.FromArgb(34, 197, 94));
_btnDeleteWML = MakeButton("Delete Selected", Color.FromArgb(239, 68, 68));
_btnAddWML.Click += (_, _) => DoAddWMLink();
_btnDeleteWML.Click += (_, _) => DoDeleteWMLink();
btnPanel.Controls.AddRange(new Control[] { _btnAddWML, _btnDeleteWML });
tab.Controls.Add(_dgvWMLinks);
tab.Controls.Add(btnPanel);
}
private void BuildWorldMapsTab(TabPage tab)
{
var split = new SplitContainer
{
Dock = DockStyle.Fill,
Orientation = Orientation.Horizontal,
SplitterDistance = 180
};
_wmList = new ListBox
{
Dock = DockStyle.Fill,
BackColor = Color.FromArgb(18, 20, 26),
ForeColor = Color.FromArgb(200, 205, 216),
BorderStyle = BorderStyle.None,
Font = new Font("Consolas", 9f)
};
_wmList.SelectedIndexChanged += (_, _) => OnWorldMapSelected();
split.Panel1.Controls.Add(_wmList);
_dgvWMNodes = MakeGrid();
_dgvWMNodes.ReadOnly = true;
_dgvWMNodes.Columns.AddRange(new DataGridViewColumn[]
{
new DataGridViewTextBoxColumn { HeaderText = "Node Name", Width = 160, Name = "NodeName" },
new DataGridViewTextBoxColumn { HeaderText = "Source X", Width = 80, Name = "SrcX" },
new DataGridViewTextBoxColumn { HeaderText = "Source Y", Width = 80, Name = "SrcY" },
new DataGridViewTextBoxColumn { HeaderText = "Map ID", Width = 80, Name = "MapId" },
new DataGridViewTextBoxColumn { HeaderText = "Target X", Width = 80, Name = "TgtX" },
new DataGridViewTextBoxColumn { HeaderText = "Target Y", Width = 80, Name = "TgtY" },
});
split.Panel2.Controls.Add(_dgvWMNodes);
tab.Controls.Add(split);
}
// ?? UI Helpers ???????????????????????????????????????????
private static Button MakeButton(string text, Color bgColor)
{
return new Button
{
Text = text,
FlatStyle = FlatStyle.Flat,
BackColor = bgColor,
ForeColor = Color.White,
Font = new Font("Segoe UI", 8.5f, FontStyle.Bold),
Height = 28,
AutoSize = true,
Padding = new Padding(8, 0, 8, 0),
Margin = new Padding(2),
Cursor = Cursors.Hand,
FlatAppearance = { BorderSize = 0 }
};
}
private static Label MakeLabel(string text)
{
return new Label
{
Text = text,
AutoSize = true,
ForeColor = Color.FromArgb(140, 146, 168),
Font = new Font("Segoe UI", 9f),
Margin = new Padding(4, 8, 8, 0)
};
}
private static NumericUpDown MakeNud(int min, int max, int val)
{
return new NumericUpDown
{
Minimum = min,
Maximum = max,
Value = Math.Clamp(val, min, max),
Width = 100,
BackColor = Color.FromArgb(18, 20, 26),
ForeColor = Color.FromArgb(210, 214, 224),
BorderStyle = BorderStyle.FixedSingle,
Font = new Font("Consolas", 9.5f)
};
}
private static DataGridView MakeGrid()
{
return new DataGridView
{
Dock = DockStyle.Fill,
BackgroundColor = Color.FromArgb(24, 26, 33),
ForeColor = Color.FromArgb(210, 214, 224),
GridColor = Color.FromArgb(40, 44, 56),
BorderStyle = BorderStyle.None,
CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal,
ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single,
RowHeadersVisible = false,
AllowUserToAddRows = false,
AllowUserToResizeRows = false,
SelectionMode = DataGridViewSelectionMode.FullRowSelect,
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill,
EnableHeadersVisualStyles = false,
DefaultCellStyle = new DataGridViewCellStyle
{
BackColor = Color.FromArgb(24, 26, 33),
ForeColor = Color.FromArgb(210, 214, 224),
SelectionBackColor = Color.FromArgb(50, 55, 75),
SelectionForeColor = Color.White,
Font = new Font("Consolas", 9f)
},
ColumnHeadersDefaultCellStyle = new DataGridViewCellStyle
{
BackColor = Color.FromArgb(18, 20, 26),
ForeColor = Color.FromArgb(140, 146, 168),
Font = new Font("Segoe UI", 8.5f, FontStyle.Bold),
Alignment = DataGridViewContentAlignment.MiddleLeft
}
};
}
// ?? Data Binding ?????????????????????????????????????????
private void FilterMapList()
{
if (_data == null) return;
string q = _searchBox.Text.Trim().ToLower();
_mapList.BeginUpdate();
_mapList.Items.Clear();
foreach (var map in _data.Maps)
{
if (string.IsNullOrEmpty(q)
|| map.Name.ToLower().Contains(q)
|| map.Id.ToString().Contains(q))
{
_mapList.Items.Add(map);
}
}
_mapList.EndUpdate();
_statusLabel.Text = $" {_mapList.Items.Count} / {_data.Maps.Count} maps";
}
private void OnMapSelected()
{
_selectedMap = _mapList.SelectedItem as MapEntry;
if (_selectedMap == null) return;
// Properties
_nudId.Value = _selectedMap.Id;
_txtName.Text = _selectedMap.Name;
_nudSizeX.Value = _selectedMap.SizeX;
_nudSizeY.Value = _selectedMap.SizeY;
_nudFlags.Value = _selectedMap.Flags;
_nudMusic.Value = _selectedMap.Music;
// Warps
RefreshWarpsGrid();
// WM Links
RefreshWMLinksGrid();
// Switch to properties tab if on world maps tab
if (_tabs.SelectedIndex == 3) _tabs.SelectedIndex = 0;
}
private void RefreshWarpsGrid()
{
_dgvWarps.Rows.Clear();
if (_selectedMap == null) return;
foreach (var w in _selectedMap.Warps)
{
_dgvWarps.Rows.Add(w.SourceX, w.SourceY, w.TargetMapId, w.TargetX, w.TargetY);
}
}
private void RefreshWMLinksGrid()
{
_dgvWMLinks.Rows.Clear();
if (_selectedMap == null) return;
foreach (var wl in _selectedMap.WorldMapLinks)
{
_dgvWMLinks.Rows.Add(wl.X, wl.Y, $"0x{wl.Key:X8}");
}
}
private void RefreshWorldMapsList()
{
_wmList.Items.Clear();
if (_data == null) return;
for (int i = 0; i < _data.WorldMaps.Count; i++)
{
var wm = _data.WorldMaps[i];
_wmList.Items.Add($"[{i}] {wm.Name} ({wm.Nodes.Count} nodes)");
}
}
private void OnWorldMapSelected()
{
_dgvWMNodes.Rows.Clear();
if (_data == null || _wmList.SelectedIndex < 0) return;
var wm = _data.WorldMaps[_wmList.SelectedIndex];
foreach (var n in wm.Nodes)
{
_dgvWMNodes.Rows.Add(n.Name, n.SourceX, n.SourceY, n.MapId, n.TargetX, n.TargetY);
}
}
// ?? Actions ??????????????????????????????????????????????
private void DoLoad()
{
using var dlg = new OpenFileDialog
{
Filter = "DAT files (*.dat)|*.dat|All files (*.*)|*.*",
Title = "Open maps.dat"
};
if (dlg.ShowDialog() != DialogResult.OK) return;
try
{
_originalBytes = File.ReadAllBytes(dlg.FileName);
_data = MapsFile.Load(dlg.FileName);
_dirty = false;
FilterMapList();
RefreshWorldMapsList();
UpdateTitle();
MessageBox.Show(
$"Loaded successfully!\n\n" +
$"Header version: {_data.Header}\n" +
$"Maps: {_data.Maps.Count}\n" +
$"World Maps: {_data.WorldMaps.Count}\n" +
$"File size: {_originalBytes.Length:N0} bytes",
"Loaded", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Error loading file:\n{ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void DoSave()
{
if (_data == null) { ShowNoData(); return; }
using var dlg = new SaveFileDialog
{
Filter = "DAT files (*.dat)|*.dat|All files (*.*)|*.*",
Title = "Save maps.dat",
FileName = "maps.dat"
};
if (dlg.ShowDialog() != DialogResult.OK) return;
try
{
MapsFile.Save(dlg.FileName, _data);
var info = new FileInfo(dlg.FileName);
_dirty = false;
UpdateTitle();
MessageBox.Show($"Saved {info.Length:N0} bytes to:\n{dlg.FileName}", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Error saving:\n{ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void DoVerify()
{
if (_data == null || _originalBytes == null) { ShowNoData(); return; }
bool match = MapsFile.Verify(_originalBytes, _data);
if (match)
MessageBox.Show("Roundtrip verified byte-perfect match!\n\nThe serializer produces identical output to the original file.",
"Verification Passed", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
{
byte[] reserialized = MapsFile.Serialize(_data);
MessageBox.Show(
$"Data has been modified since loading.\n\n" +
$"Original: {_originalBytes.Length:N0} bytes\n" +
$"Current: {reserialized.Length:N0} bytes",
"Modified", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void ApplyProperties()
{
if (_selectedMap == null) return;
_selectedMap.Id = (short)_nudId.Value;
_selectedMap.Name = _txtName.Text;
_selectedMap.SizeX = (byte)_nudSizeX.Value;
_selectedMap.SizeY = (byte)_nudSizeY.Value;
_selectedMap.Flags = (byte)_nudFlags.Value;
_selectedMap.Music = (sbyte)_nudMusic.Value;
_dirty = true;
UpdateTitle();
// Refresh the list item display
int idx = _mapList.SelectedIndex;
FilterMapList();
if (idx >= 0 && idx < _mapList.Items.Count) _mapList.SelectedIndex = idx;
}
private void DoAddMap()
{
if (_data == null) { ShowNoData(); return; }
using var dlg = new AddMapDialog();
if (dlg.ShowDialog() != DialogResult.OK) return;
_data.Maps.Add(dlg.NewMap);
_dirty = true;
FilterMapList();
UpdateTitle();
// Select the new map
_mapList.SelectedItem = dlg.NewMap;
}
private void DoDuplicateMap()
{
if (_selectedMap == null) { ShowNoSelection(); return; }
var dup = new MapEntry
{
Id = (short)(_selectedMap.Id + 1),
Name = _selectedMap.Name + " (copy)",
SizeX = _selectedMap.SizeX,
SizeY = _selectedMap.SizeY,
Flags = _selectedMap.Flags,
Music = _selectedMap.Music,
Warps = _selectedMap.Warps.Select(w => new Warp
{
SourceX = w.SourceX,
SourceY = w.SourceY,
TargetMapId = w.TargetMapId,
TargetX = w.TargetX,
TargetY = w.TargetY
}).ToList(),
WorldMapLinks = _selectedMap.WorldMapLinks.Select(wl => new WorldMapLink
{
X = wl.X,
Y = wl.Y,
Key = wl.Key
}).ToList()
};
_data!.Maps.Add(dup);
_dirty = true;
FilterMapList();
_mapList.SelectedItem = dup;
UpdateTitle();
}
private void DoDeleteMap()
{
if (_selectedMap == null || _data == null) { ShowNoSelection(); return; }
var result = MessageBox.Show(
$"Delete map '{_selectedMap.Name}' (ID {_selectedMap.Id})?\n\nThis cannot be undone.",
"Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result != DialogResult.Yes) return;
_data.Maps.Remove(_selectedMap);
_selectedMap = null;
_dirty = true;
FilterMapList();
UpdateTitle();
}
private void DoAddWarp()
{
if (_selectedMap == null) { ShowNoSelection(); return; }
_selectedMap.Warps.Add(new Warp());
_dirty = true;
RefreshWarpsGrid();
UpdateTitle();
}
private void DoDeleteWarp()
{
if (_selectedMap == null || _dgvWarps.SelectedRows.Count == 0) return;
int idx = _dgvWarps.SelectedRows[0].Index;
if (idx >= 0 && idx < _selectedMap.Warps.Count)
{
_selectedMap.Warps.RemoveAt(idx);
_dirty = true;
RefreshWarpsGrid();
UpdateTitle();
}
}
private void DoAddWMLink()
{
if (_selectedMap == null) { ShowNoSelection(); return; }
_selectedMap.WorldMapLinks.Add(new WorldMapLink());
_dirty = true;
RefreshWMLinksGrid();
UpdateTitle();
}
private void DoDeleteWMLink()
{
if (_selectedMap == null || _dgvWMLinks.SelectedRows.Count == 0) return;
int idx = _dgvWMLinks.SelectedRows[0].Index;
if (idx >= 0 && idx < _selectedMap.WorldMapLinks.Count)
{
_selectedMap.WorldMapLinks.RemoveAt(idx);
_dirty = true;
RefreshWMLinksGrid();
UpdateTitle();
}
}
// ?? Grid Cell Editing ????????????????????????????????????
private void DgvWarps_CellEndEdit(object? sender, DataGridViewCellEventArgs e)
{
if (_selectedMap == null || e.RowIndex < 0 || e.RowIndex >= _selectedMap.Warps.Count) return;
var warp = _selectedMap.Warps[e.RowIndex];
var val = _dgvWarps.Rows[e.RowIndex].Cells[e.ColumnIndex].Value?.ToString() ?? "0";
if (!int.TryParse(val, out int n)) return;
switch (e.ColumnIndex)
{
case 0: warp.SourceX = (byte)n; break;
case 1: warp.SourceY = (byte)n; break;
case 2: warp.TargetMapId = (short)n; break;
case 3: warp.TargetX = (byte)n; break;
case 4: warp.TargetY = (byte)n; break;
}
_dirty = true;
UpdateTitle();
}
private void DgvWMLinks_CellEndEdit(object? sender, DataGridViewCellEventArgs e)
{
if (_selectedMap == null || e.RowIndex < 0 || e.RowIndex >= _selectedMap.WorldMapLinks.Count) return;
var link = _selectedMap.WorldMapLinks[e.RowIndex];
var val = _dgvWMLinks.Rows[e.RowIndex].Cells[e.ColumnIndex].Value?.ToString() ?? "0";
switch (e.ColumnIndex)
{
case 0:
if (int.TryParse(val, out int x)) link.X = (byte)x;
break;
case 1:
if (int.TryParse(val, out int y)) link.Y = (byte)y;
break;
case 2:
string hex = val.Replace("0x", "").Replace("0X", "");
if (uint.TryParse(hex, System.Globalization.NumberStyles.HexNumber, null, out uint key))
link.Key = key;
break;
}
_dirty = true;
UpdateTitle();
}
// ?? Misc Helpers ?????????????????????????????????????????
private void UpdateTitle()
{
string mod = _dirty ? " *" : "";
string count = _data != null ? $" ({_data.Maps.Count} maps)" : "";
Text = $"maps.dat Editor{count}{mod}";
}
private static void ShowNoData() =>
MessageBox.Show("No file loaded. Use Load to open a maps.dat file.", "No Data", MessageBoxButtons.OK, MessageBoxIcon.Information);
private static void ShowNoSelection() =>
MessageBox.Show("No map selected.", "No Selection", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}