Merge pull request #737 from teyrana/fix/fffzoom

Fix/FreeformFinConfig - editing fin points
This commit is contained in:
Daniel Williams 2020-08-14 16:48:27 -04:00 committed by GitHub
commit 168fa689c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 239 additions and 96 deletions

View File

@ -24,7 +24,10 @@ public class FreeformFinSet extends FinSet {
private static final double SNAP_SMALLER_THAN = 5e-3;
private static final double IGNORE_SMALLER_THAN = 1e-12;
// attempts to set a fin value any larger than this will be snapped to this max value
public static final double SNAP_LARGER_THAN = 2.5; // in meters
public FreeformFinSet() {
points.add(Coordinate.ZERO);
points.add(new Coordinate(0.025, 0.05));
@ -164,6 +167,16 @@ public class FreeformFinSet extends FinSet {
newPoints = translatePoints( newPoints, delta);
}
for ( int i =0; i < newPoints.size(); ++i ) {
final Coordinate p = newPoints.get(i);
if( p.x > SNAP_LARGER_THAN){
newPoints.set(i, p.setX(SNAP_LARGER_THAN));
}
if( p.y > SNAP_LARGER_THAN){
newPoints.set(i, p.setY(SNAP_LARGER_THAN));
}
}
// copy the old points, in case validation fails
final ArrayList<Coordinate> pointsCopy = new ArrayList<>(this.points);
final double lengthCopy = this.length;
@ -203,14 +216,39 @@ public class FreeformFinSet extends FinSet {
* @param yRequest the y-coordinate.
*/
public void setPoint(final int index, final double xRequest, final double yRequest) {
final Coordinate revertPoint = points.get(index);
if(null != this.getParent()) {
final Coordinate prior = points.get(index);
points.set(index, new Coordinate(xRequest, yRequest));
if(null == this.getParent()) {
return;
}
if((points.size() - 1) == index){
clampLastPoint(xRequest-prior.x);
// if the new x,y would cause a fin larger than our max-size, limit the new request:
double xAccept = xRequest;
double yAccept = yRequest;
if(0 == index) {
final Coordinate cl = points.get(points.size() - 1);
double newLength = cl.x - xRequest;
if (newLength > SNAP_LARGER_THAN) {
xAccept = SNAP_LARGER_THAN - cl.x;
}
}else{
if (xAccept > SNAP_LARGER_THAN) {
xAccept = SNAP_LARGER_THAN;
}
if (yAccept > SNAP_LARGER_THAN) {
yAccept = SNAP_LARGER_THAN;
}
}
final Coordinate revertPoint = points.get(index);
points.set(index, new Coordinate(xAccept, yAccept));
if( IGNORE_SMALLER_THAN > Math.abs(revertPoint.x - xAccept) && IGNORE_SMALLER_THAN > Math.abs(revertPoint.y - yAccept) ){
// no-op. ignore
return;
}
if ((points.size() - 1) == index) {
clampLastPoint(xAccept - revertPoint.x);
}
update();

View File

@ -1,8 +1,11 @@
package net.sf.openrocket.gui.configdialog;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
@ -32,6 +35,7 @@ import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
import net.sf.openrocket.util.MathUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -74,9 +78,10 @@ public class FreeformFinSetConfig extends FinSetConfig {
private FinPointTableModel tableModel = null;
private int dragIndex = -1;
private Point dragPoint = null;
private FinPointFigure figure = null;
private ScaleSelector selector;
public FreeformFinSetConfig(OpenRocketDocument d, RocketComponent component) {
super(d, component);
@ -226,11 +231,11 @@ public class FreeformFinSetConfig extends FinSetConfig {
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent ev) {
figure.setSelectedIndex(table.getSelectedRow());
figure.updateFigure();
}
figure.setSelectedIndex(table.getSelectedRow());
figure.updateFigure();
}
});
});
JScrollPane tablePane = new JScrollPane(table);
JButton scaleButton = new JButton(trans.get("FreeformFinSetConfig.lbl.scaleFin"));
@ -270,7 +275,7 @@ public class FreeformFinSetConfig extends FinSetConfig {
importImage();
}
});
ScaleSelector selector = new ScaleSelector(figurePane);
selector = new ScaleSelector(figurePane);
// fit on first start-up
figurePane.setFitting(true);
@ -390,58 +395,134 @@ public class FreeformFinSetConfig extends FinSetConfig {
private class FinPointScrollPane extends ScaleScrollPane {
private static final int ANY_MASK = (MouseEvent.ALT_DOWN_MASK | MouseEvent.ALT_GRAPH_DOWN_MASK | MouseEvent.META_DOWN_MASK | MouseEvent.CTRL_DOWN_MASK | MouseEvent.SHIFT_DOWN_MASK);
private FinPointScrollPane( final FinPointFigure _figure) {
super( _figure);
private FinPointScrollPane(final FinPointFigure _figure) {
super(_figure);
}
@Override
public void mousePressed(MouseEvent event) {
int mods = event.getModifiersEx();
final FreeformFinSet finset = (FreeformFinSet)component;
final FreeformFinSet finset = (FreeformFinSet) component;
final int pressIndex = getPoint(event);
if ( pressIndex >= 0) {
if (pressIndex >= 0) {
dragIndex = pressIndex;
dragPoint = event.getPoint();
updateFields();
return;
}
final int segmentIndex = getSegment(event);
if (segmentIndex >= 0) {
Point2D.Double point = getCoordinates(event);
finset.addPoint(segmentIndex, point);
dragIndex = segmentIndex;
dragPoint = event.getPoint();
updateFields();
return;
}
super.mousePressed(event);
}
@Override
public void mouseDragged(MouseEvent event) {
int mods = event.getModifiersEx();
int mods = event.getModifiersEx();
if (dragIndex < 0 || (mods & (ANY_MASK | MouseEvent.BUTTON1_DOWN_MASK)) != MouseEvent.BUTTON1_DOWN_MASK) {
super.mouseDragged(event);
return;
}
Point2D.Double point = getCoordinates(event);
final FreeformFinSet finset = (FreeformFinSet)component;
Point2D.Double point = getCoordinates(event);
final FreeformFinSet finset = (FreeformFinSet) component;
finset.setPoint(dragIndex, point.x, point.y);
dragPoint.x = event.getX();
dragPoint.y = event.getY();
updateFields();
// if point is within borders of figure _AND_ outside borders of the ScrollPane's view:
final Rectangle dragRectangle = viewport.getViewRect();
final Point canvasPoint = new Point( dragPoint.x + dragRectangle.x, dragPoint.y + dragRectangle.y);
if( (figure.getBorderWidth() < canvasPoint.x) && (canvasPoint.x < (figure.getWidth() - figure.getBorderWidth()))
&& (figure.getBorderHeight() < canvasPoint.y) && (canvasPoint.y < (figure.getHeight() - figure.getBorderHeight())))
{
boolean hitBorder = false;
if(dragPoint.x < figure.getBorderWidth()){
hitBorder = true;
dragRectangle.x += dragPoint.x - figure.getBorderWidth();
} else if(dragPoint.x >(dragRectangle.width -figure.getBorderWidth())) {
hitBorder = true;
dragRectangle.x += dragPoint.x - (dragRectangle.width - figure.getBorderWidth());
}
if (dragPoint.y<figure.getBorderHeight()) {
hitBorder = true;
dragRectangle.y += dragPoint.y - figure.getBorderHeight();
} else if(dragPoint.y >(dragRectangle.height -figure.getBorderHeight())) {
hitBorder = true;
dragRectangle.y += dragPoint.y - (dragRectangle.height - figure.getBorderHeight());
}
if (hitBorder) {
super.setFitting(false);
selector.update();
figure.scrollRectToVisible(dragRectangle);
revalidate();
}
}
}
@Override
public void componentResized(ComponentEvent e) {
if (fit) {
// if we're fitting the whole figure in the ScrollPane, the parent behavior is fine
super.componentResized(e);
} else if (0 > dragIndex) {
// if we're not _currently_ dragging a point, the parent behavior is fine
super.componentResized(e);
} else {
// currently dragging a point.
// ... and if we drag out-of-bounds, we want to move the viewport to keep up
boolean hitBorder = false;
final Rectangle dragRectangle = viewport.getViewRect();
if(dragPoint.x<figure.getBorderWidth()){
hitBorder = true;
dragRectangle.x += dragPoint.x - figure.getBorderWidth();
} else if(dragPoint.x >(dragRectangle.width -figure.getBorderWidth())) {
hitBorder = true;
dragRectangle.x += dragPoint.x - (dragRectangle.width - figure.getBorderWidth());
}
if (dragPoint.y<figure.getBorderHeight()) {
hitBorder = true;
dragRectangle.y += dragPoint.y - figure.getBorderHeight();
} else if(dragPoint.y >(dragRectangle.height -figure.getBorderHeight())) {
hitBorder = true;
dragRectangle.y += dragPoint.y - (dragRectangle.height - figure.getBorderHeight());
}
if (hitBorder) {
super.setFitting(false);
selector.update();
figure.scrollRectToVisible(dragRectangle);
revalidate();
}
}
}
@Override
public void mouseReleased(MouseEvent event) {
dragIndex = -1;
dragPoint = null;
super.mouseReleased(event);
}
@ -453,7 +534,6 @@ public class FreeformFinSetConfig extends FinSetConfig {
if ( 0 < clickIndex) {
// if ctrl+click, delete point
try {
Point2D.Double point = getCoordinates(event);
final FreeformFinSet finset = (FreeformFinSet)component;
finset.removePoint(clickIndex);
} catch (IllegalFinPointException ignore) {

View File

@ -76,7 +76,10 @@ public abstract class AbstractScaleFigure extends JPanel {
setBackground(Color.WHITE);
setOpaque(true);
}
public int getBorderHeight(){ return borderThickness_px.height; }
public int getBorderWidth(){ return borderThickness_px.width; }
public double getUserScale(){
return userScale;
}
@ -106,12 +109,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 +122,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 +140,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);

View File

@ -52,7 +52,7 @@ public class FinPointFigure extends AbstractScaleFigure {
private final FreeformFinSet finset;
private int modID = -1;
protected BoundingBox finBounds_m = null;
protected BoundingBox mountBounds_m = null;
@ -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
@ -356,8 +346,10 @@ public class FinPointFigure extends AbstractScaleFigure {
assert (false) : "Should not occur";
return new Point2D.Double(0, 0);
}
p.setLocation(p.x, p.y);
p.x = MathUtil.clamp(p.x, -FreeformFinSet.SNAP_LARGER_THAN, FreeformFinSet.SNAP_LARGER_THAN);
p.y = MathUtil.clamp(p.y, -FreeformFinSet.SNAP_LARGER_THAN, FreeformFinSet.SNAP_LARGER_THAN);
return p;
}

View File

@ -9,11 +9,12 @@ import java.awt.Graphics2D;
import java.awt.Point;
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;
import java.awt.geom.Point2D;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
@ -43,7 +44,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;
@ -59,11 +60,13 @@ public class ScaleScrollPane extends JScrollPane
private Ruler verticalRuler;
// is the subject *currently* being fitting
private boolean fit = false;
protected boolean fit = false;
// magic number. I don't know why this number works, but this nudges the figures to zoom correctly.
// n.b. it is slightly large than the ruler.width + scrollbar.width
final Dimension viewportMarginPx = new Dimension( 40, 40);
final protected Dimension viewportMarginPx = new Dimension( 40, 40);
private Point2D.Double viewCenter_frac = new Point2D.Double(0.5f, 0.5f);
/**
* Create a scale scroll pane.
@ -106,9 +109,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 +117,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,31 +140,30 @@ 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();
}
public void setScaling(final double newScale) {
// match if closer than 1%:
if( MathUtil.equals(newScale, figure.getUserScale(), 0.01)){
return;
}
// match if closer than 1%:
if( MathUtil.equals(newScale, figure.getUserScale(), 0.01)){
return;
}
// if explicitly setting a zoom level, turn off fitting
final Rectangle viewRect = viewport.getViewRect();
viewCenter_frac = new Point2D.Double( (viewRect.getX() + viewRect.getWidth()/2) / figure.getWidth(),
(viewRect.getY() + viewRect.getHeight()/2) / figure.getHeight() );
// if explicitly setting a zoom level, turn off fitting
this.fit = false;
Dimension view = viewport.getExtentSize();
figure.scaleTo(newScale, view);
@ -259,9 +248,46 @@ 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);
}else{
final Rectangle scrollRectangle = viewport.getViewRect();
scrollRectangle.x = (int)(viewCenter_frac.x * figure.getWidth()) - (scrollRectangle.width/2);
scrollRectangle.y = (int)(viewCenter_frac.y * figure.getHeight()) - (scrollRectangle.height/2);
figure.scrollRectToVisible(scrollRectangle);
}
revalidate();
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 ////////////////

View File

@ -90,7 +90,7 @@ public class ScaleSelector extends JPanel {
scrollPane.getFigure().addChangeListener(new StateChangeListener() {
@Override
public void stateChanged(EventObject e) {
setZoomText();
update();
}
});
add(scaleSelector, "gap rel");
@ -103,7 +103,7 @@ public class ScaleSelector extends JPanel {
double scale = scrollPane.getUserScale();
scale = getNextSmallerScale(scale);
scrollPane.setScaling(scale);
setZoomText();
update();
}
});
add(button, "gapleft rel");
@ -157,4 +157,8 @@ public class ScaleSelector extends JPanel {
super.setEnabled(b);
}
public void update(){
setZoomText();
}
}