-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathVcl.SkAnimatedImageHelper.pas
More file actions
324 lines (284 loc) · 8.86 KB
/
Vcl.SkAnimatedImageHelper.pas
File metadata and controls
324 lines (284 loc) · 8.86 KB
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
{******************************************************************************}
{ }
{ SkAnimatedImageHelper: an helper class for TSkAnimatedImage }
{ }
{ Copyright (c) 2022-2026 (Ethea S.r.l.) }
{ Author: Carlo Barazzetta }
{ Contributors: }
{ }
{ https://github.com/EtheaDev/StyledComponents }
{ }
{******************************************************************************}
{ }
{ Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 }
{ }
{ Unless required by applicable law or agreed to in writing, software }
{ distributed under the License is distributed on an "AS IS" BASIS, }
{ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{ See the License for the specific language governing permissions and }
{ limitations under the License. }
{ }
{******************************************************************************}
unit Vcl.SkAnimatedImageHelper;
interface
{$INCLUDE StyledComponents.inc}
uses
Winapi.Windows
, Winapi.Messages
, System.Types
, System.Classes
(*
, System.UITypes
, Vcl.Controls
, Vcl.ImgList
*)
, Vcl.Graphics
, Vcl.Skia
;
type
TSkAnimatedImageHelper = class helper for TSkAnimatedImage
strict private
function AnimationLoaded: Boolean;
public
//Initialization functions
procedure InitAnimation(ALoop, AInverse: Boolean;
AProgress: Double);
procedure ClearAnimationData;
function CanPlayAnimation: Boolean;
function CanResumeAnimation: Boolean;
function CanPauseAnimation: Boolean;
function CanStopAnimation: Boolean;
function AnimationRunning: Boolean;
function AnimationRunningInverse: Boolean;
function AnimationRunningNormal: Boolean;
function GetProgress: Double;
function GetProgressPercentage: SmallInt;
procedure SetProgress(const AValue: Double);
procedure SetProgressPercentage(const AValue: SmallInt);
function GetLoop: Boolean;
procedure SetLoop(const Value: Boolean);
function GetInverse: Boolean;
procedure SetInverse(const Value: Boolean);
function GetLottieText: string;
procedure StopAnimation;
procedure PauseAnimation;
procedure ResumeAnimation;
procedure StartAnimation(const AFromBegin: Boolean = True);
procedure RenderTo(ACanvas: TCanvas; ARect: TRectF;
AProgress: Double; AOpacity: Single);
end;
implementation
uses
System.Skia
, System.SysUtils
, System.math
;
{ TSkAnimatedImageHelper }
function TSkAnimatedImageHelper.AnimationLoaded: Boolean;
begin
Result := (Length(Source.Data) > 0) and Assigned(Codec) and
not Codec.Isstatic;
end;
procedure TSkAnimatedImageHelper.InitAnimation(ALoop, AInverse: Boolean;
AProgress: Double);
begin
Animation.Loop := ALoop;
Animation.Inverse := AInverse;
Animation.Progress := AProgress;
end;
function TSkAnimatedImageHelper.AnimationRunning: Boolean;
begin
Result := AnimationLoaded and Animation.Enabled and Animation.Running;
end;
function TSkAnimatedImageHelper.AnimationRunningInverse: Boolean;
begin
Result := AnimationRunning and Animation.Inverse;
end;
function TSkAnimatedImageHelper.AnimationRunningNormal: Boolean;
begin
Result := AnimationRunning and not Animation.Inverse;
end;
function TSkAnimatedImageHelper.CanPlayAnimation: Boolean;
begin
Result := AnimationLoaded and not Animation.Running;
end;
function TSkAnimatedImageHelper.CanResumeAnimation: Boolean;
begin
Result := AnimationLoaded and not AnimationRunning and
(Animation.Progress <> 0) and
(Animation.Loop or (Animation.Progress <> 1));
end;
function TSkAnimatedImageHelper.CanPauseAnimation: Boolean;
begin
Result := AnimationRunning;
end;
function TSkAnimatedImageHelper.CanStopAnimation: Boolean;
begin
Result := AnimationRunning;
end;
procedure TSkAnimatedImageHelper.ClearAnimationData;
var
LEmpty: TBytes;
begin
SetLength(LEmpty,0);
Source.Data := LEmpty;
end;
function TSkAnimatedImageHelper.GetProgressPercentage: SmallInt;
begin
if AnimationLoaded then
Result := Round(inherited Animation.Progress * 100)
else
Result := 0;
end;
procedure TSkAnimatedImageHelper.SetProgress(const AValue: Double);
var
LEvent: TNotifyEvent;
begin
if Assigned(Animation) then
begin
if Assigned(OnAnimationProcess) then
LEvent := OnAnimationProcess
else
LEvent := nil;
OnAnimationProcess := nil;
Try
Animation.Stop;
Animation.Progress := AValue;
Finally
if Assigned(LEvent) then
OnAnimationProcess := LEvent;
End;
end;
end;
function TSkAnimatedImageHelper.GetLottieText: string;
var
LFormat: TFormatInfo;
LStringStream: TStringStream;
begin
Result := '';
if AnimationLoaded then
begin
if Codec.TryDetectFormat(Source.Data, LFormat) then
begin
if SameText(LFormat.Name, 'Lottie') then
begin
LStringStream := TStringStream.Create('', TEncoding.UTF8);
try
LStringStream.writebuffer(Source.Data[0], length(Source.Data));
Result := LStringStream.DataString;
finally
LStringStream.Free;
end;
end;
end;
end;
end;
function TSkAnimatedImageHelper.GetProgress: Double;
begin
if AnimationLoaded then
Result := inherited Animation.Progress
else
Result := 0;
end;
procedure TSkAnimatedImageHelper.SetProgressPercentage(const AValue: SmallInt);
begin
if Assigned(Animation) then
Animation.Progress := AValue / 100;
end;
function TSkAnimatedImageHelper.GetLoop: Boolean;
begin
Result := Assigned(Animation) and Animation.Loop;
end;
procedure TSkAnimatedImageHelper.SetLoop(const Value: Boolean);
begin
Animation.Loop := Value;
end;
function TSkAnimatedImageHelper.GetInverse: Boolean;
begin
Result := Assigned(Animation) and Animation.Inverse;
end;
procedure TSkAnimatedImageHelper.SetInverse(const Value: Boolean);
begin
if Assigned(Animation) then
Animation.Inverse := Value;
end;
procedure TSkAnimatedImageHelper.StopAnimation;
begin
if Assigned(Animation) then
Animation.Stop;
end;
procedure TSkAnimatedImageHelper.PauseAnimation;
begin
if Assigned(Animation) then
Animation.StopAtCurrent;
end;
procedure TSkAnimatedImageHelper.ResumeAnimation;
begin
if Assigned(Animation) then
begin
Animation.StartFromCurrent := Animation.Progress <> 1;
Animation.start;
end;
end;
procedure TSkAnimatedImageHelper.StartAnimation(const AFromBegin: Boolean = True);
begin
if Assigned(Animation) then
begin
Animation.StartFromCurrent := not AFromBegin;
if AFromBegin then
begin
if Animation.Inverse then
inherited Animation.Progress := 1
else
inherited Animation.Progress := 0;
end
else
begin
if not Animation.Inverse and (Animation.Progress = 1) then
inherited Animation.Progress := 0
else if Animation.Inverse and (Animation.Progress = 0) then
inherited Animation.Progress := 1;
end;
if AnimationLoaded and not Animation.Running then
Animation.Start;
end;
end;
procedure TSkAnimatedImageHelper.RenderTo(ACanvas: TCanvas; ARect: TRectF;
AProgress: Double; AOpacity: Single);
var
LBitmap: TBitmap;
LRect: TRectF;
LTop, LLeft, LWidth, LHeight: Integer;
begin
ACanvas.Lock;
try
Animation.Progress := AProgress;
LTop := Round(ARect.Top);
LLeft := Round(ARect.Left);
LWidth := Round(ARect.Width);
LHeight := Round(ARect.Height);
LBitmap := TBitmap.Create;
try
LBitmap.PixelFormat := pf32bit;
LBitmap.AlphaFormat := afPremultiplied;
LBitmap.SetSize(LWidth, LHeight);
LRect := TRectF.Create(0,0,LWidth, LHeight);
LBitmap.SkiaDraw(
procedure (const ASkCanvas: ISkCanvas)
begin
RenderFrame(ASkCanvas, LRect, AProgress, AOpacity);
end
);
ACanvas.Draw(LTop, LLeft, LBitmap);
finally
LBitmap.Free;
end;
finally
ACanvas.Unlock;
end;
end;
end.