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