Skip to content
Snippets Groups Projects
Commit 38fa27b1 authored by Lukas Ladenberger's avatar Lukas Ladenberger
Browse files

added animated gif support

parent 82831a57
Branches
No related tags found
No related merge requests found
...@@ -6,27 +6,31 @@ ...@@ -6,27 +6,31 @@
package de.bmotionstudio.gef.editor.figure; package de.bmotionstudio.gef.editor.figure;
import org.eclipse.draw2d.Graphics; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.draw2d.ImageFigure; import org.eclipse.draw2d.ImageFigure;
import org.eclipse.draw2d.StackLayout; import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.geometry.Rectangle; import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.widgets.Display;
public class BMSImageFigure extends AbstractBMotionFigure { public class BMSImageFigure extends AbstractBMotionFigure {
private ImageFigure imageFigure; private ImageFigure imageFigure;
final ImageLoader loader = new ImageLoader();
private Map<String, List<Image>> images = new HashMap<String, List<Image>>();
private GIFThread currentGIFThread;
public BMSImageFigure() { public BMSImageFigure() {
setLayoutManager(new StackLayout()); setLayoutManager(new StackLayout());
imageFigure = new ImageFigure() { imageFigure = new ImageFigure();
public void paintFigure(Graphics g) {
if (getImage() == null)
return;
Rectangle rectangle = getClientArea();
g.drawImage(getImage(), new Rectangle(getImage().getBounds()),
rectangle);
}
};
add(imageFigure); add(imageFigure);
} }
...@@ -34,10 +38,28 @@ public class BMSImageFigure extends AbstractBMotionFigure { ...@@ -34,10 +38,28 @@ public class BMSImageFigure extends AbstractBMotionFigure {
getParent().setConstraint(imageFigure, rect); getParent().setConstraint(imageFigure, rect);
} }
public void setImage(Image image) { public void setImage(String myPath) {
if (imageFigure.getImage() != null)
imageFigure.getImage().dispose(); if (currentGIFThread != null)
imageFigure.setImage(image); currentGIFThread.interrupt();
loader.load(myPath);
List<Image> imgList = images.get(myPath);
if (imgList == null) {
imgList = new ArrayList<Image>();
for (ImageData imageData : loader.data)
imgList.add(new Image(Display.getDefault(), imageData));
images.put(myPath, imgList);
}
if (loader.data.length > 1) { // GIF file
currentGIFThread = new GIFThread(this.imageFigure, myPath, imgList);
currentGIFThread.start();
} else { // Non GIF file
imageFigure.setImage(imgList.get(0));
}
} }
/* /*
...@@ -47,8 +69,68 @@ public class BMSImageFigure extends AbstractBMotionFigure { ...@@ -47,8 +69,68 @@ public class BMSImageFigure extends AbstractBMotionFigure {
*/ */
@Override @Override
public void deactivateFigure() { public void deactivateFigure() {
if (currentGIFThread != null)
currentGIFThread.interrupt();
if (imageFigure.getImage() != null) if (imageFigure.getImage() != null)
imageFigure.getImage().dispose(); imageFigure.getImage().dispose();
for (List<Image> l : images.values())
for (Image img : l)
img.dispose();
}
class GIFThread extends Thread {
ImageFigure imgFigure;
int imageNumber;
final ImageLoader loader = new ImageLoader();
boolean stopped = false;
List<Image> imgList;
public GIFThread(ImageFigure imgFigure, String imgPath,
List<Image> imgList) {
this.imgFigure = imgFigure;
this.imgList = imgList;
loader.load(imgPath);
}
@Override
public void run() {
stopped = false;
while (!stopped) {
int delayTime = loader.data[imageNumber].delayTime;
try {
Thread.sleep(delayTime * 10);
} catch (InterruptedException e) {
// e.printStackTrace();
}
Display.getDefault().asyncExec(new Runnable() {
public void run() {
// Increase the variable holding the frame
// number
imageNumber = imageNumber == loader.data.length - 1 ? 0
: imageNumber + 1;
Image image = imgList.get(imageNumber);
if (image != null && !image.isDisposed() && !stopped) {
imgFigure.setImage(image);
}
}
});
}
}
@Override
public void interrupt() {
stopped = true;
super.interrupt();
}
} }
} }
...@@ -7,14 +7,10 @@ ...@@ -7,14 +7,10 @@
package de.bmotionstudio.gef.editor.part; package de.bmotionstudio.gef.editor.part;
import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeEvent;
import java.io.File;
import java.util.HashMap;
import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFile;
import org.eclipse.draw2d.IFigure; import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPolicy; import org.eclipse.gef.EditPolicy;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import de.bmotionstudio.gef.editor.AttributeConstants; import de.bmotionstudio.gef.editor.AttributeConstants;
import de.bmotionstudio.gef.editor.editpolicy.AppDeletePolicy; import de.bmotionstudio.gef.editor.editpolicy.AppDeletePolicy;
...@@ -28,8 +24,6 @@ import de.bmotionstudio.gef.editor.model.BControl; ...@@ -28,8 +24,6 @@ import de.bmotionstudio.gef.editor.model.BControl;
public class BImagePart extends AppAbstractEditPart { public class BImagePart extends AppAbstractEditPart {
private HashMap<String, File> fileMap = new HashMap<String, File>();
@Override @Override
public void refreshEditFigure(IFigure figure, BControl model, public void refreshEditFigure(IFigure figure, BControl model,
PropertyChangeEvent evt) { PropertyChangeEvent evt) {
...@@ -44,17 +38,7 @@ public class BImagePart extends AppAbstractEditPart { ...@@ -44,17 +38,7 @@ public class BImagePart extends AppAbstractEditPart {
IFile pFile = model.getVisualization().getProjectFile(); IFile pFile = model.getVisualization().getProjectFile();
String myPath = (pFile.getProject().getLocation() String myPath = (pFile.getProject().getLocation()
+ "/images/" + imgPath).replace("file:", ""); + "/images/" + imgPath).replace("file:", "");
File file = null; ((BMSImageFigure) figure).setImage(myPath);
if (fileMap.containsKey(myPath)) {
file = fileMap.get(myPath);
} else {
file = new File(myPath);
fileMap.put(myPath, file);
}
if (file.exists()) {
((BMSImageFigure) figure).setImage(new Image(Display
.getDefault(), myPath));
}
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment