Revision 15388
import/ext3D/branches/ext3D_v1.1/libAnimation/src/com/iver/cit/gvsig/animation/AnimationPlayer.java | ||
---|---|---|
3 | 3 |
import java.util.Timer; |
4 | 4 |
import java.util.TimerTask; |
5 | 5 |
|
6 |
import com.iver.cit.gvsig.animation.test.Animation3D; |
|
7 |
import com.iver.cit.gvsig.animation.test.AnimationTypeFactory3D; |
|
8 |
import com.iver.cit.gvsig.animation.timer.AnimationTimer; |
|
9 |
import com.iver.cit.gvsig.animation.timer.IUpdateCallBack; |
|
10 |
import com.iver.utiles.extensionPoints.ExtensionPoint; |
|
11 |
import com.iver.utiles.extensionPoints.ExtensionPoints; |
|
12 |
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton; |
|
13 |
|
|
6 | 14 |
/** |
7 | 15 |
* @author julio |
8 | 16 |
* |
9 | 17 |
*/ |
10 |
public class AnimationPlayer extends TimerTask { |
|
18 |
public class AnimationPlayer extends TimerTask implements IUpdateCallBack {
|
|
11 | 19 |
|
12 | 20 |
public AnimationPlayer() { |
13 |
|
|
21 |
|
|
14 | 22 |
} |
15 | 23 |
|
24 |
public static int ANIMATION_PLAY = 0; |
|
16 | 25 |
|
26 |
public static int ANIMATION_STOP = 1; |
|
17 | 27 |
|
18 |
private static int PLAY = 0; |
|
19 |
private static int STOP = 1; |
|
20 |
private static int PAUSE = 2; |
|
21 |
private static int RECORD = 3; |
|
22 |
|
|
23 |
private static int LOOP = 0; |
|
24 |
private static int LOOP_FORWARD = 1; |
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
28 |
public static int ANIMATION_PAUSE = 2; |
|
29 |
|
|
30 |
public static int ANIMATION_RECORD = 3; |
|
31 |
|
|
32 |
public static int LOOP = 0; |
|
33 |
|
|
34 |
public static int LOOP_FORWARD = 1; |
|
35 |
|
|
29 | 36 |
private AnimationContainer animationContainer; |
37 |
|
|
30 | 38 |
private int mode; |
39 |
|
|
31 | 40 |
private int frameRate; |
32 | 41 |
|
42 |
private Timer timer; |
|
43 |
|
|
44 |
private int animationPlayerState; |
|
45 |
|
|
46 |
private long animationFrequency = 30; |
|
47 |
|
|
48 |
private double preTime = 0.0; |
|
49 |
|
|
50 |
private double globalTime; |
|
51 |
|
|
52 |
private double time = 0.0; |
|
53 |
|
|
54 |
private AnimationTimer AT; |
|
55 |
|
|
56 |
private boolean pauseFlag = false; |
|
57 |
|
|
58 |
private static Animation3D a3D; |
|
59 |
|
|
33 | 60 |
public void play() { |
61 |
|
|
62 |
if (animationPlayerState != AnimationPlayer.ANIMATION_PLAY) { |
|
63 |
|
|
64 |
animationPlayerState = AnimationPlayer.ANIMATION_PLAY; |
|
65 |
// Timer Set Up |
|
66 |
// timer = new Timer(); |
|
67 |
// timer.scheduleAtFixedRate(this, 0, animationFrequency); |
|
68 |
AT = new AnimationTimer(); |
|
69 |
AT.addCallBackObject(this); |
|
70 |
AT.start(animationFrequency); |
|
71 |
|
|
72 |
} else if (animationPlayerState != AnimationPlayer.ANIMATION_STOP) { |
|
73 |
|
|
74 |
animationPlayerState = AnimationPlayer.ANIMATION_PLAY; |
|
75 |
|
|
76 |
} else if (animationPlayerState != AnimationPlayer.ANIMATION_PAUSE) { |
|
77 |
|
|
78 |
animationPlayerState = AnimationPlayer.ANIMATION_PLAY; |
|
79 |
|
|
80 |
} else if (animationPlayerState != AnimationPlayer.ANIMATION_RECORD) { |
|
81 |
|
|
82 |
animationPlayerState = AnimationPlayer.ANIMATION_PLAY; |
|
83 |
} |
|
84 |
|
|
34 | 85 |
// TODO generar metodo play |
35 | 86 |
|
36 |
// Timer Set Up |
|
37 |
Timer timer = new Timer(); |
|
38 |
timer.scheduleAtFixedRate(this, 0, 1000); |
|
39 | 87 |
} |
40 | 88 |
|
41 | 89 |
public void stop() { |
42 |
// TODO generar metodo de play |
|
90 |
|
|
43 | 91 |
|
92 |
if ((animationPlayerState != AnimationPlayer.ANIMATION_PLAY) || (animationPlayerState != AnimationPlayer.ANIMATION_PAUSE)) { |
|
93 |
|
|
94 |
animationPlayerState = AnimationPlayer.ANIMATION_STOP; |
|
95 |
// Stop timer |
|
96 |
// timer.cancel(); |
|
97 |
// time = 0.0; |
|
98 |
|
|
99 |
} else if (animationPlayerState != AnimationPlayer.ANIMATION_RECORD) { |
|
100 |
|
|
101 |
animationPlayerState = AnimationPlayer.ANIMATION_STOP; |
|
102 |
} |
|
103 |
AT.end(); |
|
104 |
this.time = 0; |
|
44 | 105 |
} |
45 | 106 |
|
46 | 107 |
public void pause() { |
47 |
// TODO generar metodo de pause |
|
108 |
if (animationPlayerState != AnimationPlayer.ANIMATION_PLAY) { |
|
109 |
|
|
110 |
animationPlayerState = AnimationPlayer.ANIMATION_PAUSE; |
|
111 |
|
|
112 |
} else if (animationPlayerState != AnimationPlayer.ANIMATION_STOP) { |
|
113 |
|
|
114 |
animationPlayerState = AnimationPlayer.ANIMATION_PAUSE; |
|
115 |
|
|
116 |
} else if (animationPlayerState != AnimationPlayer.ANIMATION_PAUSE) { |
|
117 |
|
|
118 |
animationPlayerState = AnimationPlayer.ANIMATION_PAUSE; |
|
119 |
|
|
120 |
} else if (animationPlayerState != AnimationPlayer.ANIMATION_RECORD) { |
|
121 |
|
|
122 |
animationPlayerState = AnimationPlayer.ANIMATION_PAUSE; |
|
123 |
} |
|
124 |
AT.end(); |
|
125 |
pauseFlag = true; |
|
48 | 126 |
} |
49 | 127 |
|
50 | 128 |
public void record() { |
51 |
// TODO gerenar metodo de record
|
|
129 |
if (animationPlayerState != AnimationPlayer.ANIMATION_PLAY) {
|
|
52 | 130 |
|
131 |
animationPlayerState = AnimationPlayer.ANIMATION_RECORD; |
|
132 |
|
|
133 |
} else if (animationPlayerState != AnimationPlayer.ANIMATION_STOP) { |
|
134 |
|
|
135 |
animationPlayerState = AnimationPlayer.ANIMATION_RECORD; |
|
136 |
|
|
137 |
} else if (animationPlayerState != AnimationPlayer.ANIMATION_PAUSE) { |
|
138 |
|
|
139 |
animationPlayerState = AnimationPlayer.ANIMATION_RECORD; |
|
140 |
|
|
141 |
} else if (animationPlayerState != AnimationPlayer.ANIMATION_RECORD) { |
|
142 |
|
|
143 |
animationPlayerState = AnimationPlayer.ANIMATION_RECORD; |
|
144 |
} |
|
53 | 145 |
} |
54 | 146 |
|
55 | 147 |
public void setFrameRate(int frameRate) { |
... | ... | |
59 | 151 |
} |
60 | 152 |
|
61 | 153 |
public void setMode(int mode) { |
62 |
// TODO generate method to set mode
|
|
154 |
// TODO generate method to set mode |
|
63 | 155 |
this.mode = mode; |
64 | 156 |
} |
65 | 157 |
|
... | ... | |
79 | 171 |
public AnimationPlayer(AnimationContainer animationContainer) { |
80 | 172 |
this.animationContainer = animationContainer; |
81 | 173 |
} |
82 |
|
|
83 |
private void apply(double Tini,double Tend) { |
|
84 |
getAnimationContainer().apply(Tini,Tend); |
|
174 |
|
|
175 |
private void apply(double Tini, double Tend) {
|
|
176 |
getAnimationContainer().apply(Tini, Tend);
|
|
85 | 177 |
} |
86 | 178 |
|
87 |
|
|
88 |
|
|
89 | 179 |
public void run() { |
90 | 180 |
// TODO Auto-generated method stub |
91 |
// Call back method for timer. |
|
92 |
System.out.println("estoy aki tiempo:" + System.currentTimeMillis()); |
|
181 |
|
|
182 |
if (this.animationPlayerState == AnimationPlayer.ANIMATION_PLAY) { |
|
183 |
|
|
184 |
// Calculate the delay |
|
185 |
double currentTime = System.currentTimeMillis(); |
|
186 |
double delay = currentTime - preTime; |
|
187 |
preTime = currentTime; |
|
188 |
// System.out.println("Retardo: " + delay); |
|
189 |
|
|
190 |
// if the delay could be hightest than the frequency, we have had |
|
191 |
// increased the time with this delay |
|
192 |
if (delay > animationFrequency) |
|
193 |
time += delay; |
|
194 |
else |
|
195 |
time += animationFrequency; |
|
196 |
// time += (delay > animationFrequency) ? delay : |
|
197 |
// animationFrequency; |
|
198 |
|
|
199 |
System.out.println("tiempo: " + time); |
|
200 |
time = (time > this.getGlobalTime()) ? 0 : time; |
|
201 |
|
|
202 |
// Apply and repaint all tracks. |
|
203 |
// apply(time / this.getGlobalTime(), 0); |
|
204 |
|
|
205 |
} else if (this.animationPlayerState == AnimationPlayer.ANIMATION_STOP) { |
|
206 |
|
|
207 |
} else if (this.animationPlayerState == AnimationPlayer.ANIMATION_PAUSE) { |
|
208 |
|
|
209 |
try { |
|
210 |
timer.wait(); |
|
211 |
} catch (InterruptedException e) { |
|
212 |
// TODO Auto-generated catch block |
|
213 |
e.printStackTrace(); |
|
214 |
} |
|
215 |
} else if (this.animationPlayerState == AnimationPlayer.ANIMATION_RECORD) { |
|
216 |
|
|
217 |
} |
|
218 |
System.out.println("sigo pasando por aki!!!!"); |
|
219 |
|
|
93 | 220 |
} |
94 | 221 |
|
95 |
} |
|
222 |
public static void main(String[] args) { |
|
223 |
Timer timer = new Timer(); |
|
224 |
final long frequency = 30; |
|
225 |
final long interval = 2; |
|
226 |
// timer.scheduleAtFixedRate(new TimerTask() { |
|
227 |
// |
|
228 |
// private double preTime = 0.0; |
|
229 |
// |
|
230 |
// private double globalTime = 0.0; |
|
231 |
// |
|
232 |
// public void run() { |
|
233 |
// // Aqu? el c?digo que queremos ejecutar. |
|
234 |
// |
|
235 |
// double delay = System.currentTimeMillis() - preTime; |
|
236 |
// System.out.println("ejecuto: " + delay); |
|
237 |
// if (delay > frequency + interval) |
|
238 |
// globalTime += delay; |
|
239 |
// else |
|
240 |
// globalTime += frequency; |
|
241 |
// |
|
242 |
// System.out.println("tiempo: " + globalTime); |
|
243 |
// globalTime = (globalTime > 3000) ? 0 : globalTime; |
|
244 |
// preTime = System.currentTimeMillis(); |
|
245 |
// try { |
|
246 |
// Thread.sleep(300); |
|
247 |
// } catch (InterruptedException e) { |
|
248 |
// // TODO Auto-generated catch block |
|
249 |
// e.printStackTrace(); |
|
250 |
// } |
|
251 |
// } |
|
252 |
// }, 0, frequency); |
|
253 |
|
|
254 |
// // Rellenando la estructura de datos |
|
255 |
// AnimationContainer AC = new AnimationContainer(); |
|
256 |
// |
|
257 |
// // Creando track del tipo de animacion |
|
258 |
// AnimationDatedTrack ADTrack = (AnimationDatedTrack) AC |
|
259 |
// .CreateDatedTrack(a3D); |
|
260 |
// // Inicializando el track |
|
261 |
// ADTrack.setName("Track 1"); |
|
262 |
// ADTrack.setDescription("track de prueba para series temporales"); |
|
263 |
// ADTrack.setBeginDate(new Date(0)); |
|
264 |
// ADTrack.setEndDate(new Date(0)); |
|
265 |
// // Creando intervalo |
|
266 |
// AnimationDatedInterval ADInterval = (AnimationDatedInterval) ADTrack |
|
267 |
// .createInterval(); |
|
268 |
// |
|
269 |
// // Inicializando intervalo |
|
270 |
// Date beginDateInterval = new Date(0); |
|
271 |
// ADInterval.setBeginDateInterval(beginDateInterval); |
|
272 |
// Date endDateInterval = new Date(0); |
|
273 |
// ADInterval.setEndDateInterval(endDateInterval); |
|
274 |
// |
|
275 |
// // Creando intervalo |
|
276 |
// AnimationDatedInterval ADInterval1 = (AnimationDatedInterval) ADTrack |
|
277 |
// .createInterval(); |
|
278 |
// |
|
279 |
// // Inicializando intervalo |
|
280 |
// Date beginDateInterval1 = new Date(0); |
|
281 |
// ADInterval1.setBeginDateInterval(beginDateInterval1); |
|
282 |
// Date endDateInterval1 = new Date(0); |
|
283 |
// ADInterval1.setEndDateInterval(endDateInterval1); |
|
284 |
// |
|
285 |
AnimationPlayer AP = new AnimationPlayer(); |
|
286 |
// AP.setAnimationContainer(AC); |
|
287 |
AP.setGlobalTime(10); |
|
288 |
// AP.setAnimationContainer(a); |
|
289 |
AP.play(); |
|
290 |
|
|
291 |
try { |
|
292 |
Thread.sleep(3000); |
|
293 |
} catch (InterruptedException e) { |
|
294 |
// TODO Auto-generated catch block |
|
295 |
e.printStackTrace(); |
|
296 |
} |
|
297 |
System.out.println("parando"); |
|
298 |
AP.stop(); |
|
299 |
|
|
300 |
|
|
301 |
|
|
302 |
} |
|
303 |
|
|
304 |
/** |
|
305 |
* Method only for TESTIN AND DEBUGING |
|
306 |
* |
|
307 |
* @throws Throwable |
|
308 |
*/ |
|
309 |
public void RegisterType() throws Throwable { |
|
310 |
|
|
311 |
AnimationTypeFactory3D.register(); |
|
312 |
ExtensionPoints extensionPoints = ExtensionPointsSingleton |
|
313 |
.getInstance(); |
|
314 |
System.out.println("Getting extension point..."); |
|
315 |
ExtensionPoint extPoint = ((ExtensionPoint) extensionPoints |
|
316 |
.get("Animation")); |
|
317 |
System.out.println("Creating animation type ..."); |
|
318 |
a3D = (Animation3D) extPoint.create("Animation3D"); |
|
319 |
System.out.println("Done " + a3D); |
|
320 |
// System.out.println("Resultados:\n " + a3D.getName() + "\n " |
|
321 |
// + a3D.getClassName() + "\n " + a3D.getDescription()); |
|
322 |
|
|
323 |
} |
|
324 |
|
|
325 |
public double getGlobalTime() { |
|
326 |
return (globalTime * 1000); |
|
327 |
} |
|
328 |
|
|
329 |
public void setGlobalTime(double globalTime) { |
|
330 |
this.globalTime = globalTime; |
|
331 |
} |
|
332 |
|
|
333 |
public int getAnimationPlayerState() { |
|
334 |
return animationPlayerState; |
|
335 |
} |
|
336 |
|
|
337 |
public void setAnimationPlayerState(int animationPlayerState) { |
|
338 |
this.animationPlayerState = animationPlayerState; |
|
339 |
} |
|
340 |
|
|
341 |
public void update() { |
|
342 |
// TODO Auto-generated method stub |
|
343 |
// System.out.println("refresco"); |
|
344 |
|
|
345 |
|
|
346 |
|
|
347 |
if (this.animationPlayerState == AnimationPlayer.ANIMATION_PLAY) { |
|
348 |
|
|
349 |
// Calculate the delay |
|
350 |
double currentTime = System.currentTimeMillis(); |
|
351 |
double delay = currentTime - preTime; |
|
352 |
preTime = currentTime; |
|
353 |
// System.out.println("Retardo: " + delay); |
|
354 |
|
|
355 |
// if the delay could be hightest than the frequency, we have had |
|
356 |
// increased the time with this delay |
|
357 |
if ((delay > animationFrequency) && (!pauseFlag)) |
|
358 |
time += delay; |
|
359 |
else |
|
360 |
time += animationFrequency; |
|
361 |
// time += (delay > animationFrequency) ? delay : |
|
362 |
// animationFrequency; |
|
363 |
|
|
364 |
System.out.println("tiempo: " + time); |
|
365 |
time = (time > this.getGlobalTime()) ? 0 : time; |
|
366 |
|
|
367 |
// Apply and repaint all tracks. |
|
368 |
// apply(time / this.getGlobalTime(), 0); |
|
369 |
|
|
370 |
} else if (this.animationPlayerState == AnimationPlayer.ANIMATION_STOP) { |
|
371 |
|
|
372 |
} else if (this.animationPlayerState == AnimationPlayer.ANIMATION_PAUSE) { |
|
373 |
|
|
374 |
try { |
|
375 |
timer.wait(); |
|
376 |
} catch (InterruptedException e) { |
|
377 |
// TODO Auto-generated catch block |
|
378 |
e.printStackTrace(); |
|
379 |
} |
|
380 |
} else if (this.animationPlayerState == AnimationPlayer.ANIMATION_RECORD) { |
|
381 |
|
|
382 |
} |
|
383 |
System.out.println("sigo pasando por aki!!!!"); |
|
384 |
} |
|
385 |
} |
Also available in: Unified diff