From ae83846e4e31acf86a7524c070af9dede0b11504 Mon Sep 17 00:00:00 2001 From: Daniel_M_Williams Date: Sat, 8 Aug 2020 16:50:17 -0400 Subject: [PATCH] [fix] restored autozoom behavior on figures -- particularly FinPointFigure --- .../gui/scalefigure/AbstractScaleFigure.java | 14 ++-- .../gui/scalefigure/FinPointFigure.java | 12 +-- .../gui/scalefigure/ScaleScrollPane.java | 76 +++++++++++-------- 3 files changed, 54 insertions(+), 48 deletions(-) diff --git a/swing/src/net/sf/openrocket/gui/scalefigure/AbstractScaleFigure.java b/swing/src/net/sf/openrocket/gui/scalefigure/AbstractScaleFigure.java index e9519cce4..3c479d074 100644 --- a/swing/src/net/sf/openrocket/gui/scalefigure/AbstractScaleFigure.java +++ b/swing/src/net/sf/openrocket/gui/scalefigure/AbstractScaleFigure.java @@ -106,12 +106,12 @@ public abstract class AbstractScaleFigure extends JPanel { * and the figures internal contents are centered according to the figure's origin. * * @param newScaleRequest the scale level - * @param visibleBounds the visible bounds upon the Figure + * @param newVisibleBounds the visible bounds upon the Figure */ - public void scaleTo(final double newScaleRequest, final Dimension visibleBounds) { + public void scaleTo(final double newScaleRequest, final Dimension newVisibleBounds) { if (MathUtil.equals(this.userScale, newScaleRequest, 0.01) && - (visibleBounds_px.width == visibleBounds.width) && - (visibleBounds_px.height == visibleBounds.height) ) + (visibleBounds_px.width == newVisibleBounds.width) && + (visibleBounds_px.height == newVisibleBounds.height) ) { return;} if (Double.isInfinite(newScaleRequest) || Double.isNaN(newScaleRequest) || 0 > newScaleRequest) { @@ -119,8 +119,10 @@ public abstract class AbstractScaleFigure extends JPanel { this.userScale = MathUtil.clamp( newScaleRequest, MINIMUM_ZOOM, MAXIMUM_ZOOM); this.scale = baseScale * userScale; + updateCanvasOrigin(); - this.visibleBounds_px = visibleBounds; + this.visibleBounds_px = newVisibleBounds; + updateCanvasSize(); this.fireChangeEvent(); } @@ -135,8 +137,6 @@ public abstract class AbstractScaleFigure extends JPanel { return; updateSubjectDimensions(); - updateCanvasSize(); - updateCanvasOrigin(); final double width_scale = (visibleBounds.width - 2 * borderThickness_px.width) / (subjectBounds_m.getWidth() * baseScale); final double height_scale = (visibleBounds.height - 2 * borderThickness_px.height) / (subjectBounds_m.getHeight() * baseScale); diff --git a/swing/src/net/sf/openrocket/gui/scalefigure/FinPointFigure.java b/swing/src/net/sf/openrocket/gui/scalefigure/FinPointFigure.java index d2d6fda78..a4a26c9e1 100644 --- a/swing/src/net/sf/openrocket/gui/scalefigure/FinPointFigure.java +++ b/swing/src/net/sf/openrocket/gui/scalefigure/FinPointFigure.java @@ -73,17 +73,7 @@ public class FinPointFigure extends AbstractScaleFigure { @Override public Point getAutoZoomPoint(){ - // from canvas top/left - final Point zoomPointPx = new Point( Math.max(0, (originLocation_px.x - borderThickness_px.width)), 0); - -// System.err.println("==>> FinPointFigure.getAutoZoomPoint ==>> " + finset.getName() ); -// System.err.println(String.format(" ::scale(overall): %6.4f == %6.4f x %6.4f", scale, userScale, baseScale)); -// System.err.println(String.format(" ::ContentBounds(px): @ %d, %d [ %d x %d ]", (int)(contentBounds_m.getX()*scale), (int)(contentBounds_m.getY()*scale), (int)(contentBounds_m.getWidth()*scale), (int)(contentBounds_m.getHeight()*scale))); -// System.err.println(String.format(" ::SubjectBounds(px): @ %d, %d [ %d x %d ]", (int)(subjectBounds_m.getX()*scale), (int)(subjectBounds_m.getY()*scale), (int)(subjectBounds_m.getWidth()*scale), (int)(subjectBounds_m.getHeight()*scale))); -// System.err.println(String.format(" ::origin: @ %d, %d", originLocation_px.x, originLocation_px.y)); -// System.err.println(String.format(" ::ZoomPoint: @ %d, %d", zoomPointPx.x, zoomPointPx.y)); - - return zoomPointPx; + return new Point( Math.max(0, (originLocation_px.x - borderThickness_px.width)), 0); } @Override diff --git a/swing/src/net/sf/openrocket/gui/scalefigure/ScaleScrollPane.java b/swing/src/net/sf/openrocket/gui/scalefigure/ScaleScrollPane.java index 0af71d84e..60ee611b4 100644 --- a/swing/src/net/sf/openrocket/gui/scalefigure/ScaleScrollPane.java +++ b/swing/src/net/sf/openrocket/gui/scalefigure/ScaleScrollPane.java @@ -2,6 +2,7 @@ package net.sf.openrocket.gui.scalefigure; import java.awt.Color; +import java.awt.Component; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; @@ -11,6 +12,7 @@ import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; +import java.awt.event.ComponentListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; @@ -43,7 +45,7 @@ import net.sf.openrocket.util.MathUtil; */ @SuppressWarnings("serial") public class ScaleScrollPane extends JScrollPane - implements MouseListener, MouseMotionListener { + implements ComponentListener, MouseListener, MouseMotionListener { public static final int RULER_SIZE = 20; public static final int MINOR_TICKS = 3; @@ -106,9 +108,6 @@ public class ScaleScrollPane extends JScrollPane getVerticalScrollBar().setUnitIncrement(50); //getVerticalScrollBar().setBlockIncrement(viewport.getHeight()); // the default value is good - viewport.addMouseListener(this); - viewport.addMouseMotionListener(this); - figure.addChangeListener( e -> { horizontalRuler.updateSize(); verticalRuler.updateSize(); @@ -117,21 +116,11 @@ public class ScaleScrollPane extends JScrollPane figure.scaleTo(calculatedViewSize); } }); - - viewport.addComponentListener(new ComponentAdapter() { - @Override - public void componentResized(ComponentEvent e) { - if(fit) { - final Dimension calculatedViewSize = new Dimension(getWidth() - viewportMarginPx.width, getHeight() - viewportMarginPx.height); - figure.scaleTo(calculatedViewSize); - } - figure.updateFigure(); + figure.addComponentListener(this); - horizontalRuler.updateSize(); - verticalRuler.updateSize(); - } - }); - + viewport.addMouseListener(this); + viewport.addMouseMotionListener(this); + viewport.addComponentListener(this); } public AbstractScaleFigure getFigure() { @@ -150,20 +139,15 @@ public class ScaleScrollPane extends JScrollPane */ public void setFitting(final boolean shouldFit) { this.fit = shouldFit; + if (shouldFit) { - validate(); - - Dimension view = viewport.getExtentSize(); - figure.scaleTo(view); - - final Point zoomPoint = figure.getAutoZoomPoint(); - final Rectangle zoomRectangle = new Rectangle(zoomPoint.x, zoomPoint.y, (int)(view.getWidth()), (int)(view.getHeight())); - figure.scrollRectToVisible(zoomRectangle); + final Dimension calculatedViewSize = new Dimension(getWidth() - viewportMarginPx.width, getHeight() - viewportMarginPx.height); + figure.scaleTo(calculatedViewSize); revalidate(); } } - + public double getUserScale() { return figure.getUserScale(); } @@ -259,9 +243,41 @@ public class ScaleScrollPane extends JScrollPane @Override public void mouseMoved(MouseEvent e) { } - - - + + @Override + public void componentResized(ComponentEvent e) { + if(fit) { + final Dimension calculatedViewSize = new Dimension(getWidth() - viewportMarginPx.width, getHeight() - viewportMarginPx.height); + figure.scaleTo(calculatedViewSize); + + final Point zoomPoint = figure.getAutoZoomPoint(); + final Rectangle zoomRectangle = new Rectangle(zoomPoint.x, zoomPoint.y, viewport.getWidth(), viewport.getHeight() ); + figure.scrollRectToVisible(zoomRectangle); + + revalidate(); + } +// figure.updateFigure(); + + horizontalRuler.updateSize(); + verticalRuler.updateSize(); + } + + @Override + public void componentMoved(ComponentEvent e) { + + } + + @Override + public void componentShown(ComponentEvent e) { + + } + + @Override + public void componentHidden(ComponentEvent e) { + + } + + //////////////// The view port rulers ////////////////