[fix][refactor] Added ability for AbstractScaleFigure to auto-zoom to something besides its entire bounds

This commit is contained in:
Daniel_M_Williams 2020-07-04 12:09:03 -04:00
parent f0269b5d91
commit 6561394f24
5 changed files with 40 additions and 29 deletions

View File

@ -30,14 +30,14 @@ public class PrintFigure extends RocketFigure {
}
public double getFigureHeight() {
return this.subjectBounds_m.getHeight();
return this.contentBounds_m.getHeight();
}
public double getFigureWidth() {
return this.subjectBounds_m.getWidth();
return this.contentBounds_m.getWidth();
}
public Rectangle2D getDimensions() {
return this.subjectBounds_m.getBounds2D();
return this.contentBounds_m.getBounds2D();
}
}

View File

@ -47,7 +47,10 @@ public abstract class AbstractScaleFigure extends JPanel {
protected Dimension visibleBounds_px = new Dimension(0,0);
// ======= whatever this figure is drawing, in real-space coordinates: meters
protected Rectangle2D subjectBounds_m = null;
// all drawable content
protected Rectangle2D contentBounds_m = new Rectangle2D.Double(0,0,0,0);
// the content we should focus on (this is the auto-zoom subject)
protected Rectangle2D subjectBounds_m = new Rectangle2D.Double(0,0,0,0);
// combines the translation and scale in one place:
// which frames does this transform between ?
@ -83,8 +86,13 @@ public abstract class AbstractScaleFigure extends JPanel {
}
public Point getSubjectOrigin() {
return originLocation_px;
}
return originLocation_px;
}
public Point getAutoZoomPoint(){
return new Point(Math.max(0, originLocation_px.x - borderThickness_px.width),
Math.max(0, - borderThickness_px.height));
}
/**
* Set the scale level of the figure. A scale value of 1.0 is equivalent to 100 % scale.
@ -149,9 +157,9 @@ public abstract class AbstractScaleFigure extends JPanel {
*/
protected void updateCanvasSize() {
final int desiredWidth = Math.max((int)this.visibleBounds_px.getWidth(),
(int)(subjectBounds_m.getWidth()*scale) + 2*borderThickness_px.width);
(int)(contentBounds_m.getWidth()*scale) + 2*borderThickness_px.width);
final int desiredHeight = Math.max((int)this.visibleBounds_px.getHeight(),
(int)(subjectBounds_m.getHeight()*scale) + 2*borderThickness_px.height);
(int)(contentBounds_m.getHeight()*scale) + 2*borderThickness_px.height);
Dimension preferredFigureSize_px = new Dimension(desiredWidth, desiredHeight);
@ -172,7 +180,7 @@ public abstract class AbstractScaleFigure extends JPanel {
* Updates the figure shapes and figure size.
*/
public void updateFigure() {
log.debug(String.format("____ Updating %s to: %g user scale, %g overall scale", this.getClass().getSimpleName(), this.getAbsoluteScale(), this.scale));
log.trace(String.format("____ Updating %s to: %g user scale, %g overall scale", this.getClass().getSimpleName(), this.getAbsoluteScale(), this.scale));
updateSubjectDimensions();
updateCanvasSize();
@ -182,10 +190,6 @@ public abstract class AbstractScaleFigure extends JPanel {
revalidate();
repaint();
}
protected Dimension getBorderPixels() {
return borderThickness_px;
}
public void addChangeListener(StateChangeListener listener) {
listeners.add(0, listener);

View File

@ -15,7 +15,6 @@ import java.awt.geom.Rectangle2D;
import java.util.LinkedList;
import java.util.List;
import org.slf4j.*;
import net.sf.openrocket.rocketcomponent.FreeformFinSet;
import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.rocketcomponent.SymmetricComponent;
@ -69,7 +68,7 @@ public class FinPointFigure extends AbstractScaleFigure {
setBackground(Color.WHITE);
setOpaque(true);
updateFigure();
updateFigure();
}
@Override
@ -359,6 +358,7 @@ public class FinPointFigure extends AbstractScaleFigure {
protected void updateSubjectDimensions(){
// update subject (i.e. Fin) bounds
finBounds_m = new BoundingBox().update(finset.getFinPoints());
subjectBounds_m = finBounds_m.toRectangle();
// update to bound the parent body:
SymmetricComponent parent = (SymmetricComponent)this.finset.getParent();
@ -373,22 +373,22 @@ public class FinPointFigure extends AbstractScaleFigure {
final BoundingBox combinedBounds = new BoundingBox().update(finBounds_m).update(mountBounds_m);
subjectBounds_m = combinedBounds.toRectangle();
contentBounds_m = combinedBounds.toRectangle();
}
@Override
protected void updateCanvasOrigin() {
final int finHeight = (int)(finBounds_m.span().y*scale);
final int mountHeight = (int)(mountBounds_m.span().y*scale);
final int finFrontPx = (int)(subjectBounds_m.getX()*scale);
final int subjectHeight = (int)(subjectBounds_m.getHeight()*scale);
final int finHeightPx = (int)(finBounds_m.span().y*scale);
final int mountHeightPx = (int)(mountBounds_m.span().y*scale);
final int finFrontPx = (int)(contentBounds_m.getX()*scale);
final int contentHeightPx = (int)(contentBounds_m.getHeight()*scale);
originLocation_px.x = borderThickness_px.width - finFrontPx;
if( visibleBounds_px.height > (subjectHeight+ 2*borderThickness_px.height)) {
originLocation_px.y = getHeight() - mountHeight - borderThickness_px.height;
if( visibleBounds_px.height > (contentHeightPx + 2*borderThickness_px.height)) {
originLocation_px.y = getHeight() - mountHeightPx - borderThickness_px.height;
}else {
originLocation_px.y = borderThickness_px.height + finHeight;
originLocation_px.y = borderThickness_px.height + finHeightPx;
}
}

View File

@ -58,7 +58,7 @@ public class RocketFigure extends AbstractScaleFigure {
public static final double SELECTED_WIDTH = 2.0;
private Rocket rocket;
final private Rocket rocket;
private RocketComponent[] selection = new RocketComponent[0];
@ -86,7 +86,7 @@ public class RocketFigure extends AbstractScaleFigure {
this.rotation = 0.0;
this.axialRotation = Transformation.rotate_x(0.0);
updateFigure();
}
@ -415,9 +415,8 @@ public class RocketFigure extends AbstractScaleFigure {
if(newBounds.isEmpty())
newBounds = new BoundingBox(Coordinate.ZERO,Coordinate.X_UNIT);
final double maxR = Math.max(Math.hypot(newBounds.min.y, newBounds.min.z),
Math.hypot(newBounds.max.y, newBounds.max.z));
final double maxR = Math.max( Math.hypot(newBounds.min.y, newBounds.min.z),
Math.hypot(newBounds.max.y, newBounds.max.z));
switch (currentViewType) {
case SideView:
@ -429,6 +428,8 @@ public class RocketFigure extends AbstractScaleFigure {
default:
throw new BugException("Illegal figure type = " + currentViewType);
}
// for a rocket, these are the same
contentBounds_m = subjectBounds_m;
}
/**
@ -444,7 +445,7 @@ public class RocketFigure extends AbstractScaleFigure {
if (currentViewType == RocketPanel.VIEW_TYPE.BackView){
final int newOriginX = borderThickness_px.width + Math.max(getWidth(), subjectWidth + 2*borderThickness_px.width)/ 2;
final int newOriginY = borderThickness_px.height + getHeight() / 2;
originLocation_px = new Point(newOriginX, newOriginY);
}else if (currentViewType == RocketPanel.VIEW_TYPE.SideView){
final int newOriginX = borderThickness_px.width - subjectFront;

View File

@ -14,6 +14,7 @@ import java.awt.event.ComponentEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Rectangle2D;
import java.util.EventObject;
import javax.swing.BorderFactory;
@ -152,6 +153,11 @@ public class ScaleScrollPane extends JScrollPane
Dimension view = viewport.getExtentSize();
figure.scaleTo(view);
final Point zoomPoint = figure.getAutoZoomPoint();
viewport.scrollRectToVisible( new Rectangle(
zoomPoint.x, zoomPoint.y, (int)(view.getWidth()), (int)(view.getHeight())));
this.firePropertyChange( USER_SCALE_PROPERTY, 1.0, figure.getUserScale());
revalidate();
}