}
private void initMouseEventHandlers() {
getSkinnable().onMousePressedProperty().set((EventHandler<MouseEvent>) (MouseEvent event) -> {
final Node n = control;
final double parentScaleX = n.getParent().
localToSceneTransformProperty().getValue().getMxx();
final double parentScaleY = n.getParent().
localToSceneTransformProperty().getValue().getMyy();
mouseX = event.getSceneX();
mouseY = event.getSceneY();
nodeX = n.getLayoutX() * parentScaleX;
nodeY = n.getLayoutY() * parentScaleY;
if (control.isMoveToFront()) {
control.toFront();
}
});
//Event Listener for MouseDragged
getSkinnable().onMouseDraggedProperty().set((EventHandler<MouseEvent>) (MouseEvent event) -> {
final Node n = control;
final double parentScaleX = n.getParent().
localToSceneTransformProperty().getValue().getMxx();
final double parentScaleY = n.getParent().
localToSceneTransformProperty().getValue().getMyy();
final double scaleX = n.localToSceneTransformProperty().
getValue().getMxx();
final double scaleY = n.localToSceneTransformProperty().
getValue().getMyy();
Bounds boundsInScene =
control.localToScene(control.getBoundsInLocal());
double sceneX = boundsInScene.getMinX();
double sceneY = boundsInScene.getMinY();
double offsetX = event.getSceneX() - mouseX;
double offsetY = event.getSceneY() - mouseY;
if (resizeMode == ResizeMode.NONE && control.isMovable()) {
nodeX += offsetX;
nodeY += offsetY;
double scaledX = nodeX * 1 / parentScaleX;
double scaledY = nodeY * 1 / parentScaleY;
double offsetForAllX = scaledX - n.getLayoutX();
double offsetForAllY = scaledY - n.getLayoutY();
n.setLayoutX(scaledX);
n.setLayoutY(scaledY);
dragging = true;
} else {
double width = n.getBoundsInLocal().getMaxX()
- n.getBoundsInLocal().getMinX();
double height = n.getBoundsInLocal().getMaxY()
- n.getBoundsInLocal().getMinY();
if (RESIZE_TOP) {
// System.out.println("TOP");
double insetOffset = getSkinnable().getInsets().getTop() / 2;
double yDiff =
sceneY / parentScaleY
+ insetOffset
- event.getSceneY() / parentScaleY;
double newHeight = control.getPrefHeight() + yDiff;
if (newHeight > control.minHeight(0)) {
control.setLayoutY(control.getLayoutY() - yDiff);
control.setPrefHeight(newHeight);
}
}
if (RESIZE_LEFT) {
// System.out.println("LEFT");
double insetOffset = getSkinnable().getInsets().getLeft() / 2;
double xDiff = sceneX / parentScaleX
+ insetOffset
- event.getSceneX() / parentScaleX;
double newWidth = control.getPrefWidth() + xDiff;
if (newWidth > Math.max(control.minWidth(0),
control.getContentPane().minWidth(0))) {
control.setLayoutX(control.getLayoutX() - xDiff);
control.setPrefWidth(newWidth);
} else {
//
}
}
if (RESIZE_BOTTOM) {
// System.out.println("BOTTOM");
double insetOffset = getSkinnable().getInsets().getBottom() / 2;
double yDiff = event.getSceneY() / parentScaleY
- sceneY / parentScaleY - insetOffset;
double newHeight = yDiff;
newHeight = Math.max(
newHeight, control.minHeight(0));
if (newHeight < control.maxHeight(0)) {
control.setPrefHeight(newHeight);
}
}
if (RESIZE_RIGHT) {
double insetOffset = getSkinnable().getInsets().getRight() / 2;
double xDiff = event.getSceneX() / parentScaleX
- sceneX / parentScaleY - insetOffset;
double newWidth = xDiff;
newWidth = Math.max(
newWidth,
Math.max(control.getContentPane().minWidth(0),
control.minWidth(0)));
if (newWidth < control.maxWidth(0)) {
control.setPrefWidth(newWidth);
}
}
}
mouseX = event.getSceneX();
mouseY = event.getSceneY();
});
getSkinnable().onMouseClickedProperty().set((EventHandler<MouseEvent>) (MouseEvent event) -> {
dragging = false;
});
getSkinnable().onMouseMovedProperty().set((EventHandler<MouseEvent>) (MouseEvent t) -> {
if (control.isMinimized() || !control.isResizableWindow()) {
RESIZE_TOP = false;
RESIZE_LEFT = false;
RESIZE_BOTTOM = false;
RESIZE_RIGHT = false;
resizeMode = ResizeMode.NONE;
return;
}
final Node n = control;
final double parentScaleX = n.getParent().localToSceneTransformProperty().getValue().getMxx();
final double parentScaleY = n.getParent().localToSceneTransformProperty().getValue().getMyy();
final double scaleX = n.localToSceneTransformProperty().getValue().getMxx();
final double scaleY = n.localToSceneTransformProperty().getValue().getMyy();
final double border = control.getResizableBorderWidth() * scaleX;
double diffMinX = Math.abs(n.getLayoutBounds().getMinX() - t.getX() + getSkinnable().getInsets().getLeft());
double diffMinY = Math.abs(n.getLayoutBounds().getMinY() - t.getY() + getSkinnable().getInsets().getTop());
double diffMaxX = Math.abs(n.getLayoutBounds().getMaxX() - t.getX() - getSkinnable().getInsets().getRight());
double diffMaxY = Math.abs(n.getLayoutBounds().getMaxY() - t.getY() - getSkinnable().getInsets().getBottom());
boolean left = diffMinX * scaleX < Math.max(border, getSkinnable().getInsets().getLeft() / 2 * scaleX);
boolean top = diffMinY * scaleY < Math.max(border, getSkinnable().getInsets().getTop() / 2 * scaleY);
boolean right = diffMaxX * scaleX < Math.max(border, getSkinnable().getInsets().getRight() / 2 * scaleX);
boolean bottom = diffMaxY * scaleY < Math.max(border, getSkinnable().getInsets().getBottom() / 2 * scaleY);
RESIZE_TOP = false;
RESIZE_LEFT = false;
RESIZE_BOTTOM = false;
RESIZE_RIGHT = false;
if (left && !top && !bottom) {
n.setCursor(Cursor.W_RESIZE);
resizeMode = ResizeMode.LEFT;
RESIZE_LEFT = true;
} else if (left && top && !bottom) {
n.setCursor(Cursor.NW_RESIZE);
resizeMode = ResizeMode.TOP_LEFT;
RESIZE_LEFT = true;
RESIZE_TOP = true;
} else if (left && !top && bottom) {
n.setCursor(Cursor.SW_RESIZE);
resizeMode = ResizeMode.BOTTOM_LEFT;
RESIZE_LEFT = true;
RESIZE_BOTTOM = true;
} else if (right && !top && !bottom) {
n.setCursor(Cursor.E_RESIZE);
resizeMode = ResizeMode.RIGHT;
RESIZE_RIGHT = true;
} else if (right && top && !bottom) {
n.setCursor(Cursor.NE_RESIZE);
resizeMode = ResizeMode.TOP_RIGHT;
RESIZE_RIGHT = true;
RESIZE_TOP = true;
} else if (right && !top && bottom) {
n.setCursor(Cursor.SE_RESIZE);
resizeMode = ResizeMode.BOTTOM_RIGHT;
RESIZE_RIGHT = true;
RESIZE_BOTTOM = true;
} else if (top && !left && !right) {
n.setCursor(Cursor.N_RESIZE);
resizeMode = ResizeMode.TOP;
RESIZE_TOP = true;
} else if (bottom && !left && !right) {
n.setCursor(Cursor.S_RESIZE);
resizeMode = ResizeMode.BOTTOM;
RESIZE_BOTTOM = true;
} else {
n.setCursor(Cursor.DEFAULT);
resizeMode = ResizeMode.NONE;
}
control.autosize();
});