Statistics
| Revision:

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

History | View | Annotate | Download (11.3 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,IPersistance {
21

    
22
        public AnimationPlayer() {
23

    
24
        }
25

    
26
        public static int ANIMATION_PLAY = 0;
27

    
28
        public static int ANIMATION_STOP = 1;
29

    
30
        public static int ANIMATION_PAUSE = 2;
31

    
32
        public static int ANIMATION_RECORD = 3;
33

    
34
        public static int LOOP = 0;
35

    
36
        public static int LOOP_FORWARD = 1;
37

    
38
        private AnimationContainer animationContainer;
39

    
40
        private int mode;
41

    
42
        private int frameRate;
43

    
44
        private Timer timer;
45

    
46
        private int animationPlayerState;
47

    
48
        private long animationFrequency = 30;
49

    
50
        private double preTime = 0.0;
51

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

    
56
        private double time = 0.0;
57

    
58
        private AnimationTimer AT;
59

    
60
        private boolean pauseFlag = false;
61
        
62
        private double m_length;
63
        
64
        private static Animation3D a3D;
65

    
66
        public void play() {
67

    
68
                if (animationPlayerState != AnimationPlayer.ANIMATION_PLAY) {
69

    
70
                        animationPlayerState = AnimationPlayer.ANIMATION_PLAY;
71
                        // Timer Set Up
72
//                        timer = new Timer();
73
//                        timer.scheduleAtFixedRate(this, 0, animationFrequency);
74
                        AT = new AnimationTimer();
75
                        AT.addCallBackObject(this);
76
                        AT.start(animationFrequency);
77

    
78
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_STOP) {
79

    
80
                        animationPlayerState = AnimationPlayer.ANIMATION_PLAY;
81

    
82
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_PAUSE) {
83

    
84
                        animationPlayerState = AnimationPlayer.ANIMATION_PLAY;
85

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

    
88
                        animationPlayerState = AnimationPlayer.ANIMATION_PLAY;
89
                }
90

    
91
                // TODO generar metodo play
92

    
93
        }
94

    
95
        public void stop() {
96
                System.out.println("toy en metodo stop");
97

    
98
                if ((animationPlayerState != AnimationPlayer.ANIMATION_PLAY) || (animationPlayerState != AnimationPlayer.ANIMATION_PAUSE)) {
99

    
100
                        animationPlayerState = AnimationPlayer.ANIMATION_STOP;
101
                        // Stop timer
102
//                        timer.cancel();
103
//                        time = 0.0;
104

    
105
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_RECORD) {
106

    
107
                        animationPlayerState = AnimationPlayer.ANIMATION_STOP;
108
                }
109
                AT.end();
110
                this.time = 0;
111
        }
112

    
113
        public void pause() {
114
                if (animationPlayerState != AnimationPlayer.ANIMATION_PLAY) {
115

    
116
                        animationPlayerState = AnimationPlayer.ANIMATION_PAUSE;
117

    
118
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_STOP) {
119

    
120
                        animationPlayerState = AnimationPlayer.ANIMATION_PAUSE;
121

    
122
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_PAUSE) {
123

    
124
                        animationPlayerState = AnimationPlayer.ANIMATION_PAUSE;
125

    
126
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_RECORD) {
127

    
128
                        animationPlayerState = AnimationPlayer.ANIMATION_PAUSE;
129
                }
130
                AT.end();
131
                pauseFlag = true;
132
        }
133

    
134
        public void record() {
135
                if (animationPlayerState != AnimationPlayer.ANIMATION_PLAY) {
136

    
137
                        animationPlayerState = AnimationPlayer.ANIMATION_RECORD;
138

    
139
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_STOP) {
140

    
141
                        animationPlayerState = AnimationPlayer.ANIMATION_RECORD;
142

    
143
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_PAUSE) {
144

    
145
                        animationPlayerState = AnimationPlayer.ANIMATION_RECORD;
146

    
147
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_RECORD) {
148

    
149
                        animationPlayerState = AnimationPlayer.ANIMATION_RECORD;
150
                }
151
        }
152

    
153
        
154
        
155
        
156
        public void setFrameRate(int frameRate) {
157
                // TODO generate method to set frame rate
158
                this.frameRate = frameRate;
159

    
160
        }
161

    
162
        public void setMode(int mode) {
163
                // TODO generate method to set mode
164
                this.mode = mode;
165
        }
166

    
167
        public void setTime() {
168
                // TODO generate method to set time
169

    
170
        }
171
        
172
        public AnimationContainer getAnimationContainer() {
173
                return animationContainer;
174
        }
175

    
176
        public void setAnimationContainer(AnimationContainer animationContainer) {
177
                this.animationContainer = animationContainer;
178
        }
179

    
180
        public AnimationPlayer(AnimationContainer animationContainer) {
181
                this.animationContainer = animationContainer;
182
        }
183

    
184
        private void apply(double Tini, double Tend) {
185
                getAnimationContainer().apply(Tini, Tend);
186
        }
187

    
188
        public void run() {
189
                // TODO Auto-generated method stub
190

    
191
                if (this.animationPlayerState == AnimationPlayer.ANIMATION_PLAY) {
192

    
193
                        // Calculate the delay
194
                        double currentTime = System.currentTimeMillis();
195
                        double delay = currentTime - preTime;
196
                        preTime = currentTime;
197
                        // System.out.println("Retardo: " + delay);
198

    
199
                        // if the delay could be hightest than the frequency, we have had
200
                        // increased the time with this delay
201
                        if (delay > animationFrequency)
202
                                time += delay;
203
                        else
204
                                time += animationFrequency;
205
                        // time += (delay > animationFrequency) ? delay :
206
                        // animationFrequency;
207

    
208
                        System.out.println("tiempo: " + time);
209
                        time = (time > this.getGlobalTime()) ? 0 : time;
210

    
211
                        // Apply and repaint all tracks.
212
//                        apply(time / this.getGlobalTime(), 0);
213

    
214
                } else if (this.animationPlayerState == AnimationPlayer.ANIMATION_STOP) {
215

    
216
                } else if (this.animationPlayerState == AnimationPlayer.ANIMATION_PAUSE) {
217

    
218
                        try {
219
                                timer.wait();
220
                        } catch (InterruptedException e) {
221
                                // TODO Auto-generated catch block
222
                                e.printStackTrace();
223
                        }
224
                } else if (this.animationPlayerState == AnimationPlayer.ANIMATION_RECORD) {
225

    
226
                }
227
                System.out.println("sigo pasando por aki!!!!");
228

    
229
        }
230

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

    
263
//                // Rellenando la estructura de datos
264
//                AnimationContainer AC = new AnimationContainer();
265
//
266
//                // Creando track del tipo de animacion
267
//                AnimationDatedTrack ADTrack = (AnimationDatedTrack) AC
268
//                                .CreateDatedTrack(a3D);
269
//                // Inicializando el track
270
//                ADTrack.setName("Track 1");
271
//                ADTrack.setDescription("track de prueba para series temporales");
272
//                ADTrack.setBeginDate(new Date(0));
273
//                ADTrack.setEndDate(new Date(0));
274
//                // Creando intervalo
275
//                AnimationDatedInterval ADInterval = (AnimationDatedInterval) ADTrack
276
//                                .createInterval();
277
//
278
//                // Inicializando intervalo
279
//                Date beginDateInterval = new Date(0);
280
//                ADInterval.setBeginDateInterval(beginDateInterval);
281
//                Date endDateInterval = new Date(0);
282
//                ADInterval.setEndDateInterval(endDateInterval);
283
//
284
//                // Creando intervalo
285
//                AnimationDatedInterval ADInterval1 = (AnimationDatedInterval) ADTrack
286
//                                .createInterval();
287
//
288
//                // Inicializando intervalo
289
//                Date beginDateInterval1 = new Date(0);
290
//                ADInterval1.setBeginDateInterval(beginDateInterval1);
291
//                Date endDateInterval1 = new Date(0);
292
//                ADInterval1.setEndDateInterval(endDateInterval1);
293
//
294
                AnimationPlayer AP = new AnimationPlayer();
295
//                AP.setAnimationContainer(AC);
296
                AP.setGlobalTime(10);
297
//                AP.setAnimationContainer(a);
298
                AP.play();
299
                
300
                try {
301
                        Thread.sleep(3000);
302
                } catch (InterruptedException e) {
303
                        // TODO Auto-generated catch block
304
                        e.printStackTrace();
305
                }
306
                System.out.println("parando");
307
                AP.stop();
308
                
309
                
310

    
311
        }
312

    
313
        /**
314
         * Method only for TESTIN AND DEBUGING
315
         * 
316
         * @throws Throwable
317
         */
318
        public void RegisterType() throws Throwable {
319

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

    
332
        }
333

    
334
        public double getGlobalTime() {
335
                return (globalTime * 1000);
336
        }
337

    
338
        public void setGlobalTime(double globalTime) {
339
                this.globalTime = globalTime;
340
        }
341

    
342
        public int getAnimationPlayerState() {
343
                return animationPlayerState;
344
        }
345

    
346
        public void setAnimationPlayerState(int animationPlayerState) {
347
                this.animationPlayerState = animationPlayerState;
348
        }
349

    
350
        public void update() {
351
                // TODO Auto-generated method stub
352
//                System.out.println("refresco");
353
                
354
                
355
                
356
                if (this.animationPlayerState == AnimationPlayer.ANIMATION_PLAY) {
357

    
358
                        // Calculate the delay
359
                        double currentTime = System.currentTimeMillis();
360
                        double delay = currentTime - preTime;
361
                        preTime = currentTime;
362
                        // System.out.println("Retardo: " + delay);
363

    
364
                        // if the delay could be hightest than the frequency, we have had
365
                        // increased the time with this delay
366
                        if ((delay > animationFrequency) && (!pauseFlag))
367
                                time += delay;
368
                        else
369
                                time += animationFrequency;
370
                        // time += (delay > animationFrequency) ? delay :
371
                        // animationFrequency;
372

    
373
                        System.out.println("tiempo: " + time);
374
                        time = (time > this.getGlobalTime()) ? 0 : time;
375

    
376
                        // Apply and repaint all tracks.
377
//                        apply(time / this.getGlobalTime(), 0);
378

    
379
                } else if (this.animationPlayerState == AnimationPlayer.ANIMATION_STOP) {
380

    
381
                } else if (this.animationPlayerState == AnimationPlayer.ANIMATION_PAUSE) {
382

    
383
                        try {
384
                                timer.wait();
385
                        } catch (InterruptedException e) {
386
                                // TODO Auto-generated catch block
387
                                e.printStackTrace();
388
                        }
389
                } else if (this.animationPlayerState == AnimationPlayer.ANIMATION_RECORD) {
390

    
391
                }
392
                System.out.println("sigo pasando por aki!!!!");
393
        }
394
        
395

    
396
        /*         
397
         * IPersistance methods.
398
         */
399
        
400
        public String getClassName() {
401
                // TODO Auto-generated method stub
402
                return this.getClass().getName();
403
        }
404

    
405
        public XMLEntity getXMLEntity() {
406
                // TODO Auto-generated method stub
407
                XMLEntity xml = new XMLEntity();
408
                xml.putProperty("className", this.getClassName());
409
                xml.putProperty("mode", mode);
410
                xml.putProperty("length",auxTime);
411
                xml.putProperty("framerate",frameRate);
412

    
413
                return xml;
414
                
415
        }
416

    
417
        public void setXMLEntity(XMLEntity xml) {
418
                // TODO Auto-generated method stub
419
                
420
                if (xml.contains("mode"))
421
                        mode = xml.getIntProperty("mode");                
422
                if (xml.contains("length"))
423
                        auxTime = xml.getIntProperty("length");
424
                if (xml.contains("framerate"))
425
                        frameRate = xml.getIntProperty("framerate");
426
                                
427
        }
428
}