public JFXToggleButtonSkin(JFXToggleButton toggleButton) { super(toggleButton); // hide the togg le button toggleButton.setStyle("-fx-background-color:TRANSPARENT"); line = new Line(startX, startY, endX, startY); line.setStroke(unToggledColor); line.setStrokeWidth(1); circle = new Circle(startX - circleRadius, startY, circleRadius); circle.setFill(Color.TRANSPARENT); circle.setStroke(unToggledColor); circle.setStrokeWidth(strokeWidth); innerCircle = new Circle(startX - circleRadius, startY, 0); innerCircle.setStrokeWidth(0); StackPane circlePane = new StackPane(); circlePane.getChildren().add(circle); circlePane.getChildren().add(innerCircle); circlePane.setPadding(new Insets(15)); rippler = new JFXRippler(circlePane, RipplerMask.CIRCLE, RipplerPos.BACK); circles.getChildren().add(rippler); main.getChildren().add(line); main.getChildren().add(circles); main.setCursor(Cursor.HAND); AnchorPane.setTopAnchor(circles, -12.0); AnchorPane.setLeftAnchor(circles, -15.0); getSkinnable() .selectedProperty() .addListener( (o, oldVal, newVal) -> { rippler.setRipplerFill(newVal ? unToggledColor : toggledColor); transition.setRate(newVal ? 1 : -1); transition.play(); }); updateChildren(); }
public void init() { setOffsets(); Stage primaryStage = (Stage) getScene().getWindow(); mouseEventHandler = new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { CellView source = (CellView) event.getSource(); if (event.getEventType().equals(MouseEvent.MOUSE_PRESSED)) { if (event.isControlDown()) { int x = (int) Math.floor( (event.getScreenX() - primaryStage.getX() - dxProperty.get()) / cellSizeProperty.get()); int y = (int) Math.floor( (event.getScreenY() - primaryStage.getY() - dyProperty.get()) / cellSizeProperty.get()); for (int i = 0; i < pattern.length; i++) { for (int j = 0; j < pattern[0].length; j++) { try { if (cellViews[x + j][y + i].getState() != pattern[i][j]) cellViews[x + j][y + i].incrementState(); } catch (ArrayIndexOutOfBoundsException e) { } } } } else { source.incrementState(); currentState = source.getState(); } } else if (event.getEventType().equals(MouseEvent.MOUSE_DRAGGED)) { int x = (int) Math.floor( (event.getScreenX() - primaryStage.getX() - dxProperty.get()) / cellSizeProperty.get()); int y = (int) Math.floor( (event.getScreenY() - primaryStage.getY() - dyProperty.get()) / cellSizeProperty.get()); if (cellViews[x][y].getState() != currentState) cellViews[x][y].incrementState(); } } }; getChildren().clear(); for (int i = 0; i < grid.getWidth(); i++) { for (int j = 0; j < grid.getHeight(); j++) { CellView cellView = null; if ((i <= 1 || j <= 1) || (grid.getCell(i, j).getState() != 0)) { cellView = new CellView(grid.getCell(i, j)); cellView.widthProperty().bind(cellSizeProperty); cellView.heightProperty().bind(cellSizeProperty); cellView.addEventHandler(MouseEvent.ANY, mouseEventHandler); add(cellView, i, j); } cellViews[i + xOffset][j + yOffset] = cellView; totalCells--; } } timeline = new Timeline(new KeyFrame(new Duration(100), ((event) -> updateGrid()))); timeline.setCycleCount(Timeline.INDEFINITE); getScene() .setOnKeyPressed( (keyEvent) -> { if (keyEvent.getCode().equals(KeyCode.SPACE)) { if (playing) timeline.pause(); else timeline.play(); playing = !playing; } else if (keyEvent.getCode().equals(KeyCode.MINUS)) { if (keyEvent.isControlDown() && cellSizeProperty.get() > 2) zoomOut(); else if (playing && timeline.getRate() > 0.03125) timeline.setRate(timeline.getRate() / 2); } else if (keyEvent.getCode().equals(KeyCode.EQUALS)) { if (keyEvent.isControlDown() && cellSizeProperty.get() < 64) zoomIn(); else if (playing && timeline.getRate() < 16) timeline.setRate(timeline.getRate() * 2); } }); timeline.setRate(timelineRate); if (playing) timeline.play(); ((Stage) getScene().getWindow()).sizeToScene(); }