-
Notifications
You must be signed in to change notification settings - Fork 1
/
dlgDoc.pas
4118 lines (3516 loc) · 109 KB
/
dlgDoc.pas
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
(*
Greenfish Icon Editor Pro
Copyright (c) 2012-13 B. Szalkai
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*)
unit dlgDoc;
interface
uses
{$IFDEF WINDOWS} Windows, {$ENDIF} LclIntf, LclType,
Types, SysUtils, Variants, Classes, Graphics, Controls, Forms, FileUtil,
Dialogs, Math, StdCtrls, ExtCtrls, DoubleBufPB,
BitmapEx, bmExUtils, Layers, DocClass, UndoObject, ieShared, Buttons,
dlgDocPage, Menus, Clipbrd, ImageTransform, Filters, PixelFormats,
gfMath, LangPack, BMP, PNG, ICNS, StrUtils, gfListBox,
BlendModes, dlgLib, dlgStartupFrame, dlgDebug;
const
MaxShrink = 8;
MaxZoom = 30;
MaxUndo = 32;
MaxPolyCount = 10000;
clPaintBoxBgr = $f0f0f0;
dtHasInvertedOutline = [dtSelRect, dtSelEllipse, dtLasso, dtSelPencil, dtCrop];
type
// Icon Editor mouse action
TieMouseAction = (iemaNone, iemaDrawing,
iemaSizing, iemaMoving, iemaRotating, iemaHand);
TdtTransformHitTest = (xfNone, xfSize, xfMove, xfRotate);
TFilterCallback = procedure(bm: TBitmap32; Mask: TBitmap1) of object;
TCellGrid = class(TPersistent)
public
Enabled: boolean;
Size, Spacing, Offset: TPoint;
constructor Create;
procedure Clear;
procedure Assign(Src: TPersistent); override;
end;
{ TGraphicFrame }
TGraphicFrame = class(TDocumentFrame)
pBody: TPanel;
pmPages: TPopupMenu;
pToolbar: TPanel;
sbPages: TSpeedButton;
sbZoomOut: TSpeedButton;
sbGrid: TSpeedButton;
Bevel1: TBevel;
sbZoomIn: TSpeedButton;
sbZoom1: TSpeedButton;
sbZoomFit: TSpeedButton;
cbZoom: TComboBox;
tmMarquee: TTimer;
Bevel2: TBevel;
sbNewPage: TSpeedButton;
sbDeletePage: TSpeedButton;
sbCenterLines: TSpeedButton;
pDrawing: TPanel;
sbX: TScrollBar;
sbY: TScrollBar;
pb: TDoubleBufPB;
Bevel3: TBevel;
sbTest: TSpeedButton;
sbSave: TSpeedButton;
Bevel4: TBevel;
pPages: TPanel;
lb: TgfListBox;
procedure cbZoomChange(Sender: TObject);
procedure lbResize(Sender: TObject);
procedure pmPagesPopup(Sender: TObject);
procedure sbPagesClick(Sender: TObject);
procedure sbXChange(Sender: TObject);
procedure sbYChange(Sender: TObject);
procedure pbResize(Sender: TObject);
procedure pbPaint(Sender: TObject);
procedure sbZoomInClick(Sender: TObject);
procedure sbZoomOutClick(Sender: TObject);
procedure sbGridClick(Sender: TObject);
procedure sbZoomFitClick(Sender: TObject);
procedure pbUpdateCursor;
procedure sbZoom1Click(Sender: TObject);
procedure tmMarqueeTimer(Sender: TObject);
procedure pbMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: integer);
procedure pbMouseMove(Sender: TObject; Shift: TShiftState; X, Y: integer);
procedure pbMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: integer);
procedure sbNewPageClick(Sender: TObject);
procedure sbDeletePageClick(Sender: TObject);
procedure miNewPageClick(Sender: TObject);
procedure miDeletePageClick(Sender: TObject);
procedure lbKeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
procedure ControlMouseMove(Sender: TObject; Shift: TShiftState; X, Y: integer);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: integer);
procedure miPagePropClick(Sender: TObject);
procedure pbKeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
procedure pbKillFocus(Sender: TObject);
procedure pbSetFocus(Sender: TObject);
procedure sbCenterLinesClick(Sender: TObject);
procedure pbMouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: integer; MousePos: TPoint; var Handled: boolean);
procedure sbTestClick(Sender: TObject);
procedure sbSaveClick(Sender: TObject);
procedure lbGetCount(Sender: TObject; var Value: integer);
procedure lbListDblClick(Sender: TObject);
procedure lbItemGetSelected(Sender: TObject; Index: integer;
var Value: boolean);
procedure lbItemSetSelected(Sender: TObject; Index: integer; Value: boolean);
procedure lbItemMouseMove(Sender: TObject; Index: integer;
Shift: TShiftState; X, Y: integer);
procedure lbItemMouseDown(Sender: TObject; Index: integer;
Button: TMouseButton; Shift: TShiftState; X, Y: integer;
var MouseTrap: boolean);
procedure lbItemDragDrop(Sender: TObject; TargetIndex: integer);
procedure lbItemPaint(Sender: TObject; ACanvas: TCanvas; Index: integer;
ItemRect: TRect);
private
FZoom, FImageIndex: integer;
FFileName: string;
FModified: boolean;
function GetImageIndex: integer;
procedure SetImageIndex(Value: integer);
procedure SetZoom(Value: integer);
procedure SetFileName(const Value: string);
procedure SetModified(Value: boolean);
public
// <> 0: blocks events
frmUpdating: integer;
// true -> prevents repainting
frmDestroying: boolean;
// The document object
Doc: TIconDoc;
// Cell grid properties
cg: TCellGrid;
// 1) A buffer for saving the current document layers state
// 2) A buffer for storing the original layers (when applying filters)
// --- lsSrc can differ from lsSave because
// TDocPage.ApplySelectionTransform may have been called
lsSave, lsSrc: TLayers;
// The bitmap which is painted onto the paintbox
bmPaintBox: TBitmap;
// DrawMask: A bm for storing the pencil/brush layer mask
// DrawSketch: A bm for storing a shape overlay
DrawMask: TBitmap1;
DrawSketch: TBitmap32;
// Indicates the part of DrawSketch which contains the useful information
// i.e. the "support" of the currently drawn shape
rUpdate: TRect;
// lsResult: Layers for showing the result of drawing w/o altering the original ones
lsResult: TLayers;
// The active drawing and background color
// Swapped if right button was pressed
SelColor: array[0..1] of TColor32;
// A bitmap for storing temporary selection masks
SelSketch: TBitmap1;
// The current selection/hot spot marquee offset
MarqueeOffset: integer;
// Undo and Redo stacks
UndoStack, RedoStack: TList;
// Current Mouse Action
ma: TieMouseAction;
// Points which store information while using a tool
pOrig, pPrev, pCurr, pKnob, pOrigView, pOrigSBPos: TPoint;
qOrig: TQuad; // original selection quad, before transforming
xResizeDiff, yResizeDiff, AngleDiff: double;
// Mouse button pressed
mb: TMouseButton;
// Supersampling ratio (1 shl AA_BIT_SHIFT, if aaEnabled)
aaRes: integer;
// Used when drawing a polygon
FPolyCount: integer;
FPoints: array[0..MaxPolyCount - 1] of TPoint;
// In fact a property, however, there is no need to handle it that way
FileType: TImageFileType;
// For measuring performance
FPSCounter: integer;
FPSLastTick: Int64;
procedure DocFrameCreate; override;
destructor Destroy; override;
function DocFrameCanClose: boolean; override;
procedure ApplyLanguagePack;
function xToView(i: double): integer;
function yToView(i: double): integer;
function xFromView(i: integer): double;
function yFromView(i: integer): double;
// rect of (even partially) visible pixels
function GetVisibleRect: TRect;
// quadrilateral enclosing then floating selection
// (in window coordinates, if ToView is enabled)
function GetFloatingQuad(out q: TQuad; ToView: boolean): boolean;
procedure zoomFitWindow;
// update scroll bar ranges
procedure UpdateScrollBars;
// updates caption (filename, modified)
procedure UpdateCaption;
// if dtTransform is selected, returns if the mouse is over a
// knob/edge/floating image
function dtTransformHitTest(X, Y: integer; out xKnob, yKnob: integer;
out xDiff, yDiff: double): TdtTransformHitTest;
// redraws the selection marquee
procedure DrawSelectionMarquee;
// redraws the red hot spot cross
procedure DrawHotSpotFlasher;
// redraws all marquees needed, returns if any was drawn
function DrawMarquees: boolean;
// draws an inverted rect/ellipse/etc. when using a tool
procedure DrawInvertedOutline;
// User bitmap: the [temporary drawing result]/[page image]
// That's what the user sees.
procedure RenderUserBitmap(Buffer: TBitmap32; const rClip: TRect; Index: integer);
procedure DrawUserBitmap(rClip: TRect);
// draw pixel grid and cell grid
procedure DrawGrid;
// draw image frame
procedure DrawImageFrame;
// draw knobs which resize the floating selection
procedure DrawSelectionKnobs;
// redraws bmPaintBox and invalidates the PB
procedure RedrawPaintBox;
// redraws a part of bmPaintBox -- call this if a rectangle of the current document
// changed
procedure RedrawPaintBoxPart(const rImageChanged: TRect);
// Call when the count of pages have changed
procedure PageCountChanged;
// Call when the size of the active page has changed
procedure PageSizeChanged;
// Insert a new page, with a 'Background' layer
function pgInsert(Index: integer): TDocPage;
// Delete an existing page
procedure pgDelete(Index: integer);
// Move a page to another position
procedure pgMove(Index, Index2: integer);
// Puts an undo/redo object to a stack
procedure PushUndoRedo(List: TList; uo: TUndoObject);
procedure AddUndo(uo: TUndoObject);
function CanUndo: boolean;
function CanRedo: boolean;
procedure PerformUndoRedo(Undo: boolean);
procedure PurgeUndoRedo(Stack: TList);
// --- Undo constructors
// Saves all layers and selection state
// Saves only selected layers if OnlySelected
procedure cuAllLayersChanged(const Caption: string; OnlySelected: boolean);
// Use this before the floating selection is created
procedure cuCreateFloating(const Caption: string);
procedure cuEditDelete(const Caption: string);
// cREATEuNDO Filter: case SelState
// stNone, stSelecting -> saves selected layers
// stFloating -> saves floating selection
procedure cuFilter(const Caption: string);
procedure cuFlipRotate(const Caption: string);
// Creates an undo before a change to multiple frame rates
procedure cuFrameRates(const Caption: string);
// Creates an undo before hot spot change
procedure cuHotSpot(const Caption: string);
// Call this before deleting all selected layers
procedure cuLayerDelete(const Caption: string);
// Call this AFTER inserting a layer at Depth
procedure cuLayerInsert(const Caption: string; Depth: integer);
procedure cuLayerMerge(const Caption: string; Subset: TLayerSubset);
procedure cuLayerProp(const Caption: string; LayerIndex: integer);
procedure cuPageDelete(const Caption: string; Index: integer);
procedure cuPageInsert(const Caption: string; Index: integer);
procedure cuPageMove(const Caption: string; Index, Index2: integer);
// Saves all selected layers
procedure cuPaint(const Caption: string);
// Use this undo constructor before
// - the floating selection (if present) is flattened
// and then - the Selection object is edited
// e.g.: selecting something, pasting, etc.
// Saves Selection and, if stFloating, the layer underneath
procedure cuSelect(const Caption: string);
// Use this undo before modifying the floating selection box
// (whilst creating a floating selection, if state was stSelecting)
procedure cuTransform(const Caption: string);
// Actions
procedure DoLayerNew;
procedure DoLayerDupl;
procedure DoLayerDelete;
procedure DoLayerProp;
procedure DoLayerMerge(Subset: TLayerSubset);
procedure DoLayerFromSel;
procedure DoPageNew;
procedure DoPageDelete;
procedure DoPageMove(Index, Index2: integer);
procedure DoPageProp;
procedure DoPageImport;
procedure DoPageExport(ExportAll: boolean);
procedure DoTogglePages;
procedure DoUniformFrameRate(fr: integer);
procedure DoEditCopy;
procedure DoEditPaste(NewPage: boolean);
procedure DoEditDelete;
procedure DoInsertText(const Text: string; _Font: TFont;
Color: TColor32; AntiAlias: boolean);
procedure DoSelectAll;
procedure DoDeselectAll;
procedure DoInvertSelection;
procedure DoLoadSelection(const FileName: string);
procedure DoSaveSelection(const FileName: string; FileType: TImageFileType);
procedure DoCropTransparency(CreateUndo: boolean);
procedure DoFlipRotate(Mode: TSimpleFlipRotate);
function DoLoad: boolean;
function DoSave(const fn: string; ft: TImageFileType;
Options: TDocumentSaveOptions): boolean;
// Create undo, apply transform and use the filter proc
procedure UseFilter(Proc: TFilterCallback; NeedApplySelTransform: boolean;
const sUndoText: string);
// Call only the filter algorithm
procedure DoFilter(Proc: TFilterCallback);
// Filter callbacks
// They are to be passed to DoFilter
procedure DoFilter_Grayscale(bm: TBitmap32; Mask: TBitmap1);
procedure DoFilter_Invert(bm: TBitmap32; Mask: TBitmap1);
procedure DoFilter_Solarize(bm: TBitmap32; Mask: TBitmap1);
procedure DoFilter_Average(bm: TBitmap32; Mask: TBitmap1);
procedure DoFilter_SoftBlur(bm: TBitmap32; Mask: TBitmap1);
procedure DoFilter_BlurMore(bm: TBitmap32; Mask: TBitmap1);
procedure DoFilter_Sharpen(bm: TBitmap32; Mask: TBitmap1);
procedure DoFilter_PaintContour(bm: TBitmap32; Mask: TBitmap1);
// Draws the floating selection on all selected layers
procedure MakeSelectionStamp(ls: TLayers);
property ImageIndex: integer read GetImageIndex write SetImageIndex;
property Zoom: integer read FZoom write SetZoom;
property FileName: string read FFileName write SetFileName;
property Modified: boolean read FModified write SetModified;
end;
implementation
uses dlgToolSet, Main, dlgText,
dlgTest, dlgLayerProp, dlgSaveOptions;
{$R *.lfm}
procedure PaintAntialiasMask4(Dest: TBitmapEx; Mask: TBitmap1;
Color: TColor32; rClip: TRect);
const
SumOfDigits: array[0..$f] of integer = (0, 1, 1, 2, 1, 2, 2, 3,
1, 2, 2, 3, 2, 3, 3, 4);
var
i, j, k, x, y, xMin, xMax, iMax: integer;
Gradient: array[0..16] of TColor32;
begin
// cache colors
for i := 0 to 16 do
if Color = cl32Inverted then
Gradient[i] := IfThen(i < 8, cl32Transparent, cl32Inverted)
else
Gradient[i] := BlendColors(cl32Transparent, Color, Round($ff * i / 16));
// NOT shr 2, we may deal with negative numbers !
xMin := rClip.Left div 4;
xMax := (rClip.Right - 1) div 4;
for y := rClip.Top div 4 to (rClip.Bottom - 1) div 4 do
begin
iMax := Min(Mask.Height, (y + 1) shl 2) - 1;
for x := xMin to xMax do
begin
k := 0;
for i := y shl 2 to iMax do
begin
j := Mask.ScanLine(i)[x shr 1];
if Odd(x) then
j := j shr 4
else
j := j and $f;
Inc(k, SumOfDigits[j]);
end;
Dest.SetPixel(x, y, Gradient[k]);
end;
end;
end;
// TCellGrid
constructor TCellGrid.Create;
begin
inherited;
Clear;
end;
procedure TCellGrid.Clear;
begin
Enabled := False;
Size := Point(16, 16);
Spacing := Point(0, 0);
Offset := Point(0, 0);
end;
procedure TCellGrid.Assign;
var
x: TCellGrid;
begin
if Src is TCellGrid then
begin
x := Src as TCellGrid;
Enabled := x.Enabled;
Size := x.Size;
Spacing := x.Spacing;
Offset := x.Offset;
end
else
inherited;
end;
// TGraphicFrame
function TGraphicFrame.GetImageIndex: integer;
begin
// range check
SetImageIndex(FImageIndex);
Result := FImageIndex;
end;
procedure TGraphicFrame.SetImageIndex(Value: integer);
begin
Value := Max(0, Min(Doc.PageCount - 1, Value));
if FImageIndex <> Value then
begin
FImageIndex := Value;
lb.ScrollTo(Value);
PageSizeChanged;
end;
end;
procedure TGraphicFrame.SetZoom(Value: integer);
var
s: string;
begin
if (Value = 0) or (Value = -1) then
Value := 1;
Value := Max(-MaxShrink, Min(MaxZoom, Value));
if FZoom <> Value then
begin
FZoom := Value;
Inc(frmUpdating);
s := IntToStr(Abs(Zoom));
cbZoom.ItemIndex := cbZoom.Items.IndexOf(IfThen(Zoom > 0, s + 'x', '1/' + s));
Dec(frmUpdating);
UpdateScrollBars;
RedrawPaintBox;
end;
end;
procedure TGraphicFrame.SetFileName(const Value: string);
begin
if FFileName <> Value then
begin
FFileName := Value;
UpdateCaption;
end;
end;
procedure TGraphicFrame.SetModified(Value: boolean);
begin
if FModified <> Value then
begin
FModified := Value;
UpdateCaption;
end;
end;
procedure TGraphicFrame.DocFrameCreate;
var
i: integer;
begin
frmUpdating := 0;
frmDestroying := False;
// initialize Zoom combo box
Inc(frmUpdating);
for i := MaxShrink downto 2 do
cbZoom.Items.Add('1/' + IntToStr(i));
for i := 1 to MaxZoom do
cbZoom.Items.Add(IntToStr(i) + 'x');
cbZoom.ItemIndex := MaxShrink - 1;
Dec(frmUpdating);
// load tool button glyphs
GetMiscGlyph(sbSave.Glyph, mgSave);
GetMiscGlyph(sbNewPage.Glyph, mgNew);
GetMiscGlyph(sbDeletePage.Glyph, mgDelete);
GetMiscGlyph(sbZoom1.Glyph, mgZoom1);
GetMiscGlyph(sbZoomOut.Glyph, mgMinus);
GetMiscGlyph(sbZoomIn.Glyph, mgPlus);
GetMiscGlyph(sbZoomFit.Glyph, mgZoomFit);
GetMiscGlyph(sbGrid.Glyph, mgGrid);
GetMiscGlyph(sbCenterLines.Glyph, mgCenterLines);
GetMiscGlyph(sbPages.Glyph, mgPages);
GetMiscGlyph(sbTest.Glyph, mgTest);
Doc := TIconDoc.Create;
cg := TCellGrid.Create;
lsSave := TLayers.Create;
lsSrc := TLayers.Create;
bmPaintBox := TBitmap.Create;
bmPaintBox.PixelFormat := pf24bit;
DrawMask := TBitmap1.Create;
DrawSketch := TBitmap32.Create;
lsResult := TLayers.Create;
SelSketch := TBitmap1.Create;
MarqueeOffset := 0;
// initialize undo and redo
UndoStack := TList.Create;
RedoStack := TList.Create;
// we have to insert at least one page
pgInsert(0);
// init the properties
FZoom := 1;
FImageIndex := 0;
FFileName := '';
FileType := iftNone;
FModified := False;
UpdateCaption;
ApplyLanguagePack;
frmMain.TabVisible[TStartupScreenTab] := False;
FPSCounter := 0;
FPSLastTick := GetTickCount64;
end;
destructor TGraphicFrame.Destroy;
begin
// prevent form from painting and accessing freed components
frmDestroying := True;
FreeAndNil(Doc);
FreeAndNil(cg);
FreeAndNil(lsSave);
FreeAndNil(lsSrc);
FreeAndNil(bmPaintBox);
FreeAndNil(DrawMask);
FreeAndNil(DrawSketch);
FreeAndNil(lsResult);
FreeAndNil(SelSketch);
PurgeUndoRedo(UndoStack);
FreeAndNil(UndoStack);
PurgeUndoRedo(RedoStack);
FreeAndNil(RedoStack);
inherited;
end;
procedure TGraphicFrame.ApplyLanguagePack;
begin
UpdateCaption;
lb.ClientArea.Invalidate;
sbSave.Hint := lpGet('MI_FILE_SAVE') + ' (Ctrl+S)';
sbNewPage.Hint := lpGet('MI_ICON_PAGE_NEW') + ' (Shift+Ctrl+N)';
sbDeletePage.Hint := lpGet('MI_ICON_PAGE_DELETE');
sbZoom1.Hint := lpGet('MI_VIEW_100_PERCENT') + ' (*)';
sbZoomOut.Hint := lpGet('MI_VIEW_ZOOM_OUT') + ' (-)';
cbZoom.Hint := lpGet('HINT_DOC_ZOOM_MENU');
sbZoomIn.Hint := lpGet('MI_VIEW_ZOOM_IN') + ' (+)';
sbZoomFit.Hint := lpGet('MI_VIEW_FIT_WINDOW') + ' (/)';
sbGrid.Hint := lpGet('MI_VIEW_GRID') + ' (Ctrl+G)';
sbCenterLines.Hint := lpGet('MI_VIEW_CENTER_LINES');
sbTest.Hint := lpGet('MI_ICON_TEST') + ' (Shift+Ctrl+T)';
sbPages.Hint := lpGet('HINT_DOC_TOGGLE_PAGES') + ' (F5)';
end;
function TGraphicFrame.xToView(i: double): integer;
begin
if Zoom < 0 then
// shrink
Result := pb.Width div 2 - Doc.Pages[ImageIndex].Layers.Width div
(2 * -Zoom) + Floor(i / -Zoom) - sbX.Position div -Zoom
else
// zoom
Result := pb.Width div 2 - Doc.Pages[ImageIndex].Layers.Width *
Zoom div 2 + Round((i - sbX.Position) * Zoom);
end;
function TGraphicFrame.yToView(i: double): integer;
begin
if Zoom < 0 then
// shrink
Result := pb.Height div 2 - Doc.Pages[ImageIndex].Layers.Height div
(2 * -Zoom) + Floor(i / -Zoom) - sbY.Position div -Zoom
else
// zoom
Result := pb.Height div 2 - Doc.Pages[ImageIndex].Layers.Height *
Zoom div 2 + Round((i - sbY.Position) * Zoom);
end;
function TGraphicFrame.xFromView(i: integer): double;
begin
if Zoom < 0 then
Result := (i - xToView(0)) * -Zoom
else
Result := (i - xToView(0)) / Zoom;
end;
function TGraphicFrame.yFromView(i: integer): double;
begin
if Zoom < 0 then
Result := (i - yToView(0)) * -Zoom
else
Result := (i - yToView(0)) / Zoom;
end;
function TGraphicFrame.GetVisibleRect: TRect;
begin
with Doc.Pages[ImageIndex].Layers do
Result := Rect(Max(0, Floor(xFromView(0))), Max(0, Floor(yFromView(0))),
Min(Width, Ceil(xFromView(pb.Width))),
Min(Height, Ceil(yFromView(pb.Height))));
end;
function TGraphicFrame.GetFloatingQuad(out q: TQuad; ToView: boolean): boolean;
var
i: integer;
begin
with Doc.Pages[ImageIndex].Layers do
begin
if SelState <> stFloating then
Exit(False);
// rotate the box by angle
with Selection do
q := RotateRectToQuad(Box, Box.xCenter, Box.yCenter, Selection.Angle);
// convert to screen coords
if ToView then
for i := 0 to 3 do
begin
q[i].x := xToView(q[i].x);
q[i].y := yToView(q[i].y);
end;
Result := True;
end;
end;
procedure TGraphicFrame.zoomFitWindow;
var
ls: TLayers;
begin
ls := Doc.Pages[ImageIndex].Layers;
if (ls.Width = 0) or (ls.Height = 0) or (pb.Width = 0) or (pb.Height = 0) then
Zoom := 1
else
if (ls.Width > pb.Width) or (ls.Height > pb.Height) then
Zoom := -(1 + Max(ls.Width div pb.Width, ls.Height div pb.Height))
else
Zoom := Min(pb.Width div ls.Width, pb.Height div ls.Height);
sbX.Position := 0;
sbY.Position := 0;
end;
procedure TGraphicFrame.UpdateScrollBars;
begin
with Doc.Pages[ImageIndex].Layers do
begin
sbX.SetParams(sbX.Position, -Width div 2, Width div 2);
sbY.SetParams(sbY.Position, -Height div 2, Height div 2);
end;
end;
procedure TGraphicFrame.UpdateCaption;
var
s: string;
begin
if FileName = '' then
s := lpGet('UNTITLED')
else
s := SysToUTF8(ExtractFileName(FileName));
if Modified then
s := s + '*';
SetTabCaption(s);
end;
function TGraphicFrame.dtTransformHitTest(X, Y: integer;
out xKnob, yKnob: integer; out xDiff, yDiff: double): TdtTransformHitTest;
const
htBuffer = 4;
var
q: TQuad;
OnLeft, OnRight, OnTop, OnBottom: boolean;
begin
Result := xfNone;
xKnob := 0;
yKnob := 0;
xDiff := 0;
yDiff := 0;
if Doc.Pages[ImageIndex].Layers.SelState <> stFloating then
Exit;
if not GetFloatingQuad(q, True) then
Exit;
// rotate?
if Sqr(X - 0.5 * (q[0].x + q[3].x)) + Sqr(Y - 0.5 * (q[0].y + q[3].y)) <
Sqr(htBuffer * 2) then
Exit(xfRotate);
OnLeft := PointSegmentDist(X, Y, q[0].x, q[0].y, q[1].x, q[1].y) < htBuffer;
OnBottom := PointSegmentDist(X, Y, q[1].x, q[1].y, q[2].x, q[2].y) < htBuffer;
OnRight := PointSegmentDist(X, Y, q[2].x, q[2].y, q[3].x, q[3].y) < htBuffer;
OnTop := PointSegmentDist(X, Y, q[3].x, q[3].y, q[0].x, q[0].y) < htBuffer;
if OnLeft or OnRight or OnTop or OnBottom then
begin
Result := xfSize;
if OnLeft then
begin
xKnob := -1;
xDiff := PointLineDist(X, Y, q[2].x, q[2].y, q[3].x, q[3].y) -
PointLineDist(q[0].x, q[0].y, q[2].x, q[2].y, q[3].x, q[3].y);
end
else
if OnRight then
begin
xKnob := 1;
xDiff := PointLineDist(X, Y, q[0].x, q[0].y, q[1].x, q[1].y) -
PointLineDist(q[2].x, q[2].y, q[0].x, q[0].y, q[1].x, q[1].y);
end
else
xKnob := 0;
if OnTop then
begin
yKnob := -1;
yDiff := PointLineDist(X, Y, q[1].x, q[1].y, q[2].x, q[2].y) -
PointLineDist(q[3].x, q[3].y, q[1].x, q[1].y, q[2].x, q[2].y);
end
else
if OnBottom then
begin
yKnob := 1;
yDiff := PointLineDist(X, Y, q[3].x, q[3].y, q[0].x, q[0].y) -
PointLineDist(q[1].x, q[1].y, q[3].x, q[3].y, q[0].x, q[0].y);
end
else
yKnob := 0;
end
else
Result := xfMove;
end;
procedure TGraphicFrame.DrawSelectionMarquee;
var
ls: TLayers;
c: array[boolean] of TColor;
sl: TScanLines;
bmMask: TBitmap1;
i, j, x, y, x2, y2, q: integer;
rVisible: TRect;
Quad: TQuad;
pt: TDynPointArray;
procedure _PutPixel(x, y: integer);
begin
if (x >= 0) and (x < bmPaintBox.Width) and (y >= 0) and
(y < bmPaintBox.Height) then
Move3(@c[(x + y + MarqueeOffset) and 4 <> 0], @sl[y][x * 3]);
end;
procedure _QuadLine(n1, n2: integer);
var
i: integer;
begin
EnumLinePoints(Round(Quad[n1].x), Round(Quad[n1].y),
Round(Quad[n2].x), Round(Quad[n2].y), pt);
for i := 0 to Length(pt) - 1 do
_PutPixel(pt[i].x, pt[i].y);
end;
begin
ls := Doc.Pages[ImageIndex].Layers;
case ls.SelState of
stNone: Exit;
stSelecting:
begin
c[False] := clWhite;
c[True] := clBlack;
end;
stFloating:
begin
c[False] := clWhite;
c[True] := FlipColor(clBlue);
end;
end;
sl := TScanLines.Create(bmPaintBox);
try
case ls.SelState of
stSelecting:
begin
bmMask := ls.Selection.Mask;
rVisible := GetVisibleRect;
// one-to-one/shrunk?
if (Zoom = 1) or (Zoom < 0) then
begin
q := Abs(Zoom);
for x := rVisible.Left div q to Ceil(rVisible.Right / q) - 1 do
for y := rVisible.Top div q to Ceil(rVisible.Bottom / q) - 1 do
begin
i := x * q;
j := y * q;
if bmMask[i, j] and ((i < q) or not bmMask[i - q, j] or
(j < q) or not bmMask[i, j - q] or (i >= bmMask.Width - q) or
not bmMask[i + q, j] or (j >= bmMask.Height - q) or
not bmMask[i, j + q]) then
_PutPixel(xToView(i), yToView(j));
end;
end
else
// zoomed
begin
for x := rVisible.Left to rVisible.Right - 1 do
for y := rVisible.Top to rVisible.Bottom - 1 do
begin
// left side
if (x = 0) and bmMask[x, y] then
begin
x2 := xToView(x);
for y2 := yToView(y) to yToView(y + 1) do
_PutPixel(x2, y2);
end;
// right side
if ((x = bmMask.Width - 1) and bmMask[x, y]) or
((x <> bmMask.Width - 1) and (bmMask[x, y] xor bmMask[x + 1, y])) then
begin
x2 := xToView(x + 1);
for y2 := yToView(y) to yToView(y + 1) do
_PutPixel(x2, y2);
end;
// bottom side
if ((y = bmMask.Height - 1) and bmMask[x, y]) or
((y <> bmMask.Height - 1) and (bmMask[x, y] xor bmMask[x, y + 1])) then
begin
y2 := yToView(y + 1);
for x2 := xToView(x) to xToView(x + 1) do
_PutPixel(x2, y2);
end; // bottom
end; // for x, y
// left side
if rVisible.Left = 0 then
begin
x2 := xToView(0);
for y := rVisible.Top to rVisible.Bottom - 1 do
if bmMask[0, y] then
for y2 := yToView(y) to yToView(y + 1) do
_PutPixel(x2, y2);
end;
// top side
if rVisible.Top = 0 then
begin
y2 := yToView(0);
for x := rVisible.Left to rVisible.Right - 1 do
if bmMask[x, 0] then
for x2 := xToView(x) to xToView(x + 1) do
_PutPixel(x2, y2);
end;
end; // zoomed
end; // stSelecting
stFloating:
begin
if not GetFloatingQuad(Quad, True) then
Exit;
_QuadLine(0, 1);
_QuadLine(1, 2);
_QuadLine(2, 3);
_QuadLine(3, 0);
end;
end; // case SelState
finally
sl.Free;
end;
end;
procedure TGraphicFrame.DrawHotSpotFlasher;
var
i, x, y, Size: integer;
Page: TDocPage;
begin
with bmPaintBox.Canvas do
begin
Page := Doc.Pages[ImageIndex];
x := xToView(Page.HotSpot.X + 0.5);
y := yToView(Page.HotSpot.Y + 0.5);
if Zoom >= 8 then
Size := Zoom div 2
else
Size := 4;
// calc the flasher color
i := IfThen(MarqueeOffset <= 4, MarqueeOffset, 8 - MarqueeOffset);
Pen.Color := Min($ff, i * $40);
Pen.Width := IfThen(Size <= 4, 2, 3);
MoveTo(x - Size, y - Size);
LineTo(x + Size, y + Size);
MoveTo(x - Size, y + Size);
LineTo(x + Size, y - Size);
Pen.Width := 1;
end;
end;
function TGraphicFrame.DrawMarquees;
begin
Result := False;
if Doc.Pages[ImageIndex].Layers.SelState <> stNone then
begin
DrawSelectionMarquee;
DrawSelectionKnobs;
Result := True;
end;
if frmMain.frmToolbar.DrawTool = dtHotSpot then
begin