forked from zsdwxm/KernowSoftwareFMX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ksFormTransition.pas
538 lines (461 loc) · 15.8 KB
/
ksFormTransition.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
{*******************************************************************************
* *
* TksFormTransition - Animated Form Transition Component *
* *
* https://github.com/gmurt/KernowSoftwareFMX *
* *
* Copyright 2015 Graham Murt *
* *
* email: [email protected] *
* *
* 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 ksFormTransition;
interface
{$I ksComponents.inc}
uses System.UITypes, FMX.Controls, FMX.Layouts, FMX.Objects, System.Classes,
FMX.Types, Generics.Collections, FMX.Graphics, System.UIConsts, FMX.Effects,
FMX.StdCtrls, System.Types, FMX.Forms, ksTypes;
const
C_TRANSITION_DELAY = 0.3;
C_TRANSITION_FADE = 0.3;
C_TRANSITION_PART_SCROLL_FACTOR = 0.3;
C_INTERPOLATION_TYPE = TInterpolationType.Quadratic;
C_ANIMATION_TYPE = TAnimationType.InOut;
type
TAnimateDirection = (ksAdHorizontal, ksAdVertical);
TksFormTransitionType = (ksFtSlideInFromLeft,
ksFtSlideInFromTop,
ksFtSlideInFromRight,
ksFtSlideInFromBottom,
ksFtSlideOutToLeft,
ksFtSlideOutToTop,
ksFtSlideOutToRight,
ksFtSlideOutToBottom);
TksFormImage = class(TImage)
private
FRectangle: TRectangle;
procedure SetFade(const Value: single);
function GetFade: single;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Fade;
procedure UnFade;
published
property FadeValue: single read GetFade write SetFade;
end;
TksFormTransitionInfo = class
private
[weak]FFormFrom: TForm;
[weak]FFormTo: TForm;
FTransitionType: TksFormTransitionType;
FBackgroundScroll: Boolean;
function GetReverseTransition: TksFormTransitionType;
public
property FormFrom: TForm read FFormFrom write FFormFrom;
property FormTo: TForm read FFormTo write FFormTo;
property TransitionType: TksFormTransitionType read FTransitionType write FTransitionType;
property ReverseTransition: TksFormTransitionType read GetReverseTransition;
property BackgroundScroll: Boolean read FBackgroundScroll write FBackgroundScroll;
end;
TksFormTransitioIntoList = class(TObjectList<TksFormTransitionInfo>)
public
procedure AddTransition(AFrom, ATo: TForm; AType: TksFormTransitionType; ABackgroundScroll: Boolean);
end;
[ComponentPlatformsAttribute(pidWin32 or pidWin64 or
{$IFDEF XE8_OR_NEWER} pidiOSDevice32 or pidiOSDevice64
{$ELSE} pidiOSDevice {$ENDIF} or pidiOSSimulator or pidAndroid)]
TksFormTransition = class(TksComponent)
private
FPreventAdd: Boolean;
procedure AddBorder(ABmp: TBitmap; ABorder: TSide);
procedure AnimateImage(AImage: TksFormImage; ADirection: TAnimateDirection;
ANewValue: single; AWait: Boolean);
class function GenerateFormImage(AForm: TForm): TBitmap;
procedure PushForm(AFrom, ATo: TForm; ATransition: TksFormTransitionType; const ScrollBackgroundForm: Boolean = True);
procedure PopForm(const Animate: Boolean = True);
procedure PopAllForms;
public
constructor Create(AOwner: TComponent); override;
end;
{$R *.dcr}
procedure Register;
procedure PushForm(AFrom, ATo: TForm; ATransition: TksFormTransitionType; const ScrollBackgroundForm: Boolean = True);
procedure PopForm;
procedure PopAllForms;
procedure ClearTransitionTrail;
var
TransitionFading: Boolean;
implementation
uses FMX.Ani, SysUtils, ksCommon;
var
AAnimating: Boolean;
ATransitionList: TksFormTransitioIntoList;
procedure Register;
begin
RegisterComponents('Kernow Software FMX', [TksFormTransition]);
end;
procedure PushForm(AFrom, ATo: TForm; ATransition: TksFormTransitionType; const ScrollBackgroundForm: Boolean = True);
var
ATran: TksFormTransition;
begin
if AAnimating then
Exit; ATran := TksFormTransition.Create(nil);
try
ATran.PushForm(AFrom, ATo, ATransition, ScrollBackgroundForm);
finally
ATran.DisposeOf;
end;
end;
procedure PopForm;
var
ATran: TksFormTransition;
begin
if AAnimating then
Exit;
ATran := TksFormTransition.Create(nil);
try
ATran.PopForm;
finally
ATran.DisposeOf;
end;
end;
procedure ClearTransitionTrail;
begin
ATransitionList.Clear;
end;
procedure PopAllForms;
var
ATran: TksFormTransition;
begin
ATran := TksFormTransition.Create(nil);
try
ATran.PopAllForms;
finally
ATran.DisposeOf;
end;
end;
procedure TksFormTransition.AddBorder(ABmp: TBitmap; ABorder: TSide);
{var
ASides: TSides;
ASaveState: TCanvasSaveState;
ARect: TRectF;}
begin
{ ASides := [ABorder];
ASaveState := ABmp.Canvas.SaveState;
try
ABmp.Canvas.BeginScene;
try
ARect := RectF(0, 0, ABmp.Width/GetScreenScale, ABmp.Height/GetScreenScale);
ABmp.Canvas.IntersectClipRect(ARect);
ABmp.Canvas.Stroke.Color := claSilver;
ABmp.Canvas.Stroke.Thickness := GetScreenScale;
ABmp.Canvas.DrawRectSides(ARect, 0, 0, AllCorners, 1, ASides);
finally
ABmp.Canvas.EndScene;
end;
finally
ABmp.Canvas.RestoreState(ASaveState);
end;
Application.ProcessMessages;}
end;
procedure TksFormTransition.AnimateImage(AImage: TksFormImage; ADirection: TAnimateDirection;
ANewValue: single; AWait: Boolean);
var
AProperty: string;
begin
case ADirection of
ksAdHorizontal: AProperty := 'X';
ksAdVertical: AProperty := 'Y';
end;
if AWait then
begin
AImage.BringToFront;
//
end;
//Application.ProcessMessages;
//AImage.Fade := 0;
//if AFade then
// AImage.Fade;
case AWait of
False: TAnimator.AnimateFloat(AImage, 'Position.'+AProperty, ANewValue, C_TRANSITION_DELAY);
True: TAnimator.AnimateFloatWait(AImage, 'Position.'+AProperty, ANewValue, C_TRANSITION_DELAY);
end;
end;
constructor TksFormTransition.Create(AOwner: TComponent);
begin
inherited;
FPreventAdd := False;
end;
class function TksFormTransition.GenerateFormImage(AForm: TForm): TBitmap;
var
AScale: single;
begin
Result := TBitmap.Create;
AScale := GetScreenScale;
Result.BitmapScale := AScale;
Result.Width := Round(AForm.Width * AScale);
Result.Height := Round(AForm.Height * AScale);
Result.Clear(claWhite);
Result.Canvas.BeginScene;
AForm.PaintTo(Result.Canvas);
Result.Canvas.EndScene;
end;
procedure TksFormTransition.PopForm(const Animate: Boolean = True);
var
AInfo: TksFormTransitionInfo;
begin
if ATransitionList.Count = 0 then
Exit;
AInfo := ATransitionList.Last;
FPreventAdd := True;
//if Animate then
PushForm(AInfo.FormTo, AInfo.FormFrom, AInfo.ReverseTransition, AInfo.BackgroundScroll);
FPreventAdd := False;
Application.ProcessMessages;
ATransitionList.Delete(ATransitionList.Count-1);
end;
procedure TksFormTransition.PopAllForms;
var
AFirst: TksFormTransitionInfo;
ALast: TksFormTransitionInfo;
begin
if ATransitionList.Count = 0 then
Exit;
ALast := ATransitionList.Last;
AFirst := ATransitionList.First;
FPreventAdd := True;
//if Animate then
PushForm(ALast.FormTo, AFirst.FormFrom, ALast.ReverseTransition, ALast.BackgroundScroll);
FPreventAdd := False;
ClearTransitionTrail;
Application.ProcessMessages;
end;
procedure TksFormTransition.PushForm(AFrom, ATo: TForm;
ATransition: TksFormTransitionType; const ScrollBackgroundForm: Boolean = True);
var
AImageFrom: TksFormImage;
AImageTo: TksFormImage;
ABmp: TBitmap;
begin
if AAnimating then
Exit;
AAnimating := True;
if FPreventAdd = False then
ATransitionList.AddTransition(AFrom, ATo, ATransition, ScrollBackgroundForm);
AImageFrom := TksFormImage.Create(nil);
AImageTo := TksFormImage.Create(nil);
try
ATo.SetBounds(AFrom.Left, AFrom.Top, AFrom.Width, AFrom.Height);
AFrom.SetBounds(AFrom.Left, AFrom.Top, AFrom.Width, AFrom.Height);
AImageFrom.Width := AFrom.Width;
AImageFrom.Height := AFrom.Height;
AImageTo.Width := ATo.Width;
AImageTo.Height := ATo.Height;
ABmp := TksFormTransition.GenerateFormImage(AFrom);
try
AImageFrom.Bitmap := ABmp;
finally
FreeAndNil(ABmp);
end;
AImageFrom.Position.X := 0;
AImageFrom.Position.Y := 0;
AFrom.AddObject(AImageFrom);
AImageTo.Position.X := 0;
AImageTo.Position.Y := 0;
AFrom.AddObject(AImageTo);
Application.ProcessMessages;
ABmp := TksFormTransition.GenerateFormImage(ATo);
try
AImageTo.Bitmap := ABmp;
finally
FreeAndNil(ABmp);
end;
case ATransition of
ksFtSlideInFromLeft: AImageTo.Position.X := 0-AImageTo.Width;
ksFtSlideInFromTop: AImageTo.Position.Y := 0-AImageTo.Height;
ksFtSlideInFromRight: AImageTo.Position.X := AFrom.Width;
ksFtSlideInFromBottom: AImageTo.Position.Y := AFrom.Height;
end;
if ScrollBackgroundForm then
begin
case ATransition of
ksFtSlideOutToLeft: AImageTo.Position.X := (AImageTo.Width * C_TRANSITION_PART_SCROLL_FACTOR);
ksFtSlideOutToTop: AImageTo.Position.Y := (AImageTo.Height * C_TRANSITION_PART_SCROLL_FACTOR);
ksFtSlideOutToRight: AImageTo.Position.X := 0-(AImageTo.Width * C_TRANSITION_PART_SCROLL_FACTOR);
ksFtSlideOutToBottom: AImageTo.Position.Y := 0-(AImageTo.Height * C_TRANSITION_PART_SCROLL_FACTOR);
end;
end;
if (ATransition in [ksFtSlideOutToLeft, ksFtSlideOutToRight, ksFtSlideOutToBottom, ksFtSlideOutToTop]) then
AImageFrom.BringToFront;
Application.ProcessMessages;
case ATransition of
// slide ins...
ksFtSlideInFromRight:
begin
AImageTo.BringToFront;
AddBorder(AImageTo.Bitmap, TSide.Left);
AImageFrom.Fade;
if ScrollBackgroundForm then
AnimateImage(AImageFrom, ksAdHorizontal, 0-(AImageFrom.Width * C_TRANSITION_PART_SCROLL_FACTOR), False);
//TAnimator.AnimateFloat(AImageFrom, 'Fade', 0.5, C_TRANSITION_DELAY);
AnimateImage(AImageTo, ksAdHorizontal, 0, True);
end;
ksFtSlideInFromLeft:
begin
AImageTo.BringToFront;
AddBorder(AImageTo.Bitmap, TSide.Right);
AImageFrom.Fade;
if ScrollBackgroundForm then
AnimateImage(AImageFrom, ksAdHorizontal, AImageFrom.Width * C_TRANSITION_PART_SCROLL_FACTOR, False);
AnimateImage(AImageTo, ksAdHorizontal, 0, True);
end;
ksFtSlideInFromBottom:
begin
AddBorder(AImageTo.Bitmap, TSide.Top);
AImageFrom.Fade;
if ScrollBackgroundForm then
AnimateImage(AImageFrom, ksAdVertical, 0-(AImageFrom.Height * C_TRANSITION_PART_SCROLL_FACTOR), False);
AnimateImage(AImageTo, ksAdVertical, 0, True);
end;
ksFtSlideInFromTop:
begin
AddBorder(AImageTo.Bitmap, TSide.Bottom);
AImageFrom.Fade;
if ScrollBackgroundForm then
AnimateImage(AImageFrom, ksAdVertical, AImageFrom.Height * C_TRANSITION_PART_SCROLL_FACTOR, False);
AnimateImage(AImageTo, ksAdVertical, 0, True);
end;
// slide outs...
ksFtSlideOutToLeft:
begin
AddBorder(AImageFrom.Bitmap, TSide.Right);
AImageTo.UnFade;
if ScrollBackgroundForm then
AnimateImage(AImageTo, ksAdHorizontal, 0, False);
AnimateImage(AImageFrom, ksAdHorizontal, 0-AImageFrom.Width, True);
end;
ksFtSlideOutToTop:
begin
AddBorder(AImageFrom.Bitmap, TSide.Bottom);
AImageTo.UnFade;
if ScrollBackgroundForm then
AnimateImage(AImageTo, ksAdVertical, 0, False);
AnimateImage(AImageFrom, ksAdVertical, 0-AImageFrom.Height, True);
end;
ksFtSlideOutToRight:
begin
AddBorder(AImageFrom.Bitmap, TSide.Left);
AImageTo.UnFade;
if ScrollBackgroundForm then
AnimateImage(AImageTo, ksAdHorizontal, 0, False);
AnimateImage(AImageFrom, ksAdHorizontal, AImageFrom.Width, True);
end;
ksFtSlideOutToBottom:
begin
AddBorder(AImageFrom.Bitmap, TSide.Top);
AImageTo.UnFade;
if ScrollBackgroundForm then
AnimateImage(AImageTo, ksAdVertical, 0, False);
AnimateImage(AImageFrom, ksAdVertical, AImageFrom.Height, True);
end;
end;
finally
ATo.Show;
ATo.Invalidate;
Application.ProcessMessages;
AImageFrom.DisposeOf;
AImageTo.DisposeOf;
AFrom.Hide;
AAnimating := False;
end;
end;
{ TksFormTransitioIntoList }
procedure TksFormTransitioIntoList.AddTransition(AFrom, ATo: TForm; AType: TksFormTransitionType; ABackgroundScroll: Boolean);
var
AInfo: TksFormTransitionInfo;
begin
AInfo := TksFormTransitionInfo.Create;
AInfo.FormFrom := AFrom;
AInfo.FormTo := ATo;
AInfo.TransitionType := AType;
AInfo.BackgroundScroll := ABackgroundScroll;
Add(AInfo);
end;
{ TksFormTransitionInfo }
function TksFormTransitionInfo.GetReverseTransition: TksFormTransitionType;
begin
Result := ksFtSlideInFromLeft;
case FTransitionType of
ksFtSlideInFromLeft: Result := ksFtSlideOutToLeft;
ksFtSlideInFromTop: Result := ksFtSlideOutToTop;
ksFtSlideInFromRight: Result := ksFtSlideOutToRight;
ksFtSlideInFromBottom: Result := ksFtSlideOutToBottom;
ksFtSlideOutToLeft: Result := ksFtSlideInFromLeft;
ksFtSlideOutToTop: Result := ksFtSlideInFromTop;
ksFtSlideOutToRight: Result := ksFtSlideInFromRight;
ksFtSlideOutToBottom: Result := ksFtSlideInFromBottom;
end;
end;
{ TksFormImage }
constructor TksFormImage.Create(AOwner: TComponent);
begin
inherited;
FRectangle := TRectangle.Create(Self);
FRectangle.Align := TAlignLayout.Client;
FRectangle.Fill.Color := claBlack;
FRectangle.Fill.Kind := TBrushKind.Solid;
FRectangle.Opacity := 0;
AddObject(FRectangle);
end;
destructor TksFormImage.Destroy;
begin
{$IFDEF IOS}
FRectangle.DisposeOf;
{$ELSE}
FRectangle.Free;
{$ENDIF}
inherited;
end;
procedure TksFormImage.Fade;
begin
FRectangle.Opacity := 0;
if TransitionFading then
TAnimator.AnimateFloat(Self, 'FadeValue', C_TRANSITION_FADE, C_TRANSITION_DELAY);
end;
function TksFormImage.GetFade: single;
begin
Result := FRectangle.Opacity;
end;
procedure TksFormImage.SetFade(const Value: single);
begin
FRectangle.Opacity := Value;
end;
procedure TksFormImage.UnFade;
begin
if TransitionFading = False then
begin
FRectangle.Opacity := 0;
Exit;
end;
FRectangle.Opacity := C_TRANSITION_FADE;
TAnimator.AnimateFloat(Self, 'FadeValue', 0, C_TRANSITION_DELAY);
end;
initialization
ATransitionList := TksFormTransitioIntoList.Create;
TransitionFading := True;
finalization
FreeAndNil(ATransitionList);
end.