示例#1
0
  public void zoomIn() {
    int width = grid.getWidth();
    int height = grid.getHeight();
    grid.cropLeft(width / 4);
    grid.cropRight(width / 4);
    grid.cropTop(height / 4);
    grid.cropBottom(height / 4);
    cellSizeProperty.set(cellSizeProperty.get() * 2);

    timelineRate = timeline.getRate();
    init();
    runner.setGrid(grid);
  }
示例#2
0
  public void zoomOut() {
    int width = grid.getWidth();
    int height = grid.getHeight();
    grid.padLeft(width / 2);
    grid.padRight(width / 2);
    grid.padTop(height / 2);
    grid.padBottom(height / 2);
    cellSizeProperty.set(cellSizeProperty.get() / 2);

    timelineRate = timeline.getRate();
    init();
    runner.setGrid(grid);
  }
示例#3
0
  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();
  }