Code cleanup

This commit is contained in:
SiboVG 2024-10-12 16:25:18 +02:00
parent 1ee09f2850
commit 3ac5953b94

View File

@ -590,44 +590,14 @@ public class FreeformFinSetConfig extends FinSetConfig {
Point2D.Double point = getCoordinates(event); Point2D.Double point = getCoordinates(event);
final FreeformFinSet finset = (FreeformFinSet) component; final FreeformFinSet finset = (FreeformFinSet) component;
// If shift is held down and a point is being dragged, constrain angle relative to previous or following point // If shift is held down, apply snapping
int lockIndex = -1;
int highlightIndex = -1; int highlightIndex = -1;
if ((mods & MouseEvent.SHIFT_DOWN_MASK) != 0) { if ((mods & MouseEvent.SHIFT_DOWN_MASK) != 0) {
if ((mods & MouseEvent.CTRL_DOWN_MASK) != 0) { int lockIndex = getLockIndex(mods);
if (dragIndex < finset.getFinPoints().length-1) { highlightIndex = getHighlightIndex(lockIndex);
lockIndex = dragIndex + 1;
highlightIndex = dragIndex;
}
}
else if (dragIndex > 0) {
lockIndex = dragIndex - 1;
highlightIndex = dragIndex - 1;
}
if (lockIndex >= 0) { if (lockIndex >= 0) {
// Fetch point to lock to point = snapPoint(point, finset.getFinPoints()[lockIndex]);
final Coordinate lockPoint = finset.getFinPoints()[lockIndex];
// Distances to vertical and horizontal lines
final double diffX = point.x - lockPoint.x;
final double diffY = point.y - lockPoint.y;
final double distanceX = Math.abs(diffX);
final double distanceY = Math.abs(diffY);
// Calculate distance to 45 or 135 degree line, as appropriate
final double a = 1; // always
final double b = (Math.signum(diffX) == Math.signum(diffY)) ? -1 : 1;
final double c = -(a*lockPoint.x + b*lockPoint.y);
final double distanceDiag = Math.abs(a*point.x + b*point.y + c) / Math.sqrt(2);
// Snap in the appropriate direction
if (distanceX <= distanceY && distanceX <= distanceDiag) // snap horizontal
point.x = lockPoint.x;
else if (distanceY <= distanceX && distanceY <= distanceDiag) // snap vertical
point.y = lockPoint.y;
else { // snap diagonal
point.x = (b*( b*point.x - a*point.y) - a*c) / 2;
point.y = (a*(-b*point.x + a*point.y) - b*c) / 2;
}
} }
} }
figure.setHighlightIndex(highlightIndex); figure.setHighlightIndex(highlightIndex);
@ -643,29 +613,59 @@ public class FreeformFinSetConfig extends FinSetConfig {
updateFields(); updateFields();
// if point is within borders of figure _AND_ outside borders of the ScrollPane's view: // Handle scrolling if point is dragged out of view
final Rectangle dragRectangle = viewport.getViewRect(); handleScrolling();
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()) { private int getLockIndex(int mods) {
hitBorder = true; if ((mods & MouseEvent.CTRL_DOWN_MASK) != 0) {
dragRectangle.y += dragPoint.y - figure.getBorderHeight(); return (dragIndex < ((FreeformFinSet) component).getFinPoints().length - 1) ? dragIndex + 1 : -1;
} else if(dragPoint.y >(dragRectangle.height -figure.getBorderHeight())) { } else {
hitBorder = true; return (dragIndex > 0) ? dragIndex - 1 : -1;
dragRectangle.y += dragPoint.y - (dragRectangle.height - figure.getBorderHeight()); }
} }
private int getHighlightIndex(int lockIndex) {
return (lockIndex == dragIndex + 1) ? dragIndex : lockIndex;
}
private Point2D.Double snapPoint(Point2D.Double point, Coordinate lockPoint) {
Point2D.Double snappedPoint = new Point2D.Double(point.x, point.y);
double diffX = point.x - lockPoint.x;
double diffY = point.y - lockPoint.y;
double distanceX = Math.abs(diffX);
double distanceY = Math.abs(diffY);
// Calculate distance to 45 or 135 degree line
double a = 1;
double b = (Math.signum(diffX) == Math.signum(diffY)) ? -1 : 1;
double c = -(a * lockPoint.x + b * lockPoint.y);
double distanceDiag = Math.abs(a * point.x + b * point.y + c) / Math.sqrt(2);
// Snap to the closest constraint
if (distanceX <= distanceY && distanceX <= distanceDiag) {
// Snap horizontal
snappedPoint.x = lockPoint.x;
} else if (distanceY <= distanceX && distanceY <= distanceDiag) {
// Snap vertical
snappedPoint.y = lockPoint.y;
} else {
// Snap diagonal (45 degrees)
double avgDist = (Math.abs(diffX) + Math.abs(diffY)) / 2;
snappedPoint.x = lockPoint.x + Math.signum(diffX) * avgDist;
snappedPoint.y = lockPoint.y + Math.signum(diffY) * avgDist;
}
return snappedPoint;
}
private void handleScrolling() {
Rectangle dragRectangle = viewport.getViewRect();
Point canvasPoint = new Point(dragPoint.x + dragRectangle.x, dragPoint.y + dragRectangle.y);
if (isPointWithinFigureBounds(canvasPoint)) {
boolean hitBorder = updateScrollPosition(dragRectangle);
if (hitBorder) { if (hitBorder) {
super.setFitting(false); super.setFitting(false);
selector.update(); selector.update();
@ -675,43 +675,31 @@ public class FreeformFinSetConfig extends FinSetConfig {
} }
} }
@Override private boolean isPointWithinFigureBounds(Point point) {
public void componentResized(ComponentEvent e) { return figure.getBorderWidth() < point.x && point.x < (figure.getWidth() - figure.getBorderWidth())
if (fit) { && figure.getBorderHeight() < point.y && point.y < (figure.getHeight() - figure.getBorderHeight());
// 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()){ private boolean updateScrollPosition(Rectangle dragRectangle) {
hitBorder = true; boolean hitBorder = false;
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()) { if (dragPoint.x < figure.getBorderWidth()) {
hitBorder = true; hitBorder = true;
dragRectangle.y += dragPoint.y - figure.getBorderHeight(); dragRectangle.x += dragPoint.x - figure.getBorderWidth();
} else if(dragPoint.y >(dragRectangle.height -figure.getBorderHeight())) { } else if (dragPoint.x > (dragRectangle.width - figure.getBorderWidth())) {
hitBorder = true; hitBorder = true;
dragRectangle.y += dragPoint.y - (dragRectangle.height - figure.getBorderHeight()); dragRectangle.x += dragPoint.x - (dragRectangle.width - figure.getBorderWidth());
}
if (hitBorder) {
super.setFitting(false);
selector.update();
figure.scrollRectToVisible(dragRectangle);
revalidate();
}
} }
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());
}
return hitBorder;
} }
@Override @Override