1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| public class CornerButton extends Button {
private static final String STYLE_CLASS = "corner-button";
private final ImageView topLeft = new ImageView();
private final ImageView bottomRight = new ImageView();
public CornerButton() {
super();
setup();
}
public CornerButton(String text) {
super(text);
setup();
}
public CornerButton(String text, Node graphic) {
super(text, graphic);
setup();
}
private final void setup() {
topLeft.setId("topLeft");
topLeft.getStyleClass().add("top-left");
bottomRight.setId("bottomRight");
bottomRight.getStyleClass().add("bottom-right");
getStyleClass().add(STYLE_CLASS);
getStylesheets().add(getClass().getResource("cornerbutton.css").toExternalForm());
// Permet de laisser setOnMousePressed() disponible pour une utilisation externe.
addEventFilter(MouseEvent.MOUSE_PRESSED, mouseEvent -> animateIn());
// Permet de laisser setOnMouseReleased() disponible pour une utilisation externe.
addEventFilter(MouseEvent.MOUSE_RELEASED, mouseEvent -> animateOut());
}
@Override
protected void layoutChildren() {
super.layoutChildren();
if (!getChildren().contains(topLeft)) {
getChildren().addAll(topLeft, bottomRight);
}
final double width = getWidth();
final double height = getHeight();
//
final double topLeftWidth = topLeft.getBoundsInLocal().getWidth();
final double topLeftHeight = topLeft.getBoundsInLocal().getHeight();
layoutInArea(topLeft, 0, 0, topLeftWidth, topLeftHeight, 0, HPos.LEFT, VPos.TOP);
//
final double bottomRightWidth = bottomRight.getBoundsInLocal().getWidth();
final double bottonRightHeight = bottomRight.getBoundsInLocal().getHeight();
layoutInArea(bottomRight, width - bottomRightWidth, height - bottonRightHeight, bottomRightWidth, bottonRightHeight, 0, HPos.LEFT, VPos.TOP);
}
private static final int CORNER_OFFSET = 10;
private static final Duration CORNER_ANIMATION_DURATION = Duration.millis(150);
private final ParallelTransition cornersAnimation = new ParallelTransition();
private void animateIn() {
if (cornersAnimation != null) {
cornersAnimation.stop();
cornersAnimation.getChildren().clear();
}
final TranslateTransition moveTopLeft = new TranslateTransition(CORNER_ANIMATION_DURATION, topLeft);
moveTopLeft.setToX(CORNER_OFFSET);
moveTopLeft.setToY(CORNER_OFFSET);
//
final TranslateTransition moveBottomRight = new TranslateTransition(CORNER_ANIMATION_DURATION, bottomRight);
moveBottomRight.setToX(-CORNER_OFFSET);
moveBottomRight.setToY(-CORNER_OFFSET);
//
cornersAnimation.getChildren().setAll(moveTopLeft, moveBottomRight);
cornersAnimation.playFromStart();
}
private void animateOut() {
if (cornersAnimation != null) {
cornersAnimation.stop();
cornersAnimation.getChildren().clear();
}
final TranslateTransition moveTopLeft = new TranslateTransition(CORNER_ANIMATION_DURATION, topLeft);
moveTopLeft.setToX(0);
moveTopLeft.setToY(0);
//
final TranslateTransition moveBottomRight = new TranslateTransition(CORNER_ANIMATION_DURATION, bottomRight);
moveBottomRight.setToX(0);
moveBottomRight.setToY(0);
//
cornersAnimation.getChildren().setAll(moveTopLeft, moveBottomRight);
cornersAnimation.playFromStart();
}
} |
Partager