Statistics
| Revision:

svn-gvsig-desktop / import / ext3D / branches / ext3D_v1.1 / libAnimation / src / com / iver / cit / gvsig / animation / AnimationPlayer.java @ 15445

History | View | Annotate | Download (11.6 KB)

1
package com.iver.cit.gvsig.animation;
2

    
3
import java.util.Timer;
4
import java.util.TimerTask;
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.IPersistance;
11
import com.iver.utiles.XMLEntity;
12
import com.iver.utiles.extensionPoints.ExtensionPoint;
13
import com.iver.utiles.extensionPoints.ExtensionPoints;
14
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
15

    
16
/**
17
 * @author julio
18
 * 
19
 */
20
public class AnimationPlayer extends TimerTask implements IUpdateCallBack,
21
                IPersistance {
22

    
23
        public AnimationPlayer() {
24

    
25
        }
26

    
27
        public static int ANIMATION_PLAY = 0;
28

    
29
        public static int ANIMATION_STOP = 1;
30

    
31
        public static int ANIMATION_PAUSE = 2;
32

    
33
        public static int ANIMATION_RECORD = 3;
34

    
35
        public static int LOOP = 0;
36

    
37
        public static int LOOP_FORWARD = 1;
38

    
39
        private AnimationContainer animationContainer;
40

    
41
        private int mode;
42

    
43
        private int frameRate;
44

    
45
        private Timer timer;
46

    
47
        private int animationPlayerState;
48

    
49
        private long animationFrequency = 30;
50

    
51
        private double preTime = 0.0;
52

    
53
        private double globalTime;
54
        
55
        private double auxTime;
56

    
57
        private double time = 0.0;
58

    
59
        private AnimationTimer AT;
60

    
61
        private boolean pauseFlag = false;
62

    
63
        
64
        private double m_length;
65

    
66
        private String className;
67

    
68

    
69
        private static boolean occupied = false;
70

    
71

    
72
        private static Animation3D a3D;
73

    
74
        public void play() {
75

    
76
                if (animationPlayerState != AnimationPlayer.ANIMATION_PLAY) {
77

    
78
                        animationPlayerState = AnimationPlayer.ANIMATION_PLAY;
79
                        // Timer Set Up
80
                        // timer = new Timer();
81
                        // timer.scheduleAtFixedRate(this, 0, animationFrequency);
82
                        AT = new AnimationTimer();
83
                        AT.addCallBackObject(this);
84
                        AT.start(animationFrequency);
85

    
86
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_STOP) {
87

    
88
                        animationPlayerState = AnimationPlayer.ANIMATION_PLAY;
89

    
90
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_PAUSE) {
91

    
92
                        animationPlayerState = AnimationPlayer.ANIMATION_PLAY;
93

    
94
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_RECORD) {
95

    
96
                        animationPlayerState = AnimationPlayer.ANIMATION_PLAY;
97
                }
98

    
99
                // TODO generar metodo play
100

    
101
        }
102

    
103
        public void stop() {
104
                System.out.println("toy en metodo stop");
105

    
106
                if ((animationPlayerState != AnimationPlayer.ANIMATION_PLAY)
107
                                || (animationPlayerState != AnimationPlayer.ANIMATION_PAUSE)) {
108

    
109
                        animationPlayerState = AnimationPlayer.ANIMATION_STOP;
110
                        // Stop timer
111
                        // timer.cancel();
112
                        // time = 0.0;
113

    
114
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_RECORD) {
115

    
116
                        animationPlayerState = AnimationPlayer.ANIMATION_STOP;
117
                }
118
                AT.end();
119
                this.time = 0;
120
        }
121

    
122
        public void pause() {
123
                if (animationPlayerState != AnimationPlayer.ANIMATION_PLAY) {
124

    
125
                        animationPlayerState = AnimationPlayer.ANIMATION_PAUSE;
126

    
127
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_STOP) {
128

    
129
                        animationPlayerState = AnimationPlayer.ANIMATION_PAUSE;
130

    
131
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_PAUSE) {
132

    
133
                        animationPlayerState = AnimationPlayer.ANIMATION_PAUSE;
134

    
135
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_RECORD) {
136

    
137
                        animationPlayerState = AnimationPlayer.ANIMATION_PAUSE;
138
                }
139
                AT.end();
140
                pauseFlag = true;
141
        }
142

    
143
        public void record() {
144
                if (animationPlayerState != AnimationPlayer.ANIMATION_PLAY) {
145

    
146
                        animationPlayerState = AnimationPlayer.ANIMATION_RECORD;
147

    
148
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_STOP) {
149

    
150
                        animationPlayerState = AnimationPlayer.ANIMATION_RECORD;
151

    
152
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_PAUSE) {
153

    
154
                        animationPlayerState = AnimationPlayer.ANIMATION_RECORD;
155

    
156
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_RECORD) {
157

    
158
                        animationPlayerState = AnimationPlayer.ANIMATION_RECORD;
159
                }
160
        }
161

    
162
        public void setFrameRate(int frameRate) {
163
                // TODO generate method to set frame rate
164
                this.frameRate = frameRate;
165

    
166
        }
167

    
168
        public void setMode(int mode) {
169
                // TODO generate method to set mode
170
                this.mode = mode;
171
        }
172

    
173
        public void setTime() {
174
                // TODO generate method to set time
175

    
176
        }
177

    
178
        public AnimationContainer getAnimationContainer() {
179
                return animationContainer;
180
        }
181

    
182
        public void setAnimationContainer(AnimationContainer animationContainer) {
183
                this.animationContainer = animationContainer;
184
        }
185

    
186
        public AnimationPlayer(AnimationContainer animationContainer) {
187
                this.animationContainer = animationContainer;
188
        }
189

    
190
        private void apply(double Tini, double Tend) {
191
                getAnimationContainer().apply(Tini, Tend);
192
        }
193

    
194
        public void run() {
195
                // TODO Auto-generated method stub
196

    
197
                if (this.animationPlayerState == AnimationPlayer.ANIMATION_PLAY) {
198

    
199
                        // Calculate the delay
200
                        double currentTime = System.currentTimeMillis();
201
                        double delay = currentTime - preTime;
202
                        preTime = currentTime;
203
                        // System.out.println("Retardo: " + delay);
204

    
205
                        // if the delay could be hightest than the frequency, we have had
206
                        // increased the time with this delay
207
                        if (delay > animationFrequency)
208
                                time += delay;
209
                        else
210
                                time += animationFrequency;
211
                        // time += (delay > animationFrequency) ? delay :
212
                        // animationFrequency;
213

    
214
                        System.out.println("tiempo: " + time);
215
                        time = (time > this.getGlobalTime()) ? 0 : time;
216

    
217
                        // Apply and repaint all tracks.
218
                        // apply(time / this.getGlobalTime(), 0);
219

    
220
                } else if (this.animationPlayerState == AnimationPlayer.ANIMATION_STOP) {
221

    
222
                } else if (this.animationPlayerState == AnimationPlayer.ANIMATION_PAUSE) {
223

    
224
                        try {
225
                                timer.wait();
226
                        } catch (InterruptedException e) {
227
                                // TODO Auto-generated catch block
228
                                e.printStackTrace();
229
                        }
230
                } else if (this.animationPlayerState == AnimationPlayer.ANIMATION_RECORD) {
231

    
232
                }
233
                System.out.println("sigo pasando por aki!!!!");
234

    
235
        }
236

    
237
        public static void main(String[] args) {
238
                Timer timer = new Timer();
239
                final long frequency = 30;
240
                final long interval = 2;
241
                // timer.scheduleAtFixedRate(new TimerTask() {
242
                //
243
                // private double preTime = 0.0;
244
                //
245
                // private double globalTime = 0.0;
246
                //
247
                // public void run() {
248
                // // Aqu? el c?digo que queremos ejecutar.
249
                //
250
                // double delay = System.currentTimeMillis() - preTime;
251
                // System.out.println("ejecuto: " + delay);
252
                // if (delay > frequency + interval)
253
                // globalTime += delay;
254
                // else
255
                // globalTime += frequency;
256
                //
257
                // System.out.println("tiempo: " + globalTime);
258
                // globalTime = (globalTime > 3000) ? 0 : globalTime;
259
                // preTime = System.currentTimeMillis();
260
                // try {
261
                // Thread.sleep(300);
262
                // } catch (InterruptedException e) {
263
                // // TODO Auto-generated catch block
264
                // e.printStackTrace();
265
                // }
266
                // }
267
                // }, 0, frequency);
268

    
269
                // // Rellenando la estructura de datos
270
                // AnimationContainer AC = new AnimationContainer();
271
                //
272
                // // Creando track del tipo de animacion
273
                // AnimationDatedTrack ADTrack = (AnimationDatedTrack) AC
274
                // .CreateDatedTrack(a3D);
275
                // // Inicializando el track
276
                // ADTrack.setName("Track 1");
277
                // ADTrack.setDescription("track de prueba para series temporales");
278
                // ADTrack.setBeginDate(new Date(0));
279
                // ADTrack.setEndDate(new Date(0));
280
                // // Creando intervalo
281
                // AnimationDatedInterval ADInterval = (AnimationDatedInterval) ADTrack
282
                // .createInterval();
283
                //
284
                // // Inicializando intervalo
285
                // Date beginDateInterval = new Date(0);
286
                // ADInterval.setBeginDateInterval(beginDateInterval);
287
                // Date endDateInterval = new Date(0);
288
                // ADInterval.setEndDateInterval(endDateInterval);
289
                //
290
                // // Creando intervalo
291
                // AnimationDatedInterval ADInterval1 = (AnimationDatedInterval) ADTrack
292
                // .createInterval();
293
                //
294
                // // Inicializando intervalo
295
                // Date beginDateInterval1 = new Date(0);
296
                // ADInterval1.setBeginDateInterval(beginDateInterval1);
297
                // Date endDateInterval1 = new Date(0);
298
                // ADInterval1.setEndDateInterval(endDateInterval1);
299
                //
300
                AnimationPlayer AP = new AnimationPlayer();
301
                // AP.setAnimationContainer(AC);
302
                AP.setGlobalTime(10);
303
                // AP.setAnimationContainer(a);
304
                AP.play();
305

    
306
                try {
307
                        Thread.sleep(3000);
308
                } catch (InterruptedException e) {
309
                        // TODO Auto-generated catch block
310
                        e.printStackTrace();
311
                }
312
                System.out.println("parando");
313
                AP.stop();
314

    
315
        }
316

    
317
        /**
318
         * Method only for TESTIN AND DEBUGING
319
         * 
320
         * @throws Throwable
321
         */
322
        public void RegisterType() throws Throwable {
323

    
324
                AnimationTypeFactory3D.register();
325
                ExtensionPoints extensionPoints = ExtensionPointsSingleton
326
                                .getInstance();
327
                System.out.println("Getting extension point...");
328
                ExtensionPoint extPoint = ((ExtensionPoint) extensionPoints
329
                                .get("Animation"));
330
                System.out.println("Creating animation type ...");
331
                a3D = (Animation3D) extPoint.create("Animation3D");
332
                System.out.println("Done " + a3D);
333
                // System.out.println("Resultados:\n " + a3D.getName() + "\n "
334
                // + a3D.getClassName() + "\n " + a3D.getDescription());
335

    
336
        }
337

    
338
        public double getGlobalTime() {
339
                return (globalTime * 1000);
340
        }
341

    
342
        public void setGlobalTime(double globalTime) {
343
                this.globalTime = globalTime;
344
        }
345

    
346
        public int getAnimationPlayerState() {
347
                return animationPlayerState;
348
        }
349

    
350
        public void setAnimationPlayerState(int animationPlayerState) {
351
                this.animationPlayerState = animationPlayerState;
352
        }
353

    
354
        public void update() {
355
                // TODO Auto-generated method stub
356
                // System.out.println("refresco");
357

    
358
                if (!occupied) {
359
                        occupied = true;
360

    
361
                        if (this.animationPlayerState == AnimationPlayer.ANIMATION_PLAY) {
362

    
363
                                // Calculate the delay
364
                                double currentTime = System.currentTimeMillis();
365
                                double delay = currentTime - preTime;
366
                                preTime = currentTime;
367
                                // System.out.println("Retardo: " + delay);
368

    
369
                                // if the delay could be hightest than the frequency, we have
370
                                // had
371
                                // increased the time with this delay
372
                                if ((delay > animationFrequency) && (!pauseFlag))
373
                                        time += delay;
374
                                else
375
                                        time += animationFrequency;
376
                                // time += (delay > animationFrequency) ? delay :
377
                                // animationFrequency;
378

    
379
//                                System.out.println("tiempo: " + time);
380
                                time = (time > this.getGlobalTime()) ? 0 : time;
381

    
382
                                // Apply and repaint all tracks.
383
                                apply(time / this.getGlobalTime(), 0);
384

    
385
                        } else if (this.animationPlayerState == AnimationPlayer.ANIMATION_STOP) {
386

    
387
                        } else if (this.animationPlayerState == AnimationPlayer.ANIMATION_PAUSE) {
388

    
389
                                try {
390
                                        timer.wait();
391
                                } catch (InterruptedException e) {
392
                                        // TODO Auto-generated catch block
393
                                        e.printStackTrace();
394
                                }
395
                        } else if (this.animationPlayerState == AnimationPlayer.ANIMATION_RECORD) {
396

    
397
                        }
398
                        // System.out.println("sigo pasando por aki!!!!");
399
                        occupied = false;
400
                }
401
                System.out.println("ocupado : " + occupied);
402
        }
403

    
404
        /*         
405
         * IPersistance methods.
406
         */
407
        
408
        public String getClassName() {
409
                // TODO Auto-generated method stub
410
                return this.getClass().getName();
411
        }
412

    
413
        public XMLEntity getXMLEntity() {
414
                // TODO Auto-generated method stub
415
                XMLEntity xml = new XMLEntity();
416
                xml.putProperty("class_name", this.getClassName());
417
                xml.putProperty("mode", mode);
418
                xml.putProperty("length",globalTime);
419
                xml.putProperty("framerate",frameRate);
420

    
421
                return xml;
422
                
423
        }
424

    
425
        public void setXMLEntity(XMLEntity xml) {
426
                // TODO Auto-generated method stub
427

    
428
                
429
                if (xml.contains("class_name"))
430
                        className = xml.getStringProperty("class_name");        
431
                if (xml.contains("mode"))
432
                        mode = xml.getIntProperty("mode");                
433
                if (xml.contains("length"))
434
                        globalTime = xml.getDoubleProperty("length");
435
                if (xml.contains("framerate"))
436
                        frameRate = xml.getIntProperty("framerate");
437
                                
438
        }
439
}