-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDelphiAnimationEngine.pas
More file actions
615 lines (508 loc) · 20.2 KB
/
Copy pathDelphiAnimationEngine.pas
File metadata and controls
615 lines (508 loc) · 20.2 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
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
unit DelphiAnimationEngine;
{ Setup:
Add the "DelphiAnimationEngine" to your project, in your main TForm, create
a TTimer, set the Interval to 8ms (you can change this if necessary), then
in the TTimer's OnTimer event, add the line "AnimationTimer"
The event AnimationTimer will run every 8ms (or whatever your interval is)
and update the Top, Left, Width and Height values of any controls being
animated as needed
Notes:
DelphiFixes is a requirement for this file and can be downloaded at:
https://github.com/BhavanBaijnath/DelphiFixes
This project was built using Delphi 2010, while it seems to be working fine
on Delphi 12, I did notice and issue with accuracy of animations if the
TForm's "Scaled" property is set to True. I'm not very experienced with
Delphi 12, but it seems this can be fixed by multiplying animation values by
TForm.FScaleFactor, or setting the "Scaled" property to False
References:
I used the website below for the formulas to calculate the different easing
types. These formula were written in TypeScript so I did modify them
slightly as needed to work with the way I designed this animation system.
https://easings.net
I was having issues with multiple animations running simultanesouly on the
same control so I asked ChatGPT for potential solutions.
The issue was mainly occuring with buttons for hover animations. If a the
cursor moved over the button too fast and the up animation doesn't finish,
the down animation would overwrite this and the button would not return to
it's correct starting position. This issue was resolved by using an array of
animations for each control, then when all animations for that specific
control are run, the final Top, Left, Width and Height values will be
outputted. This works fine for linear animations, but became an issue with
the other easing types. With the other easing types you're basically
guaranteed to have decimals since they are smooth curves. This meant that
rounding off would cause accuracy issues since the rounding error would
build up each time the animation is run, which is every 8ms and could
significantly affect its final value. To fix this, I basically created a
temporary variable for Top, Left, Width and Height as type Real, which fixed
the accuracy issues. These real numbers are adjusted as the animations run
and the rounded off value is outputted since Delphi uses integers for these
properties.
https://chatgpt.com/share/676b292a-0fec-8008-8a5d-f2ea5c2ad705 }
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Math, DelphiFixes, DateUtils, ExtCtrls,
Grids, StdCtrls, ComCtrls;
const
MaxAnimationControls = 200;
// ^^^ The maximum number of controls that can be animated simultaneously
MaxAnimations = 50;
// ^^^ The maximum number of animations per control that can happen simultaneously
type
TAnimationType = (atLinear, atEaseInSine, atEaseInQuad, atEaseInCubic,
atEaseInQuart, atEaseInQuint, atEaseInExpo, atEaseInCirc, atEaseInBack,
atEaseInElastic, atEaseOutSine, atEaseOutQuad, atEaseOutCubic,
atEaseOutQuart, atEaseOutQuint, atEaseOutExpo, atEaseOutCirc, atEaseOutBack,
atEaseOutElastic, atEaseInOutSine, atEaseInOutQuad, atEaseInOutCubic,
atEaseInOutQuart, atEaseInOutQuint, atEaseInOutExpo, atEaseInOutCirc,
atEaseInOutBack, atEaseInOutElastic);
TAnimationProperty = (apLeft, apTop, apWidth, apHeight);
TAnimation = class
private
FAnimationType: TAnimationType;
FAnimationProperty: TAnimationProperty;
FTime, FMovement: Integer;
FStartTime: TTime;
FRemainder, FPreviousCalc: Real;
public
constructor Create(AAnimationType: TAnimationType;
AAnimationProperty: TAnimationProperty; ATime: Integer;
AMovement: Integer; ADelay: Integer = 0);
end;
TAnimationControl = class
private
FControl: TControl;
FLeft, FTop, FWidth, FHeight: Real;
FStartScrollPos: Integer;
FLeftBool, FTopBool, FWidthBool, FHeightBool: Boolean;
FAnimations: Array [1 .. MaxAnimations] of TAnimation;
public
constructor Create(AControl: TControl);
end;
var
arrAnimationControls: Array [1 .. MaxAnimationControls] of TAnimationControl;
procedure AnimateControl(Control: TControl; AnimationType: TAnimationType;
AnimationProperty: TAnimationProperty; Time: Integer; Movement: Integer;
Delay: Integer = 0);
procedure AnimationTimer;
// Movement Functions
function Linear(iMovement, iX, iTime: Integer): Real;
// Ease In Functions
function EaseInSine(iMovement, iX, iTime: Integer): Real;
function EaseInQuad(iMovement, iX, iTime: Integer): Real;
function EaseInCubic(iMovement, iX, iTime: Integer): Real;
function EaseInQuart(iMovement, iX, iTime: Integer): Real;
function EaseInQuint(iMovement, iX, iTime: Integer): Real;
function EaseInExpo(iMovement, iX, iTime: Integer): Real;
function EaseInCirc(iMovement, iX, iTime: Integer): Real;
function EaseInBack(iMovement, iX, iTime: Integer): Real;
function EaseInElastic(iMovement, iX, iTime: Integer): Real;
// Ease Out Functions
function EaseOutSine(iMovement, iX, iTime: Integer): Real;
function EaseOutQuad(iMovement, iX, iTime: Integer): Real;
function EaseOutCubic(iMovement, iX, iTime: Integer): Real;
function EaseOutQuart(iMovement, iX, iTime: Integer): Real;
function EaseOutQuint(iMovement, iX, iTime: Integer): Real;
function EaseOutExpo(iMovement, iX, iTime: Integer): Real;
function EaseOutCirc(iMovement, iX, iTime: Integer): Real;
function EaseOutBack(iMovement, iX, iTime: Integer): Real;
function EaseOutElastic(iMovement, iX, iTime: Integer): Real;
// Ease In Out Functions
function EaseInOutSine(iMovement, iX, iTime: Integer): Real;
function EaseInOutQuad(iMovement, iX, iTime: Integer): Real;
function EaseInOutCubic(iMovement, iX, iTime: Integer): Real;
function EaseInOutQuart(iMovement, iX, iTime: Integer): Real;
function EaseInOutQuint(iMovement, iX, iTime: Integer): Real;
function EaseInOutExpo(iMovement, iX, iTime: Integer): Real;
function EaseInOutCirc(iMovement, iX, iTime: Integer): Real;
function EaseInOutBack(iMovement, iX, iTime: Integer): Real;
function EaseInOutElastic(iMovement, iX, iTime: Integer): Real;
implementation
procedure AnimateControl(Control: TControl; AnimationType: TAnimationType;
AnimationProperty: TAnimationProperty; Time: Integer; Movement: Integer;
Delay: Integer = 0);
var
I, J, iControlPos: Integer;
begin
iControlPos := 0;
I := 1;
// Checking if the control already has existing animations
while (I in [1 .. MaxAnimationControls]) and (iControlPos = 0) do
begin
if arrAnimationControls[I] <> nil then
if (arrAnimationControls[I].FControl = Control) then
iControlPos := I;
Inc(I);
end;
I := 1;
// If the control is not found it will be added at an empty array index
if iControlPos = 0 then
while (I in [1 .. MaxAnimationControls]) and (iControlPos = 0) do
begin
if arrAnimationControls[I] = nil then
begin
iControlPos := I;
arrAnimationControls[I] := TAnimationControl.Create(Control);
end;
Inc(I);
end;
for J := 1 to MaxAnimations do
begin
if arrAnimationControls[iControlPos].FAnimations[J] = nil then
begin
arrAnimationControls[iControlPos].FAnimations[J] :=
TAnimation.Create(AnimationType, AnimationProperty, Time,
Movement, Delay);
Exit;
end;
end;
end;
procedure AnimationTimer;
var
I, iX, iTime, iMovement, iAnimationCount, iLastX: Integer;
objAnimation: TAnimation;
objAnimationControl: TAnimationControl;
rOutput: Real;
J: Integer;
tStartTime: TTime;
begin
tStartTime := now;
for I := 1 to MaxAnimationControls do
begin
Application.ProcessMessages;
objAnimationControl := arrAnimationControls[I];
if objAnimationControl <> nil then
begin
iAnimationCount := 0;
objAnimationControl.FLeftBool := False;
objAnimationControl.FTopBool := False;
objAnimationControl.FWidthBool := False;
objAnimationControl.FHeightBool := False;
for J := 1 to MaxAnimations do
begin
objAnimation := objAnimationControl.FAnimations[J];
if objAnimation <> nil then
begin
Inc(iAnimationCount);
with objAnimation do
begin
if FStartTime > now then
iX := 0
else
iX := MilliSecondsBetween(now, FStartTime);
iTime := FTime;
iMovement := FMovement;
if iX > iTime then
iX := iTime;
case FAnimationType of
atLinear:
rOutput := Linear(iMovement, iX, iTime);
// Ease In
atEaseInSine:
rOutput := EaseInSine(iMovement, iX, iTime);
atEaseInQuad:
rOutput := EaseInQuad(iMovement, iX, iTime);
atEaseInCubic:
rOutput := EaseInCubic(iMovement, iX, iTime);
atEaseInQuart:
rOutput := EaseInQuart(iMovement, iX, iTime);
atEaseInQuint:
rOutput := EaseInQuint(iMovement, iX, iTime);
atEaseInExpo:
rOutput := EaseInQuint(iMovement, iX, iTime);
atEaseInCirc:
rOutput := EaseInCirc(iMovement, iX, iTime);
atEaseInBack:
rOutput := EaseInBack(iMovement, iX, iTime);
atEaseInElastic:
rOutput := EaseInElastic(iMovement, iX, iTime);
// Ease Out
atEaseOutSine:
rOutput := EaseOutSine(iMovement, iX, iTime);
atEaseOutQuad:
rOutput := EaseOutQuad(iMovement, iX, iTime);
atEaseOutCubic:
rOutput := EaseOutCubic(iMovement, iX, iTime);
atEaseOutQuart:
rOutput := EaseOutQuart(iMovement, iX, iTime);
atEaseOutQuint:
rOutput := EaseOutQuint(iMovement, iX, iTime);
atEaseOutExpo:
rOutput := EaseOutQuint(iMovement, iX, iTime);
atEaseOutCirc:
rOutput := EaseOutCirc(iMovement, iX, iTime);
atEaseOutBack:
rOutput := EaseOutBack(iMovement, iX, iTime);
atEaseOutElastic:
rOutput := EaseOutElastic(iMovement, iX, iTime);
// Ease In Out
atEaseInOutSine:
rOutput := EaseInOutSine(iMovement, iX, iTime);
atEaseInOutQuad:
rOutput := EaseInOutQuad(iMovement, iX, iTime);
atEaseInOutCubic:
rOutput := EaseInOutCubic(iMovement, iX, iTime);
atEaseInOutQuart:
rOutput := EaseInOutQuart(iMovement, iX, iTime);
atEaseInOutQuint:
rOutput := EaseInOutQuint(iMovement, iX, iTime);
atEaseInOutExpo:
rOutput := EaseInOutQuint(iMovement, iX, iTime);
atEaseInOutCirc:
rOutput := EaseInOutCirc(iMovement, iX, iTime);
atEaseInOutBack:
rOutput := EaseInOutBack(iMovement, iX, iTime);
atEaseInOutElastic:
rOutput := EaseInOutElastic(iMovement, iX, iTime);
end;
rOutput := rOutput - FPreviousCalc;
FPreviousCalc := FPreviousCalc + rOutput;
end; // with Animation
with objAnimationControl do
begin
if objAnimation.FAnimationProperty = apLeft then
FLeft := objAnimationControl.FLeft + rOutput
else if objAnimation.FAnimationProperty = apTop then
FTop := objAnimationControl.FTop + rOutput
else if objAnimation.FAnimationProperty = apWidth then
FWidth := objAnimationControl.FWidth + rOutput
else if objAnimation.FAnimationProperty = apHeight then
FHeight := objAnimationControl.FHeight + rOutput;
FLeftBool := (objAnimation.FAnimationProperty = apLeft) or
FLeftBool;
FTopBool := (objAnimation.FAnimationProperty = apTop) or FTopBool;
FWidthBool := (objAnimation.FAnimationProperty = apWidth) or
FWidthBool;
FHeightBool := (objAnimation.FAnimationProperty = apHeight) or
FHeightBool;
end; // with AnimationControl
if iX = iTime then
begin
objAnimation := nil;
arrAnimationControls[I].FAnimations[J] := nil;
arrAnimationControls[I].FAnimations[J].Free;
end;
end; // if Animation <> nil
end; // for J
if iAnimationCount > 0 then
begin
with objAnimationControl do
begin
if FLeftBool then
FControl.Left := ToInt(FLeft);
{ The if statement below counters the scroll movement from scroll
boxes. For some reason, whenn you start scrolling in a scroll box
the top position resets and stuff gets misaligned, especially if
you happen to be animating and scrolling simultanesouly. This finds
the difference between the last scroll position and the current
scroll position, and adds it to the new top value to correct this }
{ } if FControl.Parent is TScrollBox then
begin
FTop := FTop + (FStartScrollPos - (FControl.Parent as TScrollBox)
.VertScrollBar.ScrollPos);
FStartScrollPos := (FControl.Parent as TScrollBox)
.VertScrollBar.ScrollPos;
Application.ProcessMessages;
end; { }
if FTopBool then
FControl.Top := ToInt(FTop);
if FWidthBool then
FControl.Width := ToInt(FWidth);
if FHeightBool then
FControl.Height := ToInt(FHeight);
end;
end
else
begin
arrAnimationControls[I] := nil;
arrAnimationControls[I].Free;
end;
end; // if arrAnimationControls[I] <> nil
end; // for I
end;
{ TAnimationObject }
constructor TAnimation.Create(AAnimationType: TAnimationType;
AAnimationProperty: TAnimationProperty; ATime: Integer; AMovement: Integer;
ADelay: Integer);
begin
FAnimationType := AAnimationType;
FAnimationProperty := AAnimationProperty;
FTime := ATime;
FMovement := AMovement;
FStartTime := IncMilliSecond(now, ADelay);
FPreviousCalc := 0;
end;
{ TAnimationControl }
constructor TAnimationControl.Create(AControl: TControl);
begin
FControl := AControl;
FLeft := FControl.Left;
FTop := FControl.Top;
FWidth := FControl.Width;
FHeight := FControl.Height;
if AControl.Parent is TScrollBox then
FStartScrollPos := (AControl.Parent as TScrollBox).VertScrollBar.ScrollPos;
end;
function EaseInBack(iMovement, iX, iTime: Integer): Real;
begin
Result := iMovement * (2.70158 * Power(iX / iTime, 3) - 1.70158 *
Sqr(iX / iTime));
end;
function EaseInCirc(iMovement, iX, iTime: Integer): Real;
begin
Result := iMovement * (1 - Sqrt(1 - Sqr(iX / iTime)));
end;
function EaseInCubic(iMovement, iX, iTime: Integer): Real;
begin
Result := iMovement * Power(iX / iTime, 3);
end;
function EaseInElastic(iMovement, iX, iTime: Integer): Real;
begin
if iX = 0 then
Result := 0
else if iX / iTime = 1 then
Result := iMovement
else
Result := -iMovement * (Power(2, 10 * iX / iTime - 10) *
Sin((iX / iTime * 10 - 10.75) * (2 * pi / 3)))
end;
function EaseInExpo(iMovement, iX, iTime: Integer): Real;
begin
Result := iMovement * Power(2, 10 * iX / iTime - 10);
end;
function EaseInOutBack(iMovement, iX, iTime: Integer): Real;
begin
if iX / iTime < 0.5 then
Result := iMovement * (Sqr(2 * iX / iTime) * (3.5949095 * 2 * iX / iTime -
2.5949095)) / 2
else
Result := iMovement * (Sqr(2 * iX / iTime - 2) *
(3.5949095 * (iX / iTime * 2 - 2) + 2.5949095) + 2) / 2
end;
function EaseInOutCirc(iMovement, iX, iTime: Integer): Real;
begin
if iX / iTime < 0.5 then
Result := iMovement * (1 - Sqrt(1 - Sqr(2 * iX / iTime))) / 2
else
Result := iMovement * (Sqrt(1 - Sqr(-2 * iX / iTime + 2)) + 1) / 2
end;
function EaseInOutCubic(iMovement, iX, iTime: Integer): Real;
begin
if iX / iTime < 0.5 then
Result := iMovement * 4 * Power(iX / iTime, 3)
else
Result := iMovement * (1 - Power(-2 * iX / iTime + 2, 3) / 2);
end;
function EaseInOutElastic(iMovement, iX, iTime: Integer): Real;
begin
if iX = 0 then
Result := 0
else if iX / iTime = 1 then
Result := iMovement
else if iX / iTime < 0.5 then
Result := -iMovement * (Power(2, 20 * iX / iTime - 10) *
Sin((20 * iX / iTime - 11.125) * (2 * pi / 4.5))) / 2
else
Result := iMovement * (Power(2, -20 * iX / iTime + 10) *
Sin((20 * iX / iTime - 11.125) * (2 * pi / 4.5)) / 2 + 1)
end;
function EaseInOutExpo(iMovement, iX, iTime: Integer): Real;
begin
if iX = 0 then
Result := 0
else if iX / iTime = 1 then
Result := iMovement
else if iX / iTime < 0.5 then
Result := iMovement * Power(2, 20 * iX / iTime - 10) / 2
else
Result := iMovement * (2 - Power(2, -20 * iX / iTime + 10)) / 2;
end;
function EaseInOutQuad(iMovement, iX, iTime: Integer): Real;
begin
if iX / iTime < 0.5 then
Result := iMovement * 2 * Sqr(iX / iTime)
else
Result := iMovement * (1 - Sqr(-2 * iX / iTime + 2) / 2);
end;
function EaseInOutQuart(iMovement, iX, iTime: Integer): Real;
begin
if iX / iTime < 0.5 then
Result := iMovement * 8 * Power(iX / iTime, 4)
else
Result := iMovement * (1 - Power(-2 * iX / iTime + 2, 4) / 2);
end;
function EaseInOutQuint(iMovement, iX, iTime: Integer): Real;
begin
if iX / iTime < 0.5 then
Result := iMovement * 16 * Power(iX / iTime, 5)
else
Result := iMovement * (1 - Power(-2 * iX / iTime + 2, 5) / 2);
end;
function EaseInOutSine(iMovement, iX, iTime: Integer): Real;
begin
Result := -iMovement * (cos(pi * iX / iTime) - 1) / 2;
end;
function EaseInQuad(iMovement, iX, iTime: Integer): Real;
begin
Result := iMovement * Sqr(iX / iTime);
end;
function EaseInQuart(iMovement, iX, iTime: Integer): Real;
begin
Result := iMovement * Power(iX / iTime, 4);
end;
function EaseInQuint(iMovement, iX, iTime: Integer): Real;
begin
Result := iMovement * Power(iX / iTime, 5);
end;
function EaseInSine(iMovement, iX, iTime: Integer): Real;
begin
Result := iMovement * (1 - cos((iX / iTime) * pi) / 2) - (iMovement / 2);
end;
function EaseOutBack(iMovement, iX, iTime: Integer): Real;
begin
Result := iMovement * (1 + 2.70158 * Power(iX / iTime - 1, 3) + 1.70158 *
Sqr(iX / iTime - 1))
end;
function EaseOutCirc(iMovement, iX, iTime: Integer): Real;
begin
Result := iMovement * (Sqrt(1 - Sqr(iX / iTime - 1)));
end;
function EaseOutCubic(iMovement, iX, iTime: Integer): Real;
begin
Result := iMovement * (1 - Power(1 - (iX / iTime), 3));
end;
function EaseOutElastic(iMovement, iX, iTime: Integer): Real;
begin
if iX = 0 then
Result := 0
else if iX / iTime = 1 then
Result := iMovement
else
Result := iMovement * (Power(2, -10 * iX / iTime) *
Sin((iX / iTime * 10 - 0.75) * (2 * pi / 3)) + 1)
end;
function EaseOutExpo(iMovement, iX, iTime: Integer): Real;
begin
Result := iMovement * (1 - Power(2, -10 * iX / iTime));
end;
function EaseOutQuad(iMovement, iX, iTime: Integer): Real;
begin
Result := iMovement * (1 - Sqr(1 - iX / iTime));
end;
function EaseOutQuart(iMovement, iX, iTime: Integer): Real;
begin
Result := (iMovement * (1 - Power(1 - (iX / iTime), 4)));
end;
function EaseOutQuint(iMovement, iX, iTime: Integer): Real;
begin
Result := iMovement * (1 - Power(1 - (iX / iTime), 5));
end;
function EaseOutSine(iMovement, iX, iTime: Integer): Real;
begin
Result := iMovement * Sin(iX / iTime * pi / 2);
end;
function Linear(iMovement, iX, iTime: Integer): Real;
begin
Result := iMovement * (iX / iTime);
end;
end.