/*
* Copyright (C) 2010-2024 JPEXS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package com.jpexs.decompiler.flash.gui;
import com.jpexs.decompiler.flash.SWF;
import com.jpexs.decompiler.flash.SWFInputStream;
import com.jpexs.decompiler.flash.action.Action;
import com.jpexs.decompiler.flash.action.LocalDataArea;
import com.jpexs.decompiler.flash.action.Stage;
import com.jpexs.decompiler.flash.configuration.Configuration;
import com.jpexs.decompiler.flash.ecma.Undefined;
import com.jpexs.decompiler.flash.exporters.commonshape.ExportRectangle;
import com.jpexs.decompiler.flash.exporters.commonshape.Matrix;
import com.jpexs.decompiler.flash.gui.player.MediaDisplay;
import com.jpexs.decompiler.flash.gui.player.MediaDisplayListener;
import com.jpexs.decompiler.flash.gui.player.Zoom;
import com.jpexs.decompiler.flash.math.BezierUtils;
import com.jpexs.decompiler.flash.tags.DefineButton2Tag;
import com.jpexs.decompiler.flash.tags.DefineButtonSoundTag;
import com.jpexs.decompiler.flash.tags.DefineButtonTag;
import com.jpexs.decompiler.flash.tags.DoActionTag;
import com.jpexs.decompiler.flash.tags.base.BoundedTag;
import com.jpexs.decompiler.flash.tags.base.ButtonTag;
import com.jpexs.decompiler.flash.tags.base.CharacterTag;
import com.jpexs.decompiler.flash.tags.base.DisplayObjectCacheKey;
import com.jpexs.decompiler.flash.tags.base.DrawableTag;
import com.jpexs.decompiler.flash.tags.base.PlaceObjectTypeTag;
import com.jpexs.decompiler.flash.tags.base.RenderContext;
import com.jpexs.decompiler.flash.tags.base.SoundTag;
import com.jpexs.decompiler.flash.tags.base.TextTag;
import com.jpexs.decompiler.flash.timeline.DepthState;
import com.jpexs.decompiler.flash.timeline.Frame;
import com.jpexs.decompiler.flash.timeline.Timeline;
import com.jpexs.decompiler.flash.timeline.Timelined;
import com.jpexs.decompiler.flash.types.BUTTONCONDACTION;
import com.jpexs.decompiler.flash.types.ConstantColorColorTransform;
import com.jpexs.decompiler.flash.types.MATRIX;
import com.jpexs.decompiler.flash.types.RECT;
import com.jpexs.decompiler.flash.types.RGB;
import com.jpexs.decompiler.flash.types.SOUNDINFO;
import com.jpexs.decompiler.flash.types.filters.BlendComposite;
import com.jpexs.helpers.ByteArrayRange;
import com.jpexs.helpers.Cache;
import com.jpexs.helpers.Reference;
import com.jpexs.helpers.SerializableImage;
import com.jpexs.helpers.Stopwatch;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.VolatileImage;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.MouseInputAdapter;
import org.pushingpixels.substance.api.ColorSchemeAssociationKind;
import org.pushingpixels.substance.api.ComponentState;
import org.pushingpixels.substance.api.DecorationAreaType;
import org.pushingpixels.substance.api.SubstanceLookAndFeel;
import org.pushingpixels.substance.api.SubstanceSkin;
/**
* @author JPEXS
*/
public final class ImagePanel extends JPanel implements MediaDisplay {
private static final Logger logger = Logger.getLogger(ImagePanel.class.getName());
private final List listeners = new ArrayList<>();
private Timelined timelined;
private boolean stillFrame = false;
private volatile Timer timer;
private int frame = -1;
private int prevFrame = -1;
private boolean loop;
private LocalDataArea lda;
private boolean zoomAvailable = false;
private SWF swf;
private boolean loaded;
private int mouseButton;
private static final String DEFAULT_DEBUG_LABEL_TEXT = " - ";
private final JLabel debugLabel = new JLabel(DEFAULT_DEBUG_LABEL_TEXT);
private JPanel pointEditPanel;
private JTextField pointXTextField;
private JTextField pointYTextField;
private Point cursorPosition = null;
private MouseEvent lastMouseEvent = null;
private final List soundPlayers = new ArrayList<>();
private final Cache displayObjectCache = Cache.getInstance(false, false, "displayObject", true);
private final IconPanel iconPanel;
private int time = 0;
private int selectedDepth = -1;
private int freeTransformDepth = -1;
private Zoom zoom = new Zoom();
private final Object delayObject = new Object();
private boolean drawReady;
private final int drawWaitLimit = 50; // ms
private TextTag textTag;
private TextTag newTextTag;
private boolean showObjectsUnderCursor;
private int msPerFrame;
private final boolean lowQuality = false;
private Object lock = new Object();
private Point2D registrationPoint = null;
private Point2D registrationPointUpdated = null;
private int mode = Cursor.DEFAULT_CURSOR;
private Rectangle2D bounds;
private Matrix transform;
private AffineTransform transformUpdated;
private final double LQ_FACTOR = 2;
private static final int TOLERANCE_SCALESHEAR = 8;
private static final int TOLERANCE_ROTATE = 30;
private static final int REGISTRATION_TOLERANCE = 8;
private static final double CENTER_POINT_SIZE = 8;
private static final double HANDLES_WIDTH = 5;
private static final int HANDLES_STROKE_WIDTH = 2;
private static final int MODE_ROTATE_NE = -1;
private static final int MODE_ROTATE_SE = -2;
private static final int MODE_ROTATE_NW = -3;
private static final int MODE_ROTATE_SW = -4;
private static final int MODE_SHEAR_S = -5;
private static final int MODE_SHEAR_E = -6;
private static final int MODE_SHEAR_N = -7;
private static final int MODE_SHEAR_W = -8;
private static Cursor moveCursor;
private static Cursor moveRegPointCursor;
private static Cursor resizeNWSECursor;
private static Cursor resizeSWNECursor;
private static Cursor resizeXCursor;
private static Cursor resizeYCursor;
private static Cursor rotateCursor;
private static Cursor selectCursor;
private static Cursor shearXCursor;
private static Cursor shearYCursor;
private static Cursor movePointCursor;
private static Cursor defaultCursor;
private static Cursor addPointCursor;
private Point2D offsetPoint = new Point2D.Double(0, 0);
private ExportRectangle _viewRect = new ExportRectangle(0, 0, 1, 1);
private boolean playing = false;
private boolean autoPlayed = false;
private boolean frozen = false;
private boolean muted = false;
private boolean resample = false;
private boolean mutable = false;
private boolean alwaysDisplay = false;
private RegistrationPointPosition registrationPointPosition = RegistrationPointPosition.CENTER;
private DepthState depthStateUnderCursor = null;
private List placeObjectSelectedListeners = new ArrayList<>();
private Point[] hilightedEdge = null;
private List hilightedPoints = null;
//private DisplayPoint closestPoint = null;
private List pointsUnderCursor = new ArrayList<>();
private List selectedPoints = new ArrayList<>();
private List pathPointsUnderCursor = new ArrayList<>();
private DisplayPoint closestPoint = null;
private List selectedPointsOriginalValues = new ArrayList<>();
private int hilightEdgeColorStep = 10;
private int hilightEdgeColor = 0;
private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("0.00");
private JScrollBar horizontalScrollBar;
private JScrollBar verticalScrollBar;
private boolean updatingScrollBars = false;
private final int SCROLL_SPACE_BEFORE = (int) SWF.unitDivisor * 500;
private List showPoints1 = new ArrayList<>();
private List showPoints2 = new ArrayList<>();
private int displayedFrame = 0;
private JPanel topPanel;
private TagNameResolverInterface tagNameResolver = new DefaultTagNameResolver();
private boolean showAllDepthLevelsInfo = true;
private boolean selectionMode = false;
private boolean transformSelectionMode = false;
public void setSelectionMode(boolean selectionMode) {
this.selectionMode = selectionMode;
}
public void setTransformSelectionMode(boolean transformSelectionMode) {
this.transformSelectionMode = transformSelectionMode;
}
public void setTagNameResolver(TagNameResolverInterface tagNameResolver) {
this.tagNameResolver = tagNameResolver;
}
public void setShowAllDepthLevelsInfo(boolean showAllDepthLevelsInfo) {
this.showAllDepthLevelsInfo = showAllDepthLevelsInfo;
}
public void setTopPanelVisible(boolean visible) {
topPanel.setVisible(visible);
}
public void setShowPoints(List showPoints1, List showPoints2) {
this.showPoints1 = showPoints1;
this.showPoints2 = showPoints2;
}
private static String formatDouble(double value) {
return DECIMAL_FORMAT.format(value);
}
private static double parseDouble(String value) {
try {
return DECIMAL_FORMAT.parse(value).doubleValue();
} catch (ParseException ex) {
throw new NumberFormatException();
}
}
private static Cursor loadCursor(String name, int x, int y) throws IOException {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = ImageIO.read(MainPanel.class.getResource("/com/jpexs/decompiler/flash/gui/graphics/cursors/" + name + ".png"));
return toolkit.createCustomCursor(image, new Point(x, y), name);
}
private SerializableImage imgPlay = null;
private List boundsChangeListeners = new ArrayList<>();
private List pointUpdateListeners = new ArrayList<>();
private List transformChangeListeners = new ArrayList<>();
public void addTransformChangeListener(Runnable listener) {
transformChangeListeners.add(listener);
}
public void removeTransformChangeListener(Runnable listener) {
transformChangeListeners.remove(listener);
}
public void addPointUpdateListener(PointUpdateListener listener) {
pointUpdateListeners.add(listener);
}
public void removePointUpdateListener(PointUpdateListener listener) {
pointUpdateListeners.remove(listener);
}
private void firePointsUpdated(List points) {
for (PointUpdateListener listener : pointUpdateListeners) {
listener.pointsUpdated(points);
}
}
private void fireTransformChanged() {
for (Runnable listener : transformChangeListeners) {
listener.run();
}
}
private void fireStatusChanged(String status) {
for (MediaDisplayListener listener : listeners) {
listener.statusChanged(status);
}
}
private boolean fireEdgeSplit(int position, double splitPoint) {
boolean result = true;
for (PointUpdateListener listener : pointUpdateListeners) {
result = result && listener.edgeSplit(position, splitPoint);
}
return result;
}
private boolean firePointRemoved(int position) {
boolean result = true;
for (PointUpdateListener listener : pointUpdateListeners) {
result = result && listener.pointRemoved(position);
}
return result;
}
private void applyPointsXY() {
try {
int x = (int) Math.round(parseDouble(pointXTextField.getText()) * SWF.unitDivisor);
int y = (int) Math.round(parseDouble(pointYTextField.getText()) * SWF.unitDivisor);
java.awt.Point minSelectedPoint = getMinSelectedPoint();
if (minSelectedPoint == null) {
return;
}
for (int index : selectedPoints) {
DisplayPoint point = hilightedPoints.get(index);
point.x = point.x - minSelectedPoint.x + x;
point.y = point.y - minSelectedPoint.y + y;
}
firePointsUpdated(hilightedPoints);
} catch (NumberFormatException nfe) {
//ignore
}
}
private java.awt.Point getMinSelectedPoint() {
if (selectedPoints.isEmpty()) {
return null;
}
int minX = Integer.MAX_VALUE;
int minY = Integer.MAX_VALUE;
for (int index : selectedPoints) {
DisplayPoint point = hilightedPoints.get(index);
if (point.x < minX) {
minX = point.x;
}
if (point.y < minY) {
minY = point.y;
}
}
return new java.awt.Point(minX, minY);
}
private void calculatePointsXY() {
Point2D minPoint = getMinSelectedPoint();
if (minPoint == null) {
pointXTextField.setText("");
pointYTextField.setText("");
return;
}
pointXTextField.setText(formatDouble(minPoint.getX() / SWF.unitDivisor));
pointYTextField.setText(formatDouble(minPoint.getY() / SWF.unitDivisor));
}
public void setHilightedPoints(List hilightedPoints) {
hilightedEdge = null;
selectedPoints = new ArrayList<>();
calculatePointsXY();
this.hilightedPoints = hilightedPoints;
pointEditPanel.setVisible(hilightedPoints != null);
redraw();
}
public void setHilightedEdge(Point[] hilightedEdge) {
this.hilightedEdge = hilightedEdge;
hilightedPoints = null;
hilightEdgeColor = 255;
pointEditPanel.setVisible(false);
redraw();
}
public void setStatus(String status) {
fireStatusChanged(status);
}
public void setNoStatus() {
fireStatusChanged("");
}
public void addBoundsChangeListener(BoundsChangeListener listener) {
boundsChangeListeners.add(listener);
}
public void addPlaceObjectSelectedListener(ActionListener listener) {
placeObjectSelectedListeners.add(listener);
}
public void removePlaceObjectSelectedListener(ActionListener listener) {
placeObjectSelectedListeners.remove(listener);
}
private void firePlaceObjectSelected() {
ActionEvent e = new ActionEvent(this, 0, "");
for (ActionListener listener : placeObjectSelectedListeners) {
listener.actionPerformed(e);
}
}
public PlaceObjectTypeTag getPlaceTagUnderCursor() {
if (depthStateUnderCursor == null) {
return null;
}
return depthStateUnderCursor.placeObjectTag;
}
public void removeBoundsChangeListener(BoundsChangeListener listener) {
boundsChangeListeners.remove(listener);
}
private void fireBoundsChange(Rectangle2D bounds, Point2D registrationPoint, RegistrationPointPosition registrationPointPosition) {
for (BoundsChangeListener listener : boundsChangeListeners) {
listener.boundsChanged(bounds, registrationPoint, registrationPointPosition);
}
}
private SerializableImage getImagePlay() {
if (imgPlay != null) {
return imgPlay;
}
Color bgColor;
if (Configuration.useRibbonInterface.get()) {
SubstanceSkin skin = SubstanceLookAndFeel.getCurrentSkin();
bgColor = (skin.getColorScheme(DecorationAreaType.HEADER, ColorSchemeAssociationKind.FILL, ComponentState.ENABLED).getBackgroundFillColor());
} else {
bgColor = SystemColor.control;
}
Color fgColor;
if (Configuration.useRibbonInterface.get()) {
SubstanceSkin skin = SubstanceLookAndFeel.getCurrentSkin();
fgColor = (skin.getColorScheme(DecorationAreaType.HEADER, ColorSchemeAssociationKind.FILL, ComponentState.ENABLED).getForegroundColor());
} else {
fgColor = SystemColor.controlText;
}
int size = 200;
imgPlay = new SerializableImage(size, size, BufferedImage.TYPE_INT_ARGB_PRE);
imgPlay.fillTransparent();
Graphics2D g2d = (Graphics2D) imgPlay.getGraphics();
g2d.setStroke(new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
GeneralPath path = new GeneralPath();
path.moveTo(0, 0);
path.lineTo(size, size / 2);
path.lineTo(0, size);
path.closePath();
g2d.setComposite(AlphaComposite.SrcOver);
g2d.setPaint(new Color(fgColor.getRed(), fgColor.getGreen(), fgColor.getBlue(), fgColor.getAlpha() / 2));
g2d.fill(path);
g2d.setPaint(fgColor);
g2d.draw(path);
return imgPlay;
}
static {
try {
moveCursor = loadCursor("move", 0, 0);
moveRegPointCursor = loadCursor("move_regpoint", 0, 0);
resizeNWSECursor = loadCursor("resize_nw_se", 5, 5);
resizeSWNECursor = loadCursor("resize_sw_ne", 5, 5);
resizeXCursor = loadCursor("resize_x", 7, 4);
resizeYCursor = loadCursor("resize_y", 4, 7);
rotateCursor = loadCursor("rotate", 10, 7);
selectCursor = loadCursor("select", 0, 0);
shearXCursor = loadCursor("shear_x", 9, 5);
shearYCursor = loadCursor("shear_y", 5, 9);
movePointCursor = loadCursor("move_point", 0, 0);
defaultCursor = loadCursor("default", 0, 0);
addPointCursor = loadCursor("add_point", 0, 0);
} catch (IOException ex) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
private Matrix getNewToImageMatrix(Matrix newMatrix) {
Matrix m = new Matrix();
double zoomDouble = zoom.fit ? getZoomToFit() : zoom.value;
if (lowQuality) {
zoomDouble /= LQ_FACTOR;
}
double zoom = zoomDouble;
m.translate(-_viewRect.xMin * zoom, -_viewRect.yMin * zoom);
m.scale(zoom);
return Matrix.getScaleInstance(1 / SWF.unitDivisor).concatenate(m).concatenate(newMatrix);
}
public Matrix getNewMatrix() {
return transform;
}
public synchronized void selectDepth(int depth) {
boolean wasFreeTransform = freeTransformDepth != -1;
transformUpdated = null;
registrationPointUpdated = null;
transform = null;
if (depth != selectedDepth) {
this.selectedDepth = depth;
freeTransformDepth = -1;
}
if (selectionMode && depthStateUnderCursor != null) {
calculateFreeOrSelectionTransform();
}
hideMouseSelection();
redraw();
}
public int getSelectedDepth() {
return selectedDepth;
}
public synchronized int getFrame() {
return frame;
}
private void calculateFreeOrSelectionTransform() {
DepthState ds = null;
Timeline timeline = timelined.getTimeline();
if (freeTransformDepth > -1 && timeline.getFrameCount() > frame) {
ds = timeline.getFrame(frame).layers.get(freeTransformDepth);
}
if (freeTransformDepth == -1 && selectionMode && selectedDepth != -1) {
ds = timeline.getFrame(frame).layers.get(selectedDepth);
}
_viewRect = getViewRect();
if (ds != null) {
CharacterTag cht = ds.getCharacter();
if (cht != null) {
if (cht instanceof DrawableTag) {
DrawableTag dt = (DrawableTag) cht;
int drawableFrameCount = dt.getNumFrames();
if (drawableFrameCount == 0) {
drawableFrameCount = 1;
}
transform = new Matrix(ds.matrix);
Rectangle2D transformBounds = getTransformBounds();
registrationPointPosition = RegistrationPointPosition.CENTER;
fireBoundsChange(transformBounds, new Point2D.Double(transformBounds.getCenterX(), transformBounds.getCenterY()), registrationPointPosition);
}
}
}
}
public synchronized void freeTransformDepth(int depth) {
if (selectedDepth != depth) {
selectedDepth = depth;
}
if (depth != freeTransformDepth) {
this.freeTransformDepth = depth;
}
hilightedEdge = null;
hilightedPoints = null;
pointEditPanel.setVisible(false);
registrationPoint = null;
calculateFreeOrSelectionTransform();
hideMouseSelection();
redraw();
iconPanel.requestFocusInWindow();
}
private void centerImage() {
Timelined tim = timelined;
if (tim == null) {
return;
}
RECT rect = tim.getRect();
double zoomDouble = zoom.fit ? getZoomToFit() : zoom.value;
double w = rect.getWidth() / SWF.unitDivisor * zoomDouble;
double h = rect.getHeight() / SWF.unitDivisor * zoomDouble;
double dw = rect.Xmin * zoomDouble / SWF.unitDivisor;
double dh = rect.Ymin * zoomDouble / SWF.unitDivisor;
offsetPoint.setLocation(
iconPanel.getWidth() / 2 - w / 2 - dw,
iconPanel.getHeight() / 2 - h / 2 - dh
);
/*Timer tim = new Timer();
tim.schedule(new TimerTask() {
@Override
public void run() {
updateScrollBars();
redraw();
}
}, 100);*/
}
public void fireMediaDisplayStateChanged() {
for (MediaDisplayListener l : listeners) {
l.mediaDisplayStateChanged(this);
}
}
@Override
public void addEventListener(MediaDisplayListener listener) {
listeners.add(listener);
}
@Override
public void removeEventListener(MediaDisplayListener listener) {
listeners.remove(listener);
}
@Override
public Color getBackgroundColor() {
if (swf != null && swf.getBackgroundColor() != null) {
return swf.getBackgroundColor().backgroundColor.toColor();
}
return Color.white;
}
@Override
public void setDisplayed(boolean value) {
autoPlayed = value;
Configuration.autoPlayPreviews.set(value);
}
@Override
public void setFrozen(boolean value) {
this.frozen = value;
}
@Override
public boolean isDisplayed() {
return autoPlayed;
}
@Override
public boolean alwaysDisplay() {
return alwaysDisplay;
}
@Override
public void setMuted(boolean value) {
this.muted = value;
if (value) {
stopAllSounds();
} else {
prevFrame = -1; //initiate refreshing frame to play sounds again
}
}
@Override
public void setResample(boolean resample) {
this.resample = resample;
}
private class IconPanel extends JPanel {
private SerializableImage _img;
private ButtonTag mouseOverButton = null;
private boolean autoFit = false;
private boolean allowMove = true;
private Point2D dragStart = null;
private Point2D selectionEnd = null;
private boolean canInvert = true;
private Rectangle2D getSelectionRect() {
Point2D selectStart = dragStart;
Point2D selectEnd = selectionEnd;
if (selectStart == null || selectEnd == null) {
return null;
}
double startX = Math.min(selectStart.getX(), selectEnd.getX());
double startY = Math.min(selectStart.getY(), selectEnd.getY());
double endX = Math.max(selectStart.getX(), selectEnd.getX());
double endY = Math.max(selectStart.getY(), selectEnd.getY());
return new Rectangle2D.Double(startX, startY, endX - startX, endY - startY);
}
private synchronized Point2D getDragStart() {
return dragStart;
}
private synchronized void setDragStart(Point dragStart) {
this.dragStart = dragStart;
}
private synchronized SerializableImage getImg() {
return _img;
}
public boolean hasAllowMove() {
return allowMove;
}
VolatileImage renderImage;
public void render() {
SerializableImage img = getImg();
/*if (img == null) {
return;
}*/
Graphics2D g2 = null;
VolatileImage ri;
do {
ri = this.renderImage;
if (ri == null) {
return;
}
int valid = ri.validate(View.getDefaultConfiguration());
if (valid == VolatileImage.IMAGE_INCOMPATIBLE) {
ri = View.createRenderImage(getWidth(), getHeight(), Transparency.TRANSLUCENT);
}
try {
g2 = ri.createGraphics();
Color swfBackgroundColor = View.getSwfBackgroundColor();
if (swfBackgroundColor.getAlpha() < 255) {
g2.setPaint(View.transparentPaint);
g2.fill(new Rectangle(0, 0, getWidth(), getHeight()));
}
g2.setComposite(AlphaComposite.Src);
g2.setPaint(View.getSwfBackgroundColor());
g2.fill(new Rectangle(0, 0, getWidth(), getHeight()));
g2.setComposite(AlphaComposite.SrcOver);
if (img != null) {
int x = 0;
int y = 0;
if (timelined == null || !autoPlayed) {
x = (int) offsetPoint.getX();
y = (int) offsetPoint.getY();
}
g2.drawImage(img.getBufferedImage(), x, y, x + img.getWidth(), y + img.getHeight(), 0, 0, img.getWidth(), img.getHeight(), null);
if (hilightedEdge != null || hilightedPoints != null) {
hilightEdgeColor += hilightEdgeColorStep;
if (hilightEdgeColor < 100 || hilightEdgeColor > 255) {
hilightEdgeColorStep = -hilightEdgeColorStep;
hilightEdgeColor += hilightEdgeColorStep * 2;
}
RECT timRect = timelined.getRect();
double zoomDouble = zoom.fit ? getZoomToFit() : zoom.value;
AffineTransform trans = new AffineTransform();
trans.translate(offsetPoint.getX(), offsetPoint.getY());
trans.scale(1 / SWF.unitDivisor, 1 / SWF.unitDivisor);
trans.scale(zoomDouble, zoomDouble);
AffineTransform oldTransform = g2.getTransform();
g2.setTransform(trans);
if (hilightedEdge != null) {
g2.setStroke(new BasicStroke((float) (SWF.unitDivisor * 6 / zoomDouble)));
g2.setPaint(new Color(hilightEdgeColor, hilightEdgeColor, hilightEdgeColor));
Point[] edge = hilightedEdge;
GeneralPath path = new GeneralPath();
if (edge.length == 2) {
path.moveTo(edge[0].x, edge[0].y);
path.lineTo(edge[1].x, edge[1].y);
}
if (edge.length == 3) {
path.moveTo(edge[0].x, edge[0].y);
path.quadTo(edge[1].x, edge[1].y, edge[2].x, edge[2].y);
}
if (edge.length == 1) {
double crossSize = (SWF.unitDivisor * 10 / zoomDouble);
path.moveTo(edge[0].x - crossSize, edge[0].y);
path.lineTo(edge[0].x + crossSize, edge[0].y);
path.moveTo(edge[0].x, edge[0].y - crossSize);
path.lineTo(edge[0].x, edge[0].y + crossSize);
}
g2.draw(path);
double pointSize = SWF.unitDivisor * 4 / zoomDouble;
g2.setPaint(Color.red);
g2.fill(new Ellipse2D.Double(edge[edge.length - 1].x - pointSize, edge[edge.length - 1].y - pointSize, pointSize * 2, pointSize * 2));
g2.setPaint(Color.green);
g2.fill(new Ellipse2D.Double(edge[0].x - pointSize, edge[0].y - pointSize, pointSize * 2, pointSize * 2));
}
List points = hilightedPoints;
if (points != null) {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2.setStroke(new BasicStroke((float) (SWF.unitDivisor * 1 / zoomDouble)));
double pointSize = SWF.unitDivisor * 4 / zoomDouble;
//selectedPoints
for (int i = 0; i < points.size(); i++) {
DisplayPoint p = points.get(i);
Shape pointShape;
if (p.onPath) {
pointShape = new Rectangle2D.Double(p.x - pointSize, p.y - pointSize, pointSize * 2, pointSize * 2);
} else {
pointShape = new Ellipse2D.Double(p.x - pointSize, p.y - pointSize, pointSize * 2, pointSize * 2);
}
if (selectedPoints.contains(i)) {
g2.setPaint(Color.black);
} else {
g2.setPaint(Color.white);
}
g2.fill(pointShape);
if (selectedPoints.contains(i)) {
g2.setPaint(Color.white);
} else {
g2.setPaint(Color.black);
}
g2.draw(pointShape);
}
}
for (int i = 0; i < showPoints1.size(); i++) {
int xt = showPoints1.get(i).x;
int pointSize = 3;
int yt = showPoints1.get(i).y;
Shape pointShape;
pointShape = new Ellipse2D.Double(xt - pointSize, yt - pointSize, pointSize * 2, pointSize * 2);
g2.setPaint(Color.blue);
g2.fill(pointShape);
}
for (int i = 0; i < showPoints2.size(); i++) {
int xt = showPoints2.get(i).x;
int pointSize = 3;
int yt = showPoints2.get(i).y;
Shape pointShape;
pointShape = new Ellipse2D.Double(xt - pointSize, yt - pointSize, pointSize * 2, pointSize * 2);
g2.setPaint(Color.red);
g2.fill(pointShape);
}
g2.setTransform(oldTransform);
}
if (!(timelined instanceof SWF) && (freeTransformDepth > -1 || hilightedPoints != null)) {
int axisX = 0;
int axisY = 0;
double zoomDouble = zoom.fit ? getZoomToFit() : zoom.value;
RECT timRect = timelined.getRect();
axisX = (int) Math.round(offsetPoint.getX());
axisY = (int) Math.round(offsetPoint.getY());
if (canInvert) {
g2.setComposite(BlendComposite.Invert);
} else {
g2.setComposite(AlphaComposite.SrcOver);
}
g2.setPaint(new Color(255, 255, 255, 128));
float dp;
dp = -(float) offsetPoint.getY();
while (dp < 0) {
dp += 10;
}
g2.setStroke(new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 0, new float[]{5, 5}, dp));
GeneralPath p = new GeneralPath();
p.moveTo(axisX, 0);
p.lineTo(axisX, getHeight());
g2.draw(p);
dp = -(float) offsetPoint.getX();
while (dp < 0) {
dp += 10;
}
g2.setStroke(new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 0, new float[]{5, 5}, dp));
p = new GeneralPath();
p.moveTo(0, axisY);
p.lineTo(getWidth(), axisY);
g2.draw(p);
g2.setComposite(AlphaComposite.SrcOver);
}
Rectangle2D selectionRect = getSelectionRect();
if (selectionRect != null) {
g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND, 0, new float[]{2, 2}, 0f));
if (canInvert) {
g2.setComposite(BlendComposite.Invert);
} else {
g2.setComposite(AlphaComposite.SrcOver);
}
g2.draw(new Rectangle2D.Double(selectionRect.getX(), selectionRect.getY(), selectionRect.getWidth(), selectionRect.getHeight()));
g2.setComposite(AlphaComposite.SrcOver);
}
}
} catch (InternalError ie) {
//On some devices like Linux X11 - BlendComposite.Invert is not available
//since sun.java2d.xr.XRSurfaceData.getRaster(XRSurfaceData.java:72) is not implemented
// (tried in WSL)
if (canInvert) {
canInvert = false;
continue;
}
} finally {
if (g2 != null) {
g2.dispose();
}
}
} while (ri.contentsLost());
}
private boolean ctrlDown = false;
private boolean altDown = false;
private boolean shiftDown = false;
public boolean isAltDown() {
return altDown;
}
public boolean isCtrlDown() {
return ctrlDown;
}
public IconPanel() {
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.addKeyEventDispatcher(new KeyEventDispatcher() {
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if ((e.getID() == KeyEvent.KEY_PRESSED) || (e.getID() == KeyEvent.KEY_RELEASED)) {
ctrlDown = ((e.getModifiersEx() & InputEvent.CTRL_DOWN_MASK) == InputEvent.CTRL_DOWN_MASK);
altDown = ((e.getModifiersEx() & InputEvent.ALT_DOWN_MASK) == InputEvent.ALT_DOWN_MASK);
shiftDown = ((e.getModifiersEx() & InputEvent.SHIFT_DOWN_MASK) == InputEvent.SHIFT_DOWN_MASK);
}
return false;
}
});
addKeyListener(new KeyAdapter() {
private void move(int x, int y) {
if (hilightedPoints != null) {
java.awt.Point minPoint = getMinSelectedPoint();
if (minPoint != null) {
pointXTextField.setText(formatDouble(minPoint.x / SWF.unitDivisor + x));
pointYTextField.setText(formatDouble(minPoint.y / SWF.unitDivisor + y));
applyPointsXY();
}
} else {
Matrix matrix = new Matrix();
matrix.translate(x * SWF.unitDivisor, y * SWF.unitDivisor);
applyTransformMatrix(matrix);
}
}
@Override
public void keyPressed(KeyEvent e) {
if (hilightedPoints != null) {
if (e.getKeyCode() == KeyEvent.VK_DELETE) {
List selectedPointsDesc = new ArrayList<>(selectedPoints);
selectedPointsDesc.sort(new Comparator() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
for (int i : selectedPointsDesc) {
firePointRemoved(i);
}
selectedPoints.clear();
repaint();
}
}
if (freeTransformDepth > -1 || hilightedPoints != null) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
move(-1, 0);
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
move(1, 0);
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
move(0, -1);
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
move(0, 1);
}
}
}
});
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
int width = getWidth();
int height = getHeight();
if (width > 0 && height > 0) {
renderImage = View.createRenderImage(width, height, Transparency.TRANSLUCENT);
} else {
renderImage = null;
}
calcRect();
updateScrollBars();
render();
repaint();
}
});
MouseInputAdapter mouseInputAdapter = new MouseInputAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (shiftDown) {
List newSelectedPoints = new ArrayList<>(pointsUnderCursor);
for (int i : selectedPoints) {
if (!newSelectedPoints.contains(i)) {
newSelectedPoints.add(i);
} else {
newSelectedPoints.remove((Integer) i);
}
}
selectedPoints = newSelectedPoints;
} else {
selectedPoints = new ArrayList<>(pointsUnderCursor);
}
calculatePointsXY();
}
@Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
if (altDown || selectionMode) {
if (depthStateUnderCursor != null) {
if (transformSelectionMode) {
if (freeTransformDepth != depthStateUnderCursor.depth && mode == Cursor.DEFAULT_CURSOR) {
freeTransformDepth(depthStateUnderCursor.depth);
firePlaceObjectSelected();
}
} else if ((altDown && !selectionMode) || selectionMode){
selectDepth(depthStateUnderCursor.depth);
firePlaceObjectSelected();
}
} else {
if (transformSelectionMode) {
if (mode == Cursor.DEFAULT_CURSOR) {
freeTransformDepth(-1);
selectDepth(-1);
firePlaceObjectSelected();
}
} else if ((altDown && !selectionMode) || selectionMode) {
selectDepth(-1);
firePlaceObjectSelected();
}
}
if (!selectionMode) {
return;
}
}
mouseMoved(e); //to correctly calculate mode, because moseMoved event is not called during dragging
setDragStart(e.getPoint());
if (!shiftDown) {
boolean selectedUnderCursor = false;
for (int p : pointsUnderCursor) {
if (selectedPoints.contains(p)) {
selectedUnderCursor = true;
break;
}
}
if (!selectedUnderCursor) {
selectedPoints = new ArrayList<>(pointsUnderCursor);
calculatePointsXY();
}
}
List newPointsUnderCursorValues = new ArrayList<>();
for (int i : selectedPoints) {
newPointsUnderCursorValues.add(new DisplayPoint(hilightedPoints.get(i)));
}
selectedPointsOriginalValues = newPointsUnderCursorValues;
if (!autoPlayed) {
Configuration.autoPlayPreviews.set(true);
autoPlayed = true;
play();
}
}
requestFocusInWindow();
}
@Override
public void mouseReleased(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
if (hilightedPoints != null) {
Rectangle2D selectionRect = getSelectionRect();
if (selectionRect != null) {
List newSelectedPoints = new ArrayList<>();
for (int i = 0; i < hilightedPoints.size(); i++) {
DisplayPoint p = hilightedPoints.get(i);
Point2D ip = toImagePoint(new Point2D.Double(p.x, p.y));
if (selectionRect.contains(ip)) {
newSelectedPoints.add(i);
}
}
if (shiftDown) {
for (int p : selectedPoints) {
if (!newSelectedPoints.contains(p)) {
newSelectedPoints.add(p);
}
}
}
selectedPoints = newSelectedPoints;
calculatePointsXY();
}
if (ctrlDown && !pathPointsUnderCursor.isEmpty()) {
/*List positions = new ArrayList<>();
List splitPositions = new ArrayList<>();
for(DistanceItem di:pathPointsUnderCursor) {
positions.add(di.pathPoint);
splitPositions.add(di.pathPosition);
}*/
for (DistanceItem di : pathPointsUnderCursor) {
fireEdgeSplit(di.pathPoint, di.pathPosition);
}
selectedPoints.clear();
pointsUnderCursor.clear();
pathPointsUnderCursor.clear();
repaint();
}
updateScrollBarMinMax();
}
dragStart = null;
selectionEnd = null;
if (((freeTransformDepth > -1 && mode != Cursor.DEFAULT_CURSOR) || (selectionMode && transform != null)) && registrationPointUpdated != null && transformUpdated != null) {
synchronized (lock) {
Rectangle2D transBoundsBefore = getTransformBounds();
Point2D transRegPointBefore = registrationPoint;
Point transRegPointBeforeTwip = new Point((int) Math.round(transRegPointBefore.getX() - transBoundsBefore.getX()), (int) Math.round(transRegPointBefore.getY() - transBoundsBefore.getY()));
Point2D transRegPointPercentBefore = new Point2D.Double(transRegPointBeforeTwip.getX() / transBoundsBefore.getWidth(), transRegPointBeforeTwip.getY() / transBoundsBefore.getHeight());
registrationPoint = new Point2D.Double(registrationPointUpdated.getX(), registrationPointUpdated.getY());
transform = new Matrix(transformUpdated);
transformUpdated = null;
Rectangle2D transBoundsAfter = getTransformBounds();
Point2D transRegPointAfter = registrationPoint;
Point transRegPointAfterTwip = new Point((int) Math.round(transRegPointAfter.getX() - transBoundsAfter.getX()), (int) Math.round(transRegPointAfter.getY() - transBoundsAfter.getY()));
Point2D transRegPointPercentAfter = new Point2D.Double(transRegPointAfterTwip.getX() / transBoundsAfter.getWidth(), transRegPointAfterTwip.getY() / transBoundsAfter.getHeight());
boolean isResize
= mode == Cursor.E_RESIZE_CURSOR
|| mode == Cursor.NE_RESIZE_CURSOR
|| mode == Cursor.NW_RESIZE_CURSOR
|| mode == Cursor.N_RESIZE_CURSOR
|| mode == Cursor.SE_RESIZE_CURSOR
|| mode == Cursor.SW_RESIZE_CURSOR
|| mode == Cursor.S_RESIZE_CURSOR
|| mode == Cursor.W_RESIZE_CURSOR;
if (!isResize && mode != Cursor.DEFAULT_CURSOR && !transRegPointPercentBefore.equals(transRegPointPercentAfter)) {
registrationPointPosition = null;
}
}
calcRect(); //do not put this inside synchronized block, it cause deadlock
fireBoundsChange(getTransformBounds(), registrationPoint, registrationPointPosition);
fireTransformChanged();
repaint();
}
if (selectionMode && freeTransformDepth == -1) {
transform = null;
}
mode = Cursor.DEFAULT_CURSOR;
}
}
private void stopDragging() {
}
@Override
public void mouseDragged(MouseEvent e) {
List points = hilightedPoints;
if (dragStart != null && points != null) {
if (pointsUnderCursor.isEmpty()) {
selectionEnd = e.getPoint();
redraw();
}
if (!selectedPoints.isEmpty() && !pointsUnderCursor.isEmpty()) {
boolean selectedUnderCursor = false;
for (int p : pointsUnderCursor) {
if (selectedPoints.contains(p)) {
selectedUnderCursor = true;
break;
}
}
if (!selectedUnderCursor) {
return;
}
for (int i = 0; i < selectedPoints.size(); i++) {
int pointIndex = selectedPoints.get(i);
DisplayPoint pointStart = selectedPointsOriginalValues.get(i);
Point2D dragEnd = e.getPoint();
Point2D startTransformPoint = toTransformPoint(dragStart);
Point2D endTransformPoint = toTransformPoint(dragEnd);
Point2D delta = new Point2D.Double(endTransformPoint.getX() - startTransformPoint.getX(), endTransformPoint.getY() - startTransformPoint.getY());
DisplayPoint newPoint = new DisplayPoint((int) Math.round(pointStart.x + delta.getX()), (int) Math.round(pointStart.y + delta.getY()), pointStart.onPath);
points.set(pointIndex, newPoint);
}
firePointsUpdated(points);
calculatePointsXY();
repaint();
return;
}
}
if (dragStart != null && allowMove && mode == Cursor.DEFAULT_CURSOR) {
Point2D dragEnd = e.getPoint();
Point2D delta = new Point2D.Double(dragEnd.getX() - dragStart.getX(), dragEnd.getY() - dragStart.getY());
Point2D regPointImage = registrationPoint == null ? null : toImagePoint(registrationPoint);
offsetPoint.setLocation(offsetPoint.getX() + delta.getX(), offsetPoint.getY() + delta.getY());
updateScrollBars();
ExportRectangle oldViewRect = new ExportRectangle(_viewRect);
dragStart = dragEnd;
iconPanel.calcRect();
_viewRect = getViewRect();
double zoomDouble = zoom.fit ? getZoomToFit() : zoom.value;
synchronized (lock) {
if (transform != null) {
/*Matrix prevTransform = transform.clone();
Matrix m = new Matrix();
m.scale(1 / SWF.unitDivisor);
m.translate(-oldViewRect.xMin * zoomDouble, -oldViewRect.yMin * zoomDouble);
//m.scale(zoomDouble);
Matrix mi = m.inverse();
transform = transform.preConcatenate(mi);
Matrix m2 = new Matrix();
m2.scale(1 / SWF.unitDivisor);
m2.translate(-_viewRect.xMin * zoomDouble, -_viewRect.yMin * zoomDouble);
//m2.scale(zoomDouble);
transform = transform.preConcatenate(m2);
*/
if (registrationPoint != null) {
Point2D regPointImageUpdated = new Point2D.Double(regPointImage.getX() + delta.getX(), regPointImage.getY() + delta.getY());
registrationPoint = toTransformPoint(regPointImageUpdated);
}
}
}
repaint();
return;
}
if (dragStart != null && selectionMode && freeTransformDepth == -1) {
if (transform == null) {
return;
}
Point2D mouseTransPoint = toTransformPoint(new Point2D.Double(e.getX(), e.getY()));
double ex = mouseTransPoint.getX();
double ey = mouseTransPoint.getY();
Point2D dragStartTransPoint = toTransformPoint(dragStart);
double dsx = dragStartTransPoint.getX();
double dsy = dragStartTransPoint.getY();
double deltaX = ex - dsx;
double deltaY = ey - dsy;
AffineTransform newTransform = new AffineTransform(transform.toTransform());
AffineTransform t = new AffineTransform();
t.translate(deltaX, deltaY);
newTransform.preConcatenate(t);
Point2D newRegistrationPoint = new Point2D.Double();
t.transform(registrationPoint, newRegistrationPoint);
transformUpdated = newTransform;
registrationPointUpdated = newRegistrationPoint;
repaint();
return;
}
if (dragStart != null && freeTransformDepth > -1) {
if (transform == null) {
return;
}
Point2D mouseTransPoint = toTransformPoint(new Point2D.Double(e.getX(), e.getY()));
double ex = mouseTransPoint.getX();
double ey = mouseTransPoint.getY();
Point2D dragStartTransPoint = toTransformPoint(dragStart);
double dsx = dragStartTransPoint.getX();
double dsy = dragStartTransPoint.getY();
if (mode == MODE_SHEAR_N) {
double shearX = -(ex - dsx) / (bounds.getHeight());
AffineTransform newTransform = new AffineTransform(transform.toTransform());
AffineTransform t = new AffineTransform();
t.translate(bounds.getX(), bounds.getY());
t.shear(shearX, 0);
t.translate(-bounds.getX(), -bounds.getY());
t.translate(ex - dsx, 0);
newTransform.preConcatenate(t);
Point2D newRegistrationPoint = new Point2D.Double();
t.transform(registrationPoint, newRegistrationPoint);
transformUpdated = newTransform;
registrationPointUpdated = newRegistrationPoint;
repaint();
}
if (mode == MODE_SHEAR_S) {
double shearX = (ex - dsx) / (bounds.getHeight());
AffineTransform newTransform = new AffineTransform(transform.toTransform());
AffineTransform t = new AffineTransform();
t.translate(bounds.getX(), bounds.getY());
t.shear(shearX, 0);
t.translate(-bounds.getX(), -bounds.getY());
newTransform.preConcatenate(t);
Point2D newRegistrationPoint = new Point2D.Double();
t.transform(registrationPoint, newRegistrationPoint);
transformUpdated = newTransform;
registrationPointUpdated = newRegistrationPoint;
repaint();
}
if (mode == MODE_SHEAR_W) {
double shearY = -(ey - dsy) / (bounds.getWidth());
AffineTransform newTransform = new AffineTransform(transform.toTransform());
AffineTransform t = new AffineTransform();
t.translate(bounds.getX(), bounds.getY());
t.shear(0, shearY);
t.translate(-bounds.getX(), -bounds.getY());
t.translate(0, ey - dsy);
newTransform.preConcatenate(t);
Point2D newRegistrationPoint = new Point2D.Double();
t.transform(registrationPoint, newRegistrationPoint);
transformUpdated = newTransform;
registrationPointUpdated = newRegistrationPoint;
repaint();
}
if (mode == MODE_SHEAR_E) {
double shearY = (ey - dsy) / (bounds.getWidth());
AffineTransform newTransform = new AffineTransform(transform.toTransform());
AffineTransform t = new AffineTransform();
t.translate(bounds.getX(), bounds.getY());
t.shear(0, shearY);
t.translate(-bounds.getX(), -bounds.getY());
newTransform.preConcatenate(t);
Point2D newRegistrationPoint = new Point2D.Double();
t.transform(registrationPoint, newRegistrationPoint);
transformUpdated = newTransform;
registrationPointUpdated = newRegistrationPoint;
repaint();
}
if (mode == MODE_ROTATE_SE) {
double deltaStartX = Math.abs(dsx - registrationPoint.getX());
double deltaStartY = Math.abs(dsy - registrationPoint.getY());
double deltaEndX = Math.abs(ex - registrationPoint.getX());
double deltaEndY = Math.abs(ey - registrationPoint.getY());
double deltaTheta = 0;
if (ex >= registrationPoint.getX() && ey >= registrationPoint.getY()) {
//same
double thetaStart = Math.atan(deltaStartY / deltaStartX);
double thetaEnd = Math.atan(deltaEndY / deltaEndX);
deltaTheta = thetaEnd - thetaStart;
} else if (ex >= registrationPoint.getX() && ey <= registrationPoint.getY()) {
//anti clockwise
double thetaStart = Math.atan(deltaStartY / deltaStartX);
double thetaEnd = Math.atan(deltaEndY / deltaEndX);
deltaTheta = -(thetaStart + thetaEnd);
} else if (ex <= registrationPoint.getX() && ey >= registrationPoint.getY()) {
//clock wise
double thetaStart = Math.atan(deltaStartX / deltaStartY);
double thetaEnd = Math.atan(deltaEndX / deltaEndY);
deltaTheta = thetaStart + thetaEnd;
} else if (ex <= registrationPoint.getX() && ey <= registrationPoint.getY()) {
//opposite
double thetaStart = Math.atan(deltaStartX / deltaStartY);
double thetaEnd = Math.atan(deltaEndY / deltaEndX);
deltaTheta = thetaStart + Math.toRadians(90) + thetaEnd;
}
AffineTransform newTransform = new AffineTransform(transform.toTransform());
AffineTransform t = new AffineTransform();
t.rotate(deltaTheta, registrationPoint.getX(), registrationPoint.getY());
newTransform.preConcatenate(t);
Point2D newRegistrationPoint = new Point2D.Double();
t.transform(registrationPoint, newRegistrationPoint);
transformUpdated = newTransform;
registrationPointUpdated = newRegistrationPoint;
repaint();
}
if (mode == MODE_ROTATE_NW) {
double deltaStartX = Math.abs(dsx - registrationPoint.getX());
double deltaStartY = Math.abs(dsy - registrationPoint.getY());
double deltaEndX = Math.abs(ex - registrationPoint.getX());
double deltaEndY = Math.abs(ey - registrationPoint.getY());
double deltaTheta = 0;
if (ex >= registrationPoint.getX() && ey >= registrationPoint.getY()) {
//opposite
double thetaStart = Math.atan(deltaStartX / deltaStartY);
double thetaEnd = Math.atan(deltaEndY / deltaEndX);
deltaTheta = thetaStart + Math.toRadians(90) + thetaEnd;
} else if (ex >= registrationPoint.getX() && ey <= registrationPoint.getY()) {
//clock wise
double thetaStart = Math.atan(deltaStartX / deltaStartY);
double thetaEnd = Math.atan(deltaEndX / deltaEndY);
deltaTheta = thetaStart + thetaEnd;
} else if (ex <= registrationPoint.getX() && ey >= registrationPoint.getY()) {
//anti clockwise
double thetaStart = Math.atan(deltaStartY / deltaStartX);
double thetaEnd = Math.atan(deltaEndY / deltaEndX);
deltaTheta = -(thetaStart + thetaEnd);
} else if (ex <= registrationPoint.getX() && ey <= registrationPoint.getY()) {
//same
double thetaStart = Math.atan(deltaStartY / deltaStartX);
double thetaEnd = Math.atan(deltaEndY / deltaEndX);
deltaTheta = thetaEnd - thetaStart;
}
AffineTransform newTransform = new AffineTransform(transform.toTransform());
AffineTransform t = new AffineTransform();
t.rotate(deltaTheta, registrationPoint.getX(), registrationPoint.getY());
newTransform.preConcatenate(t);
Point2D newRegistrationPoint = new Point2D.Double();
t.transform(registrationPoint, newRegistrationPoint);
transformUpdated = newTransform;
registrationPointUpdated = newRegistrationPoint;
repaint();
}
if (mode == MODE_ROTATE_NE) {
double deltaStartX = Math.abs(dsx - registrationPoint.getX());
double deltaStartY = Math.abs(dsy - registrationPoint.getY());
double deltaEndX = Math.abs(ex - registrationPoint.getX());
double deltaEndY = Math.abs(ey - registrationPoint.getY());
double deltaTheta = 0;
if (ex >= registrationPoint.getX() && ey >= registrationPoint.getY()) {
//clock wise
double thetaStart = Math.atan(deltaStartY / deltaStartX);
double thetaEnd = Math.atan(deltaEndY / deltaEndX);
deltaTheta = thetaStart + thetaEnd;
} else if (ex >= registrationPoint.getX() && ey <= registrationPoint.getY()) {
//same
double thetaStart = Math.atan(deltaStartY / deltaStartX);
double thetaEnd = Math.atan(deltaEndY / deltaEndX);
deltaTheta = thetaStart - thetaEnd;
} else if (ex <= registrationPoint.getX() && ey >= registrationPoint.getY()) {
//opposite
double thetaStart = Math.atan(deltaStartY / deltaStartX);
double thetaEnd = Math.atan(deltaEndX / deltaEndY);
deltaTheta = thetaStart + Math.toRadians(90) + thetaEnd;
} else if (ex <= registrationPoint.getX() && ey <= registrationPoint.getY()) {
//anti clockwise
double thetaStart = Math.atan(deltaStartX / deltaStartY);
double thetaEnd = Math.atan(deltaEndX / deltaEndY);
deltaTheta = -(thetaStart + thetaEnd);
}
AffineTransform newTransform = new AffineTransform(transform.toTransform());
AffineTransform t = new AffineTransform();
t.rotate(deltaTheta, registrationPoint.getX(), registrationPoint.getY());
newTransform.preConcatenate(t);
Point2D newRegistrationPoint = new Point2D.Double();
t.transform(registrationPoint, newRegistrationPoint);
transformUpdated = newTransform;
registrationPointUpdated = newRegistrationPoint;
repaint();
}
if (mode == MODE_ROTATE_SW) {
double deltaStartX = Math.abs(dsx - registrationPoint.getX());
double deltaStartY = Math.abs(dsy - registrationPoint.getY());
double deltaEndX = Math.abs(ex - registrationPoint.getX());
double deltaEndY = Math.abs(ey - registrationPoint.getY());
double deltaTheta = 0;
if (ex >= registrationPoint.getX() && ey >= registrationPoint.getY()) {
//anti clockwise
double thetaStart = Math.atan(deltaStartX / deltaStartY);
double thetaEnd = Math.atan(deltaEndX / deltaEndY);
deltaTheta = -(thetaStart + thetaEnd);
} else if (ex >= registrationPoint.getX() && ey <= registrationPoint.getY()) {
//opposite
double thetaStart = Math.atan(deltaStartY / deltaStartX);
double thetaEnd = Math.atan(deltaEndX / deltaEndY);
deltaTheta = thetaStart + Math.toRadians(90) + thetaEnd;
} else if (ex <= registrationPoint.getX() && ey >= registrationPoint.getY()) {
//same
double thetaStart = Math.atan(deltaStartY / deltaStartX);
double thetaEnd = Math.atan(deltaEndY / deltaEndX);
deltaTheta = thetaStart - thetaEnd;
} else if (ex <= registrationPoint.getX() && ey <= registrationPoint.getY()) {
//clock wise
double thetaStart = Math.atan(deltaStartY / deltaStartX);
double thetaEnd = Math.atan(deltaEndY / deltaEndX);
deltaTheta = thetaStart + thetaEnd;
}
AffineTransform newTransform = new AffineTransform(transform.toTransform());
AffineTransform t = new AffineTransform();
t.rotate(deltaTheta, registrationPoint.getX(), registrationPoint.getY());
newTransform.preConcatenate(t);
Point2D newRegistrationPoint = new Point2D.Double();
t.transform(registrationPoint, newRegistrationPoint);
transformUpdated = newTransform;
registrationPointUpdated = newRegistrationPoint;
repaint();
}
if (mode == Cursor.HAND_CURSOR) {
transformUpdated = new AffineTransform(transform.toTransform());
registrationPointUpdated = new Point2D.Double(ex, ey);
repaint();
}
if (mode == Cursor.MOVE_CURSOR) {
double deltaX = ex - dsx;
double deltaY = ey - dsy;
AffineTransform newTransform = new AffineTransform(transform.toTransform());
AffineTransform t = new AffineTransform();
t.translate(deltaX, deltaY);
newTransform.preConcatenate(t);
Point2D newRegistrationPoint = new Point2D.Double();
t.transform(registrationPoint, newRegistrationPoint);
transformUpdated = newTransform;
registrationPointUpdated = newRegistrationPoint;
repaint();
}
if (mode == Cursor.E_RESIZE_CURSOR) {
double deltaBefore = bounds.getX() + bounds.getWidth() - registrationPoint.getX();
double deltaX = ex - registrationPoint.getX();
double scaleX = deltaX / deltaBefore;
AffineTransform newTransform = new AffineTransform(transform.toTransform());
AffineTransform t = new AffineTransform();
t.translate(registrationPoint.getX(), 0);
t.scale(scaleX, 1);
t.translate(-registrationPoint.getX(), 0);
newTransform.preConcatenate(t);
Point2D newRegistrationPoint = new Point2D.Double();
t.transform(registrationPoint, newRegistrationPoint);
transformUpdated = newTransform;
registrationPointUpdated = newRegistrationPoint;
repaint();
}
if (mode == Cursor.W_RESIZE_CURSOR) {
double deltaBefore = registrationPoint.getX() - bounds.getX();
double deltaX = registrationPoint.getX() - ex;
double scaleX = deltaX / deltaBefore;
AffineTransform newTransform = new AffineTransform(transform.toTransform());
AffineTransform t = new AffineTransform();
t.translate(registrationPoint.getX(), 0);
t.scale(scaleX, 1);
t.translate(-registrationPoint.getX(), 0);
newTransform.preConcatenate(t);
Point2D newRegistrationPoint = new Point2D.Double();
t.transform(registrationPoint, newRegistrationPoint);
transformUpdated = newTransform;
registrationPointUpdated = newRegistrationPoint;
repaint();
}
if (mode == Cursor.S_RESIZE_CURSOR) {
double deltaBefore = bounds.getY() + bounds.getHeight() - registrationPoint.getY();
double deltaY = ey - registrationPoint.getY();
double scaleY = deltaY / deltaBefore;
AffineTransform newTransform = new AffineTransform(transform.toTransform());
AffineTransform t = new AffineTransform();
t.translate(0, registrationPoint.getY());
t.scale(1, scaleY);
t.translate(0, -registrationPoint.getY());
newTransform.preConcatenate(t);
Point2D newRegistrationPoint = new Point2D.Double();
t.transform(registrationPoint, newRegistrationPoint);
transformUpdated = newTransform;
registrationPointUpdated = newRegistrationPoint;
repaint();
}
if (mode == Cursor.N_RESIZE_CURSOR) {
double deltaBefore = registrationPoint.getY() - bounds.getY();
double deltaY = registrationPoint.getY() - ey;
double scaleY = deltaY / deltaBefore;
AffineTransform newTransform = new AffineTransform(transform.toTransform());
AffineTransform t = new AffineTransform();
t.translate(0, registrationPoint.getY());
t.scale(1, scaleY);
t.translate(0, -registrationPoint.getY());
newTransform.preConcatenate(t);
Point2D newRegistrationPoint = new Point2D.Double();
t.transform(registrationPoint, newRegistrationPoint);
transformUpdated = newTransform;
registrationPointUpdated = newRegistrationPoint;
repaint();
}
if (mode == Cursor.SE_RESIZE_CURSOR) {
double deltaXBefore = bounds.getX() + bounds.getWidth() - registrationPoint.getX();
double deltaYBefore = bounds.getY() + bounds.getHeight() - registrationPoint.getY();
double deltaX = ex - registrationPoint.getX();
double deltaY = ey - registrationPoint.getY();
double scaleX = deltaX / deltaXBefore;
double scaleY = deltaY / deltaYBefore;
AffineTransform newTransform = new AffineTransform(transform.toTransform());
AffineTransform t = new AffineTransform();
t.translate(registrationPoint.getX(), registrationPoint.getY());
t.scale(scaleX, scaleY);
t.translate(-registrationPoint.getX(), -registrationPoint.getY());
newTransform.preConcatenate(t);
Point2D newRegistrationPoint = new Point2D.Double();
t.transform(registrationPoint, newRegistrationPoint);
transformUpdated = newTransform;
registrationPointUpdated = newRegistrationPoint;
repaint();
}
if (mode == Cursor.NE_RESIZE_CURSOR) {
double deltaXBefore = bounds.getX() + bounds.getWidth() - registrationPoint.getX();
double deltaYBefore = registrationPoint.getY() - bounds.getY();
double deltaX = ex - registrationPoint.getX();
double deltaY = registrationPoint.getY() - ey;
double scaleX = deltaX / deltaXBefore;
double scaleY = deltaY / deltaYBefore;
AffineTransform newTransform = new AffineTransform(transform.toTransform());
AffineTransform t = new AffineTransform();
t.translate(registrationPoint.getX(), registrationPoint.getY());
t.scale(scaleX, scaleY);
t.translate(-registrationPoint.getX(), -registrationPoint.getY());
newTransform.preConcatenate(t);
Point2D newRegistrationPoint = new Point2D.Double();
t.transform(registrationPoint, newRegistrationPoint);
transformUpdated = newTransform;
registrationPointUpdated = newRegistrationPoint;
repaint();
}
if (mode == Cursor.SW_RESIZE_CURSOR) {
double deltaXBefore = registrationPoint.getX() - bounds.getX();
double deltaYBefore = bounds.getY() + bounds.getHeight() - registrationPoint.getY();
double deltaX = registrationPoint.getX() - ex;
double deltaY = ey - registrationPoint.getY();
double scaleX = deltaX / deltaXBefore;
double scaleY = deltaY / deltaYBefore;
AffineTransform newTransform = new AffineTransform(transform.toTransform());
AffineTransform t = new AffineTransform();
t.translate(registrationPoint.getX(), registrationPoint.getY());
t.scale(scaleX, scaleY);
t.translate(-registrationPoint.getX(), -registrationPoint.getY());
newTransform.preConcatenate(t);
Point2D newRegistrationPoint = new Point2D.Double();
t.transform(registrationPoint, newRegistrationPoint);
transformUpdated = newTransform;
registrationPointUpdated = newRegistrationPoint;
repaint();
}
if (mode == Cursor.NW_RESIZE_CURSOR) {
double deltaXBefore = registrationPoint.getX() - bounds.getX();
double deltaYBefore = registrationPoint.getY() - bounds.getY();
double deltaX = registrationPoint.getX() - ex;
double deltaY = registrationPoint.getY() - ey;
double scaleX = deltaX / deltaXBefore;
double scaleY = deltaY / deltaYBefore;
AffineTransform newTransform = new AffineTransform(transform.toTransform());
AffineTransform t = new AffineTransform();
t.translate(registrationPoint.getX(), registrationPoint.getY());
t.scale(scaleX, scaleY);
t.translate(-registrationPoint.getX(), -registrationPoint.getY());
newTransform.preConcatenate(t);
Point2D newRegistrationPoint = new Point2D.Double();
t.transform(registrationPoint, newRegistrationPoint);
transformUpdated = newTransform;
registrationPointUpdated = newRegistrationPoint;
repaint();
}
}
}
@Override
public void mouseMoved(MouseEvent e) {
List points = hilightedPoints;
if (points != null) {
int maxDistance = 5;
double zoomDouble = zoom.fit ? getZoomToFit() : zoom.value;
List newPointsUnderCursor = new ArrayList<>();
for (int i = 0; i < points.size(); i++) {
DisplayPoint p = points.get(i);
Point2D ip = toImagePoint(new Point2D.Double(p.x, p.y));
int ex = e.getX();
int ey = e.getY();
if (ex > ip.getX() - maxDistance && ex < ip.getX() + maxDistance) {
if (ey > ip.getY() - maxDistance && ey < ip.getY() + maxDistance) {
newPointsUnderCursor.add(i);
}
}
}
Point2D p = toTransformPoint(e.getPoint());
List distanceList = new ArrayList<>();
for (int i = 0; i < points.size() - 1; i++) {
if (points.get(i).onPath && points.get(i + 1).onPath) {
DisplayPoint p0 = points.get(i);
DisplayPoint p1 = points.get(i + 1);
//y = mx + b
double lineDistance;
Point2D closestPoint;
if (p1.x == p0.x) {
lineDistance = Math.abs(p1.x - p.getX());
closestPoint = new Point2D.Double(p1.x, p.getY());
} else if (p1.y == p0.y) {
lineDistance = Math.abs(p1.y - p.getY());
closestPoint = new Point2D.Double(p.getX(), p1.y);
} else {
double m = (p1.y - p0.y) / (double) (p1.x - p0.x);
double b = p0.y - m * p0.x;
double m_perp = -1 / m;
double b_perp = p.getY() - m_perp * p.getX();
double x = (b_perp - b) / (m - m_perp);
double y = m * x + b;
closestPoint = new Point2D.Double(x, y);
lineDistance = p.distance(closestPoint);
}
double minX = Math.min(p0.x, p1.x) - maxDistance * SWF.unitDivisor / zoomDouble;
double minY = Math.min(p0.y, p1.y) - maxDistance * SWF.unitDivisor / zoomDouble;
double maxX = Math.max(p0.x, p1.x) + maxDistance * SWF.unitDivisor / zoomDouble;
double maxY = Math.max(p0.y, p1.y) + maxDistance * SWF.unitDivisor / zoomDouble;
if (p.getX() >= minX && p.getX() <= maxX && p.getY() >= minY && p.getY() <= maxY) {
double t = p0.toPoint2D().distance(closestPoint) / p0.toPoint2D().distance(p1.toPoint2D());
if (lineDistance <= maxDistance * SWF.unitDivisor / zoomDouble) {
distanceList.add(new DistanceItem(lineDistance, i + 1, t, new DisplayPoint(closestPoint)));
}
}
}
if (i < points.size() - 2 && !points.get(i + 1).onPath) {
DisplayPoint p0 = points.get(i);
DisplayPoint p1 = points.get(i + 1);
DisplayPoint p2 = points.get(i + 2);
BezierUtils bezierUtils = new BezierUtils();
double t = bezierUtils.closestPointToBezier(p, p0.toPoint2D(), p1.toPoint2D(), p2.toPoint2D());
DisplayPoint closestPoint = new DisplayPoint(bezierUtils.pointAt(t, p0.toPoint2D(), p1.toPoint2D(), p2.toPoint2D()));
double curveDistance = Math.sqrt((p.getX() - closestPoint.x) * (p.getX() - closestPoint.x)
+ (p.getY() - closestPoint.y) * (p.getY() - closestPoint.y));
if (curveDistance <= maxDistance * SWF.unitDivisor / zoomDouble) {
distanceList.add(new DistanceItem(curveDistance, i + 1, t, closestPoint));
}
}
}
distanceList.sort(new Comparator() {
@Override
public int compare(DistanceItem o1, DistanceItem o2) {
return o2.pathPoint - o1.pathPoint; //Double.compare(o1.distance, o2.distance);
}
});
if (dragStart == null) {
/*if (!distanceList.isEmpty()) {
DistanceItem di = distanceList.get(0);
pathPointUnderCursor = di.pathPoint;
pathPointPosition = di.pathPosition;
closestPoint = di.closestPoint;
} else {
pathPointUnderCursor = null;
pathPointPosition = null;
closestPoint = null;
}*/
pathPointsUnderCursor = distanceList;
pointsUnderCursor = newPointsUnderCursor;
}
return;
}
if (freeTransformDepth > -1) {
if (bounds == null) {
return;
}
if (registrationPoint == null) {
return;
}
Rectangle2D boundsImage = toImageRect(bounds);
Point2D regPointImage = toImagePoint(registrationPoint);
int ex = e.getX();
int ey = e.getY();
boolean left = ex >= boundsImage.getX() - TOLERANCE_SCALESHEAR && ex <= boundsImage.getX() + TOLERANCE_SCALESHEAR;
boolean right = ex >= boundsImage.getX() + boundsImage.getWidth() - TOLERANCE_SCALESHEAR && ex <= boundsImage.getX() + boundsImage.getWidth() + TOLERANCE_SCALESHEAR;
boolean top = ey >= boundsImage.getY() - TOLERANCE_SCALESHEAR && ey <= boundsImage.getY() + TOLERANCE_SCALESHEAR;
boolean bottom = ey >= boundsImage.getY() + boundsImage.getHeight() - TOLERANCE_SCALESHEAR && ey <= boundsImage.getY() + boundsImage.getHeight() + TOLERANCE_SCALESHEAR;
boolean xcenter = ex >= boundsImage.getCenterX() - TOLERANCE_SCALESHEAR && ex <= boundsImage.getCenterX() + TOLERANCE_SCALESHEAR;
boolean ycenter = ey >= boundsImage.getCenterY() - TOLERANCE_SCALESHEAR && ey <= boundsImage.getCenterY() + TOLERANCE_SCALESHEAR;
boolean registration = ex >= regPointImage.getX() - REGISTRATION_TOLERANCE
&& ex <= regPointImage.getX() + REGISTRATION_TOLERANCE
&& ey >= regPointImage.getY() - REGISTRATION_TOLERANCE
&& ey <= regPointImage.getY() + REGISTRATION_TOLERANCE;
boolean rightRotate = ex > boundsImage.getX() + boundsImage.getWidth() - TOLERANCE_ROTATE && ex
<= boundsImage.getX() + boundsImage.getWidth() + TOLERANCE_ROTATE;
boolean bottomRotate = ey > boundsImage.getY() + boundsImage.getHeight() - TOLERANCE_ROTATE && ey
<= boundsImage.getY() + boundsImage.getHeight() + TOLERANCE_ROTATE;
boolean leftRotate = ex < boundsImage.getX() + TOLERANCE_ROTATE
&& ex >= boundsImage.getX() - TOLERANCE_ROTATE;
boolean topRotate = ey < boundsImage.getY() + TOLERANCE_ROTATE
&& ey >= boundsImage.getY() - TOLERANCE_ROTATE;
boolean inBounds = boundsImage.contains(ex, ey);
boolean shearX = ex > boundsImage.getX() && ex < boundsImage.getX() + boundsImage.getWidth();
boolean shearY = ey > boundsImage.getY() && ey < boundsImage.getY() + boundsImage.getHeight();
Cursor cursor;
int newMode;
if (top && left) {
newMode = Cursor.NW_RESIZE_CURSOR;
cursor = resizeNWSECursor;
} else if (bottom && left) {
newMode = Cursor.SW_RESIZE_CURSOR;
cursor = resizeSWNECursor;
} else if (top && right) {
newMode = Cursor.NE_RESIZE_CURSOR;
cursor = resizeSWNECursor;
} else if (bottom && right) {
newMode = Cursor.SE_RESIZE_CURSOR;
cursor = resizeNWSECursor;
} else if (top && xcenter) {
newMode = Cursor.N_RESIZE_CURSOR;
cursor = resizeYCursor;
} else if (bottom && xcenter) {
newMode = Cursor.S_RESIZE_CURSOR;
cursor = resizeYCursor;
} else if (left && ycenter) {
newMode = Cursor.W_RESIZE_CURSOR;
cursor = resizeXCursor;
} else if (right && ycenter) {
newMode = Cursor.E_RESIZE_CURSOR;
cursor = resizeXCursor;
} else if (registration) {
newMode = Cursor.HAND_CURSOR;
cursor = moveRegPointCursor;
} else if (!inBounds && rightRotate && topRotate) {
newMode = MODE_ROTATE_NE;
cursor = rotateCursor;
} else if (!inBounds && rightRotate && bottomRotate) {
newMode = MODE_ROTATE_SE;
cursor = rotateCursor;
} else if (!inBounds && leftRotate && topRotate) {
newMode = MODE_ROTATE_NW;
cursor = rotateCursor;
} else if (!inBounds && leftRotate && bottomRotate) {
newMode = MODE_ROTATE_SW;
cursor = rotateCursor;
} else if (shearY && (left || right)) {
if (left) {
newMode = MODE_SHEAR_W;
} else {
newMode = MODE_SHEAR_E;
}
cursor = shearYCursor;
} else if (shearX && (top || bottom)) {
if (top) {
newMode = MODE_SHEAR_N;
} else {
newMode = MODE_SHEAR_S;
}
cursor = shearXCursor;
} else if (inBounds) {
newMode = Cursor.MOVE_CURSOR;
cursor = moveCursor;
} else {
newMode = Cursor.DEFAULT_CURSOR;
cursor = defaultCursor;
}
if (getCursor() != cursor) {
setCursor(cursor);
}
mode = newMode;
}
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if (ctrlDown) {
if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
int rotation = e.getWheelRotation();
if (rotation < 0) {
zoomIn();
} else {
zoomOut();
}
}
}
}
};
addMouseListener(mouseInputAdapter);
addMouseMotionListener(mouseInputAdapter);
addMouseWheelListener(mouseInputAdapter);
}
public void setAutoFit(boolean autoFit) {
this.autoFit = autoFit;
repaint();
}
public synchronized BufferedImage getLastImage() {
if (_img == null) {
return null;
}
return _img.getBufferedImage();
}
public synchronized void setImg(SerializableImage img) {
this._img = img;
calcRect();
View.execInEventDispatchLater(new Runnable() {
@Override
public void run() {
render();
repaint();
}
});
}
private void setAllowMove(boolean allowMove) {
this.allowMove = allowMove;
}
private void calcRect() {
synchronized (ImagePanel.this) {
synchronized (this) {
calcRect(zoom);
}
}
}
private void calcRect(Zoom z) {
synchronized (ImagePanel.this) {
if (_img != null || timelined != null) {
//int w1 = (int) (_img.getWidth() * (lowQuality ? LQ_FACTOR : 1));
//int h1 = (int) (_img.getHeight() * (lowQuality ? LQ_FACTOR : 1));
double zoomDouble = z.fit ? getZoomToFit() : z.value;
int w1;
int h1;
int dx;
int dy;
if (timelined == null || (!autoPlayed && _img != null)) {
w1 = (int) (_img.getWidth() * (lowQuality ? LQ_FACTOR : 1));
h1 = (int) (_img.getHeight() * (lowQuality ? LQ_FACTOR : 1));
dx = 0;
dy = 0;
} else {
w1 = (int) (timelined.getRect().getWidth() * zoomDouble / SWF.unitDivisor);
h1 = (int) (timelined.getRect().getHeight() * zoomDouble / SWF.unitDivisor);
dx = (int) (timelined.getRect().Xmin * zoomDouble / SWF.unitDivisor);
dy = (int) (timelined.getRect().Ymin * zoomDouble / SWF.unitDivisor);
}
//HERE
if (freeTransformDepth > -1) {
//w1 = Math.max(w1, getWidth());
//h1 = Math.max(h1, getHeight());
}
int w2 = getWidth();
int h2 = getHeight();
int w;
int h;
if (autoFit) {
if (w1 <= w2 && h1 <= h2) {
w = w1;
h = h1;
} else {
h = h1 * w2 / w1;
if (h > h2) {
w = w1 * h2 / h1;
h = h2;
} else {
w = w2;
}
}
} else {
w = w1;
h = h1;
}
if (hilightedPoints != null) {
setAllowMove(false);
//updateScrollBars();
} else if (freeTransformDepth > -1) {
setAllowMove(true);
} else if (selectionMode) {
setAllowMove(false);
} else {
boolean doMove = h > h2 || w > w2;
if (zoom.fit) {
doMove = false;
}
setAllowMove(doMove);
if (!doMove) {
offsetPoint.setLocation(iconPanel.getWidth() / 2 - w / 2 - dx, iconPanel.getHeight() / 2 - h / 2 - dy);
updateScrollBars();
}
}
}
}
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
VolatileImage ri = this.renderImage;
if (ri != null) {
if (ri.validate(View.getDefaultConfiguration()) != VolatileImage.IMAGE_OK) {
ri = View.createRenderImage(getWidth(), getHeight(), Transparency.TRANSLUCENT);
render();
}
if (ri != null) {
g2d.drawImage(ri, 0, 0, null);
}
}
if (Configuration._debugMode.get()) {
g2d.setColor(Color.red);
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
df.setMinimumFractionDigits(0);
df.setGroupingUsed(false);
g2d.drawString("frameLoss:" + df.format(getFrameLoss()) + "%", 20, 20);
}
}
}
@Override
public void setBackground(Color bg) {
if (iconPanel != null) {
iconPanel.setBackground(bg);
}
super.setBackground(bg);
}
@Override
public synchronized void addMouseListener(MouseListener l) {
iconPanel.addMouseListener(l);
}
@Override
public synchronized void removeMouseListener(MouseListener l) {
iconPanel.removeMouseListener(l);
}
@Override
public synchronized void addMouseMotionListener(MouseMotionListener l) {
iconPanel.addMouseMotionListener(l);
}
@Override
public synchronized void removeMouseMotionListener(MouseMotionListener l) {
iconPanel.removeMouseMotionListener(l);
}
private void updatePos(Timelined timelined, MouseEvent lastMouseEvent, Timer thisTimer) {
if (timelined != null) {
BoundedTag bounded = (BoundedTag) timelined;
RECT rect = bounded.getRect();
int width = rect.getWidth();
double scale = 1.0;
/*if (width > swf.displayRect.getWidth()) {
scale = (double) swf.displayRect.getWidth() / (double) width;
}*/
Matrix m = Matrix.getTranslateInstance(-rect.Xmin, -rect.Ymin);
m.scale(scale);
Point p = lastMouseEvent == null ? null : lastMouseEvent.getPoint();
synchronized (ImagePanel.this) {
if (timer == thisTimer) {
cursorPosition = p;
}
}
}
}
private void showSelectedName() {
if (selectedDepth > -1 && frame > -1 && timelined != null) {
Frame f = timelined.getTimeline().getFrame(frame);
if (f == null) {
return;
}
DepthState ds = f.layers.get(selectedDepth);
if (ds != null) {
CharacterTag cht = ds.getCharacter();
if (cht != null) {
debugLabel.setText(cht.getName());
}
}
}
}
public void hideMouseSelection() {
if (selectedDepth > -1) {
showSelectedName();
} else {
debugLabel.setText(DEFAULT_DEBUG_LABEL_TEXT);
}
}
public ImagePanel() {
super(new BorderLayout());
//iconPanel.setHorizontalAlignment(JLabel.CENTER);
setOpaque(true);
setBackground(View.getDefaultBackgroundColor());
loop = true;
iconPanel = new IconPanel();
//labelPan.add(label, new GridBagConstraints());
add(iconPanel, BorderLayout.CENTER);
topPanel = new JPanel();
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
debugLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
topPanel.add(debugLabel);
DocumentListener documentListener = new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
applyPointsXY();
}
@Override
public void removeUpdate(DocumentEvent e) {
applyPointsXY();
}
@Override
public void changedUpdate(DocumentEvent e) {
applyPointsXY();
}
};
pointEditPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
pointEditPanel.add(new JLabel(AppStrings.translate("edit.points.x")));
pointXTextField = new JTextField(6);
pointXTextField.getDocument().addDocumentListener(documentListener);
pointEditPanel.add(pointXTextField);
pointEditPanel.add(new JLabel(AppStrings.translate("edit.points.y")));
pointYTextField = new JTextField(6);
pointYTextField.getDocument().addDocumentListener(documentListener);
pointEditPanel.add(pointYTextField);
pointEditPanel.setAlignmentX(JPanel.LEFT_ALIGNMENT);
topPanel.add(pointEditPanel);
pointEditPanel.setVisible(false);
add(topPanel, BorderLayout.NORTH);
horizontalScrollBar = new JScrollBar(JScrollBar.HORIZONTAL);
verticalScrollBar = new JScrollBar(JScrollBar.VERTICAL);
horizontalScrollBar.addAdjustmentListener(new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
if (updatingScrollBars) {
return;
}
updatingScrollBars = true;
double zoomDouble = ImagePanel.this.zoom.fit ? getZoomToFit() : ImagePanel.this.zoom.value;
updateScrollBarMinMax();
//horizontalScrollBar.setVisible(horizontalScrollBar.getVisibleAmount() < horizontalScrollBar.getMaximum() - horizontalScrollBar.getMinimum());
//verticalScrollBar.setVisible(verticalScrollBar.getVisibleAmount() < verticalScrollBar.getMaximum() - verticalScrollBar.getMinimum());
offsetPoint.setLocation(-(horizontalScrollBar.getValue()) * zoomDouble / SWF.unitDivisor, offsetPoint.getY());
iconPanel.calcRect();
_viewRect = getViewRect();
updatingScrollBars = false;
redraw();
}
});
verticalScrollBar.addAdjustmentListener(new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
if (updatingScrollBars) {
return;
}
updatingScrollBars = true;
double zoomDouble = ImagePanel.this.zoom.fit ? getZoomToFit() : ImagePanel.this.zoom.value;
updateScrollBarMinMax();
//horizontalScrollBar.setVisible(horizontalScrollBar.getVisibleAmount() < horizontalScrollBar.getMaximum() - horizontalScrollBar.getMinimum());
//verticalScrollBar.setVisible(verticalScrollBar.getVisibleAmount() < verticalScrollBar.getMaximum() - verticalScrollBar.getMinimum());
offsetPoint.setLocation(offsetPoint.getX(), -(verticalScrollBar.getValue()) * zoomDouble / SWF.unitDivisor);
iconPanel.calcRect();
_viewRect = getViewRect();
redraw();
updatingScrollBars = false;
}
});
add(horizontalScrollBar, BorderLayout.SOUTH);
add(verticalScrollBar, BorderLayout.EAST);
iconPanel.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
synchronized (ImagePanel.this) {
lastMouseEvent = e;
redraw();
}
}
@Override
public void mouseExited(MouseEvent e) {
synchronized (ImagePanel.this) {
lastMouseEvent = null;
hideMouseSelection();
redraw();
}
}
@Override
public void mousePressed(MouseEvent e) {
synchronized (ImagePanel.this) {
mouseButton = e.getButton();
lastMouseEvent = e;
redraw();
ButtonTag button = iconPanel.mouseOverButton;
if (button != null && freeTransformDepth == -1 && !frozen) {
DefineButtonSoundTag sounds = button.getSounds();
if (!muted && sounds != null && sounds.buttonSoundChar2 != 0) { // OverUpToOverDown
CharacterTag soundCharTag = swf.getCharacter(sounds.buttonSoundChar2);
if (soundCharTag instanceof SoundTag) {
playSound((SoundTag) soundCharTag, sounds.buttonSoundInfo2, timer);
}
}
List actions = new ArrayList<>();
if (button instanceof DefineButton2Tag) {
DefineButton2Tag button2 = (DefineButton2Tag) button;
for (BUTTONCONDACTION ca : button2.actions) {
if (ca.condOverUpToOverDown) { //press
actions.add(ca.actionBytes);
}
}
}
if (button instanceof DefineButtonTag) {
DefineButtonTag button1 = (DefineButtonTag) button;
actions.add(button1.actionBytes);
}
for (ByteArrayRange actionBytes : actions) {
try {
int prevLength = actionBytes.getPos();
SWFInputStream rri = new SWFInputStream(swf, actionBytes.getArray(), 0, prevLength + actionBytes.getLength());
if (prevLength != 0) {
rri.seek(prevLength);
}
execute(rri);
} catch (IOException ex) {
Logger.getLogger(ImagePanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
@Override
public void mouseReleased(MouseEvent e) {
synchronized (ImagePanel.this) {
mouseButton = 0;
lastMouseEvent = e;
redraw();
ButtonTag button = iconPanel.mouseOverButton;
if (!muted && button != null && freeTransformDepth == -1 && !frozen) {
DefineButtonSoundTag sounds = button.getSounds();
if (sounds != null && sounds.buttonSoundChar3 != 0) { // OverDownToOverUp
CharacterTag soundCharTag = swf.getCharacter(sounds.buttonSoundChar3);
if (soundCharTag instanceof SoundTag) {
playSound((SoundTag) soundCharTag, sounds.buttonSoundInfo3, timer);
}
}
}
}
}
});
iconPanel.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
synchronized (ImagePanel.this) {
lastMouseEvent = e;
redraw();
}
}
@Override
public void mouseDragged(MouseEvent e) {
synchronized (ImagePanel.this) {
lastMouseEvent = e;
redraw();
}
}
});
}
private synchronized void redraw() {
final Timer thisTimer = timer;
if (timelined == null) {
return;
}
if (thisTimer == null) {
startTimer(timelined.getTimeline(), false);
} else {
//if there is no frameloss (no frames waiting in the queue),
// then we can draw immediately to avoid long waiting between frames.
// This can happen on SWFs with small frameRate
if (Float.compare(getFrameLoss(), 0f) == 0) {
drawFrame(thisTimer, true);
}
}
}
public Timelined getTimelined() {
return timelined;
}
private void updateScrollBarMinMax() {
if (timelined == null) {
horizontalScrollBar.setVisible(false);
verticalScrollBar.setVisible(false);
return;
}
RECT timRect = timelined.getRect();
/*
int h_value = horizontalScrollBar.getValue();
int h_visibleAmount = horizontalScrollBar.getVisibleAmount();
*/
int h_maximum = timRect.Xmax;
if (hilightedPoints != null || freeTransformDepth > -1) {
h_maximum += SCROLL_SPACE_BEFORE;
}
/*if (h_value + h_visibleAmount > h_maximum) {
h_maximum = h_value + h_visibleAmount;
}*/
int h_minimum = timRect.Xmin;
if (hilightedPoints != null || freeTransformDepth > -1) {
h_minimum = timRect.Xmin > 0 ? 0 : timRect.Xmin;
h_minimum -= SCROLL_SPACE_BEFORE;
}
horizontalScrollBar.setMinimum(h_minimum);
horizontalScrollBar.setMaximum(h_maximum);
/*int v_value = verticalScrollBar.getValue();
int v_visibleAmount = verticalScrollBar.getVisibleAmount();
*/
int v_maximum = timRect.Ymax;
if (hilightedPoints != null || freeTransformDepth > -1) {
v_maximum += SCROLL_SPACE_BEFORE;
}
/*if (v_value + v_visibleAmount > v_maximum) {
v_maximum = v_value + v_visibleAmount;
}*/
int v_minimum = timRect.Ymin;
if (hilightedPoints != null || freeTransformDepth > -1) {
v_minimum = timRect.Ymin > 0 ? 0 : timRect.Ymin;
v_minimum -= SCROLL_SPACE_BEFORE;
}
verticalScrollBar.setMinimum(v_minimum);
verticalScrollBar.setMaximum(v_maximum);
horizontalScrollBar.setVisible(horizontalScrollBar.getVisibleAmount() < horizontalScrollBar.getMaximum() - horizontalScrollBar.getMinimum());
verticalScrollBar.setVisible(verticalScrollBar.getVisibleAmount() < verticalScrollBar.getMaximum() - verticalScrollBar.getMinimum());
}
private synchronized void updateScrollBars() {
if (!zoomAvailable) {
View.execInEventDispatchLater(new Runnable() {
@Override
public void run() {
horizontalScrollBar.setVisible(false);
verticalScrollBar.setVisible(false);
}
});
return;
}
View.execInEventDispatchLater(new Runnable() {
@Override
public void run() {
if (timelined == null) {
return;
}
updatingScrollBars = true;
double zoomDouble = ImagePanel.this.zoom.fit ? getZoomToFit() : ImagePanel.this.zoom.value;
RECT timRect = timelined.getRect();
int w = iconPanel.getWidth();
int h = iconPanel.getHeight();
int h_visibleAmount = (int) Math.round(w * SWF.unitDivisor / zoomDouble);
Point2D leftTop = toTransformPoint(new Point2D.Double(0, 0));
Point2D rightBottom = toTransformPoint(new Point2D.Double(w, h));
int h_value = (int) Math.round(leftTop.getX());
horizontalScrollBar.setVisibleAmount(h_visibleAmount);
horizontalScrollBar.setValue(h_value);
int v_visibleAmount = (int) Math.round(h * SWF.unitDivisor / zoomDouble);
int v_value = (int) Math.round(leftTop.getY());
verticalScrollBar.setVisibleAmount(v_visibleAmount);
verticalScrollBar.setValue(v_value);
updateScrollBarMinMax();
if (zoom.fit) {
verticalScrollBar.setVisible(false);
horizontalScrollBar.setVisible(false);
updatingScrollBars = false;
return;
}
boolean hVisibleBefore = horizontalScrollBar.isVisible();
horizontalScrollBar.setVisible(horizontalScrollBar.getVisibleAmount() < horizontalScrollBar.getMaximum() - horizontalScrollBar.getMinimum());
boolean hVisibleAfter = horizontalScrollBar.isVisible();
boolean vVisibleBefore = verticalScrollBar.isVisible();
verticalScrollBar.setVisible(verticalScrollBar.getVisibleAmount() < verticalScrollBar.getMaximum() - verticalScrollBar.getMinimum());
boolean vVisibleAfter = verticalScrollBar.isVisible();
if (hVisibleAfter != hVisibleBefore || vVisibleAfter != vVisibleBefore) {
updateScrollBars();
}
updatingScrollBars = false;
}
});
}
@Override
public synchronized void zoom(Zoom zoom) {
zoom(zoom, false, false);
}
public void zoomFit() {
Zoom z = new Zoom();
z.value = 1.0;
z.fit = true;
zoom(z, false, true);
}
private synchronized void zoom(Zoom zoom, boolean useCursor, boolean forced) {
if (!zoomAvailable) {
return;
}
double zoomDoubleBefore = this.zoom.fit ? getZoomToFit() : this.zoom.value;
boolean modified = this.zoom.value != zoom.value || this.zoom.fit != zoom.fit;
if (modified || forced) {
Point localCursorPosition = this.cursorPosition;
if (!useCursor || localCursorPosition == null) {
localCursorPosition = new Point(iconPanel.getWidth() / 2, iconPanel.getHeight() / 2);
}
Point2D cursorTransBefore = toTransformPoint(localCursorPosition);
ExportRectangle oldViewRect = new ExportRectangle(_viewRect);
this.zoom = zoom;
displayObjectCache.clear();
double zoomDouble = zoom.fit ? getZoomToFit() : zoom.value;
iconPanel.calcRect();
_viewRect = getViewRect();
Point2D cursorTransAfter = toTransformPoint(localCursorPosition);
int dx = (int) (((cursorTransAfter.getX() - cursorTransBefore.getX()) * zoomDouble) / SWF.unitDivisor);
int dy = (int) (((cursorTransAfter.getY() - cursorTransBefore.getY()) * zoomDouble) / SWF.unitDivisor);
offsetPoint.setLocation(offsetPoint.getX() + dx, offsetPoint.getY() + dy);
updateScrollBars();
iconPanel.calcRect();
_viewRect = getViewRect();
synchronized (lock) {
if (registrationPoint != null) {
//registrationPoint = new Point2D.Double(registrationPoint.getX() * zoomDouble / zoomDoubleBefore, registrationPoint.getY() * zoomDouble / zoomDoubleBefore);
}
}
redraw();
if (textTag != null) {
setText(textTag, newTextTag);
}
fireMediaDisplayStateChanged();
}
}
@Override
public synchronized BufferedImage printScreen() {
return iconPanel.getLastImage();
}
@Override
public synchronized double getZoomToFit() {
if (timelined != null) {
RECT bounds = timelined.getRect();
double w1 = bounds.getWidth() / SWF.unitDivisor;
double h1 = bounds.getHeight() / SWF.unitDivisor;
double w2 = iconPanel.getWidth();
double h2 = iconPanel.getHeight();
double w;
double h;
h = h1 * w2 / w1;
if (h > h2) {
w = w1 * h2 / h1;
} else {
w = w2;
}
if (w1 <= Double.MIN_NORMAL) {
return 1.0;
}
return (double) w / (double) w1;
}
return 1;
}
@Override
public synchronized boolean zoomAvailable() {
return zoomAvailable;
}
private Timer setTimelinedTimer = null;
public void setTimelined(final Timelined drawable, final SWF swf, int frame, boolean showObjectsUnderCursor, boolean autoPlay, boolean frozen, boolean alwaysDisplay, boolean muted, boolean mutable, boolean allowZoom) {
Stage stage = new Stage(drawable) {
@Override
public void callFrame(int frame) {
executeFrame(frame);
}
@Override
public Object callFunction(long functionAddress, long functionLength, List