ImagePanel+SWFPreviewPanel fixes, removed deprecated toImage functions

This commit is contained in:
Honfika
2014-03-02 23:30:39 +01:00
parent e3135e97a8
commit 4d657fddea
19 changed files with 101 additions and 137 deletions
@@ -84,6 +84,7 @@ import com.jpexs.decompiler.flash.tags.base.CharacterTag;
import com.jpexs.decompiler.flash.tags.base.Container;
import com.jpexs.decompiler.flash.tags.base.ContainerItem;
import com.jpexs.decompiler.flash.tags.base.DrawableTag;
import com.jpexs.decompiler.flash.tags.base.FontTag;
import com.jpexs.decompiler.flash.tags.base.ImageTag;
import com.jpexs.decompiler.flash.tags.base.PlaceObjectTypeTag;
import com.jpexs.decompiler.flash.tags.base.RemoveTag;
@@ -2471,9 +2472,12 @@ public final class SWF implements TreeItem {
gr.setColor(new Color(0, 0, 0, 0f));
gr.fillRect(0, 0, img.getWidth(), image.getHeight());
drawable.toImage(dframe, layer.ratio, allTags, characters, visited, img, m, clrTrans);
} else {
} else if (drawable instanceof FontTag) {
// only DefineFont tags
img = drawable.toImage(dframe, layer.ratio, allTags, characters, visited, transformation, clrTrans);
FontTag fontTag = (FontTag) drawable;
img = fontTag.toImage(dframe, layer.ratio, allTags, characters, visited, transformation, clrTrans);
} else {
throw new Error("Unsupported drawable.");
}
if (layer.filters != null) {
for (FILTER filter : layer.filters) {
@@ -23,6 +23,8 @@ import com.jpexs.decompiler.flash.gui.player.FlashDisplay;
import com.jpexs.decompiler.flash.tags.base.BoundedTag;
import com.jpexs.decompiler.flash.tags.base.CharacterTag;
import com.jpexs.decompiler.flash.tags.base.DrawableTag;
import com.jpexs.decompiler.flash.tags.base.FontTag;
import com.jpexs.decompiler.flash.tags.base.MorphShapeTag;
import com.jpexs.decompiler.flash.types.ColorTransform;
import com.jpexs.decompiler.flash.types.RECT;
import com.jpexs.helpers.SerializableImage;
@@ -47,21 +49,24 @@ import javax.swing.JPanel;
public final class ImagePanel extends JPanel implements ActionListener, FlashDisplay {
static final String ACTION_SELECT_BKCOLOR = "SELECTCOLOR";
static final int frameRate = 25;
// play morph shape in 2 second(s)
// this settings should be synchronized with frameCount and frameRate
// settings in Mainpanel.createAndShowTempSwf
static final int morphShapeAnimationLength = 2;
public JLabel label = new JLabel();
public DrawableTag drawable;
private Timer timer;
private int percent;
private int frame = -1;
private SWF swf;
private HashMap<Integer, CharacterTag> characters;
private int frameRate;
private boolean loaded;
@Override
public void setBackground(Color bg) {
if (label != null) {
label.setBackground(bg);//bg);
label.setBackground(bg);
}
super.setBackground(bg);
}
@@ -125,7 +130,7 @@ public final class ImagePanel extends JPanel implements ActionListener, FlashDis
label.setIcon(null);
return;
}
percent = 0;
frame = 0;
if (drawable.getNumFrames() == 1) {
Matrix mat = new Matrix();
mat.translateX = swf.displayRect.Xmin;
@@ -150,8 +155,10 @@ public final class ImagePanel extends JPanel implements ActionListener, FlashDis
m.translate(-rect.Xmin, -rect.Ymin);
drawable.toImage(0, 0, swf.tags, characters, new Stack<Integer>(), image, m, new ColorTransform());
img = image;
} else {
img = drawable.toImage(0, 0, swf.tags, characters, new Stack<Integer>(), Matrix.getScaleInstance(1 / SWF.unitDivisor), new ColorTransform());
} else if (drawable instanceof FontTag) {
// only DefineFont tags
FontTag fontTag = (FontTag) drawable;
img = fontTag.toImage(0, 0, swf.tags, characters, new Stack<Integer>(), Matrix.getScaleInstance(1 / SWF.unitDivisor), new ColorTransform());
}
SWF.putToCache(key, img);
}
@@ -176,11 +183,7 @@ public final class ImagePanel extends JPanel implements ActionListener, FlashDis
@Override
public int getCurrentFrame() {
if (drawable == null) {
return 0;
}
int ret = percent * (drawable.getNumFrames() - 1) / 100;
return ret;
return frame;
}
@Override
@@ -199,44 +202,62 @@ public final class ImagePanel extends JPanel implements ActionListener, FlashDis
}
}
private void nextFrame() {
int newframe = frame == drawable.getNumFrames() - 1 ? 0 : frame + 1;
if (drawable instanceof MorphShapeTag) {
newframe = frame + drawable.getNumFrames() / frameRate / morphShapeAnimationLength;
if (newframe > drawable.getNumFrames()) {
newframe = 0;
}
}
if (newframe != frame) {
frame = newframe;
drawFrame();
}
}
private void drawFrame() {
if (drawable == null) {
return;
}
int nframe = percent * (drawable.getNumFrames() - 1) / 100;
if (nframe != frame) {
Matrix mat = new Matrix();
mat.translateX = swf.displayRect.Xmin;
mat.translateY = swf.displayRect.Ymin;
String key = "drawable_" + nframe + "_" + drawable.hashCode();
SerializableImage img = SWF.getFromCache(key);
if (img == null) {
if (drawable instanceof BoundedTag) {
BoundedTag bounded = (BoundedTag) drawable;
RECT rect = bounded.getRect(characters, new Stack<Integer>());
SerializableImage image = new SerializableImage((int) (rect.getWidth() / SWF.unitDivisor) + 1,
(int) (rect.getHeight() / SWF.unitDivisor) + 1, SerializableImage.TYPE_INT_ARGB);
//Make all pixels transparent
Graphics2D g = (Graphics2D) image.getGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setComposite(AlphaComposite.Src);
g.setColor(new Color(0, 0, 0, 0f));
g.fillRect(0, 0, image.getWidth(), image.getHeight());
Matrix m = new Matrix();
m.translate(-rect.Xmin, -rect.Ymin);
drawable.toImage(nframe, nframe, swf.tags, characters, new Stack<Integer>(), image, m, new ColorTransform());
img = image;
} else {
img = drawable.toImage(nframe, nframe, swf.tags, characters, new Stack<Integer>(), Matrix.getScaleInstance(1 / SWF.unitDivisor), new ColorTransform());
}
SWF.putToCache(key, img);
Matrix mat = new Matrix();
mat.translateX = swf.displayRect.Xmin;
mat.translateY = swf.displayRect.Ymin;
String key = "drawable_" + frame + "_" + drawable.hashCode();
SerializableImage img = SWF.getFromCache(key);
if (img == null) {
if (drawable instanceof BoundedTag) {
BoundedTag bounded = (BoundedTag) drawable;
RECT rect = bounded.getRect(characters, new Stack<Integer>());
SerializableImage image = new SerializableImage((int) (rect.getWidth() / SWF.unitDivisor) + 1,
(int) (rect.getHeight() / SWF.unitDivisor) + 1, SerializableImage.TYPE_INT_ARGB);
//Make all pixels transparent
Graphics2D g = (Graphics2D) image.getGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setComposite(AlphaComposite.Src);
g.setColor(new Color(0, 0, 0, 0f));
g.fillRect(0, 0, image.getWidth(), image.getHeight());
Matrix m = new Matrix();
m.translate(-rect.Xmin, -rect.Ymin);
drawable.toImage(frame, frame, swf.tags, characters, new Stack<Integer>(), image, m, new ColorTransform());
img = image;
} else if (drawable instanceof FontTag) {
// only DefineFont tags
FontTag fontTag = (FontTag) drawable;
img = fontTag.toImage(frame, frame, swf.tags, characters, new Stack<Integer>(), Matrix.getScaleInstance(1 / SWF.unitDivisor), new ColorTransform());
}
ImageIcon icon = new ImageIcon(img.getBufferedImage());
label.setIcon(icon);
frame = nframe;
SWF.putToCache(key, img);
}
ImageIcon icon = new ImageIcon(img.getBufferedImage());
label.setIcon(icon);
}
public void stop() {
if (timer != null) {
timer.cancel();
timer = null;
}
}
@@ -249,21 +270,15 @@ public final class ImagePanel extends JPanel implements ActionListener, FlashDis
timer.schedule(new TimerTask() {
@Override
public void run() {
drawFrame();
if (percent == 100) {
percent = 0;
} else {
percent++;
}
nextFrame();
}
}, 0, 20);
}, 0, 1000 / frameRate);
}
}
@Override
public void rewind() {
percent = 0;
frame = 0;
drawFrame();
}
@@ -277,11 +292,7 @@ public final class ImagePanel extends JPanel implements ActionListener, FlashDis
@Override
public void gotoFrame(int frame) {
if (drawable == null) {
percent = 0;
} else {
percent = frame * 100 / drawable.getNumFrames();
}
this.frame = frame;
drawFrame();
}
@@ -290,7 +301,10 @@ public final class ImagePanel extends JPanel implements ActionListener, FlashDis
if (drawable == null) {
return 1;
}
return drawable.getNumFrames() / 2;
if (drawable instanceof MorphShapeTag) {
return drawable.getNumFrames() / morphShapeAnimationLength;
}
return frameRate;
}
@Override
@@ -893,6 +893,7 @@ public final class MainPanel extends JPanel implements ActionListener, TreeSelec
public void load(SWFList newSwfs, boolean first) {
swfPreviewPanel.stop();
imagePanel.stop();
swfs.add(newSwfs);
@@ -2333,6 +2334,7 @@ public final class MainPanel extends JPanel implements ActionListener, TreeSelec
flashPanel.specialPlayback = false;
}
swfPreviewPanel.stop();
imagePanel.stop();
stopFlashPlayer();
if (tagObj instanceof ScriptPack) {
final ScriptPack scriptLeaf = (ScriptPack) tagObj;
@@ -2542,6 +2544,7 @@ public final class MainPanel extends JPanel implements ActionListener, TreeSelec
}
if ((tagObj instanceof DefineMorphShapeTag) || (tagObj instanceof DefineMorphShape2Tag)) {
// this setting should be synchronized with ImagePanel.morphShapeAnimationLength
frameCount = 100;
frameRate = 50;
}
@@ -98,18 +98,24 @@ public class SWFPreviewPanel extends JPanel implements FlashDisplay {
timer.schedule(new TimerTask() {
@Override
public void run() {
int newframe = (frame == swf.frameCount ? 1 : frame + 1);
if (frameImages != null && frameImages.size() >= newframe) {
frame = newframe;
drawFrame();
pan.setBackground(View.swfBackgroundColor);
}
nextFrame();
}
}, 0, 1000 / swf.frameRate);
}
private void nextFrame() {
int newframe = frame == swf.frameCount - 1 ? 0 : frame + 1;
if (newframe != frame) {
if (frameImages != null && frameImages.size() > newframe) {
frame = newframe;
drawFrame();
pan.setBackground(View.swfBackgroundColor);
}
}
}
private void drawFrame() {
pan.setImage(frameImages.get(frame - 1));
pan.setImage(frameImages.get(frame));
}
@Override
@@ -135,7 +141,7 @@ public class SWFPreviewPanel extends JPanel implements FlashDisplay {
@Override
public void rewind() {
frame = 1;
frame = 0;
drawFrame();
}
@@ -84,7 +84,7 @@ public class PlayerControls extends JPanel implements ActionListener {
progress.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
int frame = 1 + (int) Math.floor(e.getX() * display.getTotalFrames() / (double) progress.getWidth());
int frame = (int) Math.floor(e.getX() * display.getTotalFrames() / (double) progress.getWidth());
boolean p = paused;
display.gotoFrame(frame);
if (!p) {
@@ -134,14 +134,14 @@ public class PlayerControls extends JPanel implements ActionListener {
if (totalFrames == 0) {
progress.setIndeterminate(true);
} else {
progress.setMaximum(totalFrames);
progress.setMinimum(1);
progress.setMaximum(totalFrames - 1);
progress.setMinimum(0);
progress.setValue(currentFrame);
progress.setIndeterminate(false);
}
if (frameRate != 0) {
timeLabel.setText(formatMs((currentFrame * 1000) / frameRate));
totalTimeLabel.setText(formatMs((totalFrames * 1000) / frameRate));
totalTimeLabel.setText(formatMs(((totalFrames - 1) * 1000) / frameRate));
}
if (totalFrames <= 1 && isVisible()) {
setVisible(false);
@@ -252,11 +252,6 @@ public class DefineButton2Tag extends CharacterTag implements Container, Bounded
return trackAsMenu;
}
@Override
public SerializableImage toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, Matrix transformation, ColorTransform colorTransform) {
throw new Error("this overload of toImage call is not supported on BoundedTag");
}
@Override
public void toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, SerializableImage image, Matrix transformation, ColorTransform colorTransform) {
if (visited.contains(buttonId)) {
@@ -273,11 +273,6 @@ public class DefineButtonTag extends CharacterTag implements ASMSource, BoundedT
return false;
}
@Override
public SerializableImage toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, Matrix transformation, ColorTransform colorTransform) {
throw new Error("this overload of toImage call is not supported on BoundedTag");
}
@Override
public void toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, SerializableImage image, Matrix transformation, ColorTransform colorTransform) {
if (visited.contains(buttonId)) {
@@ -754,11 +754,6 @@ public class DefineEditTextTag extends TextTag {
return needed;
}
@Override
public SerializableImage toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, Matrix transformation, ColorTransform colorTransform) {
throw new Error("this overload of toImage call is not supported on BoundedTag");
}
@Override
public void toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, SerializableImage image, Matrix transformation, ColorTransform colorTransform) {
if (hasText) {
@@ -314,11 +314,6 @@ public class DefineMorphShape2Tag extends CharacterTag implements BoundedTag, Mo
return shape;
}
@Override
public SerializableImage toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, Matrix transformation, ColorTransform colorTransform) {
throw new Error("this overload of toImage call is not supported on BoundedTag");
}
@Override
public void toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, SerializableImage image, Matrix transformation, ColorTransform colorTransform) {
SHAPEWITHSTYLE shape = getShapeAtRatio(ratio);
@@ -297,11 +297,6 @@ public class DefineMorphShapeTag extends CharacterTag implements BoundedTag, Mor
return shape;
}
@Override
public SerializableImage toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, Matrix transformation, ColorTransform colorTransform) {
throw new Error("this overload of toImage call is not supported on BoundedTag");
}
@Override
public void toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, SerializableImage image, Matrix transformation, ColorTransform colorTransform) {
SHAPEWITHSTYLE shape = getShapeAtRatio(ratio);
@@ -72,11 +72,6 @@ public class DefineShape2Tag extends CharacterTag implements ShapeTag {
return exporter.getSVG();
}
@Override
public SerializableImage toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, Matrix transformation, ColorTransform colorTransform) {
throw new Error("this overload of toImage call is not supported on BoundedTag");
}
@Override
public void toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, SerializableImage image, Matrix transformation, ColorTransform colorTransform) {
BitmapExporter.exportTo(swf, getShapes(), null, image, transformation, colorTransform);
@@ -77,11 +77,6 @@ public class DefineShape3Tag extends CharacterTag implements ShapeTag {
return exporter.getSVG();
}
@Override
public SerializableImage toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, Matrix transformation, ColorTransform colorTransform) {
throw new Error("this overload of toImage call is not supported on BoundedTag");
}
@Override
public void toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, SerializableImage image, Matrix transformation, ColorTransform colorTransform) {
BitmapExporter.exportTo(swf, getShapes(), null, image, transformation, colorTransform);
@@ -80,11 +80,6 @@ public class DefineShape4Tag extends CharacterTag implements ShapeTag {
return exporter.getSVG();
}
@Override
public SerializableImage toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, Matrix transformation, ColorTransform colorTransform) {
throw new Error("this overload of toImage call is not supported on BoundedTag");
}
@Override
public void toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, SerializableImage image, Matrix transformation, ColorTransform colorTransform) {
BitmapExporter.exportTo(swf, getShapes(), null, image, transformation, colorTransform);
@@ -105,11 +105,6 @@ public class DefineShapeTag extends CharacterTag implements ShapeTag {
return exporter.getSVG();
}
@Override
public SerializableImage toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, Matrix transformation, ColorTransform colorTransform) {
throw new Error("this overload of toImage call is not supported on BoundedTag");
}
@Override
public void toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, SerializableImage image, Matrix transformation, ColorTransform colorTransform) {
BitmapExporter.exportTo(swf, getShapes(), null, image, transformation, colorTransform);
@@ -276,11 +276,6 @@ public class DefineSpriteTag extends CharacterTag implements Container, BoundedT
return ret;
}
@Override
public SerializableImage toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, Matrix transformation, ColorTransform colorTransform) {
throw new Error("this overload of toImage call is not supported on BoundedTag");
}
@Override
public void toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, SerializableImage image, Matrix transformation, ColorTransform colorTransform) {
if (visited.contains(spriteId)) {
@@ -489,11 +489,6 @@ public class DefineText2Tag extends TextTag {
return ret;
}
@Override
public SerializableImage toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, Matrix transformation, ColorTransform colorTransform) {
throw new Error("this overload of toImage call is not supported on BoundedTag");
}
@Override
public void toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, SerializableImage image, Matrix transformation, ColorTransform colorTransform) {
staticTextToImage(swf, characters, textRecords, 2, image, getTextMatrix(), transformation, colorTransform);
@@ -505,11 +505,6 @@ public class DefineTextTag extends TextTag {
return ret;
}
@Override
public SerializableImage toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, Matrix transformation, ColorTransform colorTransform) {
throw new Error("this overload of toImage call is not supported on BoundedTag");
}
@Override
public void toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, SerializableImage image, Matrix transformation, ColorTransform colorTransform) {
staticTextToImage(swf, characters, textRecords, 1, image, getTextMatrix(), transformation, colorTransform);
@@ -31,8 +31,6 @@ import java.util.Stack;
*/
public interface DrawableTag {
public SerializableImage toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, Matrix transformation, ColorTransform colorTransform);
public void toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, SerializableImage image, Matrix transformation, ColorTransform colorTransform);
public Point getImagePos(int frame, Map<Integer, CharacterTag> characters, Stack<Integer> visited);
@@ -246,14 +246,13 @@ public abstract class FontTag extends CharacterTag implements AloneTag, Drawable
return defaultFontName;
}
@Override
public SerializableImage toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, Matrix transformation, ColorTransform colorTransform) {
return SHAPERECORD.shapeListToImage(swf, getGlyphShapeTable(), 500, 500, Color.black, colorTransform);
}
@Override
public void toImage(int frame, int ratio, List<Tag> tags, Map<Integer, CharacterTag> characters, Stack<Integer> visited, SerializableImage image, Matrix transformation, ColorTransform colorTransform) {
throw new Error("this overload of toImage call is not supported on FontTag");
}
@Override