-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLifeBox.pas
3986 lines (3628 loc) · 118 KB
/
LifeBox.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
unit LifeBox;
(* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. *)
interface
uses
Classes, Windows, SysUtils, Graphics, Controls, StdCtrls, ExtCtrls, Dialogs,
Forms, Messages, ShellAPI,
LifeGen, LifeCel, LifeUtil, LifeConst, Snapshot, ActiveX;
type
TDropFileEvent = procedure(Sender: TObject; var Msg: TWMDropFiles) of object;
TMouseDown = (mdNone, mdLeft, mdRight, mdRightDrag, mdDragDrop, mdTorusResize);
TLifeBox = class;
TDataObject = class;
ILife32CutOut = interface(IUnknown)
['{09871F80-94EF-11D1-AAE6-E27B60467027}']
function GetLifeBoxClipRect: TRect; stdcall;
function GetCutOut: TUniverse; stdcall;
function IsSpecialDrag: WordBool; stdcall;
function SourceID: integer; stdcall;
end;
TDropTarget = class(TInterfacedObject, IDropTarget)
private
MyLifeBox: TLifeBox;
MyCutOut: TUniverse;
CanAccept: Boolean;
FKeyPressed: byte;
Target: TPoint;
ClipRect: TRect;
DrawRect: TRect;
Oldpt: TPoint;
IsSpecialDrag: boolean;
function GetTarget(Cursorpt: TPoint): TPoint;
protected
function DragEnter(const dataObj: IDataObject; grfKeyState: Longint;
pt: TPoint; var dwEffect: Longint): HResult; stdcall;
function DragOver(grfKeyState: Longint; pt: TPoint;
var dwEffect: Longint): HResult; stdcall;
function DragLeave: HResult; stdcall;
function Drop(const dataObj: IDataObject; grfKeyState: Longint; pt: TPoint;
var dwEffect: Longint): HResult; stdcall;
procedure SetKeyPressed(value: byte);
public
constructor Create(AParent: TLifeBox);
destructor Destroy; override;
property KeyPressed: byte read FKeyPressed write SetKeyPressed;
end;
TDropSource = class(TInterfacedObject, IDropSource)
private
MyLifeBox: TLifeBox;
MyTarget: TDropTarget;
FKey: longint;
MouseButton: longint;
KeyboardDelay: integer;
protected
function QueryContinueDrag(fEscapePressed: BOOL; grfKeyState: Longint): HResult; stdcall;
function GiveFeedback(dwEffect: Longint): HResult; stdcall;
public
constructor Create(AParent: TLifeBox; ATarget: TDropTarget);
destructor Destroy; override;
property Key: longint read FKey;
end;
TDataObject = class(TInterfacedObject, IDataObject, ILife32Cutout)
private
MyLifeBox: TLifeBox;
CutOut: TUniverse;
protected
function GetLifeBoxClipRect: TRect; stdcall;
function GetCutOut: TUniverse; stdcall;
function IsSpecialDrag: WordBool; stdcall;
function SourceID: integer; stdcall;
function GetData(const formatetcIn: TFormatEtc; out medium: TStgMedium):
HResult; stdcall;
function GetDataHere(const formatetc: TFormatEtc; out medium: TStgMedium):
HResult; stdcall;
function QueryGetData(const formatetc: TFormatEtc): HResult; stdcall;
function GetCanonicalFormatEtc(const formatetc: TFormatEtc;
out formatetcOut: TFormatEtc): HResult; stdcall;
function SetData(const formatetc: TFormatEtc; var medium: TStgMedium;
fRelease: BOOL): HResult; stdcall;
function EnumFormatEtc(dwDirection: Longint; out enumFormatEtc:
IEnumFormatEtc): HResult; stdcall;
function DAdvise(const formatetc: TFormatEtc; advf: Longint;
const advSink: IAdviseSink; out dwConnection: Longint): HResult; stdcall;
function DUnadvise(dwConnection: Longint): HResult; stdcall;
function EnumDAdvise(out enumAdvise: IEnumStatData): HResult; stdcall;
public
constructor Create(AParent: TLifeBox; ACutOut: TUniverse);
destructor Destroy; override;
procedure BeforeDestruction; override;
end;
TChangePatternEvent = procedure(Sender: TObject; Change: integer) of object;
TShowDragEvent = procedure(Sender: TObject; DragOffset: TPoint) of object;
TCanPaint = (cpCanPaint, cpDialogShowing, cpDontPaint, cpInit);
TLifeBox = class(TPanel)
private
SourceID: integer;
FPixelsPerCel, PPC: integer;
FIsPaused: boolean;
FNegZoom: integer;
FFillPercentage: integer;
FDropOffset: TPoint;
FCelColor: TColor;
FBackColor: TColor;
FGridColor: TColor;
FGrid2Color: TColor;
FSelRectColor: TColor;
FZoomRectColor: TColor;
FDragColor: TColor;
FTorusColor: TColor;
FWhiteDisplay: boolean;
HandX, HandY: integer;
DrawX,DrawY: integer;
OldDrawX, OldDrawY: integer;
FSelectionRect: TRect;
FOrgSelectionRect: TRect;
FSelectionVisible: boolean;
CursorDirty: Boolean;
FViewChanged: boolean;
Dragstate: Boolean;
FGrid: boolean;
FCanPaint: TCanPaint;
FFrameDropTime: integer;
FSmallScroll: integer;
FBoldGridSpacing: integer;
FHandScroll: integer;
FIsLimited: boolean;
FFreezeSelection: Boolean;
FSelectionExcluded: Boolean;
FCutOut: TUniverse;
FRevision: integer;
FPatternID: integer;
FMostRecentPatternID: integer;
FUniverse: TUniverse;
FCopyUniverse: TUniverse;
FWidthCels, FHeightCels: integer;
FOnPaint: TNotifyEvent;
FOnMustPause: TNotifyEvent;
FOnChangePattern: TChangePatternEvent;
FOnEditorModeChange: TNotifyEvent;
FOnPasteModeChange: TNotifyEvent;
FOnDropFiles: TDropFileEvent;
FOnZoomChange: TNotifyEvent;
FOnRuleChange: TRuleChangeEvent;
FOnSaveProgress: TSaveProgressEvent;
FOnShowDrag: TShowDragEvent;
FAfterInfoChange: TNotifyEvent;
FOnSelectionChange: TNotifyEvent;
FScrolling: Boolean;
FPasteMode: TPasteMode;
FEditorMode: integer;
FDropTarget: TDropTarget;
FDropSource: TDropSource;
FDropData: TDataObject;
FInSelection: Boolean;
FScreenSaverActive: Boolean;
FMyDC: HDC;
function Display(ARect: TRect): boolean;
procedure RedrawBackground(ARect: TRect);
procedure KeepWithinBounds(var x,y: integer);
procedure DrawSelRect;
procedure EraseSelRect(ClearSel: boolean);
procedure RedrawLifeRect(ARect: TRect);
procedure CorrectSelRect(Shrink: boolean);
procedure FakeLifeLine(x1,y1,x2,y2: integer);
function ConvertColor(OldColor, WinColor: integer): integer;
function GetLifeBoxClipRect: TRect;
property WhiteDisplay: boolean read FWhiteDisplay write FWhiteDisplay;
protected
IsMouseDown: TMouseDown;
procedure CreateWnd; override;
//procedure Loaded; override;
procedure CreateParams(var Params: TCreateParams); override;
procedure CreateWindowHandle(const Params: TCreateParams); override;
procedure DestroyWnd; override;
procedure SetPatternID(Value: integer);
procedure SetIsPaused(Value: Boolean);
procedure SetUniverse(Value: TUniverse);
procedure SetGrid(Value: Boolean);
procedure SetEditorMode(Value: integer);
procedure SetPasteMode(Value: TPasteMode);
procedure SetScrolling(Value: Boolean);
procedure SetBoldGridSpacing(Value: integer);
procedure SetPixelsPerCel(Value: integer);
function GetPixelsPerCel: integer;
function GetIsCounting: boolean;
procedure SetXScroll(value: integer);
function GetXScroll: integer;
procedure SetYScroll(value: integer);
function GetYScroll: integer;
procedure SetOnRuleChange(Value: TRuleChangeEvent);
procedure SetOnSaveProgress(Value: TSaveProgressEvent);
procedure SetAfterInfoChange(Value: TNotifyEvent);
function IsCursorAtTorusEdge: boolean;
procedure ShowTorusResizeCursor(Show: boolean);
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure WMChar(var Msg: TWMChar); message WM_Char;
procedure WMDropFiles(var Msg: TWMDropFiles); message WM_DropFiles;
procedure Paint; override;
procedure Validate;
procedure SetSelectionRect(value: TRect);
procedure SetCanPaint(Value: TCanPaint);
function GetGeneration: Integer;
procedure SetGeneration(Value: integer);
function GetFramedropTime: integer;
function GetZoomRectColor: TColor;
function GetSelRectColor: TColor;
procedure SetCelColor(Value: TColor);
procedure SetGridColor(Value: TColor);
procedure SetGrid2Color(Value: TColor);
procedure SetBackColor(Value: TColor);
procedure SetDragColor(Value: TColor);
procedure SetTorusColor(Value: TColor);
procedure SetSelRectColor(Value: TColor);
procedure SetZoomRectColor(Value: TColor);
procedure SetScreenSaverActive(Value: Boolean);
function IsScreenSaverStillActive: Boolean;
procedure SetLimit(Value: TRect);
function GetLimit: TRect;
procedure SetIsLimited(Value: boolean);
procedure SetTorusKind(Value: TTorusKind);
procedure SetDeadEdges(Value: boolean);
function GetTorusKind: TTorusKind;
function GetDeadEdges: boolean;
public
oX,oY: integer;
xorig, yorig: integer; // universe location at the upper left of the viewing window
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Init;
procedure Invalidate; override;
procedure RefreshColors(DoRedraw: boolean);
procedure RedrawCursorArea(x,y: integer);
procedure DisplayCutOut(APos: TPoint; ACutOut: TUniverse);
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure KeyUp(var Key: Word; Shift: TShiftState); override;
procedure MoveToFast(NewX,NewY: integer);
procedure MoveTo(NewX,NewY: integer);
function MoveByFast(Dx,Dy: integer): boolean;
function MoveBy(Dx,Dy: integer): boolean;
procedure CenterOnPattern(Redraw: Boolean);
function CelsAcross: integer;
function CelsDown: integer;
procedure ClientToBox(var x,y: integer);
procedure BoxToClient(var x,y: integer);
procedure BoxToCelFast(var x,y: integer);
procedure BoxToCel(var x,y: integer);
procedure CelToBox(var x,y: integer);
procedure ClientToCelFast(var x,y: integer);
procedure ClientToCel(var x,y: integer);
procedure CelToClient(var x,y: integer);
function CelToClientRect(ARect: TRect): TRect;
//procedure GetCelCount(OnReady: TNotifyEvent);
procedure RedrawCel(x, y: integer);
procedure DrawCel(x, y: integer; state: boolean);
procedure DrawGrid(ARect: TRect);
function IsCelOn(x, y: integer): boolean;
procedure InvertCel(x, y: integer);
procedure Clear(MoveIt, ClearDesc, Snapshot: boolean);
procedure LoadFromFile(AFileName: string);
procedure ChangeCel(x, y: integer; state: boolean);
procedure ChangeDrawCel(x,y: integer; state: boolean);
procedure DrawLine(x1,y1,x2,y2: integer; XorIt: Boolean);
function UpdateAll: boolean;
procedure RedrawAll;
procedure Redraw;
procedure Generate(Direction: Boolean);
procedure RandomDot;
procedure MirrorHorz;
procedure MirrorVert;
procedure Rotate90;
procedure Rotate180;
procedure Rotate270;
procedure DrawBox;
procedure FillRandom;
procedure FillBlack;
procedure InvertSelection;
procedure ZoomToSelection(MaxFit: integer);
procedure ZoomToFit(Redraw: Boolean; MaxFit: integer);
procedure SelectAll(Redraw: Boolean);
procedure CutSelection;
procedure CopySelection;
procedure PasteSelection;
procedure ClearSelection;
procedure ClearOutsideSelection;
procedure MirrorSelHorz;
procedure MirrorSelVert;
procedure RotateSel90;
procedure RotateSel180;
procedure RotateSel270;
procedure InsertShape(AShape: TUniverse);
function GetCutout: TUniverse;
procedure SaveShape(AShape: TUniverse; AFilename: string; FileFormat: integer);
//Saves the entire universe to file.
procedure SaveToFile(AFilename: string; FileFormat: integer; IncludeTorusData: boolean);
procedure DoPaint(Sender: TObject);
function EnableScreen: boolean;
function CanPlay: Boolean;
function IsEmpty: boolean;
procedure ClearOutsideTorus;
procedure ExcludeSelection;
procedure IncludeSelection;
procedure CancelSelection;
procedure HideSelection;
procedure ShowSelection;
function MakeSnapshotCopy: TSnapshot;
function MakeSnapshotDummy: TSnapshot;
procedure RewindToSnapshot(ASnapshot: TSnapshot);
procedure RewindToSnapshotSilent(ASnapshot: TSnapshot);
function NewUniverse: TUniverse;
procedure DoAutoScroll(APoint: TPoint; MoveCursor: Boolean);
procedure DrawDropRect(ARect: TRect);
procedure DrawDropTarget(x,y: integer); //x,y in cell coordinates.
property DropOffset: TPoint read FDropOffset write FDropOffset;
property FreezeSelection: Boolean read FFreezeSelection write FFreezeSelection;
property PatternID: integer read FPatternID write SetPatternID;
property MostRecentPatternID: integer read FMostRecentPatternID;
property Revision: integer read FRevision write FRevision;
property SelectionRect: TRect read FSelectionRect write SetSelectionRect;
property OrgSelectionRect: TRect read FOrgSelectionRect;
property SelectionVisible: boolean read FSelectionVisible;
property FillPercentage: integer read FFillPercentage write FFillPercentage;
property IsPaused: Boolean read FIsPaused write SetIsPaused;
property CanPaint: TCanPaint read FCanPaint write SetCanPaint;
property Scrolling: Boolean read FScrolling write SetScrolling;
property Universe: TUniverse read FUniverse write SetUniverse;
property PasteMode: TPasteMode read FPasteMode write SetPasteMode;
property EditorMode: integer read FEditorMode write SetEditorMode;
property Generation: integer read GetGeneration write SetGeneration;
property XScroll: integer read GetXScroll write SetXScroll;
property YScroll: integer read GetYScroll write SetYScroll;
property SmallScroll: integer read FSmallScroll write FSmallScroll default 2;
property HandScroll: integer read FHandScroll write FHandScroll default 2;
property Canvas;
property CelColor: TColor read FCelColor write SetCelColor;
property GridColor: TColor read FGridColor write SetGridColor;
property Grid2Color: TColor read FGrid2Color write SetGrid2Color;
property BackColor: TColor read FBackColor write SetBackColor;
property SelRectColor: TColor read GetSelRectColor write SetSelRectColor;
property ZoomRectColor: TColor read GetZoomRectColor write SetZoomRectColor;
property DragColor: TColor read FDragColor write SetDragColor;
property TorusColor: TColor read FTorusColor write SetTorusColor;
property PixelsPerCel: integer read GetPixelsPerCel write SetPixelsPerCel;
property FrameDropTime: integer read GetFrameDropTime write FFrameDropTime;
property Grid: Boolean read FGrid write SetGrid default false;
property BoldGridSpacing: integer read FBoldGridSpacing write SetBoldGridSpacing;
property ScreenSaverActive: Boolean read FScreenSaverActive write
SetScreenSaverActive;
property Limit: TRect read GetLimit write SetLimit;
property IsLimited: boolean read FIsLimited write SetIsLimited;
property TorusKind: TTorusKind read GetTorusKind write SetTorusKind;
property DeadEdges: boolean read GetDeadEdges write SetDeadEdges;
property IsCounting: boolean read GetIsCounting;
published
property OnPaint: TNotifyEvent read FOnPaint write FOnPaint;
property OnMustPause: TNotifyEvent read FOnMustPause write FOnMustPause;
property OnChangePattern: TChangePatternEvent read FOnChangePattern
write FOnChangePattern;
property OnEditorModeChange: TNotifyEvent read FOnEditorModeChange
write FOnEditorModeChange;
property OnPasteModeChange: TNotifyEvent read FOnPasteModeChange
write FOnPasteModeChange;
property OnRuleChange: TRuleChangeEvent read FOnRuleChange write SetOnRuleChange;
property OnSaveProgress: TSaveProgressEvent read FOnSaveProgress write SetOnSaveProgress;
property OnDropFiles: TDropFileEvent read FOnDropFiles write FOnDropFiles;
property OnZoomChange: TNotifyEvent read FOnZoomChange write FOnZoomChange;
property OnShowDrag: TShowDragEvent read FOnShowDrag write FOnShowDrag;
property OnSelectionChange: TNotifyEvent read FOnSelectionChange write FOnSelectionChange;
property AfterInfoChange: TNotifyEvent read FAfterInfoChange write SetAfterInfoChange;
property OnKeyPress;
property OnResize;
end;
procedure Register;
procedure DisplayChange(BitsPerPixel,Width,Height: integer);
implementation
uses
System.Types, System.UITypes;
const
IsOleActive: boolean = false;
procedure Register;
begin
RegisterComponents('Johan', [TLifeBox]);
end;
procedure SwapPoints(var a,b: integer);
var
Swapper: integer;
begin
Swapper:= a;
a:= b;
b:= Swapper;
end;
constructor TDropSource.Create(AParent: TLifeBox; ATarget: TDropTarget);
begin
inherited Create;
MyLifeBox:= AParent;
MyTarget:= ATarget;
MouseButton:= 0;
//SystemParametersInfo(SPI_GETKEYBOARDDELAY,0,@KeyboardDelay,0)
KeyboardDelay:= 300;
end;
destructor TDropSource.Destroy;
begin
while (RefCount > 1) do _Release;
inherited Destroy;
end;
function TDropSource.QueryContinueDrag(fEscapePressed: BOOL;
grfKeyState: Longint): HResult;
type
TKeys = set of byte;
var
APos: TPoint;
Offset: integer;
Keys: TKeys;
CurrentTickCount: integer;
EnterPressed: Boolean;
KeyState: TKeyboardstate;
const
TimeTillRepeating: integer = 0;
procedure CheckKeyBoard;
begin
GetKeyBoardState(KeyState);
if (KeyState[vk_Left] and $fe) <> 0 then Keys:= Keys + [vk_Left];
if (KeyState[vk_Right] and $fe) <> 0 then Keys:= Keys + [vk_Right];
if (KeyState[vk_Down] and $fe) <> 0 then Keys:= Keys + [vk_Down];
if (KeyState[vk_Up] and $fe) <> 0 then Keys:= Keys + [vk_Up];
if (KeyState[vk_numpad1] and $fe) <> 0 then Keys:= Keys + [vk_Left,vk_Down];
if (KeyState[vk_numpad2] and $fe) <> 0 then Keys:= Keys + [vk_Down];
if (KeyState[vk_numpad3] and $fe) <> 0 then Keys:= Keys + [vk_Down,vk_Right];
if (KeyState[vk_numpad4] and $fe) <> 0 then Keys:= Keys + [vk_left];
if (KeyState[vk_numpad6] and $fe) <> 0 then Keys:= Keys + [vk_Right];
if (KeyState[vk_numpad7] and $fe) <> 0 then Keys:= Keys + [vk_up,vk_Left];
if (KeyState[vk_numpad8] and $fe) <> 0 then Keys:= Keys + [vk_up];
if (KeyState[vk_numpad9] and $fe) <> 0 then Keys:= Keys + [vk_up,vk_Right];
//Store the keys the droptarget should handle in FKey.
//Dummy:= GetAsyncKeyState(vk_X);
if (KeyState[vk_X] and $fe) <> 0 then fKey:= vk_x;
if (KeyState[vk_y] and $fe) <> 0 then fKey:= vk_y;
if (KeyState[vk_1] and $fe) <> 0 then fKey:= vk_1;
if (KeyState[vk_2] and $fe) <> 0 then fKey:= vk_2;
if (KeyState[vk_L] and $fe) <> 0 then fKey:= vk_2;
if (KeyState[vk_9] and $fe) <> 0 then fKey:= vk_9;
if (KeyState[vk_R] and $fe) <> 0 then fKey:= vk_9;
if FKey <> 0 then begin
if assigned(MyTarget) then MyTarget.KeyPressed:= FKey;
FKey:= 0;
end;
end;
begin
GetCursorPos(APos);
//Check the enter key. if it is pressed, then drop.
Enterpressed:= ((GetASyncKeyState(vk_Return) and $FFFE) <> 0);
//Now check the cursor keys.
Keys:= [];
CheckKeyboard;
if (Keys <> []) then begin
CurrentTickCount:= MyGetTickCount;
if (TimeTillRepeating = 0) or (TimeTillRepeating <= CurrentTickCount) then begin
if TimeTillRepeating = 0 then
TimeTillRepeating:= CurrentTickCount + KeyboardDelay;
Offset:= MyLifeBox.FPixelsPerCel;
if (vk_Right in Keys) then Inc(APos.x,Offset);
if (vk_Left in Keys) then Dec(APos.x,Offset);
if (vk_Down in Keys) then Inc(APos.y,Offset);
if (vk_Up in Keys) then Dec(APos.y,Offset);
SetCursorPos(APos.x,APos.y);
end; {if}
end {if}
else TimeTillRepeating:= 0;
Result:= S_OK;
//if both mousebuttons are pressed (shord click), cancel the drag.
if ((grfKeyState and (MK_LButton or MK_RButton)) =
(MK_LButton or MK_RButton)) then
Result:= DRAGDROP_S_CANCEL
//also if <esc> is pressed, cancel.
else if fEscapePressed then Result:= DRAGDROP_S_CANCEL;
//if the button released is the same one that started the drag
//then drop the bomb.
if Result = S_OK then begin
if ((MouseButton and MK_LButton) = MK_LButton) and
((grfKeyState and MK_LButton) <> MK_LButton) then
Result:= DRAGDROP_S_DROP
else if ((MouseButton and MK_RButton) = MK_RButton) and
((grfKeyState and MK_RButton) <> MK_RButton) then
Result:= DRAGDROP_S_DROP
else if EnterPressed then Result:= DRAGDROP_S_DROP;
end;
if Result = S_OK then MouseButton:= grfKeyState
else MouseButton:= 0;
end;
function TDropSource.GiveFeedback(dwEffect: Longint): HResult;
begin
//This is a bit of a hack, more advanced stuff will follow.
Result:= DRAGDROP_S_USEDEFAULTCURSORS;
end;
{IDataObject}
constructor TDataObject.Create(AParent: TLifeBox; ACutOut: TUniverse);
begin
inherited Create;
MyLifeBox:= AParent;
CutOut:= ACutOut;
CutOut.AddRef;
end;
procedure TDataObject.BeforeDestruction;
begin
{do nothing}
end;
destructor TDataObject.Destroy;
begin
CutOut.Release;
inherited Destroy;
end;
function TDataObject.GetLifeBoxClipRect: TRect;
begin
if Assigned(CutOut) then Result:= CutOut.ClipRect
else Result:= Rect(0,0,0,0);
end;
function TDataObject.GetCutOut: TUniverse;
begin
Result:= CutOut;
end;
function TDataObject.IsSpecialDrag: WordBool;
begin
Result:= false;
end;
function TDataObject.SourceID: integer;
begin
Result:= integer(MyLifeBox.SourceID);
end;
{IDataObject}
function TDataObject.GetData(const formatetcIn: TFormatEtc; out medium: TStgMedium):
HResult;
var
AText: TStringList;
ABitmap: TBitmap;
TempText: PChar;
Size: integer;
Dest: PChar;
begin
Result:= S_OK;
AText:= nil;
ABitmap:= nil;
Medium.unkForRelease:= nil;
with FormatetcIn do begin {FormatETC}
case cfFormat of
cf_Text, CF_OEMText, cf_Locale: begin
AText:= CutOut.SaveToStringList(smDefault,false);
end;
cf_Bitmap, CF_DIB: begin
ABitmap:= CutOut.SaveToBitmap;
if cfFormat = cf_DIB then ABitmap.HandleType:= bmDIB
else ABitmap.HandleType:= bmDDB;
end;
else Result:= E_INVALIDARG;
end; {case}
if Result = S_OK then begin
if ((TYMED and TYMED_HGlobal) = TYMED_HGlobal) then case cfFormat of
cf_Text, cf_OEMText, cf_Locale: begin
TempText:= AText.GetText; //HGlobal can be a pointer.
Size:= StrLen(TempText);
if Medium.HGlobal = 0 then Medium.HGlobal:= GlobalAlloc(GHnd, Size+1);
if GlobalSize(Medium.HGlobal) <= Size then Result:= STG_E_MEDIUMFULL
else try
Dest:= GlobalLock(Medium.hGlobal);
CopyMemory(Dest,TempText,Size+1);
Medium.tymed:= TYMED_HGlobal;
finally GlobalUnLock(Medium.hGlobal);
end; {else try}
end; {cf_Text..}
cf_Bitmap, cf_DIB: Result:= E_INVALIDARG;
end {if TYMED_HGlobal}
else if ((TYMED and TYMED_GDI) = TYMED_GDI) then case cfFormat of
cf_Text, cf_OEMText, cf_Locale: Result:= E_INVALIDARG;
cf_Bitmap, cf_DIB: begin
Medium.hBitmap:= ABitmap.Handle;
Medium.tymed:= tymed_GDI;
end; {cf_Bitmap..}
end {else}
else Result:= E_INVALIDARG;
end; {if}
end; {with}
AText.Free;
ABitmap.Free;
end;
function TDataObject.GetDataHere(const formatetc: TFormatEtc; out medium: TStgMedium):
HResult;
begin
Result:= GetData(formatetc, medium);
end;
function TDataObject.QueryGetData(const formatetc: TFormatEtc): HResult;
begin
with Formatetc do begin
case cfFormat of
cf_Text, cf_Bitmap, CF_OEMTEXT, CF_DIB, CF_LOCALE: Result:= S_OK;
else Result:= E_INVALIDARG;
end; {case}
if (Result = S_OK) then begin
if ((TYMED and TYMED_HGlobal) = TYMED_HGlobal) then {OK}
else if ((TYMED and TYMED_GDI) = TYMED_GDI) then begin
if ((cfFormat <> CF_Bitmap) and (cfFormat <> cf_DIB)) then
Result:= E_INVALIDARG
end
else Result:= E_INVALIDARG;
end; {if}
end; {with}
end;
function TDataObject.SetData(const formatetc: TFormatEtc; var medium: TStgMedium;
fRelease: BOOL): HResult;
begin
Result:= E_NOTIMPL;
end;
function TDataObject.EnumFormatEtc(dwDirection: Longint; out enumFormatEtc:
IEnumFormatEtc): HResult;
begin
//Let the OLE lib do the enumeration from the Registry.
Result:= OLE_S_USEREG;
end;
function TDataObject.GetCanonicalFormatEtc(const formatetc: TFormatEtc;
out formatetcOut: TFormatEtc): HResult;
begin
formatetcOut:= PFormatETC(nil)^;
Result:= DATA_S_SAMEFORMATETC;
end;
function TDataObject.DAdvise(const formatetc: TFormatEtc; advf: Longint;
const advSink: IAdviseSink; out dwConnection: Longint): HResult;
begin
Result:= OLE_E_ADVISENOTSUPPORTED;
end;
function TDataObject.DUnadvise(dwConnection: Longint): HResult;
begin
Result:= OLE_E_ADVISENOTSUPPORTED;
end;
function TDataObject.EnumDAdvise(out enumAdvise: IEnumStatData): HResult;
begin
Result:= OLE_E_ADVISENOTSUPPORTED;
end;
constructor TDropTarget.Create(AParent: TLifeBox);
begin
inherited Create;
MyLifeBox:= AParent;
CanAccept:= false;
end;
destructor TDropTarget.Destroy;
begin
while (RefCount > 1) do _Release;
inherited Destroy;
end;
function TDropTarget.GetTarget(Cursorpt: TPoint): TPoint;
begin
Result:= Cursorpt;
Result:= MyLifeBox.ScreenToClient(Result);
with Result do begin
MyLifeBox.ClientToCel(x,y);
with MyLifeBox do begin
Inc(x,DropOffset.x);
Inc(y,DropOffset.y);
end; {with}
end; {with}
end;
function TDropTarget.DragEnter(const dataObj: IDataObject; grfKeyState: Longint;
pt: TPoint; var dwEffect: Longint): HResult;
var
FormatETC: TFormatETC;
Life32CutOut: ILife32CutOut;
begin
with FormatETC do begin
cfFormat:= cf_text;
ptd:= nil;
dwAspect:= DVASPECT_CONTENT;
lindex:= -1;
tymed:= TYMED_HGLOBAL;
end; {with}
CanAccept:= dataObj.QueryGetData(FormatETC) = S_OK;
if CanAccept then with Target do begin
Target:= GetTarget(pt);
dataObj.QueryInterface(ILife32CutOut,Life32CutOut);
if Assigned(Life32CutOut) then begin
//Life32CutOut:= dataObj as ILife32CutOut;
if ((grfKeyState and MK_Control) = MK_Control) then dwEffect:= DROPEFFECT_COPY
else dwEffect:= DROPEFFECT_MOVE;
IsSpecialDrag:= Life32CutOut.IsSpecialDrag or (Life32CutOut.SourceID <> MyLifeBox.SourceID);
if IsSpecialDrag then dwEffect:= DROPEFFECT_COPY;
ClipRect:= Life32CutOut.GetLifeBoxClipRect;
with ClipRect do OffsetRect(ClipRect,-Left, -Top);
MyCutOut:= Life32CutOut.GetCutOut;
DrawRect:= ClipRect;
OffsetRect(DrawRect,x,y);
MyLifeBox.DrawDropRect(DrawRect);
MyLifeBox.DisplayCutOut(Target,MyCutOut);
end
else begin
if ((grfKeyState and MK_Shift) = MK_Shift) then dwEffect:= DROPEFFECT_MOVE
else dwEffect:= DROPEFFECT_COPY;
MyLifeBox.DrawDropTarget(x,y);
ClipRect:= Rect(0,0,0,0);
DrawRect:= ClipRect;
end; {else}
end {if}
else dwEffect:= DROPEFFECT_NONE;
OldPt:= Target;
Result:= S_OK;
end;
function TDropTarget.DragOver(grfKeyState: Longint; pt: TPoint;
var dwEffect: Longint): HResult;
type
TKeys = set of byte;
var
Keys: TKeys;
procedure ManipulateCutOut;
begin
if (KeyPressed = vk_x) then MyCutOut.FlipX
else if (KeyPressed = vk_y) then MyCutOut.FlipY
else if (KeyPressed = vk_1) then MyCutOut.Rotate180
else if (KeyPressed = vk_2) then MyCutOut.Rotate270
else if (KeyPressed = vk_9) then MyCutOut.Rotate90;
end;
begin
Keys:= [];
if CanAccept then with Target do begin
//Second draw clears, not while playing tough Mmm.
if not IsMyRectEmpty(ClipRect) then begin
Target:= GetTarget(pt);
if (Target.x <> OldPt.x) or (Target.y <> OldPt.y) or (KeyPressed <> 0) then begin
MyLifeBox.DrawDropRect(DrawRect); //Erase old dropRect;
MyLifeBox.DisplayCutOut(OldPt,MyCutOut);
ManipulateCutOut;
MyLifeBox.DoAutoScroll(pt,false);
DrawRect:= MyCutOut.ClipRect;
OffsetRect(DrawRect,-DrawRect.Left, -DrawRect.Top);
OffsetRect(DrawRect,x,y);
//with MyLifeBox.DropOffset do OffsetRect(DrawRect,x,y);
oldPt:= Point(x,y);
MyLifeBox.DisplayCutOut(OldPt,MyCutOut);
MyLifeBox.DrawDropRect(DrawRect);
end; {if}
if ((grfKeyState and MK_Control) = MK_Control) then
dwEffect:= DROPEFFECT_COPY
else dwEffect:= DROPEFFECT_MOVE;
if IsSpecialDrag then begin
dwEffect:= DROPEFFECT_COPY;
end;
end {if}
else begin
if ((grfKeyState and MK_Shift) = MK_Shift) then
dwEffect:= DROPEFFECT_MOVE
else dwEffect:= DROPEFFECT_COPY;
Target:= GetTarget(pt);
if (Target.x <> OldPt.x) or (Target.y <> OldPt.y) then begin
MyLifeBox.DrawDropTarget(OldPt.x,OldPt.y);
MyLifeBox.DrawDropTarget(x,y);
end; {if}
end; {else}
end {if}
else dwEffect:= DROPEFFECT_NONE;
OldPt:= Target;
Result:= S_OK;
end;
function TDropTarget.DragLeave: HResult;
begin
CanAccept:= false;
MyLifeBox.DrawDropRect(DrawRect); //Erase old dropRect;
if Assigned(MyCutOut) then begin
MyLifeBox.DisplayCutOut(OldPt,MyCutOut);
MyCutOut:= nil;
end;
Result:= S_OK;
If Assigned(MyLifeBox.OnEndDrag) then MyLifeBox.OnEndDrag(MyLifeBox,MyLifeBox,0,0);
end;
function TDropTarget.Drop(const dataObj: IDataObject; grfKeyState: Longint;
pt: TPoint; var dwEffect: Longint): HResult;
const
MK_Alt = 32;
var
Life32CutOut: ILife32CutOut;
Status: HResult;
DataSource: PChar;
LifeLines: TStringList;
CutOut: TUniverse;
TryRect: TRect;
CanPaste: Boolean;
x,y: integer;
function GetCutoutViaOLE: HResult;
var
FormatETC: TFormatETC;
StgMedium: TStgMedium;
begin
if ((grfKeyState and MK_Shift) = MK_Shift) then dwEffect:= DROPEFFECT_MOVE
else dwEffect:= DROPEFFECT_COPY;
with FormatETC do begin
cfFormat:= cf_text;
ptd:= nil;
dwAspect:= DVASPECT_CONTENT;
lindex:= -1;
tymed:= TYMED_HGLOBAL;
end; {with}
status:= dataObj.QueryGetData(FormatETC);
if status = S_OK then begin
status:= dataObj.GetData(FormatEtc,StgMedium);
if Status = S_OK then with StgMedium do try
DataSource:= GlobalLock(hGlobal);
if Assigned(DataSource) then try
LifeLines:= TStringList.Create;
LifeLines.SetText(DataSource);
CutOut:= TUniverse.Create(MyLifeBox.Universe.RuleString,
MyLifeBox.Universe.Neighborhood);
CutOut.OnRuleChange:= MyLifeBox.Universe.OnRuleChange;
CutOut.LoadFromStringList(LifeLines);
finally GlobalUnlock(hGlobal);
end; {try}
finally ReleaseStgMedium(StgMedium);
end; {with}
end;
Result:= Status;
end; {GetCutoutViaOLE}
begin
try
Status:= -1; //negative values indicate failure
Life32CutOut:= dataObj as ILife32CutOut;
if ((grfKeyState and MK_Control) = MK_Control) then dwEffect:= DROPEFFECT_COPY
else dwEffect:= DROPEFFECT_MOVE;
//if we use the scrapbook drag, always copy.
if IsSpecialDrag then dwEffect:= DROPEFFECT_COPY;
CutOut:= Life32CutOut.GetCutOut;
Status:= S_OK;
except
Status:= GetCutoutViaOLE;
end; {try}
if Status = S_OK then begin
if (grfKeyState and MK_Alt) = MK_Alt then MyLifeBox.Clear(true,false,true);
pt:= MyLifeBox.ScreenToClient(pt);
x:= pt.x; y:= pt.y;
MyLifeBox.ClientToCel(x,y);
Inc(x,MyLifeBox.DropOffset.x);
Inc(y,MyLifeBox.DropOffset.y);
CanPaste:= true;
if (MyLifeBox.PasteMode = lpmError) then with CutOut.ClipRect do begin
TryRect:= Rect(x,y,x+Right-Left,y+Bottom-Top);
MyLifeBox.Universe.ShrinkSelRect(TryRect);
if not IsMyRectEmpty(TryRect) then CanPaste:= false;
end; {if}
if CanPaste then begin
if dwEffect = DROPEFFECT_MOVE then begin
MyLifeBox.ClearSelection;
//MyLifeBox.Universe.FillRect(MyLifeBox.FSelectionRect, faClear);
end; {if}
with CutOut.ClipRect do //!!!!!!!!!!!!!!@@@@@@@@@@@
MyLifeBox.SelectionRect:= Rect(x,y,x+Abs(Right-Left),y+Abs(Bottom-Top));
MyLifeBox.InsertShape(CutOut);
if Assigned(MyLifeBox.OnDragDrop) then MyLifeBox.OnDragDrop(MyLifeBox,MyLifeBox,0,0);
end; {if CanPaste}
MyLifeBox.RedrawAll; //Also draws selectionRect.
end; {if}
Result:= Status;
end;
procedure TDropTarget.SetKeyPressed(value: byte);
var
CursorPos: TPoint;
Dummy: longint;
begin
FKeyPressed:= value;
GetCursorPos(CursorPos);
DragOver(0,CursorPos,dummy);
FKeyPressed:= 0;
end;
constructor TLifeBox.Create(AOwner: TComponent);
var
Ole_Error: HResult;
begin
inherited Create(AOwner);
SourceID:= Random(MaxInt);
if not(csDesigning in ComponentState) then begin
Application.Tag:= 1; {enable DDraw}
if not IsOLEActive then begin
Ole_error:= OleInitialize(nil);
IsOleActive:= (Ole_error = S_OK) or (Ole_error = S_False);
if not(IsOleActive) then ShowMessage(IntToStr(Ole_Error));
end; {if}
end; {if}
Parent:= TWinControl(AOwner);
FCanPaint:= cpInit;
//Generation:= 0; objects vars are automatically set to 0.
//FFrameDropTime:= 0; //no framedropping
FOnPaint:= nil;
FPixelsPerCel:= 2;
Fgrid:= false;
FPasteMode:= lpmOr;
FScrolling:= false;
FEditorMode:= emSelect;
FSmallScroll:= 2;
FHandScroll:= 2;
FBoldGridSpacing:= DefaultBoldGridSpacing;
FUniverse:= TUniverse.Create('',nbDefault);
Universe.OnRuleChange:= OnRuleChange;
CursorDirty:= true;
ControlStyle:= ControlStyle + [csOpaque];
if IsOleActive then FDropTarget:= TDropTarget.Create(Self);
if IsOleActive then FDropSource:= TDropSource.Create(Self, FDropTarget);
OnPaint:= DoPaint;