root / trunk / libraries / libAnimation / src / com / iver / cit / gvsig / animation / AnimationPlayer.java @ 19391
History | View | Annotate | Download (10.7 KB)
1 |
package com.iver.cit.gvsig.animation; |
---|---|
2 |
|
3 |
import com.iver.cit.gvsig.animation.timer.AnimationTimer; |
4 |
import com.iver.cit.gvsig.animation.timer.IUpdateCallBack; |
5 |
import com.iver.utiles.IPersistence; |
6 |
import com.iver.utiles.XMLEntity; |
7 |
|
8 |
/**
|
9 |
* @author ?ngel.
|
10 |
*
|
11 |
*/
|
12 |
public class AnimationPlayer implements IUpdateCallBack, |
13 |
IPersistence { |
14 |
|
15 |
public static int ANIMATION_PLAY = 0; |
16 |
|
17 |
public static int ANIMATION_STOP = 1; |
18 |
|
19 |
public static int ANIMATION_PAUSE = 2; |
20 |
|
21 |
public static int ANIMATION_RECORD = 3; |
22 |
|
23 |
public static int ANIMATION_INITIAL = 4;//Default initial state. |
24 |
//our animation modes.
|
25 |
public static int LOOP = 0;//begin..end - begin..end - begin..end... |
26 |
|
27 |
public static int PING_PONG_LOOP = 1;//begin..end - end..begin - begin..end... |
28 |
|
29 |
public static int PLAY_ONCE = 2;//begin..end |
30 |
|
31 |
public static int PLAY_ONCE_BACKWARDS = 3;//end..begin |
32 |
|
33 |
private AnimationContainer animationContainer;
|
34 |
|
35 |
private int frameRate = 30; |
36 |
|
37 |
private int animationPlayerState; //initial, play, pause, stop. |
38 |
|
39 |
private int animationMode = 0;//loop, ping pong, play once, play once backwards. |
40 |
|
41 |
private double animationFrequency = 1.0 / frameRate * 1000;// ms. |
42 |
|
43 |
private double preTime = 0.0; |
44 |
|
45 |
private double globalTime = 10.0; |
46 |
|
47 |
private static double time = 0.0; |
48 |
|
49 |
private static AnimationTimer AT; |
50 |
|
51 |
private boolean pauseFlag = false; |
52 |
|
53 |
private boolean playFlag = false; |
54 |
|
55 |
private String className; |
56 |
|
57 |
private static boolean occupied = false; |
58 |
|
59 |
private int direcction = 1;//1: 255-0, 0: 0-255. |
60 |
|
61 |
|
62 |
static {
|
63 |
AT = new AnimationTimer();
|
64 |
} |
65 |
|
66 |
public AnimationPlayer() {
|
67 |
} |
68 |
|
69 |
|
70 |
/**
|
71 |
* Logic actions when the play button is pressed.
|
72 |
*/
|
73 |
public void play() { |
74 |
double mia = ((double)1) / frameRate * 1000; |
75 |
if (animationPlayerState != AnimationPlayer.ANIMATION_PLAY){
|
76 |
animationPlayerState = AnimationPlayer.ANIMATION_PLAY; |
77 |
|
78 |
System.out.println(mia);
|
79 |
//AT = new AnimationTimer();
|
80 |
if (AT.isAlive() == false){ |
81 |
AT = new AnimationTimer();
|
82 |
AT.addCallBackObject(this);
|
83 |
AT.start(animationFrequency); |
84 |
} |
85 |
|
86 |
//Initial time control, backward animation: initial time = end time, forward animation: initial time = 0.
|
87 |
if ((this.getAnimationMode() == PLAY_ONCE_BACKWARDS)&& (!pauseFlag)) { |
88 |
time = this.getGlobalTime();
|
89 |
} |
90 |
// Play after pause or no.
|
91 |
else if(!pauseFlag) |
92 |
time = 0.0;
|
93 |
//Control of state PAUSE->PLAY.
|
94 |
// if (pauseFlag == true)
|
95 |
// pauseFlag = false;
|
96 |
System.err.println("time dentro del play: "+ time); |
97 |
playFlag = true;// flag of play running. |
98 |
//}IF DEL playFlag.
|
99 |
} |
100 |
} |
101 |
|
102 |
|
103 |
/**
|
104 |
* Logic actions when the stop button is pressed.
|
105 |
*/
|
106 |
public void stop() { |
107 |
if ((animationPlayerState != AnimationPlayer.ANIMATION_STOP) && (playFlag == true || pauseFlag == true)) {//If the state is not stop. |
108 |
animationPlayerState = AnimationPlayer.ANIMATION_STOP; |
109 |
if(playFlag == true)//flag to control that the actual state is play. |
110 |
playFlag = false;
|
111 |
if(pauseFlag == true)//flag to control that the actual state is pause. |
112 |
pauseFlag = false;
|
113 |
AnimationPlayer.occupied = false;
|
114 |
AT.end(); |
115 |
} |
116 |
} |
117 |
|
118 |
|
119 |
/**
|
120 |
* Logic actions when the pause button is pressed.
|
121 |
*/
|
122 |
public void pause() { |
123 |
if (animationPlayerState != AnimationPlayer.ANIMATION_PAUSE && playFlag == true) {//If the state is not pause. |
124 |
animationPlayerState = AnimationPlayer.ANIMATION_PAUSE; |
125 |
pauseFlag = true;//flag to control that is not first play pressing. |
126 |
playFlag = false;
|
127 |
AnimationPlayer.occupied = false;
|
128 |
AT.end(); |
129 |
} |
130 |
} |
131 |
|
132 |
|
133 |
/**
|
134 |
* Logic actions when the record button is pressed.
|
135 |
*/
|
136 |
public void record() { |
137 |
if (animationPlayerState != AnimationPlayer.ANIMATION_RECORD) {
|
138 |
animationPlayerState = AnimationPlayer.ANIMATION_RECORD; |
139 |
this.stop();//Provisional operation of record. |
140 |
} |
141 |
} |
142 |
|
143 |
/**
|
144 |
* Set of our player frequency.
|
145 |
*
|
146 |
* @param animationFrequency
|
147 |
*/
|
148 |
|
149 |
public void setFrameRate(int frameRate) { |
150 |
this.frameRate = frameRate;
|
151 |
} |
152 |
|
153 |
public void setTime() { |
154 |
// TODO generate method to set time
|
155 |
} |
156 |
|
157 |
public AnimationContainer getAnimationContainer() {
|
158 |
return animationContainer;
|
159 |
} |
160 |
|
161 |
public void setAnimationContainer(AnimationContainer animationContainer) { |
162 |
this.animationContainer = animationContainer;
|
163 |
} |
164 |
|
165 |
public AnimationPlayer(AnimationContainer animationContainer) {
|
166 |
this.animationContainer = animationContainer;
|
167 |
} |
168 |
|
169 |
private void apply(double Tini, double Tend) { |
170 |
getAnimationContainer().apply(Tini, Tend); |
171 |
} |
172 |
|
173 |
/**
|
174 |
* Getting the total duration of the animation.
|
175 |
* @return
|
176 |
*/
|
177 |
public double getGlobalTime() { |
178 |
return (globalTime * 1000); |
179 |
} |
180 |
|
181 |
|
182 |
/**
|
183 |
* Setting the total time of the animation.
|
184 |
* @param globalTime
|
185 |
*/
|
186 |
public void setGlobalTime(double globalTime) { |
187 |
this.globalTime = globalTime;
|
188 |
} |
189 |
|
190 |
/**
|
191 |
* Getting the actual state running.
|
192 |
* @return animationPlayerState: INITIAL, PLAY, STOP, PAUSE, RECORD.
|
193 |
*/
|
194 |
public int getAnimationPlayerState() { |
195 |
return animationPlayerState;
|
196 |
} |
197 |
|
198 |
|
199 |
/**
|
200 |
* Setting the state selected in the interface.
|
201 |
* @param animationPlayerState
|
202 |
*/
|
203 |
public void setAnimationPlayerState(int animationPlayerState) { |
204 |
this.animationPlayerState = animationPlayerState;
|
205 |
} |
206 |
|
207 |
|
208 |
/**
|
209 |
* Updating the timer according to our state and mode.
|
210 |
*/
|
211 |
public void update() { |
212 |
double currentTime=0, delay=0; |
213 |
if (!AnimationPlayer.occupied) {
|
214 |
AnimationPlayer.occupied = true;
|
215 |
//System.err.println("time dentro del update: "+ time);
|
216 |
//System.err.println("MODO: " + this.getAnimationMode());
|
217 |
if (this.animationPlayerState == AnimationPlayer.ANIMATION_PLAY) { |
218 |
if (pauseFlag == true) {//pause->play. |
219 |
currentTime = System.currentTimeMillis();
|
220 |
preTime = currentTime; |
221 |
//System.err.println("RETARDO PAUSE TRUE: " + delay);
|
222 |
pauseFlag = false;
|
223 |
}else{//normal play. |
224 |
//Calculate the delay
|
225 |
currentTime = System.currentTimeMillis();
|
226 |
delay = currentTime - preTime; |
227 |
preTime = currentTime; |
228 |
//System.err.println("RETARDO PAUSE FALSE: " + delay);
|
229 |
} |
230 |
System.err.println("RETARDO: " + delay); |
231 |
|
232 |
// if the delay could be hightest than the frequency, we have
|
233 |
// had increased the time with this delay
|
234 |
|
235 |
//Play once or loop animation selected.
|
236 |
if ((this.getAnimationMode() == PLAY_ONCE)||(this.getAnimationMode() == LOOP)) { |
237 |
if ((delay > animationFrequency) && (!pauseFlag))
|
238 |
{ |
239 |
if(time != 0) |
240 |
time += delay; |
241 |
else
|
242 |
time += animationFrequency; |
243 |
} |
244 |
else{
|
245 |
time += animationFrequency; |
246 |
//Play once animation.
|
247 |
} |
248 |
if (this.getAnimationMode() == PLAY_ONCE) { |
249 |
if (time >= this.getGlobalTime()){ |
250 |
AnimationPlayer.occupied = false;
|
251 |
this.stop();
|
252 |
} |
253 |
} else
|
254 |
time = (time > this.getGlobalTime()) ? 0 : time; |
255 |
}//if loop.
|
256 |
//Backward animation.
|
257 |
if (this.getAnimationMode() == PLAY_ONCE_BACKWARDS) { |
258 |
if ((delay > animationFrequency) && (!pauseFlag)){
|
259 |
if((time < this.getGlobalTime())) |
260 |
time -= delay; |
261 |
else
|
262 |
time -= animationFrequency; |
263 |
} |
264 |
else
|
265 |
time -= animationFrequency; |
266 |
if(time <= 0){ |
267 |
AnimationPlayer.occupied = false;
|
268 |
pauseFlag = false;
|
269 |
this.stop();
|
270 |
} |
271 |
}//if backwards.
|
272 |
//Ping pong animation.
|
273 |
if(this.getAnimationMode() == PING_PONG_LOOP){ |
274 |
if(direcction == 1){// 0..globalTime |
275 |
if ((delay > animationFrequency) && (!pauseFlag)){
|
276 |
if((time < this.getGlobalTime())){ |
277 |
time += delay; |
278 |
} |
279 |
else
|
280 |
time += animationFrequency; |
281 |
} |
282 |
else
|
283 |
time += animationFrequency; |
284 |
} |
285 |
else{
|
286 |
if ((delay > animationFrequency) && (!pauseFlag)){
|
287 |
if((time < this.getGlobalTime())){ |
288 |
time -= delay; |
289 |
} |
290 |
else
|
291 |
time -= animationFrequency; |
292 |
} |
293 |
else
|
294 |
time -= animationFrequency; |
295 |
} |
296 |
if(time > this.getGlobalTime()){ |
297 |
time = this.getGlobalTime();
|
298 |
direcction = 0;
|
299 |
} |
300 |
else if(time < 0.0){ |
301 |
time = 0.0;
|
302 |
direcction = 1;
|
303 |
} |
304 |
}//if ping pong.
|
305 |
// Apply and repaint all tracks.
|
306 |
apply(time / this.getGlobalTime(), 0); |
307 |
|
308 |
} else if (this.animationPlayerState == AnimationPlayer.ANIMATION_STOP) { |
309 |
|
310 |
} else if (this.animationPlayerState == AnimationPlayer.ANIMATION_PAUSE) { |
311 |
//System.err.println("STATE PAUSE!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
312 |
//AT.end();
|
313 |
// try {
|
314 |
// AT.wait();
|
315 |
// } catch (InterruptedException e) {
|
316 |
// // TODO Auto-generated catch block
|
317 |
// e.printStackTrace();
|
318 |
// }
|
319 |
|
320 |
} else if (this.animationPlayerState == AnimationPlayer.ANIMATION_RECORD) { |
321 |
|
322 |
} |
323 |
AnimationPlayer.occupied = false;
|
324 |
}//ocupied.
|
325 |
//System.out.println("ocupado : " + occupied);
|
326 |
} |
327 |
|
328 |
/**
|
329 |
* Return the actual time running in the player.
|
330 |
* @return time.
|
331 |
*/
|
332 |
public static double getCurrentTime() { |
333 |
return time;
|
334 |
} |
335 |
|
336 |
|
337 |
/**
|
338 |
* Return the name of this class.
|
339 |
* @return name.
|
340 |
*/
|
341 |
public String getClassName() { |
342 |
return this.getClass().getName(); |
343 |
} |
344 |
|
345 |
|
346 |
/**
|
347 |
* This function returns the actual state of the player.
|
348 |
*
|
349 |
* @return animationMode. INITIAL, PLAY, PAUSE, STOP.
|
350 |
*/
|
351 |
public int getAnimationMode() { |
352 |
return animationMode;
|
353 |
} |
354 |
|
355 |
|
356 |
/**
|
357 |
* It Depends the selected option the mode change.
|
358 |
*
|
359 |
* @param animationMode
|
360 |
*/
|
361 |
|
362 |
public void setAnimationMode(int animationMode) { |
363 |
this.animationMode = animationMode;
|
364 |
} |
365 |
|
366 |
|
367 |
/*
|
368 |
* IPersistence get method.
|
369 |
* @see com.iver.utiles.IPersistence#getXMLEntity()
|
370 |
*/
|
371 |
public XMLEntity getXMLEntity() {
|
372 |
XMLEntity xml = new XMLEntity();
|
373 |
//xml.putProperty("animationContainer", "true");
|
374 |
xml.putProperty("className", this.getClassName()); |
375 |
xml.putProperty("animationMode", this.animationMode); |
376 |
//xml.putProperty("animationPlayerState", animationPlayerState);
|
377 |
xml.putProperty("length",this.globalTime); |
378 |
// xml.putProperty("animationFrequency",this.animationFrequency);
|
379 |
|
380 |
return xml;
|
381 |
} |
382 |
|
383 |
|
384 |
/*
|
385 |
* IPersistence set method.
|
386 |
* @see com.iver.utiles.IPersistence#setXMLEntity(com.iver.utiles.XMLEntity)
|
387 |
*/
|
388 |
public void setXMLEntity(XMLEntity xml) { |
389 |
|
390 |
//if (xml.contains("className"))
|
391 |
// this.className = xml.getStringProperty("className");
|
392 |
if (xml.contains("animationMode")) |
393 |
this.animationMode = xml.getIntProperty("animationMode"); |
394 |
// if (xml.contains("animationPlayerState"))
|
395 |
// this.animationPlayerState = xml.getIntProperty("animationPlayerState");
|
396 |
if (xml.contains("length")){ |
397 |
this.globalTime = xml.getDoubleProperty("length"); |
398 |
} |
399 |
// if (xml.contains("animationFrequency"))
|
400 |
// this.animationFrequency = xml.getIntProperty("animationFrequency");
|
401 |
|
402 |
|
403 |
} |
404 |
} |