-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
4609 lines (3941 loc) · 180 KB
/
Form1.cs
File metadata and controls
4609 lines (3941 loc) · 180 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using OMI;
using OMI.Formats.Archive;
using OMI.Formats.Pck;
using OMI.Workers.Archive;
using OMI.Workers.Pck;
using System.Windows.Forms;
namespace LegacyConsolePackEditor
{
public partial class Form1 : Form
{
private enum EditorMode
{
None,
Arc,
Pck
}
private enum ImageEditorTool
{
Pencil,
Brush,
Eyedropper,
Eraser,
MovePastedLayer,
Hand
}
private enum ThemeMode
{
Dark,
Light
}
private sealed record ThemePalette(
Color Background,
Color Surface,
Color SurfaceAlt,
Color Panel,
Color Foreground,
Color ForegroundMuted,
Color Border,
Color Accent,
Color AccentText,
Color PreviewBackground,
Color MenuHover,
Color TabInactive,
Color StatusBackground);
private static readonly Dictionary<string, Color> AccentPresets = new(StringComparer.OrdinalIgnoreCase)
{
["Teal"] = Color.FromArgb(46, 183, 163),
["Blue"] = Color.FromArgb(64, 142, 255),
["Green"] = Color.FromArgb(72, 173, 92),
["Red"] = Color.FromArgb(230, 82, 82),
["Orange"] = Color.FromArgb(246, 145, 47),
["Pink"] = Color.FromArgb(230, 94, 156)
};
private const int MaxRecentPaths = 10;
private readonly ARCFileReader _arcReader = new();
private readonly PckFileReader _pckReader = new(ByteOrder.BigEndian);
private readonly Settings _settings;
private ThemeMode _themeMode = ThemeMode.Dark;
private string _accentName = "Teal";
private ThemePalette _theme = null!;
private ToolStripMenuItem? _settingsMenuItem;
private ToolStripMenuItem? _settingsDarkModeItem;
private ToolStripMenuItem? _settingsLightModeItem;
private readonly Dictionary<string, ToolStripMenuItem> _accentMenuItems = new(StringComparer.OrdinalIgnoreCase);
private const string WorkspaceArcRootTag = "__workspace_arc_root__";
private const string WorkspacePcksRootTag = "__workspace_pcks_root__";
private const string WorkspacePckTagPrefix = "__workspace_pck__|";
private ConsoleArchive? _archive;
private string? _archivePath;
private PckFile? _pckFile;
private string? _pckFilePath;
private readonly Dictionary<string, WorkspacePckContext> _workspacePcks = new(StringComparer.OrdinalIgnoreCase);
private string? _workspaceFolderPath;
private string? _currentPckKey;
private EditorMode _mode = EditorMode.None;
private string? _lastSwfArchiveKey;
private string? _lastSwfTempFile;
private DateTime _lastSwfSyncRequestUtc;
private FileSystemWatcher? _pckTempWatcher;
private FileSystemWatcher? _swfTempWatcher;
private SwfBitmapDocument? _activeSwfDocument;
private string? _activeSwfPath;
private string? _activeSwfDisplayName;
private ListView? _listViewSwfAssets;
private PictureBox? _pictureBoxSwfPreview;
private Label? _labelSwfInfo;
private SplitContainer? _swfSplit;
private Panel? _swfPreviewPanel;
private string? _activeTemplateExtractPath;
private bool _activeTemplateDirty;
private Color SurfaceColor => _theme.Surface;
private Color SurfaceAltColor => _theme.SurfaceAlt;
private Color ForegroundColor => _theme.Foreground;
private Color BorderColor => _theme.Border;
private Color AccentColor => _theme.Accent;
private sealed class WorkspacePckContext
{
public required string Path { get; init; }
public required PckFile File { get; init; }
}
private sealed record ArchiveTreePckAsset(PckAsset Asset, string PckPath);
public Form1()
{
_settings = Settings.Load();
_accentName = AccentPresets.ContainsKey(_settings.AccentName ?? string.Empty) ? _settings.AccentName! : "Teal";
_themeMode = string.Equals(_settings.ThemeMode, "Light", StringComparison.OrdinalIgnoreCase)
? ThemeMode.Light
: ThemeMode.Dark;
_theme = BuildThemePalette(_themeMode, AccentPresets[_accentName]);
InitializeComponent();
Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
InitializeSettingsMenu();
ConfigureListAndTreeViews();
ConfigureSwfEditorSurface();
InitializeStudioChrome();
InitializeHomeScreen();
ApplyTheme();
ShowHomeScreen();
embedSwfEditorToolStripMenuItem.Checked = true;
embedSwfEditorToolStripMenuItem.Enabled = false;
RefreshRecentItemsMenu();
RefreshTemplateMenuState();
UpdateWindowTitle(null);
Shown += (s, e) => ShowHomeScreen();
Load += (s, e) => ShowHomeScreen();
}
private void ConfigureListAndTreeViews()
{
listViewPckAssets.View = View.Details;
listViewPckAssets.FullRowSelect = true;
listViewPckAssets.HideSelection = false;
listViewPckAssets.GridLines = false; // use custom separator drawing instead of default white grid lines
listViewPckAssets.MultiSelect = false;
listViewPckAssets.BorderStyle = BorderStyle.None;
listViewPckAssets.BackColor = _theme.Surface;
listViewPckAssets.ForeColor = _theme.Foreground;
if (listViewPckAssets.Columns.Count == 0)
{
listViewPckAssets.Columns.Add("Filename", 420);
listViewPckAssets.Columns.Add("Size", 120, HorizontalAlignment.Right);
listViewPckAssets.Columns.Add("Type", 120);
}
}
private void ConfigureSwfEditorSurface()
{
panelSwfHost.Controls.Clear();
panelSwfHost.Visible = true;
panelSwfHost.BackColor = SurfaceColor;
panelSwfHost.Padding = new Padding(10);
var split = new SplitContainer
{
Dock = DockStyle.Fill,
SplitterDistance = 280,
BackColor = SurfaceColor
};
_swfSplit = split;
_listViewSwfAssets = new ListView
{
Dock = DockStyle.Fill,
View = View.Details,
FullRowSelect = true,
GridLines = true,
MultiSelect = false,
HideSelection = false,
Sorting = SortOrder.Ascending
};
_listViewSwfAssets.Columns.Add("Bitmap", 180);
_listViewSwfAssets.Columns.Add("Size", 90, HorizontalAlignment.Right);
_listViewSwfAssets.Columns.Add("Type", 90);
_listViewSwfAssets.SelectedIndexChanged += listViewSwfAssets_SelectedIndexChanged;
_listViewSwfAssets.MouseDoubleClick += listViewSwfAssets_MouseDoubleClick;
var previewPanel = new Panel
{
Dock = DockStyle.Fill,
BackColor = SurfaceColor
};
_swfPreviewPanel = previewPanel;
_pictureBoxSwfPreview = new PictureBox
{
Dock = DockStyle.Fill,
BackColor = _theme.PreviewBackground,
BorderStyle = BorderStyle.FixedSingle,
SizeMode = PictureBoxSizeMode.Normal
};
_pictureBoxSwfPreview.Paint += PictureBoxSwfPreview_Paint;
_pictureBoxSwfPreview.Resize += (s, e) => _pictureBoxSwfPreview.Invalidate();
_labelSwfInfo = new Label
{
Dock = DockStyle.Bottom,
Height = 44,
Padding = new Padding(8, 6, 8, 6),
TextAlign = ContentAlignment.MiddleLeft,
Text = "Open an SWF to inspect embedded bitmaps.",
BackColor = SurfaceAltColor,
ForeColor = ForegroundColor
};
previewPanel.Controls.Add(_pictureBoxSwfPreview);
previewPanel.Controls.Add(_labelSwfInfo);
split.Panel1.Controls.Add(_listViewSwfAssets);
split.Panel2.Controls.Add(previewPanel);
panelSwfHost.Controls.Add(split);
toolStripButtonSwfCapture.Text = "Edit Bitmap";
toolStripButtonSwfCapture.Enabled = false;
}
private void InitializeStudioChrome()
{
splitContainer1.SplitterWidth = 6;
splitContainerPckRight.SplitterWidth = 6;
treeViewArchive.HideSelection = false;
treeViewArchive.DrawMode = TreeViewDrawMode.OwnerDrawText;
treeViewArchive.DrawNode -= treeViewArchive_DrawNode;
treeViewArchive.DrawNode += treeViewArchive_DrawNode;
ConfigureStudioListView(listViewPckAssets);
if (_listViewSwfAssets != null)
ConfigureStudioListView(_listViewSwfAssets);
}
private void ConfigureStudioListView(ListView list)
{
EnableDoubleBuffering(list);
list.HotTracking = false;
list.OwnerDraw = true;
list.DrawColumnHeader -= StudioList_DrawColumnHeader;
list.DrawItem -= StudioList_DrawItem;
list.DrawSubItem -= StudioList_DrawSubItem;
list.DrawColumnHeader += StudioList_DrawColumnHeader;
list.DrawItem += StudioList_DrawItem;
list.DrawSubItem += StudioList_DrawSubItem;
list.MouseMove -= ListView_MouseMoveInvalidate;
list.MouseLeave -= ListView_MouseLeaveInvalidate;
list.MouseMove += ListView_MouseMoveInvalidate;
list.MouseLeave += ListView_MouseLeaveInvalidate;
}
private static void ListView_MouseMoveInvalidate(object? sender, MouseEventArgs e)
{
if (sender is ListView list)
list.Invalidate();
}
private static void ListView_MouseLeaveInvalidate(object? sender, EventArgs e)
{
if (sender is ListView list)
list.Invalidate();
}
private static void EnableDoubleBuffering(Control control)
{
typeof(Control).GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
?.SetValue(control, true);
}
private void listViewSwfAssets_SelectedIndexChanged(object? sender, EventArgs e)
{
UpdateSwfPreview();
}
private void listViewSwfAssets_MouseDoubleClick(object? sender, MouseEventArgs e)
{
OpenSelectedSwfBitmap();
}
private void UpdateSwfPreview()
{
if (_pictureBoxSwfPreview == null || _labelSwfInfo == null || _listViewSwfAssets == null)
return;
if (_listViewSwfAssets.SelectedItems.Count != 1 || _listViewSwfAssets.SelectedItems[0].Tag is not SwfBitmapEntry entry)
{
_pictureBoxSwfPreview.Image = null;
_labelSwfInfo.Text = string.IsNullOrWhiteSpace(_activeSwfDisplayName)
? "Open an SWF to inspect embedded bitmaps."
: $"{_activeSwfDisplayName}: select an embedded bitmap to preview and edit it.";
toolStripButtonSwfCapture.Enabled = false;
return;
}
_pictureBoxSwfPreview.Image = entry.Bitmap;
_labelSwfInfo.Text = $"Character {entry.CharacterId} • {entry.Name} • {entry.Width}x{entry.Height}";
toolStripButtonSwfCapture.Enabled = true;
}
private void PopulateSwfEditor(SwfBitmapDocument document, string displayName)
{
if (_listViewSwfAssets == null)
return;
_activeSwfDisplayName = displayName;
_listViewSwfAssets.BeginUpdate();
_listViewSwfAssets.Items.Clear();
foreach (SwfBitmapEntry bitmap in document.Bitmaps.OrderBy(item => item.CharacterId))
{
var item = new ListViewItem(bitmap.Name)
{
Tag = bitmap
};
item.SubItems.Add($"{bitmap.Width}x{bitmap.Height}");
item.SubItems.Add(bitmap.Encoding.ToString());
_listViewSwfAssets.Items.Add(item);
}
_listViewSwfAssets.EndUpdate();
if (_listViewSwfAssets.Items.Count > 0)
_listViewSwfAssets.Items[0].Selected = true;
UpdateSwfPreview();
}
private void ClearSwfEditor(string message)
{
_activeSwfDocument?.Dispose();
_activeSwfDocument = null;
_activeSwfPath = null;
_activeSwfDisplayName = null;
if (_listViewSwfAssets != null)
_listViewSwfAssets.Items.Clear();
if (_pictureBoxSwfPreview != null)
_pictureBoxSwfPreview.Image = null;
if (_labelSwfInfo != null)
_labelSwfInfo.Text = message;
toolStripButtonSwfCapture.Enabled = false;
}
private void UpdateWindowTitle(string? fileName)
{
string suffix = "MCLCE Texture Pack Editor";
Text = string.IsNullOrWhiteSpace(fileName) ? suffix : $"{fileName} - {suffix}";
}
private static Font CreateFriendlyFont(float size, FontStyle style = FontStyle.Regular)
{
return new Font("Bahnschrift", size, style, GraphicsUnit.Point);
}
private void InitializeSettingsMenu()
{
_settingsMenuItem = new ToolStripMenuItem("&Settings");
var themeMenu = new ToolStripMenuItem("Theme");
_settingsDarkModeItem = new ToolStripMenuItem("Dark") { CheckOnClick = true };
_settingsLightModeItem = new ToolStripMenuItem("Light") { CheckOnClick = true };
_settingsDarkModeItem.Click += (_, _) => SetThemeMode(ThemeMode.Dark);
_settingsLightModeItem.Click += (_, _) => SetThemeMode(ThemeMode.Light);
themeMenu.DropDownItems.AddRange(new ToolStripItem[] { _settingsDarkModeItem, _settingsLightModeItem });
var accentMenu = new ToolStripMenuItem("Accent Color");
foreach ((string name, _) in AccentPresets)
{
var item = new ToolStripMenuItem(name) { CheckOnClick = true };
item.Click += (_, _) => SetAccent(name);
_accentMenuItems[name] = item;
accentMenu.DropDownItems.Add(item);
}
_settingsMenuItem.DropDownItems.AddRange(new ToolStripItem[] { themeMenu, accentMenu });
int exitIndex = fileToolStripMenuItem.DropDownItems.IndexOf(exitToolStripMenuItem);
if (exitIndex >= 0)
{
fileToolStripMenuItem.DropDownItems.Insert(exitIndex, new ToolStripSeparator());
fileToolStripMenuItem.DropDownItems.Insert(exitIndex, _settingsMenuItem);
}
else
{
fileToolStripMenuItem.DropDownItems.Add(_settingsMenuItem);
}
UpdateThemeMenuChecks();
}
private void SetThemeMode(ThemeMode mode)
{
if (_themeMode == mode)
return;
_themeMode = mode;
_theme = BuildThemePalette(_themeMode, AccentPresets[_accentName]);
_settings.ThemeMode = mode.ToString();
_settings.Save();
UpdateThemeMenuChecks();
ApplyTheme();
}
private void SetAccent(string accentName)
{
if (!AccentPresets.ContainsKey(accentName) || string.Equals(_accentName, accentName, StringComparison.OrdinalIgnoreCase))
return;
_accentName = accentName;
_theme = BuildThemePalette(_themeMode, AccentPresets[_accentName]);
_settings.AccentName = _accentName;
_settings.Save();
UpdateThemeMenuChecks();
ApplyTheme();
}
private void UpdateThemeMenuChecks()
{
if (_settingsDarkModeItem != null)
_settingsDarkModeItem.Checked = _themeMode == ThemeMode.Dark;
if (_settingsLightModeItem != null)
_settingsLightModeItem.Checked = _themeMode == ThemeMode.Light;
foreach ((string name, ToolStripMenuItem item) in _accentMenuItems)
item.Checked = string.Equals(name, _accentName, StringComparison.OrdinalIgnoreCase);
}
private static ThemePalette BuildThemePalette(ThemeMode mode, Color accent)
{
if (mode == ThemeMode.Light)
{
return new ThemePalette(
Background: Color.FromArgb(239, 241, 245),
Surface: Color.FromArgb(252, 253, 255),
SurfaceAlt: Color.FromArgb(232, 236, 243),
Panel: Color.FromArgb(245, 248, 251),
Foreground: Color.FromArgb(34, 39, 50),
ForegroundMuted: Color.FromArgb(92, 101, 117),
Border: Color.FromArgb(189, 199, 214),
Accent: accent,
AccentText: Color.White,
PreviewBackground: Color.FromArgb(245, 248, 251),
MenuHover: ControlPaint.Light(accent, 0.58f),
TabInactive: Color.FromArgb(226, 231, 239),
StatusBackground: Color.FromArgb(228, 234, 244));
}
return new ThemePalette(
Background: Color.FromArgb(23, 25, 30),
Surface: Color.FromArgb(31, 34, 41),
SurfaceAlt: Color.FromArgb(39, 43, 52),
Panel: Color.FromArgb(26, 29, 36),
Foreground: Color.FromArgb(232, 236, 243),
ForegroundMuted: Color.FromArgb(165, 173, 187),
Border: Color.FromArgb(68, 76, 92),
Accent: accent,
AccentText: Color.White,
PreviewBackground: Color.FromArgb(26, 29, 36),
MenuHover: ControlPaint.Light(accent, 0.30f),
TabInactive: Color.FromArgb(34, 37, 45),
StatusBackground: Color.FromArgb(20, 23, 28));
}
private void ApplyTheme()
{
SuspendLayout();
try
{
Font = CreateFriendlyFont(9.25f, FontStyle.Regular);
BackColor = _theme.Background;
ForeColor = _theme.Foreground;
menuStrip1.RenderMode = ToolStripRenderMode.Professional;
menuStrip1.Renderer = new StudioToolStripRenderer(_theme);
menuStrip1.BackColor = _theme.SurfaceAlt;
menuStrip1.ForeColor = _theme.Foreground;
toolStripPck.RenderMode = ToolStripRenderMode.Professional;
toolStripPck.Renderer = new StudioToolStripRenderer(_theme);
toolStripPck.BackColor = _theme.SurfaceAlt;
toolStripPck.ForeColor = _theme.Foreground;
toolStripSwf.RenderMode = ToolStripRenderMode.Professional;
toolStripSwf.Renderer = new StudioToolStripRenderer(_theme);
toolStripSwf.BackColor = _theme.SurfaceAlt;
toolStripSwf.ForeColor = _theme.Foreground;
// Ensure buttons don't show white backgrounds
foreach (ToolStripItem item in toolStripPck.Items)
{
item.BackColor = _theme.SurfaceAlt;
item.ForeColor = _theme.Foreground;
}
foreach (ToolStripItem item in toolStripSwf.Items)
{
item.BackColor = _theme.SurfaceAlt;
item.ForeColor = _theme.Foreground;
}
contextMenuTreeView.RenderMode = ToolStripRenderMode.Professional;
contextMenuTreeView.Renderer = new StudioToolStripRenderer(_theme);
tabPagePck.UseVisualStyleBackColor = false;
tabPageSwfEditor.UseVisualStyleBackColor = false;
tabPagePck.BackColor = _theme.Surface;
tabPageSwfEditor.BackColor = _theme.Surface;
tabControlMain.DrawMode = TabDrawMode.OwnerDrawFixed;
tabControlMain.ItemSize = new Size(148, 30);
tabControlMain.Padding = new Point(16, 4);
tabControlMain.DrawItem -= tabControlMain_DrawItem;
tabControlMain.DrawItem += tabControlMain_DrawItem;
if (_listViewSwfAssets != null)
ConfigureStudioListView(_listViewSwfAssets);
ConfigureStudioListView(listViewPckAssets);
treeViewArchive.Invalidate();
listViewPckAssets.Invalidate();
_listViewSwfAssets?.Invalidate();
ApplyThemeRecursive(this);
panelSwfHost.BackColor = _theme.Panel;
if (_swfSplit != null)
{
_swfSplit.BackColor = _theme.Panel;
_swfSplit.Panel1.BackColor = _theme.Panel;
_swfSplit.Panel2.BackColor = _theme.Panel;
}
if (_swfPreviewPanel != null)
_swfPreviewPanel.BackColor = _theme.Panel;
pictureBoxPckPreview.BackColor = _theme.PreviewBackground;
if (_pictureBoxSwfPreview != null)
_pictureBoxSwfPreview.BackColor = _theme.PreviewBackground;
panelHome.Invalidate();
if (_pictureBoxSwfPreview != null)
_pictureBoxSwfPreview.Invalidate();
pictureBoxPckPreview.Invalidate();
}
finally
{
ResumeLayout(true);
}
}
private void ApplyThemeRecursive(Control root)
{
switch (root)
{
case Form:
root.BackColor = _theme.Background;
root.ForeColor = _theme.Foreground;
break;
case TabPage tabPage:
tabPage.BackColor = _theme.Surface;
tabPage.ForeColor = _theme.Foreground;
break;
case TableLayoutPanel:
root.BackColor = Color.Transparent;
root.ForeColor = _theme.Foreground;
break;
case StatusStrip:
root.BackColor = _theme.StatusBackground;
root.ForeColor = _theme.Foreground;
break;
case MenuStrip:
case ToolStrip:
root.BackColor = _theme.SurfaceAlt;
root.ForeColor = _theme.Foreground;
break;
case Panel:
case SplitContainer:
root.BackColor = _theme.Panel;
root.ForeColor = _theme.Foreground;
break;
case Label:
root.BackColor = root == labelPckInfo || root == _labelSwfInfo ? _theme.SurfaceAlt : Color.Transparent;
root.ForeColor = root == labelHomeMessage ? _theme.ForegroundMuted : _theme.Foreground;
break;
case TreeView treeView:
treeView.BackColor = _theme.Surface;
treeView.ForeColor = _theme.Foreground;
treeView.BorderStyle = BorderStyle.FixedSingle;
treeView.LineColor = _theme.Border;
break;
case ListView listView:
listView.BackColor = _theme.Surface;
listView.ForeColor = _theme.Foreground;
listView.BorderStyle = BorderStyle.FixedSingle;
break;
case PictureBox picture:
if (picture == pictureBoxPckPreview || picture == _pictureBoxSwfPreview)
{
picture.BackColor = _theme.PreviewBackground;
picture.BorderStyle = BorderStyle.FixedSingle;
}
else
{
picture.BackColor = Color.Transparent;
}
break;
}
foreach (Control child in root.Controls)
ApplyThemeRecursive(child);
}
private void tabControlMain_DrawItem(object? sender, DrawItemEventArgs e)
{
if (sender is not TabControl tabs)
return;
bool selected = e.Index == tabs.SelectedIndex;
Rectangle rect = Rectangle.Inflate(e.Bounds, -4, -3);
using var path = CreateRoundedRectPath(rect, 8);
using var bg = new SolidBrush(selected ? _theme.Accent : _theme.TabInactive);
using var border = new Pen(selected ? ControlPaint.Light(_theme.Accent, 0.08f) : _theme.Border);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.FillPath(bg, path);
e.Graphics.DrawPath(border, path);
TextRenderer.DrawText(
e.Graphics,
tabs.TabPages[e.Index].Text,
CreateFriendlyFont(9.25f, selected ? FontStyle.Bold : FontStyle.Regular),
rect,
selected ? _theme.AccentText : _theme.Foreground,
TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis);
}
private void treeViewArchive_DrawNode(object? sender, DrawTreeNodeEventArgs e)
{
if (e.Node == null)
return;
bool selected = (e.State & TreeNodeStates.Selected) == TreeNodeStates.Selected;
Rectangle bounds = new Rectangle(e.Bounds.X - 2, e.Bounds.Y, treeViewArchive.ClientSize.Width - e.Bounds.X + 2, e.Bounds.Height);
Color bg = selected ? _theme.Accent : _theme.Surface;
Color fg = selected ? _theme.AccentText : _theme.Foreground;
using var backgroundBrush = new SolidBrush(bg);
using var textBrush = new SolidBrush(fg);
e.Graphics.FillRectangle(backgroundBrush, bounds);
TextRenderer.DrawText(e.Graphics, e.Node.Text, CreateFriendlyFont(9f, selected ? FontStyle.Bold : FontStyle.Regular), e.Bounds, fg, TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis);
}
private void StudioList_DrawColumnHeader(object? sender, DrawListViewColumnHeaderEventArgs e)
{
using var bg = new SolidBrush(_theme.SurfaceAlt);
using var border = new Pen(_theme.Border);
string headerText = e.Header?.Text ?? string.Empty;
e.Graphics.FillRectangle(bg, e.Bounds);
e.Graphics.DrawLine(border, e.Bounds.Left, e.Bounds.Bottom - 1, e.Bounds.Right, e.Bounds.Bottom - 1);
TextRenderer.DrawText(e.Graphics, headerText, CreateFriendlyFont(9f, FontStyle.Bold), e.Bounds, _theme.Foreground, TextFormatFlags.Left | TextFormatFlags.VerticalCenter | TextFormatFlags.LeftAndRightPadding);
}
private void StudioList_DrawItem(object? sender, DrawListViewItemEventArgs e)
{
bool selected = e.Item.Selected;
bool focused = e.Item.Focused && e.Item.ListView?.Focused == true;
bool odd = (e.ItemIndex % 2) == 1;
Color rowColor = odd ? _theme.Surface : _theme.Panel;
Color bg;
if (!selected)
{
bg = rowColor;
}
else if (focused)
{
bg = _theme.Accent;
}
else
{
bg = ControlPaint.Light(_theme.Surface, 0.1f);
}
using var bgBrush = new SolidBrush(bg);
e.Graphics.FillRectangle(bgBrush, e.Bounds);
using var separator = new Pen(_theme.Border);
e.Graphics.DrawLine(separator, e.Bounds.Left, e.Bounds.Bottom - 1, e.Bounds.Right, e.Bounds.Bottom - 1);
e.DrawDefault = false;
}
private void StudioList_DrawSubItem(object? sender, DrawListViewSubItemEventArgs e)
{
bool selected = e.Item.Selected;
bool focused = e.Item.Focused && e.Item.ListView?.Focused == true;
bool odd = (e.ItemIndex % 2) == 1;
Color rowColor = odd ? _theme.Surface : _theme.Panel;
Color bg;
Color fg = _theme.Foreground;
if (!selected)
{
bg = rowColor;
}
else if (focused)
{
bg = _theme.Accent;
fg = _theme.AccentText;
}
else
{
bg = ControlPaint.Light(_theme.Surface, 0.1f);
}
HorizontalAlignment align = e.Header?.TextAlign ?? HorizontalAlignment.Left;
string subItemText = e.SubItem?.Text ?? string.Empty;
using var bgBrush = new SolidBrush(bg);
e.Graphics.FillRectangle(bgBrush, e.Bounds);
using var separator = new Pen(_theme.Border);
e.Graphics.DrawLine(separator, e.Bounds.Left, e.Bounds.Bottom - 1, e.Bounds.Right, e.Bounds.Bottom - 1);
TextFormatFlags flags = TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis | TextFormatFlags.LeftAndRightPadding;
if (align == HorizontalAlignment.Right)
flags |= TextFormatFlags.Right;
else
flags |= TextFormatFlags.Left;
TextRenderer.DrawText(e.Graphics, subItemText, CreateFriendlyFont(9f, selected ? FontStyle.Bold : FontStyle.Regular), e.Bounds, fg, flags);
e.DrawDefault = false;
}
private static GraphicsPath CreateRoundedRectPath(Rectangle rect, int radius)
{
int diameter = radius * 2;
var path = new GraphicsPath();
path.AddArc(rect.X, rect.Y, diameter, diameter, 180, 90);
path.AddArc(rect.Right - diameter, rect.Y, diameter, diameter, 270, 90);
path.AddArc(rect.Right - diameter, rect.Bottom - diameter, diameter, diameter, 0, 90);
path.AddArc(rect.X, rect.Bottom - diameter, diameter, diameter, 90, 90);
path.CloseFigure();
return path;
}
private sealed class StudioToolStripRenderer : ToolStripProfessionalRenderer
{
private readonly ThemePalette _theme;
public StudioToolStripRenderer(ThemePalette theme) : base(new StudioColorTable(theme))
{
_theme = theme;
RoundedEdges = false;
}
protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
{
e.TextColor = e.Item.Selected ? _theme.AccentText : _theme.Foreground;
base.OnRenderItemText(e);
}
protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)
{
if (e.Item is ToolStripButton btn)
{
Rectangle rect = new Rectangle(Point.Empty, btn.Size);
using var fill = new SolidBrush(btn.Pressed || btn.Selected ? _theme.Accent : _theme.SurfaceAlt);
e.Graphics.FillRectangle(fill, rect);
using var border = new Pen(_theme.Border);
e.Graphics.DrawRectangle(border, 0, 0, rect.Width - 1, rect.Height - 1);
}
else
{
base.OnRenderButtonBackground(e);
}
}
protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
{
using var border = new Pen(_theme.Border);
e.Graphics.DrawLine(border, 0, 0, e.ToolStrip.Width, 0);
}
}
private sealed class StudioColorTable : ProfessionalColorTable
{
private readonly ThemePalette _theme;
public StudioColorTable(ThemePalette theme)
{
_theme = theme;
UseSystemColors = false;
}
public override Color MenuItemSelected => _theme.Accent;
public override Color MenuItemBorder => _theme.Border;
public override Color MenuItemSelectedGradientBegin => _theme.MenuHover;
public override Color MenuItemSelectedGradientEnd => _theme.MenuHover;
public override Color MenuItemPressedGradientBegin => _theme.Accent;
public override Color MenuItemPressedGradientMiddle => _theme.Accent;
public override Color MenuItemPressedGradientEnd => _theme.Accent;
public override Color ToolStripDropDownBackground => _theme.Surface;
public override Color ImageMarginGradientBegin => _theme.Surface;
public override Color ImageMarginGradientMiddle => _theme.Surface;
public override Color ImageMarginGradientEnd => _theme.Surface;
public override Color SeparatorDark => _theme.Border;
public override Color SeparatorLight => _theme.Border;
public override Color ToolStripBorder => _theme.Border;
public override Color MenuBorder => _theme.Border;
public override Color StatusStripGradientBegin => _theme.StatusBackground;
public override Color StatusStripGradientEnd => _theme.StatusBackground;
public override Color ToolStripGradientBegin => _theme.SurfaceAlt;
public override Color ToolStripGradientMiddle => _theme.SurfaceAlt;
public override Color ToolStripGradientEnd => _theme.SurfaceAlt;
public override Color ButtonSelectedHighlight => _theme.Accent;
public override Color ButtonSelectedHighlightBorder => _theme.Accent;
public override Color ButtonPressedGradientBegin => _theme.Accent;
public override Color ButtonPressedGradientMiddle => _theme.Accent;
public override Color ButtonPressedGradientEnd => _theme.Accent;
}
private void InitializeHomeScreen()
{
pictureBoxHomeLogo.Image = null;
panelHome.Paint -= panelHome_Paint;
panelHome.Paint += panelHome_Paint;
bool loaded = false;
try
{
loaded = TryLoadLogoFromResource();
}
catch
{
// Ignore; we'll fall back to disk.
}
if (!loaded)
{
try
{
TryLoadLogoFromDisk();
}
catch
{
// Ignore; home screen can still show without an image.
}
}
}
private bool TryLoadLogoFromResource()
{
try
{
var assembly = typeof(Form1).Assembly;
string? resourceName = assembly.GetManifestResourceNames()
.FirstOrDefault(name => name.EndsWith("minecraft_title.png", StringComparison.OrdinalIgnoreCase));
if (string.IsNullOrEmpty(resourceName))
return false;
using var stream = assembly.GetManifestResourceStream(resourceName);
if (stream == null)
return false;
pictureBoxHomeLogo.Image = Image.FromStream(stream);
return true;
}
catch
{
// Ignore; fall back to disk version if available.
return false;
}
}
private void TryLoadLogoFromDisk()
{
string diskPath = Path.Combine(AppContext.BaseDirectory, "minecraft_title.png");
if (!File.Exists(diskPath))
return;
using var stream = File.OpenRead(diskPath);
pictureBoxHomeLogo.Image = Image.FromStream(stream);
}
private static readonly UTF8Encoding StrictUtf8 = new(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
private void PictureBoxPckPreview_Paint(object? sender, PaintEventArgs e)
{
e.Graphics.Clear(_theme.PreviewBackground);
if (pictureBoxPckPreview.Image == null)
return;
e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
e.Graphics.CompositingQuality = CompositingQuality.HighSpeed;
e.Graphics.SmoothingMode = SmoothingMode.None;
Rectangle destRect = GetScaledImageRectangle(pictureBoxPckPreview.ClientRectangle, pictureBoxPckPreview.Image.Size);
e.Graphics.DrawImage(pictureBoxPckPreview.Image, destRect);
}
private void PictureBoxSwfPreview_Paint(object? sender, PaintEventArgs e)
{
if (_pictureBoxSwfPreview == null)
return;
e.Graphics.Clear(_pictureBoxSwfPreview.BackColor);
if (_pictureBoxSwfPreview.Image == null)
return;
e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
e.Graphics.CompositingQuality = CompositingQuality.HighSpeed;
e.Graphics.SmoothingMode = SmoothingMode.None;
Size imageSize = _pictureBoxSwfPreview.Image.Size;
Rectangle destRect = GetScaledImageRectangle(_pictureBoxSwfPreview.ClientRectangle, imageSize);
e.Graphics.DrawImage(_pictureBoxSwfPreview.Image, destRect);
}
private static Rectangle GetScaledImageRectangle(Rectangle bounds, Size imageSize)
{
if (imageSize.Width <= 0 || imageSize.Height <= 0 || bounds.Width <= 0 || bounds.Height <= 0)
return Rectangle.Empty;
float ratioX = (float)bounds.Width / imageSize.Width;
float ratioY = (float)bounds.Height / imageSize.Height;
float ratio = Math.Min(ratioX, ratioY);
int width = (int)Math.Round(imageSize.Width * ratio);
int height = (int)Math.Round(imageSize.Height * ratio);
int x = bounds.X + (bounds.Width - width) / 2;
int y = bounds.Y + (bounds.Height - height) / 2;
return new Rectangle(x, y, width, height);
}
private static bool WriteUtf8IntoFixedSegment(byte[] buffer, int offset, int capacity, string value)
{
if (offset < 0 || capacity < 0 || offset >= buffer.Length)
return false;
int maxWritable = Math.Min(capacity, buffer.Length - offset);
if (maxWritable <= 0)
return false;
Array.Fill(buffer, (byte)0, offset, maxWritable);
byte[] encoded = Encoding.UTF8.GetBytes(value ?? string.Empty);
int copyLength = Math.Min(encoded.Length, maxWritable);
Buffer.BlockCopy(encoded, 0, buffer, offset, copyLength);
return encoded.Length > maxWritable;
}
private static bool WriteUtf8IntoSegment(byte[] buffer, EditableTextSegment segment, string value)
{
string fullValue = (segment.HiddenPrefix ?? string.Empty) + (value ?? string.Empty);
if (!segment.HasLengthPrefix)
return WriteUtf8IntoFixedSegment(buffer, segment.TextOffset, segment.TextCapacity, fullValue);
if (segment.Offset < 0 || segment.Offset + 1 >= buffer.Length)
return false;
int maxWritable = Math.Min(segment.TextCapacity, buffer.Length - segment.TextOffset);
if (maxWritable <= 0)
return false;