78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2008 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 *******************************************************************************/
|
|
11 module snippets.toolbar.Snippet288;
|
|
12
|
|
13 /*
|
|
14 * Create a ToolBar containing animated GIFs
|
|
15 *
|
|
16 * For a list of all SWT example snippets see
|
|
17 * http://www.eclipse.org/swt/snippets/
|
|
18 */
|
|
19 import dwt.DWT;
|
|
20 import dwt.DWTException;
|
|
21 import dwt.graphics.GC;
|
|
22 import dwt.graphics.Color;
|
|
23 import dwt.graphics.Image;
|
|
24 import dwt.graphics.ImageLoader;
|
|
25 import dwt.graphics.ImageData;
|
|
26 import dwt.widgets.Display;
|
|
27 import dwt.widgets.Shell;
|
|
28 import dwt.widgets.ToolBar;
|
|
29 import dwt.widgets.ToolItem;
|
|
30 import dwt.widgets.FileDialog;
|
|
31 import dwt.dwthelper.Runnable;
|
|
32
|
|
33 import tango.io.FilePath;
|
|
34 import tango.io.FileConst;
|
|
35 import tango.core.Thread;
|
|
36 import tango.io.Stdout;
|
|
37 import tango.util.Convert;
|
|
38 import tango.core.Exception;
|
|
39
|
|
40 static Display display;
|
|
41 static Shell shell;
|
|
42 static GC shellGC;
|
|
43 static Color shellBackground;
|
|
44 static ImageLoader[] loader;
|
|
45 static ImageData[][] imageDataArray;
|
|
46 static Thread[] animateThread;
|
|
47 static Image[][] image;
|
|
48 private static ToolItem[] item;
|
|
49 static final bool useGIFBackground = false;
|
|
50
|
|
51 void main () {
|
|
52 display = new Display();
|
|
53 Shell shell = new Shell (display);
|
|
54 shellBackground = shell.getBackground();
|
|
55 FileDialog dialog = new FileDialog(shell, DWT.OPEN | DWT.MULTI);
|
|
56 dialog.setText("Select Multiple Animated GIFs");
|
|
57 dialog.setFilterExtensions(["*.gif"]);
|
|
58 char[] filename = dialog.open();
|
|
59 char[][] filenames = dialog.getFileNames();
|
|
60 int numToolBarItems = filenames.length;
|
|
61 if (numToolBarItems > 0) {
|
|
62 try {
|
|
63 loadAllImages((new FilePath(filename)).parent, filenames);
|
|
64 } catch (DWTException e) {
|
|
65 Stdout.print("There was an error loading an image.").newline;
|
|
66 e.printStackTrace();
|
|
67 }
|
|
68 ToolBar toolBar = new ToolBar (shell, DWT.FLAT | DWT.BORDER | DWT.WRAP);
|
|
69 item = new ToolItem[numToolBarItems];
|
|
70 for (int i = 0; i < numToolBarItems; i++) {
|
|
71 item[i] = new ToolItem (toolBar, DWT.PUSH);
|
|
72 item[i].setImage(image[i][0]);
|
|
73 }
|
|
74 toolBar.pack ();
|
|
75 shell.open ();
|
|
76
|
|
77 startAnimationThreads();
|
|
78
|
|
79 while (!shell.isDisposed()) {
|
|
80 if (!display.readAndDispatch ()) display.sleep ();
|
|
81 }
|
|
82
|
|
83 for (int i = 0; i < numToolBarItems; i++) {
|
|
84 for (int j = 0; j < image[i].length; j++) {
|
|
85 image[i][j].dispose();
|
|
86 }
|
|
87 }
|
|
88 display.dispose ();
|
|
89 }
|
|
90 thread_joinAll();
|
|
91 }
|
|
92
|
|
93 private static void loadAllImages(char[] directory, char[][] filenames) {
|
|
94 int numItems = filenames.length;
|
|
95 loader.length = numItems;
|
|
96 imageDataArray.length = numItems;
|
|
97 image.length = numItems;
|
|
98 for (int i = 0; i < numItems; i++) {
|
|
99 loader[i] = new ImageLoader();
|
|
100 int fullWidth = loader[i].logicalScreenWidth;
|
|
101 int fullHeight = loader[i].logicalScreenHeight;
|
|
102 imageDataArray[i] = loader[i].load(directory ~ FileConst.PathSeparatorChar ~ filenames[i]);
|
|
103 int numFramesOfAnimation = imageDataArray[i].length;
|
|
104 image[i] = new Image[numFramesOfAnimation];
|
|
105 for (int j = 0; j < numFramesOfAnimation; j++) {
|
|
106 if (j == 0) {
|
|
107 //for the first frame of animation, just draw the first frame
|
|
108 image[i][j] = new Image(display, imageDataArray[i][j]);
|
|
109 fullWidth = imageDataArray[i][j].width;
|
|
110 fullHeight = imageDataArray[i][j].height;
|
|
111 }
|
|
112 else {
|
|
113 //after the first frame of animation, draw the background or previous frame first, then the new image data
|
|
114 image[i][j] = new Image(display, fullWidth, fullHeight);
|
|
115 GC gc = new GC(image[i][j]);
|
|
116 gc.setBackground(shellBackground);
|
|
117 gc.fillRectangle(0, 0, fullWidth, fullHeight);
|
|
118 ImageData imageData = imageDataArray[i][j];
|
|
119 switch (imageData.disposalMethod) {
|
|
120 case DWT.DM_FILL_BACKGROUND:
|
|
121 /* Fill with the background color before drawing. */
|
|
122 Color bgColor = null;
|
|
123 if (useGIFBackground && loader[i].backgroundPixel != -1) {
|
|
124 bgColor = new Color(display, imageData.palette.getRGB(loader[i].backgroundPixel));
|
|
125 }
|
|
126 gc.setBackground(bgColor !is null ? bgColor : shellBackground);
|
|
127 gc.fillRectangle(imageData.x, imageData.y, imageData.width, imageData.height);
|
|
128 if (bgColor !is null) bgColor.dispose();
|
|
129 break;
|
|
130 default:
|
|
131 /* Restore the previous image before drawing. */
|
|
132 gc.drawImage(
|
|
133 image[i][j-1],
|
|
134 0,
|
|
135 0,
|
|
136 fullWidth,
|
|
137 fullHeight,
|
|
138 0,
|
|
139 0,
|
|
140 fullWidth,
|
|
141 fullHeight);
|
|
142 break;
|
|
143 }
|
|
144 Image newFrame = new Image(display, imageData);
|
|
145 gc.drawImage(newFrame,
|
|
146 0,
|
|
147 0,
|
|
148 imageData.width,
|
|
149 imageData.height,
|
|
150 imageData.x,
|
|
151 imageData.y,
|
|
152 imageData.width,
|
|
153 imageData.height);
|
|
154 newFrame.dispose();
|
|
155 gc.dispose();
|
|
156 }
|
|
157 }
|
|
158 }
|
|
159 }
|
|
160
|
|
161 private static void startAnimationThreads() {
|
|
162 animateThread = new Thread[image.length];
|
|
163 for (int ii = 0; ii < image.length; ii++) {
|
|
164 animateThread[ii] = new class(ii) Thread {
|
|
165 int imageDataIndex = 0;
|
|
166 int id = 0;
|
|
167 this(int _id) {
|
|
168 id = _id;
|
|
169 name = "Animation "~to!(char[])(ii);
|
|
170 isDaemon = true;
|
|
171 super(&run);
|
|
172 }
|
|
173 void run() {
|
|
174 try {
|
|
175 int repeatCount = loader[id].repeatCount;
|
|
176 while (loader[id].repeatCount == 0 || repeatCount > 0) {
|
|
177 imageDataIndex = (imageDataIndex + 1) % imageDataArray[id].length;
|
|
178 if (!display.isDisposed()) {
|
|
179 display.asyncExec(new class Runnable {
|
|
180 public void run() {
|
|
181 if (!item[id].isDisposed())
|
|
182 item[id].setImage(image[id][imageDataIndex]);
|
|
183 }
|
|
184 });
|
|
185 }
|
|
186
|
|
187 /* Sleep for the specified delay time (adding commonly-used slow-down fudge factors). */
|
|
188 try {
|
|
189 int ms = imageDataArray[id][imageDataIndex].delayTime * 10;
|
|
190 if (ms < 20) ms += 30;
|
|
191 if (ms < 30) ms += 10;
|
|
192 Thread.sleep(0.001*ms);
|
|
193 } catch (ThreadException e) {
|
|
194 }
|
|
195
|
|
196 /* If we have just drawn the last image, decrement the repeat count and start again. */
|
|
197 if (imageDataIndex == imageDataArray[id].length - 1) repeatCount--;
|
|
198 }
|
|
199 } catch (DWTException ex) {
|
|
200 Stdout.print("There was an error animating the GIF").newline;
|
|
201 ex.printStackTrace();
|
|
202 }
|
|
203 }
|
|
204 };
|
|
205 animateThread[ii].start();
|
|
206 }
|
|
207 }
|
|
208
|