-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathProjectF.PathAnimation.pas
151 lines (131 loc) · 4.02 KB
/
ProjectF.PathAnimation.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
unit ProjectF.PathAnimation;
interface
uses
System.Classes,
System.Math,
System.Math.Vectors,
System.Types,
FMX.Ani,
FMX.Controls,
FMX.Graphics,
FMX.Types;
type
PPolygon = ^TPolygon;
// Hack TControl để dùng thuộc tính RotationAngle
THackControl = class(TControl)
// Đưa thuộc tính RotationAngle ra ngoài
property RotationAngle;
end;
TPathAnimationFXData = record
Polygon: PPolygon;
Spline: TSpline;
end;
TPathAnimationFX = class(TAnimation)
private
FObj: TControl;
FStart: TPointF;
FRotate: Boolean;
function EnabledStored: Boolean;
protected
procedure ProcessAnimation; override;
public
// Dùng con trỏ để khỏi khởi tạo lại dữ liệu mỗi khi bắt đầu Animation
Polygon: PPolygon;
Spline: TSpline;
public
constructor Create(AOwner: TComponent); override;
procedure Start; override;
class procedure CreateAnimationData(const APath: TPathData; var APolygon: TPolygon; var ASpline: TSpline); overload;
class procedure CreateAnimationData(const APath: TPathData; var AData: TPathAnimationFXData); overload;
published
property AnimationType default TAnimationType.In;
property AutoReverse default False;
property Enabled stored EnabledStored;
property Delay;
property Duration nodefault;
property Interpolation default TInterpolationType.Linear;
property Inverse default False;
property Loop default False;
property OnProcess;
property OnFinish;
property Rotate: Boolean read FRotate write FRotate default False;
property Trigger;
property TriggerInverse;
end;
implementation
{ TPathAnimationFX }
constructor TPathAnimationFX.Create(AOwner: TComponent);
begin
inherited;
Polygon := nil;
Spline := nil;
end;
class procedure TPathAnimationFX.CreateAnimationData(const APath: TPathData; var APolygon: TPolygon; var ASpline: TSpline);
var
I: Integer;
begin
APath.FlattenToPolygon(APolygon);
for I := Length(APolygon) - 1 downto 0 do
begin
if (APolygon[I].X = PolygonPointBreak.X) and (APolygon[I].Y = PolygonPointBreak.Y) then
begin
if I < Length(APolygon) - 1 then
APolygon[I] := APolygon[I + 1];
SetLength(APolygon, Length(APolygon) - 1);
end;
end;
ASpline := TSpline.Create(APolygon);
end;
class procedure TPathAnimationFX.CreateAnimationData(const APath: TPathData; var AData: TPathAnimationFXData);
begin
// Cấp phát bộ nhớ
New(AData.Polygon);
// Tạo dữ liệu chuyển động
CreateAnimationData(APath, AData.Polygon^, AData.Spline);
end;
function TPathAnimationFX.EnabledStored: Boolean;
begin
if ActionClient then
Result := True
else
Result := Enabled;
end;
procedure TPathAnimationFX.ProcessAnimation;
function DeltaBetween(const Point1, Point2: TPointF): Single;
begin
if Point1.CrossProduct(Point2) < 0 then
Result := ArcCos(Point1.AngleCosine(Point2))
else
Result := - ArcCos(Point1.AngleCosine(Point2));
end;
var
OldP, P1, Delta: TPointF;
begin
if (Length(Polygon^) > 0) and (FObj <> nil) then
begin
OldP := FObj.Position.Point;
Spline.SplineXY(NormalizedTime * High(Polygon^), P1.X, P1.Y);
FObj.Position.X := FStart.X + P1.X;
FObj.Position.Y := FStart.Y + P1.Y;
if FRotate and (NormalizedTime <> 0) and (NormalizedTime <> 1) and ((OldP.X <> FObj.Position.X) and
(OldP.Y <> FObj.Position.Y)) then
begin
Delta := FObj.Position.Point - OldP;
if Inverse then
THackControl(FObj).RotationAngle := 180 + RadToDeg(DeltaBetween(Delta, TPointF.Create(0, 1)))
else
THackControl(FObj).RotationAngle := RadToDeg(DeltaBetween(Delta, TPointF.Create(0, 1)));
end;
end;
end;
procedure TPathAnimationFX.Start;
begin
if Parent is TControl then
FObj := TControl(Parent)
else
FObj := nil;
if FObj <> nil then
FStart := FObj.Position.Point;
inherited;
end;
end.