-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathAnyiQuack.pas
2914 lines (2542 loc) · 82.9 KB
/
AnyiQuack.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
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{**
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (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.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is AnyiQuack.pas
*
* The Initial Developer of the Original Code is Waldemar Derr.
* Portions created by Waldemar Derr are Copyright (C) Waldemar Derr.
* All Rights Reserved.
*
* @author Waldemar Derr <furevest@gmail.com>
*
* Contributors:
* - John Kouraklis <https://github.com/jkour>
*}
unit AnyiQuack;
interface
{$INCLUDE Compile.inc}
uses
{$IFDEF MSWINDOWS}
Winapi.Windows,
Winapi.Messages,
{$ENDIF}
{$IFDEF FMX}
FMX.Types,
{$ENDIF}
System.SysUtils,
System.Types,
System.Classes,
System.Character,
System.Math,
System.Diagnostics,
System.SyncObjs,
System.UITypes,
System.UIConsts,
Generics.Collections;
const
Version = '1.2.1';
type
EAQ = class(Exception);
TAQBase = class;
TAQ = class;
TAQPlugin = class;
TInterval = class;
TTimerThread = class;
// More info:
// https://github.com/WladiD/AnyiQuack/wiki/Reference-of-simple-AnyiQuack-types#TEaseType
TEaseType = (etLinear, etQuad, etCubic, etQuart, etQuint, etSext, etSinus,
etElastic, etBack, etLowWave, etMiddleWave, etHighWave, etBounce,
etCircle, etSwing10, etSwing50, etSwing100, etSwing200);
// More info:
// https://github.com/WladiD/AnyiQuack/wiki/Reference-of-simple-AnyiQuack-types#TEaseModifier
TEaseModifier = (
emIn, emInInverted, emInSnake, emInSnakeInverted,
emOut, emOutInverted, emOutSnake, emOutSnakeInverted,
emInOut, emInOutMirrored, emInOutCombined,
emOutIn, emOutInMirrored, emOutInCombined);
TActorRole = (arTimer, arInterval, arDelay, arAnimation);
TActorRoles = set of TActorRole;
TObjectArray = TArray<TObject>;
TEaseArray = array of TEaseType;
TEachFunction = reference to function(AQ: TAQ; O: TObject): Boolean;
TEachMiscFunction<T> = reference to function(AQ: TAQ; O: TObject; Misc: T): Boolean;
TAnonymNotifyEvent = reference to procedure(Sender: TObject);
TEaseFunction = reference to function(Progress: Real): Real;
TAQBase = class(TObjectList<TObject>)
protected
function Each(const EachFunction: TEachFunction): TAQ; virtual; abstract;
public
constructor Create; reintroduce; virtual;
end;
TAQPlugin = class(TAQBase)
protected
FWorkAQ: TAQ;
function GarbageCollector: TAQ;
function Each(const EachFunction: TEachFunction): TAQ; override;
procedure Autorun; virtual;
procedure SetImmortally(Value: Boolean);
function GetImmortally: Boolean;
property Immortally: Boolean read GetImmortally write SetImmortally;
public
property WorkAQ: TAQ read FWorkAQ;
end;
TComponentsNotifier = class(TObjectList<TComponent>)
private
FNexus: TComponent;
procedure NexusFreeNotify(Sender: TObject; AComponent: TComponent);
protected
procedure Notify(const Value: TComponent; Action: TCollectionNotification); override;
public
constructor Create;
destructor Destroy; override;
end;
// More info:
// https://github.com/WladiD/AnyiQuack/wiki/Reference-of-TAQ
TAQ = class sealed (TAQBase)
// Private section for local types and constants
private
type
// Enumeration for all public TAQ methods, which returns a TAQ instance
TAQMethod = (
// Each related
aqmEach,
aqmEachAnimation,
aqmEachDelay,
aqmEachInterval,
aqmEachRepeat,
aqmEachTimer,
// Append related
aqmAppend,
aqmAppendAQ,
aqmChildrenAppend,
aqmParentsAppend,
// Finish related
aqmFinishAnimations,
aqmFinishTimers,
// Cancel related
aqmCancelAnimations,
aqmCancelDelays,
aqmCancelIntervals,
aqmCancelTimers,
// Chain related
aqmNewChain,
aqmExcludeChain,
aqmFilterChain,
aqmChildrenChain,
aqmIntervalActorsChain,
aqmAnimationActorsChain,
aqmDelayActorsChain,
aqmDemultiplexChain,
aqmTimerActorsChain,
aqmMultiplexChain,
aqmParentsChain,
aqmSliceChain,
aqmEndChain,
// Conditional chain related
aqmIfThen,
aqmIfAll,
aqmIfAny,
aqmIfContainsAll,
aqmIfContainsAny,
aqmIfContains,
aqmIfElse,
aqmIfEnd,
// Misc
aqmDebugMessage,
aqmDie);
TAQMethods = set of TAQMethod;
const
AQEachMethods: TAQMethods = [aqmEach, aqmEachAnimation, aqmEachDelay,
aqmEachInterval, aqmEachRepeat, aqmEachTimer];
AQAppendMethods: TAQMethods = [aqmAppend, aqmAppendAQ,
aqmChildrenAppend, aqmParentsAppend];
AQFinishMethods: TAQMethods = [aqmFinishAnimations, aqmFinishTimers];
AQCancelMethods: TAQMethods = [aqmCancelAnimations, aqmCancelDelays,
aqmCancelIntervals, aqmCancelTimers];
AQChainMethods: TAQMethods = [aqmNewChain, aqmExcludeChain, aqmFilterChain,
aqmChildrenChain, aqmIntervalActorsChain,
aqmAnimationActorsChain, aqmDelayActorsChain,
aqmDemultiplexChain, aqmTimerActorsChain,
aqmMultiplexChain, aqmParentsChain,
aqmSliceChain, aqmEndChain];
AQConditionMethods: TAQMethods = [aqmIfThen, aqmIfAll, aqmIfAny,
aqmIfContainsAll, aqmIfContainsAny,
aqmIfContains, aqmIfElse, aqmIfEnd];
// Private class related stuff
private
class var
FGC: TAQ;
{$IFDEF UseThreadTimer}
FTimerThread: TTimerThread;
{$ELSE}
FTimerHandler: Cardinal;
{$ENDIF}
FActiveIntervalAQs: TAQ;
FStopWatch: TStopWatch;
FTick: Int64;
FComponentsNotifier: TComponentsNotifier;
FIDGenerator: Integer;
class function GarbageCollector: TAQ;
class procedure GlobalIntervalTimerEvent;
class procedure ComponentsNotification(AComponent: TComponent; Operation: TOperation);
class function EaseIntegrated(EaseType: TEaseType): TEaseFunction;
class property Tick: Int64 read FTick;
// Private instance related stuff
private
FLifeTick: Int64;
FIntervals: TObjectList<TObject>;
FCurrentInterval: TInterval;
FChainedTo: TAQ;
FConditionCount: Byte;
FBools: Byte;
FImmortally: Boolean;
function HasActors(ActorRole: TActorRole; ID: Integer = 0): Boolean;
procedure LocalIntervalTimerEvent;
function GetIntervals: TObjectList<TObject>;
procedure ClearIntervals;
procedure AddInterval(Interval: TInterval);
procedure ProcessInterval(Interval: TInterval);
procedure RemoveInterval(Interval: TInterval);
function CustomFiller(const Filler: TEachFunction; Append, Recurse: Boolean): TAQ;
procedure CustomCancel(ActorRole: TActorRole; ID: Integer; Finish: Boolean);
function CustomActors(ActorRole: TActorRole; ID: Integer; IncludeOrphans: Boolean): TAQ;
function IfContainsEach(ByClass: TClass): TEachFunction; overload;
function IfContainsEach(const Objects: TObjectArray): TEachFunction; overload;
function IfContainsEach(Objects: TObjectList<TObject>): TEachFunction; overload;
function IfContainsEach(AQ: TAQ): TEachFunction; overload;
function ChildrenFiller(AQ: TAQ; O: TObject): Boolean;
function ParentsFiller(AQ: TAQ; O: TObject): Boolean;
function IsAlive: Boolean;
procedure HeartBeat;
function SupervisorLock(out AQ: TAQ; Method: TAQMethod): Boolean;
procedure SetRecurse(Value: Boolean);
function GetRecurse: Boolean;
procedure SetConditionLock(Value: Boolean);
function GetConditionLock: Boolean;
property Recurse: Boolean read GetRecurse write SetRecurse;
property ConditionLock: Boolean read GetConditionLock write SetConditionLock;
property Immortally: Boolean read FImmortally write FImmortally;
// Because TAQ is sealed, no new methods are introduced as protected, but some must be overriden
protected
procedure Notify(const Item: TObject; Action: TCollectionNotification); override;
// Public class related stuff
public
// Maximum life time of an active TAQ instance (animation, delay, interval) in milliseconds
// Default is 10000
class var MaxLifeTime: Integer;
class constructor Create;
class procedure Initialize;
class procedure Finalize;
class function Managed: TAQ;
class function Unmanaged: TAQ;
class function Take(AObject: TObject): TAQ; overload;
class function Take(const Objects: TObjectArray): TAQ; overload;
class function Take(Objects: TObjectList<TObject>): TAQ; overload;
class function Take<T: class>(Objects: TObjectList<T>): TAQ; overload;
class function HasActiveActors(CheckActors: TActorRoles; AObject: TObject; ID: Integer = 0): Boolean;
class function GetUniqueID: Integer;
class function Ease(EaseType: TEaseType;
EaseModifier: TEaseModifier = emIn): TEaseFunction; overload;
class function Ease(const EaseTypes: array of TEaseType;
EaseModifier: TEaseModifier = emIn): TEaseFunction; overload;
class function Ease(const EaseFunction: TEaseFunction = nil;
EaseModifier: TEaseModifier = emIn): TEaseFunction; overload;
class function EaseReal(StartValue, EndValue, Progress: Real; EaseType: TEaseType;
EaseModifier: TEaseModifier = emIn): Real; overload;
class function EaseReal(StartValue, EndValue, Progress: Real;
const EaseFunction: TEaseFunction): Real; overload;
class function EaseReal(const Values: TArray<Real>; Progress: Real;
const EaseFunction: TEaseFunction): Real; overload;
class function EaseSingle(StartValue, EndValue: Single; Progress: Real; EaseType: TEaseType;
EaseModifier: TEaseModifier = emIn): Real; overload;
class function EaseSingle(StartValue, EndValue: Single; Progress: Real;
const EaseFunction: TEaseFunction): Real; overload;
class function EaseSingle(const Values: TSingleDynArray; Progress: Real;
const EaseFunction: TEaseFunction): Real; overload;
class function EaseInteger(StartValue, EndValue: Integer; Progress: Real; EaseType: TEaseType;
EaseModifier: TEaseModifier = emIn): Integer; overload;
class function EaseInteger(StartValue, EndValue: Integer; Progress: Real;
const EaseFunction: TEaseFunction): Integer; overload;
class function EaseInteger(const Values: TIntegerDynArray; Progress: Real;
const EaseFunction: TEaseFunction): Integer; overload;
class function EaseColor(
StartColor, EndColor: {$IFDEF FMX}TAlphaColor{$ELSE}TColor{$ENDIF}; Progress: Real; EaseType: TEaseType;
EaseModifier: TEaseModifier = emIn): {$IFDEF FMX}TAlphaColor{$ELSE}TColor{$ENDIF}; overload;
class function EaseColor(
StartColor, EndColor: {$IFDEF FMX}TAlphaColor{$ELSE}TColor{$ENDIF}; Progress: Real;
const EaseFunction: TEaseFunction): {$IFDEF FMX}TAlphaColor{$ELSE}TColor{$ENDIF}; overload;
class function EaseColor(const Colors: TArray<{$IFDEF FMX}TAlphaColor{$ELSE}TColor{$ENDIF}>;
Progress: Real; const EaseFunction: TEaseFunction): {$IFDEF FMX}TAlphaColor{$ELSE}TColor{$ENDIF}; overload;
class function EasePoint(const StartPoint, EndPoint: TPoint; Progress: Real; EaseType: TEaseType;
EaseModifier: TEaseModifier = emIn): TPoint; overload;
class function EasePoint(const StartPoint, EndPoint: TPoint; Progress: Real;
const EaseFunction: TEaseFunction): TPoint; overload;
class function EasePoint(const Points: TArray<TPoint>; Progress: Real;
const EaseFunction: TEaseFunction): TPoint; overload;
{$IFDEF FMX}
class function EasePointF(const StartPoint, EndPoint: TPointF; Progress: Real;
EaseType: TEaseType; EaseModifier: TEaseModifier = emIn): TPointF; overload;
class function EasePointF(const StartPoint, EndPoint: TPointF; Progress: Real;
const EaseFunction: TEaseFunction): TPointF; overload;
class function EasePointF(const Points: TArray<TPointF>; Progress: Real;
const EaseFunction: TEaseFunction): TPointF; overload;
{$ENDIF}
class function EaseRect(const StartRect, EndRect: TRect; Progress: Real; EaseType: TEaseType;
EaseModifier: TEaseModifier = emIn): TRect; overload;
class function EaseRect(const StartRect, EndRect: TRect; Progress: Real;
const EaseFunction: TEaseFunction): TRect; overload;
class function EaseRect(const Rects: TArray<TRect>; Progress: Real;
const EaseFunction: TEaseFunction): TRect; overload;
{$IFDEF FMX}
class function EaseRectF(const StartRect, EndRect: TRectF; Progress: Real; EaseType: TEaseType;
EaseModifier: TEaseModifier = emIn): TRectF; overload;
class function EaseRectF(const StartRect, EndRect: TRectF; Progress: Real;
const EaseFunction: TEaseFunction): TRectF; overload;
class function EaseRectF(const Rects: TArray<TRectF>; Progress: Real;
const EaseFunction: TEaseFunction): TRectF; overload;
{$ENDIF}
class function EaseString(const StartString, EndString: String; Progress: Real; EaseType: TEaseType;
EaseModifier: TEaseModifier = emIn): String; overload;
class function EaseString(const StartString, EndString: String; Progress: Real;
const EaseFunction: TEaseFunction): String; overload;
// Public instance related stuff
public
constructor Create; override;
destructor Destroy; override;
function Each(const EachFunction: TEachFunction): TAQ; override;
function EachInterval(Interval: Integer; const Each: TEachFunction; ID: Integer = 0): TAQ;
function EachTimer(Duration: Integer; const Each: TEachFunction; const LastEach: TEachFunction = nil;
ID: Integer = 0): TAQ;
function EachAnimation(Duration: Integer; const Each: TEachFunction; const LastEach: TEachFunction = nil;
ID: Integer = 0): TAQ;
function EachDelay(Delay: Integer; const Each: TEachFunction; ID: Integer = 0): TAQ;
function EachRepeat(Times: Integer; const EachFunction: TEachFunction): TAQ;
function NewChain: TAQ;
function EndChain: TAQ;
function Die: TAQ;
function Append(AObject: TObject): TAQ; overload;
function Append(const Objects: TObjectArray): TAQ; overload;
function Append(Objects: TObjectList<TObject>): TAQ; overload;
function AppendAQ(AQ: TAQ): TAQ;
function ChildrenAppend(Recurse: Boolean = False; ChildrenFiller: TEachFunction = nil): TAQ;
function ChildrenChain(Recurse: Boolean = False; ChildrenFiller: TEachFunction = nil): TAQ;
function ParentsAppend(Recurse: Boolean = False; ParentsFiller: TEachFunction = nil): TAQ;
function ParentsChain(Recurse: Boolean = False; ParentsFiller: TEachFunction = nil): TAQ;
function MultiplexChain: TAQ;
function DemultiplexChain: TAQ;
function AnimationActorsChain(ID: Integer = 0; IncludeOrphans: Boolean = False): TAQ;
function IntervalActorsChain(ID: Integer = 0; IncludeOrphans: Boolean = False): TAQ;
function TimerActorsChain(ID: Integer = 0; IncludeOrphans: Boolean = False): TAQ;
function DelayActorsChain(ID: Integer = 0; IncludeOrphans: Boolean = False): TAQ;
function FinishAnimations(ID: Integer = 0): TAQ;
function CancelAnimations(ID: Integer = 0): TAQ;
function FinishTimers(ID: Integer = 0): TAQ;
function CancelTimers(ID: Integer = 0): TAQ;
function CancelDelays(ID: Integer = 0): TAQ;
function CancelIntervals(ID: Integer = 0): TAQ;
function FilterChain(ByClass: TClass): TAQ; overload;
function FilterChain(const FilterEach: TEachFunction): TAQ; overload;
function ExcludeChain(ByClass: TClass): TAQ; overload;
function ExcludeChain(AObject: TObject): TAQ; overload;
function ExcludeChain(const Objects: TObjectArray): TAQ; overload;
function ExcludeChain(Objects: TObjectList<TObject>): TAQ; overload;
function ExcludeChain(AQ: TAQ): TAQ; overload;
function ExcludeChain(const ExcludeEach: TEachFunction): TAQ; overload;
function IfThen(Condition: Boolean): TAQ;
function IfElse: TAQ;
function IfEnd: TAQ;
function IfAll(const EachFunction: TEachFunction): TAQ;
function IfAny(const EachFunction: TEachFunction): TAQ;
function IfContains(AObject: TObject): TAQ;
function IfContainsAny(ByClass: TClass): TAQ; overload;
function IfContainsAny(const Objects: TObjectArray): TAQ; overload;
function IfContainsAny(Objects: TObjectList<TObject>): TAQ; overload;
function IfContainsAny(AQ: TAQ): TAQ; overload;
function IfContainsAll(ByClass: TClass): TAQ; overload;
function IfContainsAll(const Objects: TObjectArray): TAQ; overload;
function IfContainsAll(Objects: TObjectList<TObject>): TAQ; overload;
function IfContainsAll(AQ: TAQ): TAQ; overload;
function SliceChain(StartIndex: Integer; Count: Integer = 0): TAQ;
function DebugMessage(HeadMessage: String = ''; Caption: String = ''): TAQ;
function Plugin<T: TAQPlugin,CONSTRUCTOR>: T;
function Contains(AObject: TObject): Boolean;
procedure Clean;
property CurrentInterval: TInterval read FCurrentInterval;
end;
TInterval = class
private
FFirstTick,
FNextTick,
FLastTick: Int64;
FInterval: Integer;
FNextEach,
FLastEach: TEachFunction;
FActorRole: TActorRole;
FID: Integer;
protected
procedure UpdateNextTick;
public
constructor Infinite(Interval: Integer; const Each: TEachFunction; ActorRole: TActorRole;
ID: Integer);
constructor Finite(Duration: Integer; const Each, LastEach: TEachFunction;
ActorRole: TActorRole; ID: Integer);
destructor Destroy; override;
function Each: TEachFunction;
function IsCanceled: Boolean;
function IsFinished: Boolean;
function IsFinite: Boolean;
function Progress: Real;
procedure Finish;
procedure Cancel;
property ActorRole: TActorRole read FActorRole;
property ID: Integer read FID;
end;
TTimerThread = class(TThread)
private
FInterval: Integer;
FTimerProc: TThreadProcedure;
FMainSignal: TEvent;
FEnabled: Boolean;
{$IFDEF MSWINDOWS}
FWindowHandle: HWND;
procedure WndProc(var Msg: TMessage);
{$ENDIF}
procedure SetInterval(NewInterval: Integer);
protected
procedure Execute; override;
public
constructor Create(Interval: Integer; TimerProc: TThreadProcedure);
destructor Destroy; override;
procedure Enable;
procedure Disable;
{**
* DON'T USE TERMINATE FOR THIS THREAD: USE DESTRUCTOR!
*}
//procedure Terminate;
property Enabled: Boolean read FEnabled;
property Interval: Integer read FInterval write SetInterval;
end;
// Shortcuts to appropriate `TAQ.Take` methods
function Take(AObject: TObject): TAQ; overload;
function Take(const Objects: TObjectArray): TAQ; overload;
function Take(Objects: TObjectList<TObject>): TAQ; overload;
function Take(Enumerator: TEnumerable<TObject>): TAQ; overload;
function OA(const Objects: array of TObject): TObjectArray;
function MatchID(CompareID, CurrentID: Integer): Boolean;
procedure ProgressMap(EntriesCount: Integer; Progress: Real; out SubProgress: Real;
out IndexA, IndexB: Integer);
implementation
const
{$IFDEF UseThreadTimer}
IntervalResolution = 15;
{$ELSE}
IntervalResolution = 20;
{$ENDIF}
GarbageCleanInterval = 5000;
// The garbage collector has just x ms per cleanup round
GarbageCleanTime = 25;
const
RecurseBitMask = $01;
ConditionLockBitMask = $02;
var
Initialized, Finalized: Boolean;
type
TComponentListNexusEvent = procedure(Sender: TObject; AComponent: TComponent) of object;
TComponentListNexus = class(TComponent)
protected
OnFreeNotify: TComponentListNexusEvent;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
end;
{$IF RTLVersion < 22}
{**
* Cheap implementation of TSpinWait shipped since Delphi XE
*
* Just only to be backward compatible with Delphi 2010.
*}
TSpinWait = record
public
class function SpinUntil(const ACondition: TFunc<Boolean>; Timeout: LongWord): Boolean; static;
end;
{$IFEND}
function LinearEase(Progress: Real): Real;
begin
Result := Progress;
end;
function QuadEase(Progress: Real): Real;
begin
Result := Progress * Progress;
end;
function CubicEase(Progress: Real): Real;
begin
Result := Power(Progress, 3);
end;
function QuartEase(Progress: Real): Real;
begin
Result := Power(Progress, 4);
end;
function QuintEase(Progress: Real): Real;
begin
Result := Power(Progress, 5);
end;
function SextEase(Progress: Real): Real;
begin
Result := Power(Progress, 6);
end;
function SinusEase(Progress: Real): Real;
begin
Result := Sin(Progress * (Pi / 2));
end;
function ElasticEase(Progress: Real): Real;
begin
Result := (Sin(Progress * Pi * (0.2 + 2.5 * Progress * Progress * Progress)) *
Power(1 - Progress, 2.2) + Progress) * (1 + (1.2 * (1 - Progress)));
end;
function LowWaveEase(Progress: Real): Real;
begin
Result := Progress + (Sin(Progress * 3 * Pi) * 0.1);
end;
function MiddleWaveEase(Progress: Real): Real;
begin
Result := Progress + (Sin(Progress * 3 * Pi) * 0.2);
end;
function HighWaveEase(Progress: Real): Real;
begin
Result := Progress + (Sin(Progress * 3 * Pi) * 0.4);
end;
function BackEase(Progress: Real): Real;
begin
Result := Progress * Progress * ((2.70158 * Progress) - 1.70158);
end;
function BounceEase(Progress: Real): Real;
const
Base: Real = 7.5625;
begin
if Progress < (1 / 2.75) then
Result := Base * Progress * Progress
else if Progress < (2 / 2.75) then
begin
Progress := Progress - (1.5 / 2.75);
Result := (Base * Progress) * Progress + 0.75;
end
else if Progress < (2.5 / 2.75) then
begin
Progress := Progress - (2.25 / 2.75);
Result := (Base * Progress) * Progress + 0.9375;
end
else
begin
Progress := Progress - (2.625 / 2.75);
Result := (Base * Progress) * Progress + 0.984375;
end;
end;
function CircleEase(Progress: Real): Real;
begin
Result := 1 - Sqrt(1 - Progress * Progress);
end;
function SwingCustom(Progress, Swings: Real): Real;
begin
Result := Progress + (Sin(Progress * Swings * Pi) * (1 / Swings));
end;
function Swing10Ease(Progress: Real): Real;
begin
Result := SwingCustom(Progress, 10);
end;
function Swing50Ease(Progress: Real): Real;
begin
Result := SwingCustom(Progress, 50);
end;
function Swing100Ease(Progress: Real): Real;
begin
Result := SwingCustom(Progress, 100);
end;
function Swing200Ease(Progress: Real): Real;
begin
Result := SwingCustom(Progress, 200);
end;
function Take(AObject: TObject): TAQ;
begin
Result := TAQ.Take(AObject);
end;
function Take(const Objects: TObjectArray): TAQ;
begin
Result := TAQ.Take(Objects);
end;
function Take(Objects: TObjectList<TObject>): TAQ;
begin
Result := TAQ.Take(Objects);
end;
function Take(Enumerator: TEnumerable<TObject>): TAQ;
begin
Result := TAQ.Take(Enumerator);
end;
function OA(const Objects: array of TObject): TObjectArray;
var
cc: Integer;
begin
SetLength(Result, Length(Objects));
for cc := 0 to Length(Objects) - 1 do
Result[cc] := Objects[cc];
end;
// ID comparer
//
// Following rules will be applied
// - when CompareID = 0, CurrentID will not be compared and the result is always True
// - when CompareID > 0, CurrentID must be equal
// - when CompareID = -1, CurrentID must be 0
// - when CompareID = -2, CurrentID must be greater than 0
function MatchID(CompareID, CurrentID: Integer): Boolean;
begin
Result := (CompareID = 0) or
((CompareID > 0) and (CompareID = CurrentID)) or
((CompareID = -1) and (CurrentID = 0)) or
((CompareID = -2) and (CurrentID > 0));
end;
procedure ProgressMap(EntriesCount: Integer; Progress: Real; out SubProgress: Real;
out IndexA, IndexB: Integer);
var
Step: Real;
begin
if EntriesCount < 2 then
Exit;
Step := 1 / (EntriesCount - 1);
IndexA := Trunc(Progress / Step);
IndexB := (IndexA + 1);
if IndexB >= EntriesCount then
IndexB := EntriesCount - 1;
SubProgress := Min(Max((Progress - (Step * IndexA)) / Step, 0), 1);
end;
procedure SetBit(var Container: Byte; BitMask: Byte; Value: Boolean);
begin
if Value then
Container := Container or BitMask
else
Container := Container and not BitMask;
end;
function GetBit(Container: Byte; BitMask: Byte): Boolean;
begin
Result := (Container and BitMask) <> 0;
end;
{$IFDEF OutputDebug}
procedure OutputDebug(const Fmt: string; const Args: array of const); overload;
begin
{$IFDEF FMX}
Log.d(Fmt, Args)
{$ELSE}
OutputDebugString(PChar(Format(Fmt, Args)));
{$ENDIF}
end;
procedure OutputDebug(const Msg: string); overload;
begin
{$IFDEF FMX}
Log.d(Msg)
{$ELSE}
OutputDebugString(Msg);
{$ENDIF}
end;
{$ENDIF}
{ TAQBase }
constructor TAQBase.Create;
begin
inherited Create(False);
end;
{ TAQ }
// Appends all in the TObjectArray contained objects to the current TAQ instance
function TAQ.Append(const Objects: TObjectArray): TAQ;
var
cc: Integer;
begin
if SupervisorLock(Result, aqmAppend) then
Exit;
for cc := 0 to Length(Objects) - 1 do
Add(Objects[cc]);
end;
// Appends all in Objects contained objects, but not the TObjectList by itself,
// to the current TAQ instance
function TAQ.Append(Objects: TObjectList<TObject>): TAQ;
var
cc: Integer;
begin
if SupervisorLock(Result, aqmAppend) then
Exit;
// Avoid overflows
if Objects = Self then
Exit;
for cc := 0 to Objects.Count - 1 do
Add(Objects[cc]);
end;
// Appends AObject to the current TAQ instance
function TAQ.Append(AObject: TObject): TAQ;
begin
if SupervisorLock(Result, aqmAppend) then
Exit;
Add(AObject);
end;
// Appends an other TAQ instance to the current one
//
// This method is handy for recursive tasks. For example you have already some TAQ instances with
// some objects and want to perform an action on all of them:
// ```delphi
// Take(MyInstanceA).AppendAQ(MyInstanceB).AppendAQ(MyInstanceC).Each({...Your action...});
// ```
//
// In contrast to `TAQ.Append` it appends just the passed TAQ instance to the current instance
// instead of the contained object.
function TAQ.AppendAQ(AQ: TAQ): TAQ;
begin
if SupervisorLock(Result, aqmAppendAQ) then
Exit;
Add(AQ);
end;
procedure TAQ.AddInterval(Interval: TInterval);
begin
// The interval will not be taken and will be freed, when the current TAQ instance
// do not contains any objects.
//
// One exception is for the garbage collector.
if (Count = 0) and (Self <> FGC) then
begin
Interval.Free;
Exit;
end;
if (GetIntervals.Count = 0) and (FActiveIntervalAQs.IndexOf(Self) = -1) then
FActiveIntervalAQs.Add(Self);
GetIntervals.Add(Interval);
{$IFDEF OutputDebugActiveIntervals}
OutputDebug('TAQ-Instanzen mit Intervallen: ' + IntToStr(FActiveIntervalAQs.Count)));
{$ENDIF}
end;
function TAQ.AnimationActorsChain(ID: Integer; IncludeOrphans: Boolean): TAQ;
begin
if SupervisorLock(Result, aqmAnimationActorsChain) then
Exit;
Result := CustomActors(arAnimation, ID, IncludeOrphans);
end;
function TAQ.CancelAnimations(ID: Integer): TAQ;
begin
if SupervisorLock(Result, aqmCancelAnimations) then
Exit;
CustomCancel(arAnimation, ID, False);
end;
function TAQ.CancelDelays(ID: Integer): TAQ;
begin
if SupervisorLock(Result, aqmCancelDelays) then
Exit;
CustomCancel(arDelay, ID, False);
end;
function TAQ.CancelIntervals(ID: Integer): TAQ;
begin
if SupervisorLock(Result, aqmCancelIntervals) then
Exit;
CustomCancel(arInterval, ID, False);
end;
function TAQ.CancelTimers(ID: Integer): TAQ;
begin
if SupervisorLock(Result, aqmCancelTimers) then
Exit;
CustomCancel(arTimer, ID, False);
end;
function TAQ.NewChain: TAQ;
begin
if SupervisorLock(Result, aqmNewChain) then
Exit;
Result := Managed;
Result.FChainedTo := Self;
end;
procedure TAQ.Notify(const Item: TObject; Action: TCollectionNotification);
begin
if (Action = cnAdded) and (Item is TComponent) and
(FComponentsNotifier.IndexOf(Item as TComponent) < 0) then
FComponentsNotifier.Add(Item as TComponent);
inherited Notify(Item, Action);
if (Action in [cnExtracted, cnRemoved]) and (Count = 0) then
begin
Clean;
Die;
end;
end;
function TAQ.ChildrenAppend(Recurse: Boolean; ChildrenFiller: TEachFunction): TAQ;
begin
if SupervisorLock(Result, aqmChildrenAppend) then
Exit;
if not Assigned(ChildrenFiller) then
ChildrenFiller := Self.ChildrenFiller;
Result := CustomFiller(ChildrenFiller, True, Recurse);
end;
function TAQ.ChildrenChain(Recurse: Boolean; ChildrenFiller: TEachFunction): TAQ;
begin
if SupervisorLock(Result, aqmChildrenChain) then
Exit;
if not Assigned(ChildrenFiller) then
ChildrenFiller := Self.ChildrenFiller;
Result := CustomFiller(ChildrenFiller, False, Recurse);
end;
function TAQ.ChildrenFiller(AQ: TAQ; O: TObject): Boolean;
var
cc: Integer;
OC: TComponent absolute O;
begin
Result := True;
if O is TComponent then
for cc := 0 to OC.ComponentCount - 1 do
AQ.Add(OC.Components[cc]);
end;
procedure TAQ.Clean;
begin
if not Finalized then
begin
// Global effects
GarbageCollector.Each(
function(GC: TAQ; O: TObject): Boolean
begin
// When any plugin is connected with this instance, so it must be freed
if (O is TAQPlugin) and (TAQPlugin(O).WorkAQ = Self) then
GC.Remove(O)
// If this instance is chained with an other, so it must be "unchained"
else if (O is TAQ) and (TAQ(O).FChainedTo = Self) then
TAQ(O).FChainedTo := nil;
Result := True; // Full scan
end);
end;
Clear;
FConditionCount := 0;
FBools := 0;
Recurse := True;
FCurrentInterval := nil;
FChainedTo := nil;
if Assigned(FIntervals) then
begin
ClearIntervals;
FreeAndNil(FIntervals);
end;
end;
procedure TAQ.ClearIntervals;
var
cc: Integer;
begin
for cc := FIntervals.Count - 1 downto 0 do
RemoveInterval(TInterval(FIntervals[cc]));
end;
class procedure TAQ.ComponentsNotification(AComponent: TComponent; Operation: TOperation);
begin
if (Operation = opRemove) and not Finalized then
// The removed component must be removed from all living TAQ instances
GarbageCollector.Each(
function(GC: TAQ; O: TObject): Boolean
begin
if TAQ(O).IsAlive then
TAQ(O).Remove(AComponent);
Result := True;
end);
end;
function TAQ.Contains(AObject: TObject): Boolean;