forked from HemulGM/ChatGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HGM.FMX.Ani.pas
291 lines (252 loc) · 9.05 KB
/
HGM.FMX.Ani.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
unit HGM.FMX.Ani;
interface
uses
FMX.Ani, FMX.Types, System.Classes, System.SysUtils, System.Types;
type
TAniFreeNotification = class(TInterfacedObject, IFreeNotification)
private
FProc: TProc;
public
procedure FreeNotification(AObject: TObject);
constructor Create(Proc: TProc);
end;
TRectAnimation = class(FMX.Ani.TRectAnimation)
protected
procedure FirstFrame; override;
end;
{ TPositionAnimation }
TPositionAnimation = class(TCustomPropertyAnimation)
private
FStartRect: TPosition;
FCurrent: TPosition;
FStopRect: TPosition;
FStartFromCurrent: Boolean;
procedure SetStartRect(const Value: TPosition);
procedure SetStopRect(const Value: TPosition);
protected
procedure ProcessAnimation; override;
procedure FirstFrame; override;
procedure AssignTo(Dest: TPersistent); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property AnimationType default TAnimationType.in;
property AutoReverse default False;
property Enabled default False;
property Delay;
property Duration nodefault;
property Interpolation default TInterpolationType.Linear;
property Inverse default False;
property Loop default False;
property OnProcess;
property OnFinish;
property PropertyName;
property StartValue: TPosition read FStartRect write SetStartRect;
property StartFromCurrent: Boolean read FStartFromCurrent write FStartFromCurrent default False;
property StopValue: TPosition read FStopRect write SetStopRect;
property Trigger;
property TriggerInverse;
end;
TAnimatorHelper = class helper for TAnimator
class procedure DetachPropertyAnimation(const Target: TFmxObject; const APropertyName: string);
class procedure AnimateFloat(const Target: TFmxObject; const APropertyName: string; const NewValue: Single; Update: TNotifyEvent; Duration: Single = 0.2; AType: TAnimationType = TAnimationType.in; AInterpolation: TInterpolationType = TInterpolationType.Linear); overload;
class procedure AnimateRect(const Target: TFmxObject; const APropertyName: string; const NewValue: TRectF; Update: TNotifyEvent; Duration: Single = 0.2; AType: TAnimationType = TAnimationType.in; AInterpolation: TInterpolationType = TInterpolationType.Linear); overload;
class procedure AnimatePosition(const Target: TFmxObject; const APropertyName: string; const NewValue: TPointF; Update: TNotifyEvent; Duration: Single = 0.2; AType: TAnimationType = TAnimationType.in; AInterpolation: TInterpolationType = TInterpolationType.Linear); overload;
class procedure AnimateFloatWithFinish(const Target: TFmxObject; const APropertyName: string; const NewValue: Single; Finish: TProc; Duration: Single = 0.2; AType: TAnimationType = TAnimationType.in; AInterpolation: TInterpolationType = TInterpolationType.Linear); overload;
end;
implementation
uses
System.Rtti, FMX.Utils;
{ TAnimatorHelper }
class procedure TAnimatorHelper.AnimateFloat(const Target: TFmxObject; const APropertyName: string; const NewValue: Single; Update: TNotifyEvent; Duration: Single = 0.2; AType: TAnimationType = TAnimationType.in; AInterpolation: TInterpolationType = TInterpolationType.Linear);
var
Animation: TFloatAnimation;
begin
StopPropertyAnimation(Target, APropertyName);
with Self do
CreateDestroyer;
Animation := TFloatAnimation.Create(nil);
Animation.Parent := Target;
Animation.AnimationType := AType;
Animation.Interpolation := AInterpolation;
Animation.Duration := Duration;
Animation.PropertyName := APropertyName;
Animation.StartFromCurrent := True;
Animation.StopValue := NewValue;
Animation.OnProcess := Update;
with Self do
FDestroyer.RegisterAnimation(Animation);
Animation.Start;
end;
class procedure TAnimatorHelper.AnimateFloatWithFinish(const Target: TFmxObject; const APropertyName: string; const NewValue: Single; Finish: TProc; Duration: Single = 0.2; AType: TAnimationType = TAnimationType.in; AInterpolation: TInterpolationType = TInterpolationType.Linear);
var
Animation: TFloatAnimation;
begin
StopPropertyAnimation(Target, APropertyName);
with Self do
CreateDestroyer;
Animation := TFloatAnimation.Create(nil);
Animation.Parent := Target;
Animation.AnimationType := AType;
Animation.Interpolation := AInterpolation;
Animation.Duration := Duration;
Animation.PropertyName := APropertyName;
Animation.StartFromCurrent := True;
Animation.StopValue := NewValue;
Animation.AddFreeNotify(TAniFreeNotification.Create(Finish));
with Self do
FDestroyer.RegisterAnimation(Animation);
Animation.Start;
end;
class procedure TAnimatorHelper.AnimatePosition(const Target: TFmxObject; const APropertyName: string; const NewValue: TPointF; Update: TNotifyEvent; Duration: Single; AType: TAnimationType; AInterpolation: TInterpolationType);
var
Animation: TPositionAnimation;
begin
StopPropertyAnimation(Target, APropertyName);
with Self do
CreateDestroyer;
Animation := TPositionAnimation.Create(nil);
Animation.Parent := Target;
Animation.AnimationType := AType;
Animation.Interpolation := AInterpolation;
Animation.Duration := Duration;
Animation.PropertyName := APropertyName;
Animation.StartFromCurrent := True;
Animation.StopValue.Point := NewValue;
Animation.OnProcess := Update;
with Self do
FDestroyer.RegisterAnimation(Animation);
Animation.Start;
end;
class procedure TAnimatorHelper.AnimateRect(const Target: TFmxObject; const APropertyName: string; const NewValue: TRectF; Update: TNotifyEvent; Duration: Single; AType: TAnimationType; AInterpolation: TInterpolationType);
var
Animation: TRectAnimation;
begin
StopPropertyAnimation(Target, APropertyName);
with Self do
CreateDestroyer;
Animation := TRectAnimation.Create(nil);
Animation.Parent := Target;
Animation.AnimationType := AType;
Animation.Interpolation := AInterpolation;
Animation.Duration := Duration;
Animation.PropertyName := APropertyName;
Animation.StartFromCurrent := True;
Animation.StopValue.Rect := NewValue;
Animation.OnProcess := Update;
with Self do
FDestroyer.RegisterAnimation(Animation);
Animation.Start;
end;
class procedure TAnimatorHelper.DetachPropertyAnimation(const Target: TFmxObject; const APropertyName: string);
var
I: Integer;
begin
I := Target.ChildrenCount - 1;
while I >= 0 do
begin
if (Target.Children[I] is TCustomPropertyAnimation) and
(CompareText(TCustomPropertyAnimation(Target.Children[I]).PropertyName, APropertyName) = 0) then
TFloatAnimation(Target.Children[I]).StopAtCurrent;
if I > Target.ChildrenCount then
I := Target.ChildrenCount;
Dec(I);
end;
end;
{ TAniFreeNotification }
constructor TAniFreeNotification.Create(Proc: TProc);
begin
inherited Create;
FProc := Proc;
end;
procedure TAniFreeNotification.FreeNotification(AObject: TObject);
begin
if Assigned(FProc) then
FProc;
Free;
end;
{ TRectAnimation }
procedure TRectAnimation.FirstFrame;
var
T: TRttiType;
P: TRttiProperty;
begin
if StartFromCurrent then
begin
T := SharedContext.GetType(FInstance.ClassInfo);
if T <> nil then
begin
P := T.GetProperty(FPath);
if (P <> nil) and (P.PropertyType.TypeKind = tkClass) then
StartValue.Rect := TBounds(P.GetValue(FInstance).AsObject).Rect;
end;
end;
end;
{ TPositionAnimation }
procedure TPositionAnimation.AssignTo(Dest: TPersistent);
var
DestAnimation: TPositionAnimation;
begin
if Dest is TPositionAnimation then
begin
DestAnimation := TPositionAnimation(Dest);
DestAnimation.StartValue := StartValue;
DestAnimation.StopValue := StopValue;
DestAnimation.StartFromCurrent := StartFromCurrent;
end;
inherited;
end;
constructor TPositionAnimation.Create(AOwner: TComponent);
begin
inherited;
Duration := 0.2;
FStartRect := TPosition.Create(TPointF.Zero);
FStopRect := TPosition.Create(TPointF.Zero);
FCurrent := TPosition.Create(TPointF.Zero);
end;
destructor TPositionAnimation.Destroy;
begin
FreeAndNil(FCurrent);
FreeAndNil(FStartRect);
FreeAndNil(FStopRect);
inherited;
end;
procedure TPositionAnimation.FirstFrame;
var
T: TRttiType;
P: TRttiProperty;
begin
if StartFromCurrent then
begin
T := SharedContext.GetType(FInstance.ClassInfo);
if T <> nil then
begin
P := T.GetProperty(FPath);
if (P <> nil) and (P.PropertyType.TypeKind = tkClass) then
StartValue.Point := TPosition(P.GetValue(FInstance).AsObject).Point;
end;
end;
end;
procedure TPositionAnimation.ProcessAnimation;
begin
if FInstance <> nil then
begin
{ calc value }
FCurrent.X := InterpolateSingle(FStartRect.X, FStopRect.X,
NormalizedTime);
FCurrent.Y := InterpolateSingle(FStartRect.Y, FStopRect.Y,
NormalizedTime);
if (FRttiProperty <> nil) and FRttiProperty.PropertyType.IsInstance then
TBounds(FRttiProperty.GetValue(FInstance).AsObject).Assign(FCurrent);
end;
end;
procedure TPositionAnimation.SetStartRect(const Value: TPosition);
begin
FStartRect.Assign(Value);
end;
procedure TPositionAnimation.SetStopRect(const Value: TPosition);
begin
FStopRect.Assign(Value);
end;
end.