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