From 492560cf89cb531acb366579c8feb1c091d05c54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jindra=20Pet=C5=99=C3=ADk?= Date: Thu, 15 Dec 2022 19:32:46 +0100 Subject: [PATCH] Apply Scale action --- .../flash/gui/BoundsChangeListener.java | 3 +- .../decompiler/flash/gui/ImagePanel.java | 59 ++++++++++++++----- .../decompiler/flash/gui/PreviewPanel.java | 6 +- .../decompiler/flash/gui/TransformPanel.java | 44 ++++++++++---- 4 files changed, 83 insertions(+), 29 deletions(-) diff --git a/src/com/jpexs/decompiler/flash/gui/BoundsChangeListener.java b/src/com/jpexs/decompiler/flash/gui/BoundsChangeListener.java index 4022a192b..d041c7cbc 100644 --- a/src/com/jpexs/decompiler/flash/gui/BoundsChangeListener.java +++ b/src/com/jpexs/decompiler/flash/gui/BoundsChangeListener.java @@ -16,6 +16,7 @@ */ package com.jpexs.decompiler.flash.gui; +import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; /** @@ -23,5 +24,5 @@ import java.awt.geom.Rectangle2D; * @author JPEXS */ public interface BoundsChangeListener { - public void boundsChanged(Rectangle2D newBounds); + public void boundsChanged(Rectangle2D newBounds, Point2D registraionPoint); } diff --git a/src/com/jpexs/decompiler/flash/gui/ImagePanel.java b/src/com/jpexs/decompiler/flash/gui/ImagePanel.java index 119748b6d..12efa742b 100644 --- a/src/com/jpexs/decompiler/flash/gui/ImagePanel.java +++ b/src/com/jpexs/decompiler/flash/gui/ImagePanel.java @@ -265,9 +265,9 @@ public final class ImagePanel extends JPanel implements MediaDisplay { boundsChangeListeners.remove(listener); } - private void fireBoundsChange(Rectangle2D bounds) { + private void fireBoundsChange(Rectangle2D bounds, Point2D registrationPoint) { for (BoundsChangeListener listener:boundsChangeListeners) { - listener.boundsChanged(bounds); + listener.boundsChanged(bounds, registrationPoint); } } @@ -328,7 +328,7 @@ public final class ImagePanel extends JPanel implements MediaDisplay { } } - private Matrix getNewToImageMatrix(MATRIX newMatrix) { + private Matrix getNewToImageMatrix(Matrix newMatrix) { Matrix m = new Matrix(); double zoomDouble = zoom.fit ? getZoomToFit() : zoom.value; if (lowQuality) { @@ -338,10 +338,10 @@ public final class ImagePanel extends JPanel implements MediaDisplay { m.translate(-_viewRect.xMin * zoom, -_viewRect.yMin * zoom); m.scale(zoom); - return Matrix.getScaleInstance(1 / SWF.unitDivisor).concatenate(m).concatenate(new Matrix(newMatrix)); + return Matrix.getScaleInstance(1 / SWF.unitDivisor).concatenate(m).concatenate(newMatrix); } - public MATRIX getNewMatrix() { + public Matrix getNewMatrix() { synchronized (lock) { DepthState ds = null; Timeline timeline = timelined.getTimeline(); @@ -364,7 +364,7 @@ public final class ImagePanel extends JPanel implements MediaDisplay { m2.scale(zoom); Matrix eMatrix = Matrix.getScaleInstance(1 / SWF.unitDivisor).concatenate(m2).inverse(); - return transform.preConcatenate(eMatrix).toMATRIX(); + return transform.preConcatenate(eMatrix); } } } @@ -412,7 +412,8 @@ public final class ImagePanel extends JPanel implements MediaDisplay { transform = Matrix.getScaleInstance(1 / SWF.unitDivisor).concatenate(m).concatenate(new Matrix(ds.matrix)); - fireBoundsChange(getTransformBounds()); + Rectangle2D transformBounds = getTransformBounds(); + fireBoundsChange(transformBounds, new Point2D.Double(transformBounds.getCenterX(), transformBounds.getCenterY())); /*System.out.println("ds.matrix=" + ds.matrix); System.out.println("transform=" + transform); System.out.println("offset=" + offsetPoint); @@ -688,7 +689,7 @@ public final class ImagePanel extends JPanel implements MediaDisplay { registrationPoint = new Point2D.Double(registrationPointUpdated.getX(), registrationPointUpdated.getY()); transform = new Matrix(transformUpdated); transformUpdated = null; - fireBoundsChange(getTransformBounds()); + fireBoundsChange(getTransformBounds(), getTransformRegistrationPoint()); } repaint(); } @@ -3055,16 +3056,45 @@ public final class ImagePanel extends JPanel implements MediaDisplay { } public void applyTransformMatrix(Matrix matrix) { - Matrix m = new Matrix(getNewMatrix()); + System.out.println("apply "+matrix); + Matrix prevNewMatrix = getNewMatrix(); + Matrix m = prevNewMatrix; m = m.preConcatenate(matrix); - transform = getNewToImageMatrix(m.toMATRIX()); + Matrix prevTransform = transform; + Point2D transRegistrationPoint = getTransformRegistrationPoint(); + transform = getNewToImageMatrix(m); Point2D newRegistrationPoint = new Point2D.Double(); - matrix.toTransform().transform(registrationPoint, newRegistrationPoint); + matrix.toTransform().transform(transRegistrationPoint, newRegistrationPoint); + newRegistrationPoint = toImageRegistrationPoint(newRegistrationPoint); registrationPoint = newRegistrationPoint; redraw(); - fireBoundsChange(getTransformBounds()); + fireBoundsChange(getTransformBounds(), getTransformRegistrationPoint()); + } + + private Point2D getTransformRegistrationPoint() { + double zoomDouble = zoom.fit ? getZoomToFit() : zoom.value; + if (lowQuality) { + zoomDouble /= LQ_FACTOR; + } + double rx = (registrationPoint.getX() - _rect.x) * SWF.unitDivisor / zoomDouble; + double ry = (registrationPoint.getY() - _rect.y) * SWF.unitDivisor / zoomDouble; + Point2D ret = new Point2D.Double(rx, ry); + return ret; + } + + private Point2D toImageRegistrationPoint(Point2D registrationPoint) { + double zoomDouble = zoom.fit ? getZoomToFit() : zoom.value; + if (lowQuality) { + zoomDouble /= LQ_FACTOR; + } + + double rx = registrationPoint.getX() * zoomDouble / SWF.unitDivisor + _rect.x; // + offsetXRef.getVal(); + double ry = registrationPoint.getY() * zoomDouble / SWF.unitDivisor + _rect.y; // + offsetYRef.getVal(); + + Point2D ret = new Point2D.Double(rx, ry); + return ret; } private Rectangle2D getTransformBounds() { @@ -3095,11 +3125,10 @@ public final class ImagePanel extends JPanel implements MediaDisplay { drawableFrameCount = 1; } - Matrix b = new Matrix(getNewMatrix()).concatenate(new Matrix(ds.matrix).inverse()); + Matrix b = getNewMatrix().concatenate(new Matrix(ds.matrix).inverse()); int dframe = time % drawableFrameCount; Shape outline = dt.getOutline(dframe, time, ds.ratio, renderContext, b.concatenate(new Matrix(ds.matrix)), true); - Rectangle bounds = outline.getBounds(); - return new Rectangle2D.Double(bounds.x, bounds.y, bounds.width, bounds.height); + return outline.getBounds2D(); } } } diff --git a/src/com/jpexs/decompiler/flash/gui/PreviewPanel.java b/src/com/jpexs/decompiler/flash/gui/PreviewPanel.java index 8fd40c44b..e1a82b930 100644 --- a/src/com/jpexs/decompiler/flash/gui/PreviewPanel.java +++ b/src/com/jpexs/decompiler/flash/gui/PreviewPanel.java @@ -1176,9 +1176,9 @@ public class PreviewPanel extends JPersistentSplitPane implements TagEditorPanel private void savePlaceTagButtonActionPerformed(ActionEvent evt) { if (placeEditMode == PLACE_EDIT_TRANSFORM) { - MATRIX matrix = placeImagePanel.getNewMatrix(); + Matrix matrix = placeImagePanel.getNewMatrix(); placeTag.setPlaceFlagHasMatrix(true); - placeTag.setMatrix(matrix); + placeTag.setMatrix(matrix.toMATRIX()); placeTag.setModified(true); placeImagePanel.selectDepth(placeTag.getDepth()); placeImagePanel.freeTransformDepth(-1); @@ -1242,7 +1242,7 @@ public class PreviewPanel extends JPersistentSplitPane implements TagEditorPanel } private void saveImageTransformButtonActionPerformed(ActionEvent evt) { - Matrix matrix = new Matrix(imagePanel.getNewMatrix()); + Matrix matrix = imagePanel.getNewMatrix(); imageTransformPanel.setVisible(false); imagePanel.freeTransformDepth(-1); diff --git a/src/com/jpexs/decompiler/flash/gui/TransformPanel.java b/src/com/jpexs/decompiler/flash/gui/TransformPanel.java index f86058534..b3894593d 100644 --- a/src/com/jpexs/decompiler/flash/gui/TransformPanel.java +++ b/src/com/jpexs/decompiler/flash/gui/TransformPanel.java @@ -29,6 +29,7 @@ import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; +import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.text.DecimalFormat; import javax.swing.BorderFactory; @@ -79,6 +80,7 @@ public class TransformPanel extends JPanel { private ImagePanel imagePanel; private Rectangle2D bounds = new Rectangle2D.Double(0, 0, 1, 1); + private Point2D registraionPoint = new Point2D.Double(0, 0); public static enum UnitKind { LENGTH, @@ -86,7 +88,7 @@ public class TransformPanel extends JPanel { } public static enum Unit { - PX("px", 1/20.0, UnitKind.LENGTH), + PX("px", 1 / 20.0, UnitKind.LENGTH), TWIP("twip", 1.0, UnitKind.LENGTH), PERCENT("%", 0.0, UnitKind.LENGTH), TURN("turn", 1 / 360.0, UnitKind.ANGLE), @@ -128,8 +130,8 @@ public class TransformPanel extends JPanel { imagePanel.addBoundsChangeListener(new BoundsChangeListener() { @Override - public void boundsChanged(Rectangle2D newBounds) { - update(newBounds); + public void boundsChanged(Rectangle2D newBounds, Point2D registraionPoint) { + update(newBounds, registraionPoint); } }); setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); @@ -212,7 +214,7 @@ public class TransformPanel extends JPanel { scaleWidthTextField.addFocusListener(new FocusAdapter() { @Override public void focusLost(FocusEvent e) { - if (scaleProportionallyCheckBox.isSelected()) { + if (scaleProportionallyCheckBox.isSelected()) { try { double scaleWidth = Double.parseDouble(scaleWidthTextField.getText()); double scaleHeight; @@ -242,8 +244,8 @@ public class TransformPanel extends JPanel { scaleWidth = scaleHeight; } else { double ratio = bounds.getWidth() / bounds.getHeight(); - scaleWidth = ratio * scaleHeight; - } + scaleWidth = ratio * scaleHeight; + } scaleWidthTextField.setText(formatDouble(scaleWidth)); scaleHeightTextField.setText(formatDouble(scaleHeight)); } catch (NumberFormatException nfe) { @@ -340,8 +342,9 @@ public class TransformPanel extends JPanel { clearMatrixActionPerformed(null); } - private void update(Rectangle2D bounds) { + private void update(Rectangle2D bounds, Point2D registraionPoint) { this.bounds = bounds; + this.registraionPoint = registraionPoint; if (!moveRelativeCheckBox.isSelected()) { moveHorizontalTextField.setText(formatDouble(convertUnit(bounds.getX(), Unit.TWIP, (Unit) moveUnitComboBox.getSelectedItem()))); moveVerticalTextField.setText(formatDouble(convertUnit(bounds.getY(), Unit.TWIP, (Unit) moveUnitComboBox.getSelectedItem()))); @@ -369,15 +372,15 @@ public class TransformPanel extends JPanel { private void applyMoveActionPerformed(ActionEvent e) { Matrix matrix = new Matrix(); try { - double moveHorizontal = convertUnit(Double.parseDouble(moveHorizontalTextField.getText()), (Unit)moveUnitComboBox.getSelectedItem(), Unit.TWIP); - double moveVertical = convertUnit(Double.parseDouble(moveVerticalTextField.getText()), (Unit)moveUnitComboBox.getSelectedItem(), Unit.TWIP); + double moveHorizontal = convertUnit(Double.parseDouble(moveHorizontalTextField.getText()), (Unit) moveUnitComboBox.getSelectedItem(), Unit.TWIP); + double moveVertical = convertUnit(Double.parseDouble(moveVerticalTextField.getText()), (Unit) moveUnitComboBox.getSelectedItem(), Unit.TWIP); if (!moveRelativeCheckBox.isSelected()) { matrix.translate(-bounds.getX(), -bounds.getY()); } matrix.translate(moveHorizontal, moveVertical); imagePanel.applyTransformMatrix(matrix); } catch (NumberFormatException nfe) { - + } } @@ -389,7 +392,28 @@ public class TransformPanel extends JPanel { } private void applyScaleActionPerformed(ActionEvent e) { + try { + double scaleWidth = Double.parseDouble(scaleWidthTextField.getText()); + double scaleHeight = Double.parseDouble(scaleHeightTextField.getText()); + Unit scaleUnit = (Unit) scaleUnitComboBox.getSelectedItem(); + double scaleWidthFactor; + double scaleHeightFactor; + if (scaleUnit == Unit.PERCENT) { + scaleWidthFactor = scaleWidth / 100.0; + scaleHeightFactor = scaleHeight / 100.0; + } else { + scaleWidthFactor = convertUnit(scaleWidth, scaleUnit, Unit.TWIP) / bounds.getWidth(); + scaleHeightFactor = convertUnit(scaleHeight, scaleUnit, Unit.TWIP) / bounds.getHeight(); + } + Matrix matrix = new Matrix(); + matrix.translate(registraionPoint.getX(), registraionPoint.getY()); + matrix.scale(scaleWidthFactor, scaleHeightFactor); + matrix.translate(-registraionPoint.getX(), -registraionPoint.getY()); + imagePanel.applyTransformMatrix(matrix); + } catch (NumberFormatException nfe) { + + } } private void clearRotateActionPerformed(ActionEvent e) {