-
Notifications
You must be signed in to change notification settings - Fork 96
/
ParallelEffect.java
145 lines (128 loc) · 3.02 KB
/
ParallelEffect.java
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
/*******************************************************************************
* Copyright (c) 2009 Nicolas Richeton.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors :
* Nicolas Richeton (nicolas.richeton@gmail.com) - initial API and implementation
*******************************************************************************/
package org.eclipse.nebula.animation.effects;
import java.util.List;
/**
* A wrapper for running effects in parallel.
*
* @author Nicolas Richeton
*
*/
public class ParallelEffect implements IEffect {
IEffect[] effects = null;
long length = 0;
Runnable onCancel;
Runnable onStop;
/**
* Wrap several effects and start them in parallel.
*
* @param effects
*/
public ParallelEffect(IEffect[] effects) {
this(effects, null, null);
}
/**
* Wrap several effects and start them in parallel.
*
* @param effects
* @param onStop
* @param onCancel
*/
public ParallelEffect(IEffect[] effects, Runnable onStop,
Runnable onCancel) {
this.effects = effects;
this.onCancel = onCancel;
this.onStop = onStop;
// Get total length
if (effects != null) {
IEffect e = null;
for (int i = effects.length - 1; i >= 0; i--) {
e = (IEffect) effects[i];
if (e.getLength() > length) {
length = e.getLength();
}
}
}
}
/**
* Wrap several effects and start them in parallel.
*
* @param effects
*/
public ParallelEffect(List<IEffect> effects) {
this(effects, null, null);
}
/**
* Wrap several effects and start them in parallel.
*
* @param effects
* @param onStop
* @param onCancel
*/
public ParallelEffect(List<IEffect> effects, Runnable onStop,
Runnable onCancel) {
this((IEffect[]) effects.toArray(), onStop, onCancel);
}
/**
* @see org.eclipse.nebula.animation.effects.IEffect#cancel()
*/
public void cancel() {
if (effects != null) {
for (int i = effects.length - 1; i >= 0; i--) {
effects[i].cancel();
}
}
// Call cancel runnable
if (onCancel != null) {
onCancel.run();
}
}
/**
* @see org.eclipse.nebula.animation.effects.IEffect#doEffect(long)
*/
public void doEffect(long time) {
if (effects != null) {
for (int i = effects.length - 1; i >= 0; i--) {
effects[i].doEffect(time);
}
}
// Call stop runnable
if (onStop != null && isDone()) {
onStop.run();
}
}
/**
* @see org.eclipse.nebula.animation.effects.IEffect#getLength()
*/
public long getLength() {
return length;
}
/**
* @see org.eclipse.nebula.animation.effects.IEffect#isDone()
*/
public boolean isDone() {
if (effects != null) {
// Ensure all effects are done.
boolean done = true;
for (int i = effects.length - 1; i >= 0; i--) {
if (!effects[i].isDone()) {
done = false;
}
}
return done;
}
// No effects ? always done.
return true;
}
}