/
animation_group.dart
102 lines (86 loc) · 2.79 KB
/
animation_group.dart
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
part of '../animation.dart';
/// The [AnimationGroup] class is used to animate multiple Animatables.
/// Those Animatables are animated in parallel and the onComplete
/// callback is called when all Animatables have completed.
///
/// See also: [Juggler], [AnimationGroup], [Animatable]
///
/// Examples:
///
/// var ag = new AnimationGroup();
/// ag.add(new Tween(sprite, 2.0, Transition.easeOutBounce)..animate.x.to(700));
/// ag.add(new Tween(sprite, 2.0, Transition.linear)..animate.y.to(500));
/// ag.delay = 1.0;
/// ag.onStart = () => print("start");
/// ag.onComplete = () => print("complete");
/// juggler.add(ag);
///
/// juggler.addGroup([
/// new Tween(sprite, 2.0, Transition.easeOutBounce)..animate.x.to(700),
/// new Tween(sprite, 2.0, Transition.linear)..animate.y.to(500)])
/// ..onComplete = () => print("complete");
///
class AnimationGroup implements Animatable {
final List<Animatable> _animatables = <Animatable>[];
void Function()? _onStart;
void Function()? _onComplete;
num _time = 0.0;
num _delay = 0.0;
bool _started = false;
bool _completed = false;
//----------------------------------------------------------------------------
/// Adds the [animatable] to this [AnimationGroup].
void add(Animatable animatable) {
_animatables.add(animatable);
}
@override
bool advanceTime(num time) {
_time += time;
if (_started == false) {
if (_time > _delay) {
_started = true;
if (_onStart != null) _onStart!();
} else {
return true;
}
}
for (var i = 0; i < _animatables.length;) {
if (_animatables[i].advanceTime(time) == false) {
_animatables.removeAt(i);
} else {
i++;
}
}
if (_animatables.isEmpty) {
_completed = true;
if (_onComplete != null) _onComplete!();
return false;
} else {
return true;
}
}
//----------------------------------------------------------------------------
/// The delay this [AnimatableGroup] waits until it starts animating.
///
/// The delay may be changed as long as the animation has not been started.
num get delay => _delay;
set delay(num value) {
if (_started == false) {
_time = _time + _delay - value;
_delay = value;
}
}
/// Indicates if this [AnimatableGroup] is completed.
bool get isComplete => _completed;
//----------------------------------------------------------------------------
/// The function that is called when an [AnimationGroup] starts.
///
/// This happens after the specified [delay].
set onStart(void Function() function) {
_onStart = function;
}
/// The function that is called when a [AnimationGroup] is completed.
set onComplete(void Function() function) {
_onComplete = function;
}
}