|
6 | 6 | import java.util.function.DoubleConsumer; |
7 | 7 | import java.util.function.DoublePredicate; |
8 | 8 |
|
9 | | -public class Animator extends BaseAnimator implements IAnimator { |
| 9 | +public class Animator extends BaseAnimator<Animator> implements IAnimator { |
10 | 10 |
|
11 | 11 | private float min = 0.0f; |
12 | 12 | private float max = 1.0f; |
13 | 13 | private int duration = 250; |
14 | 14 | private IInterpolation curve = Interpolation.LINEAR; |
15 | | - private boolean reverseOnFinish = false; |
16 | | - private int repeats = 0; |
| 15 | + |
17 | 16 | private DoublePredicate onUpdate; |
18 | 17 | private Runnable onFinish; |
19 | 18 |
|
20 | 19 | private int progress = 0; |
21 | | - private boolean startedReverse = false; |
22 | | - private int repeated = 0; |
| 20 | + |
23 | 21 |
|
24 | 22 | @Override |
25 | 23 | public void reset(boolean atEnd) { |
| 24 | + super.reset(atEnd); |
26 | 25 | this.progress = atEnd ? this.duration : 0; |
27 | | - this.startedReverse = atEnd; |
28 | | - this.repeated = 0; |
29 | 26 | } |
30 | 27 |
|
31 | | - @Override |
32 | | - public void stop(boolean force) { |
33 | | - if (isAnimating() && !force) { |
34 | | - if (this.reverseOnFinish && this.startedReverse == isAnimatingReverse()) { |
35 | | - onAnimationFinished(false, false); |
36 | | - // started reverse -> bounce back and animate forward |
37 | | - animate(isAnimatingForward()); |
38 | | - return; |
39 | | - } |
40 | | - if (repeats != 0 && (repeated < repeats || repeats < 0)) { |
41 | | - onAnimationFinished(true, false); |
42 | | - // started forward -> full cycle finished -> try repeating |
43 | | - boolean reverse = !this.reverseOnFinish == isAnimatingReverse(); |
44 | | - animate(reverse); |
45 | | - repeated++; |
46 | | - return; |
47 | | - } |
| 28 | + public Animator copy(boolean reversed) { |
| 29 | + Animator animator = new Animator() |
| 30 | + .curve(this.curve) |
| 31 | + .reverseOnFinish(this.reverseOnFinish) |
| 32 | + .repeatsOnFinish(this.repeats) |
| 33 | + .onUpdate(this.onUpdate) |
| 34 | + .duration(this.duration) |
| 35 | + .onFinish(this.onFinish); |
| 36 | + if (reversed) { |
| 37 | + animator.bounds(this.max, this.min); |
| 38 | + } else { |
| 39 | + animator.bounds(this.min, this.max); |
48 | 40 | } |
49 | | - super.stop(force); |
| 41 | + return animator; |
50 | 42 | } |
51 | 43 |
|
52 | 44 | @Override |
@@ -179,29 +171,7 @@ public Animator curve(IInterpolation curve) { |
179 | 171 | return this; |
180 | 172 | } |
181 | 173 |
|
182 | | - /** |
183 | | - * Sets if the animation should reverse animate once after it finished. |
184 | | - * If the animation started in reverse it will animate forward on finish. |
185 | | - * |
186 | | - * @param reverseOnFinish if animation should bounce back on finish |
187 | | - * @return this |
188 | | - */ |
189 | | - public Animator reverseOnFinish(boolean reverseOnFinish) { |
190 | | - this.reverseOnFinish = reverseOnFinish; |
191 | | - return this; |
192 | | - } |
193 | 174 |
|
194 | | - /** |
195 | | - * Sets how often the animation should repeat. If {@link #reverseOnFinish(boolean)} is set to true, it will repeat the whole cycle. |
196 | | - * If the number of repeats is negative, it will repeat infinitely. |
197 | | - * |
198 | | - * @param repeats how often the animation should repeat. |
199 | | - * @return this |
200 | | - */ |
201 | | - public Animator repeatsOnFinish(int repeats) { |
202 | | - this.repeats = repeats; |
203 | | - return this; |
204 | | - } |
205 | 175 |
|
206 | 176 | /** |
207 | 177 | * Sets a function which is executed everytime the progress updates, that is on every frame. |
|
0 commit comments