Statistics
| Revision:

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

History | View | Annotate | Download (11.5 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
        private static boolean occupied = false;
64

    
65
        private static Animation3D a3D;
66

    
67
        public void play() {
68

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

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

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

    
81
                        animationPlayerState = AnimationPlayer.ANIMATION_PLAY;
82

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

    
85
                        animationPlayerState = AnimationPlayer.ANIMATION_PLAY;
86

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

    
89
                        animationPlayerState = AnimationPlayer.ANIMATION_PLAY;
90
                }
91

    
92
                // TODO generar metodo play
93

    
94
        }
95

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

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

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

    
107
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_RECORD) {
108

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

    
115
        public void pause() {
116
                if (animationPlayerState != AnimationPlayer.ANIMATION_PLAY) {
117

    
118
                        animationPlayerState = AnimationPlayer.ANIMATION_PAUSE;
119

    
120
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_STOP) {
121

    
122
                        animationPlayerState = AnimationPlayer.ANIMATION_PAUSE;
123

    
124
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_PAUSE) {
125

    
126
                        animationPlayerState = AnimationPlayer.ANIMATION_PAUSE;
127

    
128
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_RECORD) {
129

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

    
136
        public void record() {
137
                if (animationPlayerState != AnimationPlayer.ANIMATION_PLAY) {
138

    
139
                        animationPlayerState = AnimationPlayer.ANIMATION_RECORD;
140

    
141
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_STOP) {
142

    
143
                        animationPlayerState = AnimationPlayer.ANIMATION_RECORD;
144

    
145
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_PAUSE) {
146

    
147
                        animationPlayerState = AnimationPlayer.ANIMATION_RECORD;
148

    
149
                } else if (animationPlayerState != AnimationPlayer.ANIMATION_RECORD) {
150

    
151
                        animationPlayerState = AnimationPlayer.ANIMATION_RECORD;
152
                }
153
        }
154

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

    
159
        }
160

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

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

    
169
        }
170

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
228
        }
229

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

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

    
299
                try {
300
                        Thread.sleep(3000);
301
                } catch (InterruptedException e) {
302
                        // TODO Auto-generated catch block
303
                        e.printStackTrace();
304
                }
305
                System.out.println("parando");
306
                AP.stop();
307

    
308
        }
309

    
310
        /**
311
         * Method only for TESTIN AND DEBUGING
312
         * 
313
         * @throws Throwable
314
         */
315
        public void RegisterType() throws Throwable {
316

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

    
329
        }
330

    
331
        public double getGlobalTime() {
332
                return (globalTime * 1000);
333
        }
334

    
335
        public void setGlobalTime(double globalTime) {
336
                this.globalTime = globalTime;
337
        }
338

    
339
        public int getAnimationPlayerState() {
340
                return animationPlayerState;
341
        }
342

    
343
        public void setAnimationPlayerState(int animationPlayerState) {
344
                this.animationPlayerState = animationPlayerState;
345
        }
346

    
347
        public void update() {
348
                // TODO Auto-generated method stub
349
                // System.out.println("refresco");
350

    
351
                if (!occupied) {
352
                        occupied = true;
353

    
354
                        if (this.animationPlayerState == AnimationPlayer.ANIMATION_PLAY) {
355

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

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

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

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

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

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

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

    
390
                        }
391
                        // System.out.println("sigo pasando por aki!!!!");
392
                        occupied = false;
393
                }
394
                System.out.println("ocupado : " + occupied);
395
        }
396

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

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

    
414
                return xml;
415
                
416
        }
417

    
418
        public void setXMLEntity(XMLEntity xml) {
419
                // TODO Auto-generated method stub
420

    
421
                
422
                if (xml.contains("mode"))
423
                        mode = xml.getIntProperty("mode");                
424
                if (xml.contains("length"))
425
                        auxTime = xml.getIntProperty("length");
426
                if (xml.contains("framerate"))
427
                        frameRate = xml.getIntProperty("framerate");
428
                                
429
        }
430
}