More gui work, more testing to do.
[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>(7);
41                 setFPS(FPS);
42                 addFrame( SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/ani/1.png") ); //$NON-NLS-1$
43                 addFrame( SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/ani/2.png") ); //$NON-NLS-1$
44                 addFrame( SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/ani/3.png") ); //$NON-NLS-1$
45                 addFrame( SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/ani/4.png") ); //$NON-NLS-1$
46                 addFrame( SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/ani/5.png") ); //$NON-NLS-1$
47                 addFrame( SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/ani/6.png") ); //$NON-NLS-1$
48                 addFrame( SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/ani/7.png") ); //$NON-NLS-1$
49         }
50         
51         public void setPlaying(Boolean state )
52         {
53                 playing=state;
54                 if(playing)
55                 {
56                         animate();
57                 }
58         }
59
60         private void animate() {
61                 getDisplay().timerExec(msDelay, new Runnable() {
62                         
63                         @Override
64                         public void run() {
65
66                                 if( !label.isDisposed() )
67                                 {
68                                         if( isVisible() && frames.size() > 0 )
69                                         {
70                                                 curFrame++;
71
72                                                 if( curFrame > frames.size()-1 )
73                                                 {
74                                                         curFrame=0;
75                                                 }
76                                                 
77                                                 label.setImage( frames.get(curFrame) );
78         
79                                                 label.getDisplay().update();
80                                         }
81                                         if(playing)
82                                         {
83                                                 animate();
84                                         }
85                                 }
86                                 
87                         }
88                 });
89                 
90         }
91
92
93
94         @Override
95         protected void checkSubclass() {
96                 // Disable the check that prevents subclassing of SWT components
97         }
98
99         private void addFrame(Image image) {
100                 frames.addElement(image);
101         }
102         
103         private void setFPS( float fps )
104         {
105                 msDelay = (int)(1000.0 / fps);
106         }
107 }