-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathSnippet288.d
More file actions
executable file
·272 lines (258 loc) · 9.34 KB
/
Snippet288.d
File metadata and controls
executable file
·272 lines (258 loc) · 9.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/env dub
/+
dub.sdl:
name "snippet288"
dependency "dwt" path="../../../../../../"
lflags "/subsystem:console:4" platform="x86_omf"
libs \
"atk-1.0" \
"cairo" \
"dl" \
"fontconfig" \
"gdk-x11-2.0" \
"gdk_pixbuf-2.0" \
"glib-2.0" \
"gmodule-2.0" \
"gnomeui-2" \
"gnomevfs-2" \
"gobject-2.0" \
"gthread-2.0" \
"gtk-x11-2.0" \
"pango-1.0" \
"pangocairo-1.0" \
"X11" \
"Xcomposite" \
"Xcursor" \
"Xdamage" \
"Xext" \
"Xfixes" \
"Xi" \
"Xinerama" \
"Xrandr" \
"Xrender" \
"Xtst" \
platform="linux"
+/
/*******************************************************************************
* Copyright (c) 2008 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
module org.eclipse.swt.snippets.Snippet288;
/*
* Create a ToolBar containing animated GIFs
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.FileDialog;
import java.lang.all;
version(Tango){
import tango.io.FilePath;
import tango.io.model.IFile;
//import tango.core.Thread;
import tango.io.Stdout;
import tango.util.Convert;
import tango.core.Exception;
} else { // Phobos
import std.path;
import std.stdio;
import std.conv;
import core.exception;
import core.thread : ThreadException;
struct FileConst {
static const PathSeparatorChar = dirSeparator;
}
}
mixin(gshared!("
Display display;
Shell shell;
GC shellGC;
Color shellBackground;
ImageLoader[] loader;
ImageData[][] imageDataArray;
Thread[] animateThread;
Image[][] image;
ToolItem[] item;
"));
const bool useGIFBackground = false;
void main () {
display = new Display();
Shell shell = new Shell (display);
shellBackground = shell.getBackground();
FileDialog dialog = new FileDialog(shell, SWT.OPEN | SWT.MULTI);
dialog.setText("Select Multiple Animated GIFs");
dialog.setFilterExtensions(["*.gif"]);
String filename = dialog.open();
String[] filenames = dialog.getFileNames();
int numToolBarItems = cast(int)filenames.length;
if (numToolBarItems > 0) {
version(Tango){
try {
loadAllImages((new FilePath(filename)).parent, filenames);
} catch (SWTException e) {
Stdout.print("There was an error loading an image.").newline;
e.printStackTrace();
}
} else { // Phobos
try {
loadAllImages(filename.dirName, filenames);
} catch (SWTException e) {
writeln("There was an error loading an image.");
e.printStackTrace();
}
}
ToolBar toolBar = new ToolBar (shell, SWT.FLAT | SWT.BORDER | SWT.WRAP);
item = new ToolItem[numToolBarItems];
for (int i = 0; i < numToolBarItems; i++) {
item[i] = new ToolItem (toolBar, SWT.PUSH);
item[i].setImage(image[i][0]);
}
toolBar.pack ();
shell.open ();
startAnimationThreads();
while (!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
for (int i = 0; i < numToolBarItems; i++) {
for (int j = 0; j < image[i].length; j++) {
image[i][j].dispose();
}
}
display.dispose ();
}
Thread.joinAll();
}
void loadAllImages(String directory, String[] filenames) {
int numItems = cast(int)filenames.length;
loader.length = numItems;
imageDataArray.length = numItems;
image.length = numItems;
for (int i = 0; i < numItems; i++) {
loader[i] = new ImageLoader();
int fullWidth = loader[i].logicalScreenWidth;
int fullHeight = loader[i].logicalScreenHeight;
imageDataArray[i] = loader[i].load(directory ~ FileConst.PathSeparatorChar ~ filenames[i]);
int numFramesOfAnimation = cast(int)imageDataArray[i].length;
image[i] = new Image[numFramesOfAnimation];
for (int j = 0; j < numFramesOfAnimation; j++) {
if (j == 0) {
//for the first frame of animation, just draw the first frame
image[i][j] = new Image(display, imageDataArray[i][j]);
fullWidth = imageDataArray[i][j].width;
fullHeight = imageDataArray[i][j].height;
}
else {
//after the first frame of animation, draw the background or previous frame first, then the new image data
image[i][j] = new Image(display, fullWidth, fullHeight);
GC gc = new GC(image[i][j]);
gc.setBackground(shellBackground);
gc.fillRectangle(0, 0, fullWidth, fullHeight);
ImageData imageData = imageDataArray[i][j];
switch (imageData.disposalMethod) {
case SWT.DM_FILL_BACKGROUND:
/* Fill with the background color before drawing. */
Color bgColor = null;
if (useGIFBackground && loader[i].backgroundPixel != -1) {
bgColor = new Color(display, imageData.palette.getRGB(loader[i].backgroundPixel));
}
gc.setBackground(bgColor !is null ? bgColor : shellBackground);
gc.fillRectangle(imageData.x, imageData.y, imageData.width, imageData.height);
if (bgColor !is null) bgColor.dispose();
break;
default:
/* Restore the previous image before drawing. */
gc.drawImage(
image[i][j-1],
0,
0,
fullWidth,
fullHeight,
0,
0,
fullWidth,
fullHeight);
break;
}
Image newFrame = new Image(display, imageData);
gc.drawImage(newFrame,
0,
0,
imageData.width,
imageData.height,
imageData.x,
imageData.y,
imageData.width,
imageData.height);
newFrame.dispose();
gc.dispose();
}
}
}
}
void startAnimationThreads() {
animateThread = new Thread[image.length];
for (int ii = 0; ii < image.length; ii++) {
animateThread[ii] = new class(ii) Thread {
int imageDataIndex = 0;
int id = 0;
this(int _id) {
id = _id;
//name = "Animation "~to!(char[])(ii);
//isDaemon = true;
super(&run);
}
override
void run() {
try {
int repeatCount = loader[id].repeatCount;
while (loader[id].repeatCount == 0 || repeatCount > 0) {
imageDataIndex = (imageDataIndex + 1) % cast(int)imageDataArray[id].length;
if (!display.isDisposed()) {
display.asyncExec(new class Runnable {
public void run() {
if (!item[id].isDisposed())
item[id].setImage(image[id][imageDataIndex]);
}
});
}
/* Sleep for the specified delay time (adding commonly-used slow-down fudge factors). */
try {
int ms = imageDataArray[id][imageDataIndex].delayTime * 10;
if (ms < 20) ms += 30;
if (ms < 30) ms += 10;
Thread.sleep(1 * ms);
} catch (ThreadException e) {
}
/* If we have just drawn the last image, decrement the repeat count and start again. */
if (imageDataIndex == imageDataArray[id].length - 1) repeatCount--;
}
} catch (SWTException ex) {
version(Tango){
Stdout.print("There was an error animating the GIF").newline;
} else { // Phobos
writeln("There was an error animating the GIF");
}
ex.printStackTrace();
}
}
};
animateThread[ii].start();
}
}