Snapshot before changing swt list to comboviewer
[FinalKeyGui.git] / src / fkgui / Animation.java
1 package fkgui;
2
3 import java.util.Vector;
4
5 import org.eclipse.swt.graphics.Image;
6 import org.eclipse.swt.widgets.Composite;
7 import org.eclipse.swt.widgets.Display;
8 import org.eclipse.swt.widgets.Label;
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.layout.FormLayout;
11 import org.eclipse.swt.layout.FormData;
12 import org.eclipse.swt.layout.FormAttachment;
13 import org.eclipse.wb.swt.SWTResourceManager;
14
15 public class Animation extends Composite {
16
17         /**
18          * Create the composite.
19          * @param parent
20          * @param style
21          */
22         
23         public int msDelay=1000;
24         public Boolean playing=false;
25         public int curFrame=0;
26         public Vector<Image> frames;
27         Label label;
28         
29         public Animation(Composite parent, int style, float FPS) {
30                 super(parent, style);
31                 setLayout(new FormLayout());
32                 
33                 label = new Label(this, SWT.NONE);
34                 FormData fd_label = new FormData();
35                 fd_label.top = new FormAttachment(0);
36                 fd_label.left = new FormAttachment(0);
37                 fd_label.bottom = new FormAttachment(0, 32);
38                 fd_label.right = new FormAttachment(0, 32);
39                 label.setLayoutData(fd_label);
40                 frames = new Vector<Image>(0);
41                 setFPS(FPS);
42         }
43         
44         public void setPlaying(Boolean state )
45         {
46                 playing=state;
47                 if(playing)
48                 {
49                         animate();
50                 }
51         }
52
53         private void animate() {
54                 getDisplay().timerExec(msDelay, new Runnable() {
55                         
56                         @Override
57                         public void run() {
58
59                                 if( !label.isDisposed() )
60                                 {
61                                         if( isVisible() && frames.size() > 0 )
62                                         {
63                                                 curFrame++;
64
65                                                 if( curFrame > frames.size()-1 )
66                                                 {
67                                                         curFrame=0;
68                                                 }
69                                                 
70                                                 label.setImage( frames.get(curFrame) );
71         
72                                                 label.getDisplay().update();
73                                         }
74                                         if(playing)
75                                         {
76                                                 animate();
77                                         }
78                                 }
79                                 
80                         }
81                 });
82                 
83         }
84
85
86
87         @Override
88         protected void checkSubclass() {
89                 // Disable the check that prevents subclassing of SWT components
90         }
91
92         public void addFrame(Image image) {
93                 frames.addElement(image);
94         }
95         
96         public void setFPS( float fps )
97         {
98                 msDelay = (int)(1000.0 / fps);
99         }
100 }