Skip to content

Commit 0c701a3

Browse files
committed
Regression in constraining the position was fixed two commits prior.
Code refactoring.
1 parent fc549b0 commit 0c701a3

1 file changed

Lines changed: 18 additions & 22 deletions

File tree

src/main/java/com/brunomnsilva/smartgraph/graphview/SmartGraphVertexNode.java

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,8 @@ public void moveFromForces() {
420420
double height = getParent().getLayoutBounds().getHeight();
421421
double width = getParent().getLayoutBounds().getWidth();
422422

423-
updatedPosition.x = boundVertexNodeXPositioning(updatedPosition.x, 0, width);
424-
updatedPosition.y = boundVertexNodeYPositioning(updatedPosition.y, 0, height);
423+
updatedPosition.x = constrainPositionX(updatedPosition.x, 0, width);
424+
updatedPosition.y = constrainPositionY(updatedPosition.y, 0, height);
425425

426426
setPosition(updatedPosition.x, updatedPosition.y);
427427
}
@@ -518,11 +518,11 @@ private void enableDrag() {
518518
}
519519

520520
double newX = mouseEvent.getX() + dragDelta.x;
521-
double x = boundVertexNodeXPositioning(newX, 0, getParent().getLayoutBounds().getWidth());
521+
double x = constrainPositionX(newX, 0, getParent().getLayoutBounds().getWidth());
522522
setCenterX(x);
523523

524524
double newY = mouseEvent.getY() + dragDelta.y;
525-
double y = boundVertexNodeYPositioning(newY, 0, getParent().getLayoutBounds().getHeight());
525+
double y = constrainPositionY(newY, 0, getParent().getLayoutBounds().getHeight());
526526
setCenterY(y);
527527

528528
mouseEvent.consume();
@@ -543,38 +543,34 @@ private void enableDrag() {
543543
}
544544

545545
/*
546-
* Bounds the positioning of this vertex node within bounds.
547-
* It takes into account the overall size of the node.
546+
* Constrains the x-positioning of the node in [min, max].
547+
* It takes into account the width of the vertex, including the label.
548548
*/
549-
private double boundVertexNodeXPositioning(double xCoord, double minCoordValue, double maxCoordValue) {
549+
private double constrainPositionX(double x, double min, double max) {
550550
// The shape and (possibly attached) label are centered, so its bounds are equals for each side
551551
double lengthToSide = Math.max(
552552
getRadius(),
553553
(attachedLabel != null ? attachedLabel.layoutWidthProperty().get()/2 : 0));
554554

555-
if (xCoord < minCoordValue + lengthToSide) {
556-
return minCoordValue + lengthToSide;
557-
} else if (xCoord > maxCoordValue - lengthToSide) {
558-
return maxCoordValue - lengthToSide;
559-
} else {
560-
return xCoord;
561-
}
555+
if (x < min + lengthToSide) {
556+
return min + lengthToSide;
557+
} else return Math.min(x, max - lengthToSide);
562558
}
563559

564-
private double boundVertexNodeYPositioning(double yCoord, double minCoordValue, double maxCoordValue) {
560+
/*
561+
* Constrains the y-positioning of the node in [min, max].
562+
* It takes into account the height size of the vertex, including the label.
563+
*/
564+
private double constrainPositionY(double y, double min, double max) {
565565
// The length to the top from the center point is the radius of the surrogate shape
566566
// The length to the bottom from the center point is the radius of the surrogate shape, plus the label offset and height
567567
double lengthToTop = getRadius();
568568
double lengthToBottom = getRadius() +
569569
(attachedLabel != null ? ATTACHED_LABEL_VERTICAL_OFFSET + attachedLabel.layoutHeightProperty().get() : 0);
570570

571-
if (yCoord < minCoordValue + lengthToTop) {
572-
return minCoordValue + lengthToTop;
573-
} else if (yCoord > maxCoordValue - lengthToBottom) {
574-
return maxCoordValue - lengthToBottom;
575-
} else {
576-
return yCoord;
577-
}
571+
if (y < min + lengthToTop) {
572+
return min + lengthToTop;
573+
} else return Math.min(y, max - lengthToBottom);
578574
}
579575

580576
/*

0 commit comments

Comments
 (0)