I've made a class for a draggable pane but I don't know how to integrate it into scene builder, I've previously added libraries to it like IKonli and JFoenix but since I've made this one I'm not sure how to go about adding it to scene builder- I've already tried using every action in the scenebuilder library manager but none worked, I've also done some research about it and have found nothing to work with.
Code:
import javafx.geometry.Point2D;
import javafx.geometry.Rectangle2D;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import org.joml.Vector2i;
public class Drag extends StackPane {
public Rectangle2D dragArea;
public Region parent;
public Point2D cursor;
public Vector2i cursorOffset;
public Drag(Region parent) {
this.parent = parent;
setDragArea();
setOnMousePressed(event -> {
if (event.getButton() != MouseButton.MIDDLE) {
setDragArea();
Point2D cursor = getNewCursor(event);
setCursor(cursor);
setCursorOffset();
toFront();
} else {
((Pane) this.getParent()).getChildren().remove(this);
}
});
setOnMouseDragged(event -> {
if (event.getButton() != MouseButton.MIDDLE) {
Point2D cursor = getNewCursor(event);
Vector2i velocity = getVelocity(cursor);
if (Math.abs(getCursorOffset().x - getNewCursorOffset().x) < 10) {
int futureX = getLayoutX() + getWidth() + velocity.x > parent.getWidth() ? (int) ((int) parent.getWidth() - getWidth()) : (int) Math.max(0, (getLayoutX() + velocity.x));
setLayoutX(futureX);
} else if (parent.contains(cursor) && Math.abs(getCursorOffset().x - getNewCursorOffset().x) < 10) {
setLayoutX(getLayoutX() - getCursorOffset().x);
}
if (Math.abs(getCursorOffset().y - getNewCursorOffset().y) < 10) {
int futureY = getLayoutY() + getHeight() + velocity.y > parent.getHeight() ? (int) ((int) parent.getHeight() - getHeight()) : (int) Math.max(0, (getLayoutY() + velocity.y));
setLayoutY(futureY);
} else if (parent.contains(cursor) && Math.abs(getCursorOffset().y - getNewCursorOffset().y) < 10) {
setLayoutY(getLayoutY() - getCursorOffset().y);
}
setCursor(cursor);
}
});
}
public void setCursorOffset() {
cursorOffset = getNewCursorOffset();
}
public Vector2i getCursorOffset() {
return cursorOffset;
}
public Vector2i getNewCursorOffset() {
return new Vector2i((int) ((cursor.getX() - getLayoutX())-parent.getLayoutX()), (int) (cursor.getY() - getLayoutY()-parent.getLayoutX()));
}
public Vector2i getVelocity(Point2D newCursor) {
return new Vector2i((int) (newCursor.getX()-getCurrentCursor().getX()), (int) (newCursor.getY()-getCurrentCursor().getY()));
}
public void setDragArea() {
dragArea = new Rectangle2D(parent.getLayoutX(), parent.getLayoutX(), parent.getWidth(), parent.getHeight());
}
public void setCursor(Point2D cursor) {
this.cursor = cursor;
}
public Point2D getCurrentCursor() {
return cursor;
}
public Point2D getNewCursor(MouseEvent event) {
int x = (int) event.getSceneX();
int y = (int) event.getSceneY();
return new Point2D(x,y);
}
}