-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainform.pas
More file actions
6969 lines (5861 loc) · 249 KB
/
mainform.pas
File metadata and controls
6969 lines (5861 loc) · 249 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
unit MainForm;
interface
uses
Winapi.Windows, Winapi.Messages,
System.SysUtils, System.Classes, System.Math, Registry, IOUtils,
clipbrd,
Vcl.Graphics, Vcl.Controls, Vcl.Forms,
Vcl.Dialogs, Vcl.ComCtrls, Vcl.ExtCtrls,
Vcl.StdCtrls, Vcl.NumberBox, Vcl.ExtDlgs,
Vcl.Menus, Vcl.ImgList, Vcl.ImageCollection,
Vcl.VirtualImageList, System.Generics.Collections,
JamGeneral, JamSW, JamHW, JamPalette, JamAnalysis, GeneralHelpers,
newJamDlg,
Vcl.Samples.Spin, Vcl.CheckLst, Vcl.ToolWin, Vcl.BaseImageCollection,
System.ImageList, jampalettedetector, jambatch, options, about;
type
TJamTreeNodeID = (jamDimensions, jamPosX, jamposY, jamFlags, jamColour,
jamUnk, jamID, canvasDimensions, rootNode, jamwidth, jamheight,
jamData, jamPalInfo);
TJamTreeNode = class
data: integer;
jamID: integer;
editNode: boolean;
nodeType: TJamTreeNodeID;
end;
type
TResizeMode = (rmNone, rmMove, rmTopLeft, rmTop, rmTopRight, rmRight,
rmBottomRight, rmBottom, rmBottomLeft, rmLeft);
TDragState = record
Active: Boolean;
Mode: TResizeMode;
StartX, StartY, StartW, StartH: Integer;
StartMouseX, StartMouseY: Integer;
AspectRatio: Double;
end;
PJamTreeNode = ^TJamTreeNode;
TFormMain = class(TForm)
JamTree: TTreeView;
dlgOpenJam: TOpenDialog;
ScrollBox1: TScrollBox;
ImageCanvas: TImage;
dlgSaveJam: TSaveDialog;
canvasPopupMenu: TPopupMenu;
popupImportTexture: TMenuItem;
popupExportTexture: TMenuItem;
importDialog: TOpenPictureDialog;
exportDialog: TSaveDialog;
MainMenu1: TMainMenu;
File1: TMenuItem;
Edit1: TMenuItem;
Image1: TMenuItem;
New1: TMenuItem;
Open1: TMenuItem;
mainMenuClose: TMenuItem;
N1: TMenuItem;
mainMenuSave: TMenuItem;
mainMenuSaveAs: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
Exit1: TMenuItem;
N4: TMenuItem;
Browser1: TMenuItem;
mainMenuAddTexture: TMenuItem;
N5: TMenuItem;
mainMenuCut: TMenuItem;
mainMenuCopy: TMenuItem;
mainMenuPaste: TMenuItem;
N6: TMenuItem;
mainMenuModifyPalette: TMenuItem;
N7: TMenuItem;
mainMenuTextureProperties: TMenuItem;
mainMenuImportTexture: TMenuItem;
mainMenuImportCanvas: TMenuItem;
N8: TMenuItem;
mainMenuExportTexture: TMenuItem;
mainMenuExportCanvas: TMenuItem;
Help1: TMenuItem;
About1: TMenuItem;
ToolBar1: TToolBar;
toolbar_NEW: TToolButton;
toolbar_OPEN: TToolButton;
toolBar_Save: TToolButton;
ToolButton4: TToolButton;
toolBar_AddTexture: TToolButton;
toolBar_DeleteTexture: TToolButton;
ToolButton7: TToolButton;
toolBar_ImportTexture: TToolButton;
toolBar_ExportTexture: TToolButton;
ToolButton10: TToolButton;
toolBar_GP2PAL: TToolButton;
Splitter1: TSplitter;
StatusBar1: TStatusBar;
toolBar_GP3PAL: TToolButton;
ToolButton11: TToolButton;
toolBar_PalPrev: TToolButton;
toolBar_Pal1: TToolButton;
toolBar_Pal2: TToolButton;
toolBar_Pal3: TToolButton;
toolBar_Pal4: TToolButton;
toolBar_PalNext: TToolButton;
toolBarImageList: TVirtualImageList;
ImageCollection1: TImageCollection;
leftPanel: TPanel;
timer_JamRedrawPals: TTimer;
timer_redrawTree: TTimer;
N9: TMenuItem;
popUpAddTexture: TMenuItem;
N10: TMenuItem;
popUpDeleteTexture: TMenuItem;
N11: TMenuItem;
mainMenuDeleteTexture: TMenuItem;
mnuRecentFiles: TMenuItem;
ools1: TMenuItem;
BatchConvert1: TMenuItem;
View1: TMenuItem;
View2: TMenuItem;
N12: TMenuItem;
ResetZoom1: TMenuItem;
N13: TMenuItem;
menuDrawOutlines: TMenuItem;
menuSnap: TMenuItem;
menuCheckerboard: TMenuItem;
menuDPIScale: TMenuItem;
treeimagecollection: TImageCollection;
treeimagelist: TVirtualImageList;
toolbar_drawOutlines: TToolButton;
toolbarSnap: TToolButton;
toolbarCheckerboard: TToolButton;
ToolButton12: TToolButton;
toolbarBatch: TToolButton;
toolbarAutoPack: TToolButton;
toolbarJamAnalysis: TToolButton;
ToolButton5: TToolButton;
toolbarZoomIN: TToolButton;
ToolButton6: TToolButton;
toolbarZoomOUT: TToolButton;
toolbarZoomReset: TToolButton;
toolbarMove: TToolButton;
ToolButton8: TToolButton;
N14: TMenuItem;
Undo1: TMenuItem;
Redo1: TMenuItem;
N15: TMenuItem;
PreviewMask1: TMenuItem;
N16: TMenuItem;
ConverttoGP2JAM: TMenuItem;
ConverttoGP3SWJAM: TMenuItem;
ConverttoGP3HWJAM: TMenuItem;
panel_TexProperties_Generic: TPanel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
tex_height: TSpinEdit;
tex_ID: TSpinEdit;
tex_width: TSpinEdit;
tex_X: TSpinEdit;
tex_Y: TSpinEdit;
panel_flags: TPanel;
tex_flags: TCheckListBox;
chkShowAdvancedFlags: TCheckBox;
panel_TexPreview: TPanel;
ImageEntry: TImage;
panel_PalPreview: TPanel;
PaintBoxPalette: TPaintBox;
panel_PaletteEdit: TPanel;
Label2: TLabel;
Label1: TLabel;
Label3: TLabel;
comboSimpMethod: TComboBox;
chkBoxSimpPal: TCheckBox;
chkBoxTrans: TCheckBox;
numBox_BlurAmount: TNumberBox;
numBox_SimpThresh: TNumberBox;
btnRemovePal: TButton;
btnPal0: TButton;
btnPal1: TButton;
btnPal2: TButton;
btnPal3: TButton;
btnRemoveAllPals: TButton;
btnGenPal: TButton;
btnRegenAllPals: TButton;
panel_RCR: TPanel;
Label11: TLabel;
rcrOdd: TButton;
rcrEven: TButton;
rcrReset: TButton;
panel_TexScaling: TPanel;
Label13: TLabel;
Label14: TLabel;
Label15: TLabel;
scaleFlags: TCheckListBox;
texScaleX: TSpinEdit;
texScaleY: TSpinEdit;
texScale: TSpinEdit;
canvasHeight: TSpinEdit;
Button5: TButton;
panelCanvasBar: TPanel;
lblCanvasPrefix: TLabel;
lblCanvasSize: TLabel;
lblCanvasHeightLbl: TLabel;
lblCanvasZoom: TLabel;
CategoryPanelGroup1: TCategoryPanelGroup;
panel_TexturePreview: TCategoryPanel;
Panel_TexProperties: TCategoryPanel;
panel_TexFlags: TCategoryPanel;
panel_ScaleParameters: TCategoryPanel;
panel_PalEdit: TCategoryPanel;
panel_RCRControls: TCategoryPanel;
N17: TMenuItem;
JamAnalysis1: TMenuItem;
SaveDecryptedJAM: TMenuItem;
N18: TMenuItem;
autoPackTexs: TMenuItem;
undoTimer: TTimer;
timerZoom: TTimer;
Button1: TButton;
Button2: TButton;
procedure JamTreeChange(Sender: TObject; Node: TTreeNode);
procedure PaintBoxPalettePaint(Sender: TObject);
procedure btnLoadJamClick(Sender: TObject);
procedure btnGP2palClick(Sender: TObject);
procedure btnGP3palClick(Sender: TObject);
procedure btnPal0Click(Sender: TObject);
procedure btnPal1Click(Sender: TObject);
procedure btnPal2Click(Sender: TObject);
procedure btnPal3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btnDrawDataClick(Sender: TObject);
procedure ImageCanvasMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: integer);
procedure btnSaveJamClick(Sender: TObject);
procedure ImportTexture(Sender: TObject);
procedure btnExportTextureClick(Sender: TObject);
procedure toolBar_PalPrevClick(Sender: TObject);
procedure toolBar_PalNextClick(Sender: TObject);
procedure numBox_SimpThreshChange(Sender: TObject);
procedure numBox_BlurAmountChange(Sender: TObject);
procedure btnGenPalClick(Sender: TObject);
procedure btnRemovePalClick(Sender: TObject);
procedure Browser1Click(Sender: TObject);
procedure Splitter2Paint(Sender: TObject);
procedure tex_flagsClickCheck(Sender: TObject);
procedure chkShowAdvancedFlagsClick(Sender: TObject);
procedure RebuildFlagsList;
procedure tex_IDChange(Sender: TObject);
procedure tex_XChange(Sender: TObject);
procedure tex_YChange(Sender: TObject);
procedure tex_widthChange(Sender: TObject);
procedure tex_heightChange(Sender: TObject);
procedure FormMouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: integer; MousePos: TPoint; var Handled: boolean);
procedure ScrollBox1Click(Sender: TObject);
procedure rcrOddClick(Sender: TObject);
procedure rcrEvenClick(Sender: TObject);
procedure rcrResetClick(Sender: TObject);
procedure AddNewTexture(Sender: TObject);
procedure JamTreeDeletion(Sender: TObject; Node: TTreeNode);
procedure JamTreeAdvancedCustomDrawItem(Sender: TCustomTreeView;
Node: TTreeNode; State: TCustomDrawState;
Stage: TCustomDrawStage;
var PaintImages, DefaultDraw: boolean);
procedure mainMenuAddTextureClick(Sender: TObject);
procedure mainMenuImportTextureClick(Sender: TObject);
procedure timer_JamRedrawPalsTimer(Sender: TObject);
procedure timer_redrawTreeTimer(Sender: TObject);
procedure toolbar_NEWClick(Sender: TObject);
procedure popUpDeleteTextureClick(Sender: TObject);
procedure mainMenuExportCanvasClick(Sender: TObject);
procedure canvasPopupMenuPopup(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure BatchConvert1Click(Sender: TObject);
procedure View2Click(Sender: TObject);
procedure ResetZoom1Click(Sender: TObject);
procedure btnRemoveAllPalsClick(Sender: TObject);
procedure FormCloseQuery(Sender: TObject;
var CanClose: boolean);
procedure btnRegenAllPalsClick(Sender: TObject);
procedure Exit1Click(Sender: TObject);
procedure toolbar_drawOutlinesClick(Sender: TObject);
procedure toolbarMoveClick(Sender: TObject);
procedure toolbarSnapClick(Sender: TObject);
procedure menuSnapClick(Sender: TObject);
procedure toolbarCheckerboardClick(Sender: TObject);
procedure menuCheckerboardClick(Sender: TObject);
procedure menuDPIScaleClick(Sender: TObject);
procedure ImageCanvasMouseMove(Sender: TObject;
Shift: TShiftState; X, Y: integer);
procedure ImageCanvasDblClick(Sender: TObject);
procedure timerZoomTimer(Sender: TObject);
procedure toolBar_GP3PALMouseUp(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: integer);
procedure toolBar_GP2PALMouseUp(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: integer);
procedure toolbarZoomINClick(Sender: TObject);
procedure toolbarZoomResetClick(Sender: TObject);
procedure toolbarZoomOUTClick(Sender: TObject);
procedure menuDrawOutlinesClick(Sender: TObject);
procedure ShowHintInStatusBar(Sender: TObject);
procedure ImageCanvasMouseUp(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: integer);
procedure mainMenuSaveAsClick(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure FormKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure tex_XKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure tex_XKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure mainMenuCopyClick(Sender: TObject);
procedure mainMenuPasteClick(Sender: TObject);
procedure MainMenu1Change(Sender: TObject; Source: TMenuItem;
Rebuild: boolean);
procedure JamTreeMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: integer);
procedure tex_IDKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure tex_YKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure tex_widthKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure tex_heightKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure tex_IDKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure tex_YKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure tex_widthKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure tex_heightKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure mainMenuCutClick(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure mainMenuDeleteTextureClick(Sender: TObject);
procedure Undo1Click(Sender: TObject);
procedure Redo1Click(Sender: TObject);
procedure PreviewMask1Click(Sender: TObject);
procedure ConverttoGP2JAMClick(Sender: TObject);
procedure ConverttoGP3SWJAMClick(Sender: TObject);
procedure ConverttoGP3HWJAMClick(Sender: TObject);
procedure scaleFlagsClickCheck(Sender: TObject);
procedure texScaleXChange(Sender: TObject);
procedure texScaleYChange(Sender: TObject);
procedure texScaleChange(Sender: TObject);
procedure texScaleKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure texScaleXKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure texScaleYKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure texScaleKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure texScaleXKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure texScaleYKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure Panel_TexPropertiesCollapse(Sender: TObject);
procedure Panel_TexPropertiesExpand(Sender: TObject);
procedure panel_TexFlagsCollapse(Sender: TObject);
procedure panel_PalEditCollapse(Sender: TObject);
procedure panel_PalEditExpand(Sender: TObject);
procedure panel_RCRControlsCollapse(Sender: TObject);
procedure panel_RCRControlsExpand(Sender: TObject);
procedure panel_ScaleParametersCollapse(Sender: TObject);
procedure panel_ScaleParametersExpand(Sender: TObject);
procedure panel_TexFlagsExpand(Sender: TObject);
procedure panel_TexturePreviewExpand(Sender: TObject);
procedure panel_TexturePreviewCollapse(Sender: TObject);
procedure JamAnalysis1Click(Sender: TObject);
procedure SaveDecryptedJAMClick(Sender: TObject);
procedure mainMenuImportCanvasClick(Sender: TObject);
procedure autoPackTexsClick(Sender: TObject);
procedure undoTimerTimer(Sender: TObject);
procedure tex_XMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: integer);
procedure tex_XMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: integer);
procedure canvasHeightChange(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure About1Click(Sender: TObject);
// Form-level WM_MOVE handler — fires in the form's own WndProc
// so it is guaranteed to run whenever this window is repositioned,
// regardless of DPI-awareness mode or Application.OnMessage quirks.
procedure WMFormMove(var Msg: TMessage); message WM_MOVE;
public
FJamFile: TJamFile;
FHWJamFile: THWJamFile;
UpdatingFromCode: boolean;
SelectFromCanvas: boolean;
MRUList: TStringList;
procedure CopyJAM(Entries: TList<TjamEntry>);
function PasteJam: TList<TjamEntry>;
procedure CopyHWJAM(Entries: TList<THWjamEntry>);
function PasteHWJam: TList<THWjamEntry>;
procedure Copy();
procedure DoUndo();
procedure PushUndoState();
procedure RefreshCanvas;
procedure RefreshPalette();
procedure DrawTexture();
procedure PalChange(palID: integer);
procedure JamReGen;
procedure TreeReDraw;
procedure DrawTree;
procedure SelectTexture(id: integer; treeupdate: boolean);
procedure DeSelectTexture();
procedure NewJam(filename: string; height: integer);
procedure LoadJam(filename: string);
procedure UpdateJamData(id: integer);
procedure SelectTreeTex();
procedure ClearPaletteImg();
procedure LoadMRU;
procedure SaveMRU;
procedure UpdateMRU;
procedure AddToMRU(const filename: string);
procedure RecentFileClick(Sender: TObject);
procedure CheckJamModified();
procedure UISetup();
procedure UpdateCaption();
procedure UpdateUIData(id: integer);
procedure ConvertJAM(output: TJamType);
procedure DeleteTexture();
procedure JamModified(modified: boolean);
function JamSanityCheck(): boolean;
function ScreenToCanvas(const P: TPoint): TPoint;
function HitTestTextureZone(MouseX, MouseY: integer;
const R: TRect): TResizeMode;
function GetSelectedTextureScreenRect: TRect;
procedure ApplyTextureTransform(newX, newY, newW, newH: integer);
procedure UpdateCursorForHitZone(X, Y: integer);
procedure DeactivateMoveTool;
procedure CaptureDragBackground;
procedure ReleaseDragBackground;
procedure DrawDragPreview;
procedure DrawSnapGuidesOverlay;
procedure RestoreBackgroundRect(bmp: TBitmap;
const R: TRect);
procedure InvalidateDragRegion;
procedure StartZoomTo(NewZoom: Double);
procedure ApplySnap(Mode: TResizeMode;
var newX, newY, newW, newH: integer);
procedure ToggleSnap;
procedure ToggleCheckerboard;
procedure ToggleDPIScaling;
procedure ComposeCheckerboard(var bmp: TBitmap; keyColor: TColor);
procedure ApplyOutlineOverlay(bmp: TBitmap);
procedure CaptureOriginalSelectedState;
procedure RestoreOriginalSelectedState;
procedure genMask();
procedure JamSanityCheckInform(onSave: boolean);
function AddJamTreeNode(Tree: TTreeView; Parent: TTreeNode;
const Caption: string; data: integer; jamID: integer;
editNode: boolean; nodeType: TJamTreeNodeID): TTreeNode;
private
FCheckerPattern: TBitmap;
FCheckerBrush: HBRUSH;
// Tracks which monitor the window is currently on so WM_MOVE
// only triggers a DPI refresh when the window crosses a screen
// boundary rather than on every pixel of drag movement.
FCurrentMonitorHandle: HMONITOR;
procedure MsgHandler(var Msg: TMsg; var Handled: boolean);
function CheckerBrush: HBRUSH;
// Queries the physical (raw) DPI of hMon via GetDpiForMonitor
// (shcore.dll, Win8.1+), falls back to Screen.PixelsPerInch.
// If the computed factor differs from the current dblDPIFactor
// and the DPI-scale toggle is active, the canvas is refreshed.
procedure RefreshDPIFactor(hMon: HMONITOR);
// Per-frame scroll correction to keep a canvas point pinned
// under the mouse (or screen centre) while zoom animates.
procedure ApplyZoomAnchorScroll;
// Sets the zoom anchor to the centre of the visible ScrollBox
// area — used by the toolbar +/- zoom buttons.
procedure SetZoomAnchorToCenter;
public
end;
var
// change to TQueue
SWUndoStack, SWRedoStack: TStack<TJamFile>;
HWUndoStack, HWRedoStack: TStack<THWJamFile>;
FormMain: TFormMain;
boolLCtrl: boolean;
SelectedTreeNodes: TList<TTreeNode>;
FMoveToolActive: Boolean;
FDrag: TDragState;
FDragBackground: TBitmap; // cached scaled canvas with moving tex hidden
FDragTextureCache: TBitmap; // borrowed reference to moving texture's bitmap
FTargetZoom: Double; // smooth-zoom target
// Space-bar pan state
FSpacePanArmed: Boolean; // space held → ready to pan
FPanActive: Boolean; // mouse down during pan → dragging
FPanStartMouse: TPoint; // screen mouse pos at pan start
FPanStartScrollX: Integer; // ScrollBox scroll pos at pan start
FPanStartScrollY: Integer;
// Snap state (updated per drag frame)
FSnapXActive: Boolean;
FSnapYActive: Boolean;
FSnapXLine: Integer; // canvas X where vertical guide should draw
FSnapYLine: Integer; // canvas Y where horizontal guide should draw
// Dirty-rect drag preview state — track previous frame's damaged
// areas so we can restore only the affected pixels each frame
// instead of copying the whole 32MB bitmap at high zoom.
FDragPrevTexRect: TRect; // screen rect of texture last frame
FPrevSnapXActive: Boolean;
FPrevSnapYActive: Boolean;
FPrevSnapXLineScreen: Integer; // screen X of last guide
FPrevSnapYLineScreen: Integer; // screen Y of last guide
FDragDirtyRect: TRect; // union of this frame's dirty regions
// Zoom-anchor scroll state — keeps a specific canvas point pinned to
// a specific screen position while the zoom animation runs.
// FZoomAnchorRelX/Y : raw-canvas pixel that should stay fixed
// = (screenPos - ImgLeft) / intJamZoom_at_start
// FZoomAnchorScreenX/Y: ScrollBox-client pixel it must stay at
// FZoomHasAnchor : false until set by FormMouseWheel / toolbar
FZoomAnchorRelX: Double;
FZoomAnchorRelY: Double;
FZoomAnchorScreenX: Integer;
FZoomAnchorScreenY: Integer;
FZoomHasAnchor: Boolean;
// High-resolution drag-rate limiter (QueryPerformanceCounter).
// FLastDragQPC — QPF timestamp of the last accepted drag frame.
// FQPFFreq — QueryPerformanceFrequency result (counts / second).
// FDragFrameInterval — QPF counts between allowed renders; set from
// the monitor's actual refresh rate so we never
// submit frames faster than the display can show.
FLastDragQPC: Int64;
FQPFFreq: Int64;
FDragFrameInterval: Int64;
// "Original" texture state captured when a texture is first
// selected in move mode. Escape restores this state (like Photoshop
// cancelling a Transform operation).
FOriginalSelectedIdx: Integer;
FOriginalSelectedX: Integer;
FOriginalSelectedY: Integer;
FOriginalSelectedW: Integer;
FOriginalSelectedH: Integer;
userisTyping: boolean = false;
boolFlagChange: boolean;
boolXChange: boolean;
boolYChange: boolean;
boolWidthChange: boolean;
boolHeightChange: boolean;
boolIDChange: boolean;
boolScaleXChange: boolean;
boolScaleYChange: boolean;
boolScaleChange: boolean;
boolScaleFlagsChange: boolean;
boolCanvasChange: boolean;
implementation
{$R *.dfm}
uses JamBrowser;
procedure ForceCategoryPanelRedraw(CPG: TCategoryPanelGroup);
var
i: integer;
panel: TCategoryPanel;
begin
CPG.DisableAlign;
try
CPG.Realign;
CPG.Invalidate;
CPG.Update;
finally
CPG.EnableAlign;
end;
begin
for i := 0 to CPG.ControlCount - 1 do
begin
if CPG.Controls[i] is TCategoryPanel then
begin
panel := TCategoryPanel(CPG.Controls[i]);
RedrawWindow(panel.Handle, nil, 0,
RDW_INVALIDATE or RDW_ERASE or RDW_FRAME or
RDW_ALLCHILDREN);
end;
end;
RedrawWindow(CPG.Handle, nil, 0, RDW_INVALIDATE or RDW_ERASE or
RDW_FRAME or RDW_ALLCHILDREN);
end;
end;
procedure SaveExpandedNodes(Tree: TTreeView; ExpandedPaths: TStrings;
out SelectedPath: string);
var
Node: TTreeNode;
function GetNodePath(Node: TTreeNode): string;
begin
Result := '';
while Node <> nil do
begin
Result := Node.Text + '/' + Result;
Node := Node.Parent;
end;
Result := '/' + Result;
end;
begin
ExpandedPaths.Clear;
Node := Tree.Items.GetFirstNode;
while Node <> nil do
begin
if Node.Expanded then
ExpandedPaths.Add(GetNodePath(Node));
Node := Node.GetNext;
end;
if Assigned(Tree.Selected) then
SelectedPath := GetNodePath(Tree.Selected)
else
SelectedPath := '';
end;
procedure RestoreExpandedNodes(Tree: TTreeView; ExpandedPaths: TStrings;
const SelectedPath: string);
var
Node: TTreeNode;
function FindNodeByPath(Path: string): TTreeNode;
var
Parts: TArray<string>;
i: integer;
Current: TTreeNode;
begin
Result := nil;
if Path = '' then
Exit;
Parts := Path.Split(['/'], TStringSplitOptions.ExcludeEmpty);
Current := nil;
for i := 0 to High(Parts) do
begin
Result := nil;
if Current = nil then
Current := Tree.Items.GetFirstNode
else
Current := Current.getFirstChild;
while Current <> nil do
begin
if SameText(Current.Text, Parts[i]) then
begin
Result := Current;
Break;
end;
Current := Current.getNextSibling;
end;
if Result = nil then
Exit;
Current := Result;
end;
Result := Current;
end;
var
i: integer;
NodePath: string;
begin
for i := 0 to ExpandedPaths.Count - 1 do
begin
NodePath := ExpandedPaths[i];
Node := FindNodeByPath(NodePath);
if Assigned(Node) then
Node.Expand(false);
end;
Node := FindNodeByPath(SelectedPath);
if Assigned(Node) then
Tree.Selected := Node;
end;
function TFormMain.ScreenToCanvas(const P: TPoint): TPoint;
var
ez: Double;
begin
ez := EffectiveJamZoom;
if ez = 0 then
Result := P
else
begin
Result.X := Round(P.X / ez);
Result.Y := Round(P.Y / ez);
end;
end;
function TFormMain.HitTestTextureZone(MouseX, MouseY: integer;
const R: TRect): TResizeMode;
// Returns which zone the mouse is in for the selected texture's screen
// rect R. Handles are padded OUTWARD by PADDING pixels (forgiveness for
// lazy clicks) and extend INWARD up to HANDLE pixels. Body = inside R.
// Outside the outer bounds (R + PADDING) returns rmNone so cursor resets.
const
HANDLE = 8; // how far inward the corner/edge grab extends
PADDING = 6; // how far outward the corner/edge grab extends
var
W, H, grabW, grabH: integer;
outerL, outerT, outerR, outerB: integer;
innerNorth, innerSouth, innerWest, innerEast: integer;
begin
Result := rmNone;
W := R.Right - R.Left;
H := R.Bottom - R.Top;
if (W <= 0) or (H <= 0) then Exit;
// Tiny textures: shrink the inward grab so corners don't overlap.
grabW := Min(HANDLE, W div 2);
grabH := Min(HANDLE, H div 2);
// Outer bounds = texture rect extended by PADDING. Outside this,
// nothing to grab.
outerL := R.Left - PADDING;
outerT := R.Top - PADDING;
outerR := R.Right + PADDING;
outerB := R.Bottom + PADDING;
if (MouseX < outerL) or (MouseX > outerR) or
(MouseY < outerT) or (MouseY > outerB) then
Exit;
// Corner zones: a rectangle anchored at the corner extending
// PADDING outward and grabW/grabH inward.
if (MouseX <= R.Left + grabW) and (MouseY <= R.Top + grabH) then
Exit(rmTopLeft);
if (MouseX >= R.Right - grabW) and (MouseY <= R.Top + grabH) then
Exit(rmTopRight);
if (MouseX >= R.Right - grabW) and (MouseY >= R.Bottom - grabH) then
Exit(rmBottomRight);
if (MouseX <= R.Left + grabW) and (MouseY >= R.Bottom - grabH) then
Exit(rmBottomLeft);
// Edges — only if the texture has a middle region between corners.
// (innerNorth, innerSouth, innerWest, innerEast) define the body
// area inside all four corner zones.
innerNorth := R.Top + grabH;
innerSouth := R.Bottom - grabH;
innerWest := R.Left + grabW;
innerEast := R.Right - grabW;
if (innerEast > innerWest) and (innerSouth > innerNorth) then
begin
// Top edge strip
if (MouseY <= R.Top + HANDLE) and
(MouseX > innerWest) and (MouseX < innerEast) then
Exit(rmTop);
// Bottom edge strip
if (MouseY >= R.Bottom - HANDLE) and
(MouseX > innerWest) and (MouseX < innerEast) then
Exit(rmBottom);
// Left edge strip
if (MouseX <= R.Left + HANDLE) and
(MouseY > innerNorth) and (MouseY < innerSouth) then
Exit(rmLeft);
// Right edge strip
if (MouseX >= R.Right - HANDLE) and
(MouseY > innerNorth) and (MouseY < innerSouth) then
Exit(rmRight);
end;
// Body of the texture itself = move zone (no padding — only when
// actually inside the texture rect)
if PtInRect(R, Point(MouseX, MouseY)) then
Result := rmMove;
end;
function TFormMain.GetSelectedTextureScreenRect: TRect;
var
W, H: integer;
ez: Double;
begin
FillChar(Result, SizeOf(Result), 0);
if intSelectedTexture < 0 then
Exit;
ez := EffectiveJamZoom;
if boolHWJAM then
begin
if (FHWJamFile = nil) or
(intSelectedTexture >= FHWJamFile.Entries.Count) then
Exit;
with FHWJamFile.FEntries[intSelectedTexture].FInfo do
begin
Result.Left := Round(X * ez);
Result.Top := Round(Y * ez);
W := Round(Width * ez);
H := Round(Height * ez);
end;
end
else
begin
if (FJamFile = nil) or
(intSelectedTexture >= FJamFile.Entries.Count) then
Exit;
with FJamFile.FEntries[intSelectedTexture].FInfo do
begin
Result.Left := Round(X * ez);
Result.Top := Round(Y * ez);
W := Round(Width * ez);
H := Round(Height * ez);
end;
end;
Result.Right := Result.Left + W;
Result.Bottom := Result.Top + H;
end;
procedure TFormMain.ApplyTextureTransform(newX, newY, newW, newH: integer);
var
shiftHeld: Boolean;
begin
if intSelectedTexture < 0 then
Exit;
if newW < 1 then newW := 1;
if newH < 1 then newH := 1;
// Apply edge snapping during active drag. Snap is active when
// either the toolbar toggle is on OR the user is holding Shift
// (temporary snap override — Photoshop-style).
shiftHeld := (GetKeyState(VK_SHIFT) and $8000) <> 0;
if FDrag.Active and (boolSnapEnabled or shiftHeld) then
ApplySnap(FDrag.Mode, newX, newY, newW, newH);
// Clamp X/Y to stay within the canvas — prevents dragging textures
// outside 0,0. For SW JAMs this is also required by the packed
// record format (byte X / Word Y) which would wrap on negatives.
if newX < 0 then newX := 0;
if newY < 0 then newY := 0;
// Upper bound for SW only (byte X / Word Y storage limit)
if not boolHWJAM then
begin
if newX > 255 then newX := 255;
if newY > 65535 then newY := 65535;
end;
if boolHWJAM then
begin
FHWJamFile.FEntries[intSelectedTexture].FInfo.X := newX;
FHWJamFile.FEntries[intSelectedTexture].FInfo.Y := newY;
FHWJamFile.FEntries[intSelectedTexture].FInfo.Width := newW;
FHWJamFile.FEntries[intSelectedTexture].FInfo.Height := newH;
end
else
begin
FJamFile.FEntries[intSelectedTexture].FInfo.X := newX;
FJamFile.FEntries[intSelectedTexture].FInfo.Y := newY;
FJamFile.FEntries[intSelectedTexture].FInfo.Width := newW;
FJamFile.FEntries[intSelectedTexture].FInfo.Height := newH;
end;
// Update the spin-edit UI (suppress change events)
UpdatingFromCode := true;
try
tex_X.Value := newX;
tex_Y.Value := newY;
tex_width.Value := newW;
tex_height.Value := newH;
finally
UpdatingFromCode := false;
end;
boolJamModified := true;
// Fast-path during an active drag with a cached background:
// only re-draw the moving texture on top of the cached bitmap.
if FDrag.Active and (FDragBackground <> nil) and (not boolHWJAM) then
begin
DrawDragPreview;
// Snap guides (SW fast-path)
DrawSnapGuidesOverlay;
InvalidateDragRegion;
end
else
begin
RefreshCanvas;
if FDrag.Active then
DrawSnapGuidesOverlay;
ImageCanvas.Invalidate;
end;
end;
procedure TFormMain.UpdateCursorForHitZone(X, Y: integer);
var
zone: TResizeMode;
R: TRect;
begin
if (intSelectedTexture < 0) or boolRcrJam then
begin
ImageCanvas.Cursor := crDefault;
Exit;
end;
R := GetSelectedTextureScreenRect;
zone := HitTestTextureZone(X, Y, R);
case zone of
rmMove:
ImageCanvas.Cursor := crSizeAll;
rmTopLeft, rmBottomRight:
ImageCanvas.Cursor := crSizeNWSE;
rmTopRight, rmBottomLeft:
ImageCanvas.Cursor := crSizeNESW;
rmTop, rmBottom:
ImageCanvas.Cursor := crSizeNS;
rmLeft, rmRight:
ImageCanvas.Cursor := crSizeWE;
else
ImageCanvas.Cursor := crDefault;
end;
end;
procedure TFormMain.DeactivateMoveTool;
begin
FMoveToolActive := false;
boolMoveToolActive := false;
FDrag.Active := false;
FDrag.Mode := rmNone;
FSnapXActive := False;
FSnapYActive := False;
FOriginalSelectedIdx := -1;
ReleaseDragBackground;
if toolbarMove.Down then
toolbarMove.Down := false;
ImageCanvas.Cursor := crDefault;
if booljamLoaded then
RefreshCanvas;
end;
procedure TFormMain.CaptureDragBackground;
var
src: TBitmap;
begin
// Called once when a move/resize drag begins.
// We render the canvas WITHOUT the moving texture (via the
// intDragSkipEntry mechanism), capture that as the drag
// background, and restore normal rendering. During drag, per-move
// frames blit this pristine background and composite the moving
// texture on top — so it always appears ABOVE other textures,
// with overlapping textures underneath correctly preserved.
ReleaseDragBackground;
FLastDragQPC := 0; // ensure first drag frame is always rendered
if intSelectedTexture < 0 then Exit;
if boolHWJAM then Exit; // HW: still re-renders fully for now
// Tell DrawFullJam + DrawOutlines to skip the moving entry
intDragSkipEntry := intSelectedTexture;
try
RefreshCanvas;