-
Notifications
You must be signed in to change notification settings - Fork 8
/
GR32_ElasticLayers.pas
3002 lines (2441 loc) · 80.5 KB
/
GR32_ElasticLayers.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 GR32_ElasticLayers;
(* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1 or LGPL 2.1 with linking exception
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* Alternatively, the contents of this file may be used under the terms of the
* Free Pascal modified version of the GNU Lesser General Public License
* Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
* of this license are applicable instead of those above.
* Please see the file LICENSE.txt for additional information concerning this
* license.
*
* The Original Code is Elastic Layer for Graphics32
*
* The Initial Developer of the Original Code is
* Fathony Luthfillah - www.x2nie.com
*
*
* Portions created by the Initial Developer are Copyright (C) 2014
* the Initial Developer. All Rights Reserved.
*
* The code was partially taken from GR32_Layers.pas
* www.graphics32.org
* http://graphics32.org/documentation/Docs/Units/GR32_Layers/_Body.htm
*
* The code was partially taken from GR32_ExtLayers.pas by
* Mike Lischke www.delphi-gems.com www.lischke-online.de
*
* Contributor(s):
*
*
* ***** END LICENSE BLOCK ***** *)
interface
uses
Windows, Classes, SysUtils, Controls, Forms, Graphics,
GR32_Types, GR32, GR32_Image, GR32_Layers, GR32_Transforms, GR32_Polygons,
GR32_Add_BlendModes;
const
// These states can be entered by the rubber band layer.
{
WIND DIRECTION: INDEX: normal cursor: rotated cursor:
^ ^
NW N NE 0 4 1 \ ^ / \ E
W C E 7 8 5 <-W E-> <- ->
SW S SE 3 6 2 / v \ W \
v v
9 CELLS:
--------------
0 1 2
3 4 5
6 7 8
}
ZoneToClockwiseIndex : array[0..8] of Integer = (0, 4, 1, 7, 8, 5, 3, 6, 2);
type
TTicDragState = (
{tdsResizeNW, tdsResizeN, tdsResizeNE,
tdsResizeE, tdsResizeSE,
tdsResizeS, tdsResizeSW, tdsResizeW,
tdsSheerN, tdsSheerE, tdsSheerS, tdsSheerW,}
tdsMoveLayer, tdsMovePivot,
tdsResizeCorner, tdsRotate,
tdsResizeSide, tdsSkew,
tdsDistortion, tdsPerspective,
tdsNone
);
TCursorDirection = (
cdNotUsed,
cdNorthWest,
cdNorth,
cdNorthEast,
cdEast,
cdSouthEast,
cdSouth,
cdSouthWest,
cdWest
);
{
TTicDragState = (
tdsResizeNW, tdsResizeNE, tdsResizeSE, tdsResizeSW,
tdsResizeN, tdsResizeE, tdsResizeS, tdsResizeW,
tdsSheerN, tdsSheerE, tdsSheerS, tdsSheerW,
tdsMoveLayer, tdsMovePivot,
tdsRotate,
tdsNone
);
}
{
TTicDragState = (
tdsNone, tdsMoveLayer, tdsMovePivot,
tdsResizeN, tdsResizeNE, tdsResizeE, tdsResizeSE,
tdsResizeS, tdsResizeSW, tdsResizeW, tdsResizeNW,
tdsSheerN, tdsSheerE, tdsSheerS, tdsSheerW,
tdsRotate
);
}
TExtRubberBandOptions = set of (
rboAllowPivotMove,
rboAllowCornerResize,
rboAllowEdgeResize,
rboAllowMove,
rboAllowRotation,
rboShowFrame,
rboShowHandles
);
const
DefaultRubberbandOptions = [rboAllowCornerResize, rboAllowEdgeResize, rboAllowMove,
rboAllowRotation, rboShowFrame, rboShowHandles];
type
TTicTransformation = class;
TElasticLayer = class(TCustomLayer)
private
FScaled: Boolean;
FCropped: Boolean;
function GetTic(index: Integer): TFloatPoint;
procedure SetTic(index: Integer; const Value: TFloatPoint);
procedure SetScaled(const Value: Boolean);
procedure SetCropped(const Value: Boolean);
procedure SetEdges(const Value: TArrayOfFloatPoint);
function GetSourceRect: TFloatRect;
procedure SetSourceRect(const Value: TFloatRect);
function GetEdges: TArrayOfFloatPoint;
protected
FTransformation : TTicTransformation {T3x3Transformation}; //Non ViewPort world
FInViewPortTransformation : TTicTransformation ; //used in Paint() and MouseMove
//function Matrix : TFloatMatrix; //read only property
//FTic : array[0..3] of TFloatPoint;
//FQuadX: array [0..3] of TFloat;
//FQuadY: array [0..3] of TFloat;
//FEdges: TArrayOfFloatPoint;
procedure DoSetEdges(const Value: TArrayOfFloatPoint); virtual;
public
constructor Create(ALayerCollection: TLayerCollection); override;
destructor Destroy; override;
function GetScaledRect(const R: TFloatRect): TFloatRect; virtual;
function GetScaledEdges : TArrayOfFloatPoint;
procedure SetBounds(APosition: TFloatPoint; ASize: TFloatPoint); overload;
procedure SetBounds(ABoundsRect: TFloatRect); overload;
property Tic[index : Integer] : TFloatPoint read GetTic write SetTic; // expected index: 0 .. 3
published
property Edges: TArrayOfFloatPoint read GetEdges write SetEdges;
property Scaled: Boolean read FScaled write SetScaled;
property SourceRect : TFloatRect read GetSourceRect write SetSourceRect;
property Cropped: Boolean read FCropped write SetCropped;
end;
TElasticBitmapLayer = class(TElasticLayer)
private
FBitmap: TBitmap32;
FBlendMode: TBlendMode32;
procedure BitmapChanged(Sender: TObject);
procedure SetBitmap(const Value: TBitmap32);
procedure SetBlendMode(const Value: TBlendMode32);
protected
function DoHitTest(X, Y: Integer): Boolean; override;
procedure Paint(Buffer: TBitmap32); override;
public
constructor Create(ALayerCollection: TLayerCollection); override;
destructor Destroy; override;
//procedure PaintTo(Buffer: TBitmap32; const R: TRect);
published
property Bitmap: TBitmap32 read FBitmap write SetBitmap;
property BlendMode : TBlendMode32 read FBlendMode write SetBlendMode;
end;
TElasticRubberBandLayer = class(TElasticLayer)
private
FChildLayer: TElasticLayer;
// Drag/resize support
FIsDragging: Boolean;
FOldEdges : TArrayOfFloatPoint;
FDragState: TTicDragState;
FCompass : Integer;
{FOldPosition: TFloatPoint; // Keep the old values to restore in case of a cancellation.
FOldScaling: TFloatPoint;
FOldPivot: TFloatPoint;
FOldSkew: TFloatPoint;
FOldAbsAnchor, FOldAnchor : TFloatPoint;
FOldAngle: Single;}
FDragPos: TPoint; // set on mouseDown
FOriginDragPos,
FOldOriginPivot,
FOldInViewPortPivot,
FOldTransformedPivot : TFloatPoint;
FMouseOverPos : TPoint; // set on mouseMove
FThreshold: Integer;
FPivotPoint: TFloatPoint;
FOptions: TExtRubberBandOptions;
FHandleSize: Integer;
FHandleFrame: TColor;
FHandleFill: TColor;
procedure SetChildLayer(const Value: TElasticLayer);
procedure SetOptions(const Value: TExtRubberBandOptions);
procedure SetHandleFill(const Value: TColor);
procedure SetHandleFrame(const Value: TColor);
procedure SetHandleSize(Value: Integer);
procedure SetPivotOrigin(Value: TFloatPoint);
protected
//function DoHitTest(X, Y: Integer): Boolean; override;
procedure DoSetEdges(const Value: TArrayOfFloatPoint); override;
//function GetHitCode(X, Y: Integer; Shift: TShiftState): TTicDragState;
function GetRotatedCompass(LocalCompas: Integer) : Integer;
function GetPivotOrigin : TFloatPoint;
function GetPivotTransformed : TFloatPoint;
function GetRotatedEdges( AEdges: TArrayOfFloatPoint; dx,dy : TFloat):TArrayOfFloatPoint;
procedure Paint(Buffer: TBitmap32); override;
procedure SetLayerOptions(Value: Cardinal); override;
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;
public
constructor Create(ALayerCollection: TLayerCollection); override;
//destructor Destroy; override;
property ChildLayer: TElasticLayer read FChildLayer write SetChildLayer;
property Options: TExtRubberBandOptions read FOptions write SetOptions default DefaultRubberbandOptions;
property HandleSize: Integer read FHandleSize write SetHandleSize default 3;
property HandleFill: TColor read FHandleFill write SetHandleFill default clWhite;
property HandleFrame: TColor read FHandleFrame write SetHandleFrame default clBlack;
property PivotPoint: TFloatPoint read FPivotPoint write FPivotPoint;
property Threshold: Integer read FThreshold write FThreshold default 8;
end;
// A TProjectiveTransformation that doesnt store the values in itself
TTicTransformation = class(T3x3Transformation)
private
FEdges: TArrayOfFloatPoint;
FMiddleEdges: TArrayOfFloatPoint;
FMiddleEdgesValid : Boolean;
procedure SetEdges(const Value: TArrayOfFloatPoint);
protected
//FOwner : TTicLayer;
procedure AssignTo(Dest: TPersistent); override;
procedure PrepareTransform; override;
procedure ReverseTransformFixed(DstX, DstY: TFixed; out SrcX, SrcY: TFixed); override;
procedure ReverseTransformFloat(DstX, DstY: TFloat; out SrcX, SrcY: TFloat); override;
procedure TransformFixed(SrcX, SrcY: TFixed; out DstX, DstY: TFixed); override;
procedure TransformFloat(SrcX, SrcY: TFloat; out DstX, DstY: TFloat); override;
public
//constructor Create(AOwner: TTicLayer); virtual;
constructor Create; virtual;
function GetTransformedBounds(const ASrcRect: TFloatRect): TFloatRect; override;
function GetMiddleEdges: TArrayOfFloatPoint;
//procedure Scale(Sx, Sy: TFloat); //overload;
//procedure Scale(Value: TFloat); overload;
//procedure Translate(Dx, Dy: TFloat);
property Edges: TArrayOfFloatPoint read FEdges write SetEdges;
end;
implementation
uses
Math, GR32_Blend, GR32_LowLevel, GR32_Math, GR32_Bindings,
GR32_Resamplers;
type
TLayerCollectionAccess = class(TLayerCollection);
TImage32Access = class(TCustomImage32);
var
UPivotBitmap : TBitmap32 = nil;
function GetPivotBitmap : TBitmap32;
begin
if not Assigned(UPivotBitmap) then
begin
UPivotBitmap := TBitmap32.Create;
UPivotBitmap.SetSize(16,16);
UPivotBitmap.Clear(0);
UPivotBitmap.DrawMode := dmBlend;
DrawIconEx(UPivotBitmap.Handle, 0,0, Screen.Cursors[crGrCircleCross], 0, 0, 0, 0, DI_NORMAL);
end;
Result := UPivotBitmap;
end;
function SafelyGetEdgeIndex(AIndex: integer):integer;
begin
Result := AIndex;
while Result < 0 do
inc(Result,4);
while Result > 3 do
dec(Result, 4);
end;
function EdgesToFloatRect(AEdges: TArrayOfFloatPoint): TFloatRect ;
// Rotation supported
begin
Result.Left := Min(Min(AEdges[0].X, AEdges[1].X), Min(AEdges[2].X, AEdges[3].X));
Result.Right := Max(Max(AEdges[0].X, AEdges[1].X), Max(AEdges[2].X, AEdges[3].X));
Result.Top := Min(Min(AEdges[0].Y, AEdges[1].Y), Min(AEdges[2].Y, AEdges[3].Y));
Result.Bottom := Max(Max(AEdges[0].Y, AEdges[1].Y), Max(AEdges[2].Y, AEdges[3].Y));
end;
function MiddlePointOf2Lines(const x1,x2 : TFloat;
divo: Integer = 2; mulo : Integer = 1): TFloat; overload;
begin
if x1 < x2 then
Result := x1 + (x2 - x1) / divo * mulo
else
Result := x2 + (x1 - x2) / divo * mulo;
end;
function MiddlePointOf2Lines(const x1,y1,x2,y2 : TFloat;
divo: Integer = 2; mulo : Integer = 1): TFloatPoint; overload;
begin
if x1 < x2 then
Result.X := x1 + (x2 - x1) / divo * mulo
else
Result.X := x2 + (x1 - x2) / divo * mulo;
if y1 < y2 then
Result.Y := y1 + (y2 - y1) / divo * mulo
else
Result.Y := y2 + (y1 - y2) / divo * mulo;
end;
function MiddlePointOf2Lines(const P1,P2 : TFloatPoint;
divo: Integer = 2; mulo : Integer = 1): TFloatPoint; overload;
begin
Result := MiddlePointOf2Lines(P1.X, P1.Y, P2.X, P2.Y, divo, mulo );
{
x1 := Min(A.X, B.X);
y1 := Min(A.Y, B.Y);
x2 := Max(A.X, B.X);
y2 := Max(A.Y, B.Y);
//Result.X := x1 -1 + (x2 - x1 + 1) /2; //precise pixels width
//Result.Y := y1 -1 + (y2 - y1 + 1) /2; //precise pixels height
Result.X := x1 + (x2 - x1 ) /2;
Result.Y := y1 + (y2 - y1 ) /2;
}
end;
function CentroidOf(AEdges: TArrayOfFloatPoint): TFloatPoint ;
// use bounding. non skew, non perspective
begin
with EdgesToFloatRect(AEdges) do
begin
Result.X := Left + (Right - Left) / 2;
Result.Y := Top + (Bottom - Top) / 2;
end;
end;
function CenterOf(MiddleEdges: TArrayOfFloatPoint): TFloatPoint ;
// direct distance
// skew+distortion+perspective supported
begin
Result.X := MiddlePointOf2Lines(MiddleEdges[0].X, MiddleEdges[2].X);
Result.Y := MiddlePointOf2Lines(MiddleEdges[1].Y, MiddleEdges[3].Y);
end;
function DegreeOfBuggy(MidPoint,OppositePoint: TFloatPoint): TFloat;
var
Radians : TFloat;
begin
Radians := ArcTan2( OppositePoint.Y - MidPoint.Y, MidPoint.X - OppositePoint.X );
Result := RadToDeg( Radians );
end;
function DegreeOf(Center,OppositePoint: TFloatPoint): TFloat;
var
Radians : TFloat;
begin
//Radians := ArcTan2( OppositePoint.Y - MidPoint.Y, MidPoint.X - OppositePoint.X );
//Radians := ArcTan2( MidPoint.Y - OppositePoint.Y, OppositePoint.X - MidPoint.X );
Radians := ArcTan2( Center.Y - OppositePoint.Y, OppositePoint.X - Center.X );
//this follows degree quadrant used by GR32 to draw arc= WRONG.
//Result := RadToDeg( Radians ) * -1;
//this follows degree quadrant used by GR32 to rotate in TAffineTransformation = OKE!
Result := RadToDeg( Radians ) ;
while Result < 0 do
Result := Result + 360;
while Result > 360 do
Result := Result - 360;
end;
function PtIn9Zones(P: TFloatPoint; TT: TTicTransformation; var MouseInside : Boolean): Integer ; overload;
const FTreshold : TFloat = 8;
var
Mids,LEdges : TArrayOfFloatPoint;
Xdeg, Ydeg, prevDeg, nextDeg, dx,dy, compassDeg, half, pDeg : TFloat;
X,Y,Center : TFloatPoint;
AT : TAffineTransformation;
prev,next, cx,cy,Zone,Compass : Integer;
begin
AT := TAffineTransformation.Create;
Mids := TT.GetMiddleEdges;
LEdges := TT.Edges;
Xdeg := DegreeOf(Mids[3], Mids[1]);
AT.Rotate(Mids[3].X, Mids[3].Y, -Xdeg);
Y := AT.Transform(P);
Dy := Mids[3].Y - Y.Y;
if Abs(dy) <= FTreshold then
cy := 1
else
if Dy > 0 then
cy := 0
else
cy := 2;
Ydeg := DegreeOf(Mids[2], Mids[0]);
AT.Clear;
AT.Rotate(Mids[2].X, Mids[2].Y, -Ydeg);
X := AT.Transform(P);
Dx := Mids[2].Y - X.Y ;
if Abs(Dx) <= FTreshold then
cx := 1
else
if Dx > 0 then
cx := 0
else
cx := 2;
// get index in: left-to-right-then-bottom order
Zone := cx + cy * 3;
// get index
Compass := ZoneToClockwiseIndex[Zone];
Result := Compass;
MouseInside := PtInRect(TT.SrcRect, TT.ReverseTransform(P) );
// correct by toleranced-degree if cx=0 or cy=0 =========================================
// I am worry only about the range of Edges; its too wide.
// It also report as "false positive" due the middleEdges is too narrow,
// while calculated by FTreshold. So, this is the correction
if Compass <=3 then
begin
Center := CenterOf(Mids);
pDeg := Abs( DegreeOf(Center, P) );
compassDeg:= Abs( DegreeOf(Center, LEdges[Compass]) );
//debug
//MouseInside := pDeg < compassDeg;
//exit;
if pDeg > compassDeg then
begin
// compas = edge; prev&next = mid
prev := SafelyGetEdgeIndex(Compass -1);
prevDeg := Abs(DegreeOf(Center, Mids[prev]));
half := Abs( compassDeg - prevDeg) /2;
if pDeg > compassDeg + half then
Result := prev +4 ;
end
else
begin
next := SafelyGetEdgeIndex(Compass );
nextDeg := Abs( DegreeOf(Center, Mids[next]) );
half := Abs( compassDeg - nextDeg) /2;
if pDeg < nextDeg + half then
begin
Result := next +4 ;
end;
end;
end;
(*else
begin
// compas = mid; prev&next = edge
prev := SafelyGetEdgeIndex(Compass -4);
next := SafelyGetEdgeIndex(Compass -3);
prevDeg := DegreeOf(Center, Ledges[prev]);
nextDeg := DegreeOf(Center, Ledges[next]);
compassDeg:= DegreeOf(Center, Mids[SafelyGetEdgeIndex(Compass-4)]);
half := Abs( compassDeg - prevDeg) /2;
Result := prev ;
if Abs( pDeg - compassDeg) < half then
begin
Result := Compass
end
else
begin
Result := next;
half := Abs( compassDeg - nextDeg) /2;
if Abs( pDeg - compassDeg) < half then
begin
Result := Compass
end;
end;
end;
*)
end;
function PtIn9ZonesBuggy(P: TPoint; SrcRect: TRect; var MouseInside : Boolean): Integer ; overload;
// Non rotated world, cells clamped to range of [0..2]
{ 0 1 2
3 4 5
6 7 8 }
var
W,H, X,Y, cx, cy : Integer;
dx,dy : TFloat;
begin
// get bounds of whole grid
with SrcRect do
begin
W := Right - Left;
H := Bottom - Top;
// in case Transformation.SrcRect.TopLeft < Point(0,0)
X := P.X - Left;
Y := P.Y - Top;
end;
// get bounds of a cell.
// Precisely. don't div here
dx := W / 3;
dy := H / 3;
//get cell contained XY
cx := Floor(X / dx);
cy := Floor(Y / dy);
//detect wether mouse in rect
MouseInside := (cx in [0..2]) and (cy in [0..2]);
cx := Clamp(cx, 0, 2);
cy := Clamp(cy, 0, 2);
// get index in: left-to-right-then-bottom order
Result := cx + cy * 3;
end;
{function PtIn9Zones(X,Y:TFloat;OriginEdges: TArrayOfFloatPoint): Integer ; overload;
// Non rotated world, cells clamped to range of [0..2]
var
R : TRect;
W,H, i, cx, cy : Integer;
dx,dy : TFloat;
begin
Result := -1;
R := MakeRect(FloatRect(OriginEdges[0], OriginEdges[2]));
// in case Transformation.SrcRect.TopLeft < Point(0,0)
X := X - R.Left;
Y := Y - R.Top;
// get bounds of whole grid
with R do
begin
W := Right - Left;
H := Bottom - Top;
end;
// get bounds of a cell.
// Precisely. don't div here
dx := W / 3;
dy := H / 3;
//get cell contained XY
cx := Round(X / dx);
cy := Round(Y / dy);
Clamp(cx, 0, 2);
Clamp(cy, 0, 2);
// get index in: left-to-right-then-bottom order
Result := cx + cy * 3;
end;}
function LineDistance(const A,B: TFloatPoint; UsingRadius: Boolean = False): TFloat ;
var
i,j,c:TFloat;
begin
i:=A.X - B.X;
j:=A.Y - B.Y;
if i < 0 then i := i * -1;
if j < 0 then j := j * -1;
if UsingRadius then
begin
c:=sqr(i)+sqr(j);
//if c > 0 then
Result := sqrt(c)
//else
//Result := 0;
end
else
Result := Max(i,j);
end;
procedure IncF(var P : TFloatPoint; dx, dy : TFloat); overload;
begin
P.X := P.X + dx;
P.Y := P.Y + dy;
end;
{procedure Inc(AEdges : TArrayOfFloatPoint; Index: Integer; dx, dy : TFloat); overload;
begin
AEdges[Index].X := AEdges[Index].X + dx;
AEdges[Index].Y := AEdges[Index].Y + dy;
end;
procedure Inc(var F : TFloat; Delta : TFloat); overload;
begin
F := F + Delta;
end;
procedure Inc(var X: Integer; N: Integer =1); overload;
begin
System.Inc(X,N);
end; }
function IncOf(APointF : TFloatPoint; dx, dy : TFloat):TFloatPoint;
begin
Result.X := APointF.X + dx;
Result.Y := APointF.Y + dy;
end;
function MoveEdges(AEdges : TArrayOfFloatPoint; dx, dy : TFloat): TArrayOfFloatPoint;
var i : Integer;
begin
//Result := AEdges; //Wrong, share the content
//SetLength(Result,4);
//Move(AEdges[0], Result[0], 4 * SizeOf(TFloatPoint) );
Result := Copy(AEdges,0,4);
for i := 0 to 3 do
begin
Result[i].X := Result[i].X + dx;
Result[i].Y := Result[i].Y + dy;
end;
end;
function SlopeOf(I, Opposite: TFloatPoint): TFloatPoint;
begin
Result := FloatPoint(I.X - Opposite.X, I.Y - Opposite.Y );
end;
function Straight90degreeAt(MidPoint,OppositePoint,MousePoint : TFloatPoint ): TFloatPoint;
var
Radians, Angle : TFloat;
hypotenuse, opposite, adjacent : TFloat;
TT : TAffineTransformation;
M : TFloatPoint;
begin
Result := MousePoint;
Radians := ArcTan2( OppositePoint.Y - MidPoint.Y, MidPoint.X - OppositePoint.X );
Angle := RadToDeg( Radians );
with OppositePoint do
//inc(MousePoint, -X, -Y);
MousePoint := IncOf(MousePoint, -X, -Y);
TT := TAffineTransformation.Create;
try
TT.Rotate(-Angle);
M := TT.Transform( MousePoint );
M.Y := 0;
TT.Clear;
TT.Rotate(Angle);
TT.Translate(OppositePoint.X, OppositePoint.Y);
Result := TT.Transform(M);
//Result := M;
finally
TT.Free;
end;
end;
function StraightPointWithTailDegrees(MidPoint,OppositePoint,TailPoint, MousePoint : TFloatPoint ): TFloatPoint;
// P is the point of intersection
function Intersect(const A1, A2, B1, B2: TFloatPoint; out P: TFloatPoint): Boolean;
var
Adx, Ady, Bdx, Bdy, ABy, ABx: TFloat;
t, ta, tb: TFloat;
begin
Result := False;
Adx := A2.X - A1.X;
Ady := A2.Y - A1.Y;
Bdx := B2.X - B1.X;
Bdy := B2.Y - B1.Y;
t := (Bdy * Adx) - (Bdx * Ady);
if t = 0 then Exit; // lines are parallell
ABx := A1.X - B1.X;
ABy := A1.Y - B1.Y;
ta := Bdx * ABy - Bdy * ABx;
tb := Adx * ABy - Ady * ABx;
// if InSignedRange(ta, 0, t) and InSignedRange(tb, 0, t) then
begin
Result := True;
ta := ta / t;
P.X := A1.X + ta * Adx;
P.Y := A1.Y + ta * Ady;
end;
end;
var
Radians, Angle, Angle1, Angle2 : TFloat;
hypotenuse, opposite, adjacent : TFloat;
TT : TAffineTransformation;
M,M1,M2,distance : TFloatPoint;
begin
Radians := ArcTan2( OppositePoint.Y - MidPoint.Y, MidPoint.X - OppositePoint.X );
Angle1 := RadToDeg( Radians );
Radians := ArcTan2( TailPoint.Y - MidPoint.Y, MidPoint.X - TailPoint.X );
Angle2 := RadToDeg( Radians ) - 90;
{M := MousePoint;
with MidPoint do
inc(M, -X, -Y);}
with MidPoint do
M := IncOf(MousePoint, -X, -Y);
TT := TAffineTransformation.Create;
try
//TT.Translate(-OppositePoint.X, -OppositePoint.Y);
TT.Clear;
TT.Rotate(0,0, -Angle2);
M2 := TT.Transform( M );
M2.Y := 0;
TT.Clear;
TT.Rotate(0,0, Angle2);
TT.Translate(MidPoint.X , MidPoint.Y);
M2 := TT.Transform(M2); // X oke here
//--------------------------------------
//second pass
{distance := FloatPoint(M1.X - M.X, M1.Y - M.Y);
TT.Clear;
TT.Translate(-distance.X, -distance.Y);
Angle := Angle2 - 180;
TT.Rotate(0,0, Angle);
TT.Translate(distance.X, distance.Y);
M2 := TT.Transform( M1 );}
//Result := M1;
if not Intersect(MousePoint,M2, MidPoint,OppositePoint, Result) then
//Result := FloatPoint(M2.Y, M1.Y);
begin
TT.Clear;
TT.Rotate(0,0, -Angle1);
M1 := TT.Transform( M );
M1.Y := 0;
TT.Clear;
TT.Rotate(0,0, Angle1);
TT.Translate(MidPoint.X , MidPoint.Y);
M1 := TT.Transform(M1); // Y oke here
//--------------------------------------
Result := M1;
end;
finally
TT.Free;
end;
end;
function Move2Edges(AEdges : TArrayOfFloatPoint; Start: Integer;
dx, dy : TFloat): TArrayOfFloatPoint;
var i,s : Integer;
begin
Result := Copy(AEdges,0,4);
for i := Start to Start + 1 do
begin
s := SafelyGetEdgeIndex(i);
IncF(Result[s], dx,dy);
end;
end;
function ResizeBySide(AEdges : TArrayOfFloatPoint; Start: Integer;
dx, dy : TFloat; AStraight: Boolean): TArrayOfFloatPoint;
// RESIZE
var i,k : Integer;
m1, m2, mid, ops, pair, needed, adjusted : TFloatPoint;
ax,ay : TFloat;
begin
if Odd(Start) then
// locally horizontal resize
begin
m1 := MiddlePointOf2Lines(Aedges[0], AEdges[3]);
m2 := MiddlePointOf2Lines(Aedges[1], AEdges[2]);
end
else
// locally vertical resize
begin
m1 := MiddlePointOf2Lines(Aedges[0], AEdges[1]);
m2 := MiddlePointOf2Lines(Aedges[2], AEdges[3]);
end;
if Start in [1,2] then
begin
mid := m2;
ops := m1;
end
else //start = 3
begin
mid := m1;
ops := m2;
end;
needed := IncOf(mid, dx,dy);
if AStraight then
begin
//adjusted := StraightLinePointAt(mid, ops, needed);
adjusted := StraightPointWithTailDegrees(mid, ops, AEdges[start], needed);
end
else
begin
adjusted := needed;
end;
dx := adjusted.X -mid.X;
dy := adjusted.Y -mid.Y;
Result := Move2Edges(AEdges, Start, dx, dy);
end;
function SkewBySide(AEdges : TArrayOfFloatPoint; Start: Integer;
dx, dy : TFloat; AStraight : Boolean): TArrayOfFloatPoint;
// SKEW
var pair, i,k : Integer;
mid, opposite, needed, adjusted : TFloatPoint;
begin
pair := SafelyGetEdgeIndex(Start+1);
mid := MiddlePointOf2Lines(AEdges[start], AEdges[pair]);
opposite := AEdges[pair];
needed := IncOf(mid, dx,dy);
if AStraight then
begin
adjusted := Straight90degreeAt(mid, opposite, needed);
end
else
begin
adjusted := needed;
end;
dx := adjusted.X -mid.X;
dy := adjusted.Y -mid.Y;
Result := Move2Edges(AEdges, Start, dx, dy);
end;
function MirrorPoint(const P,Axis: TFloatPoint): TFloatPoint ;
var
ZeroP : TFloatPoint;
begin
ZeroP := IncOf(P, -Axis.X, -Axis.Y); // get distance
Result := IncOf(Axis, - ZeroP.X, - ZeroP.Y);
end;
function PerspectiveByCorner(AEdges : TArrayOfFloatPoint; Start: Integer;
dx, dy : TFloat; AStraight : Boolean): TArrayOfFloatPoint;
// PERSPECTIVE
var pair, iPrev,iNext, i,k : Integer;
mid, node, prev, next, draggedPrev,draggedNext, opposite, needed, adjusted : TFloatPoint;
begin
Result := Copy(AEdges,0,4);
iPrev := SafelyGetEdgeIndex(Start-1);
iNext := SafelyGetEdgeIndex(Start+1);
node := AEdges[start];
prev := AEdges[iPrev];
next := Aedges[iNext];
needed := IncOf(node, dx,dy);
draggedPrev := Straight90degreeAt(node, prev, needed);
draggedNext := Straight90degreeAt(node, next, needed);
// which XY is dragged further ?
if LineDistance(node, draggedPrev, True) > LineDistance(node, draggedNext, True) then
begin
Result[Start] := draggedPrev;
mid := MiddlePointOf2Lines(AEdges[start], AEdges[iPrev]);
Result[iPrev] := MirrorPoint(Result[Start], mid);
// reduction := FloatPoint()
end
else
begin
Result[Start] := draggedNext;
mid := MiddlePointOf2Lines(AEdges[start], AEdges[iNext]);
Result[iNext] := MirrorPoint(Result[Start], mid);
end;
end;
function SkewBySideBuggy(AEdges : TArrayOfFloatPoint; Start: Integer; dx, dy : TFloat): TArrayOfFloatPoint;
// SKEW
var opposite, i,k : Integer;
slope : TFloatPoint;
begin
opposite := SafelyGetEdgeIndex(Start+1);
// calc slope
slope:= SlopeOf(AEdges[Opposite], AEdges[Start]);
if Odd(Start) then // locally vertical skew
begin
with slope do