mourning-ember / src / main / java / dev / llan / view / game / map / MapView.java
MapView.java
Raw
package dev.llan.view.game.map;

import dev.llan.controller.Controller;
import dev.llan.model.GameRecord;
import dev.llan.model.board.Point;
import dev.llan.view.RenderComponent;
import dev.llan.view.GameComponent;
import dev.llan.view.View;
import dev.llan.view.game.map.grid.GridDisplayManager;
import dev.llan.view.game.map.Direction.*;

import javafx.scene.Parent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;

import static dev.llan.view.game.map.Constants.*;

public class MapView implements RenderComponent, GameComponent {
  private final GridDisplayManager manager;

  private final StackPane gridContainer;
  private final ScoreView scoreView;

  private XScrollDirection xDirection = XScrollDirection.NONE;
  private YScrollDirection yDirection = YScrollDirection.NONE;

  public MapView(View view, Controller controller, Pane parent) {
    this.gridContainer = new StackPane();
    gridContainer.getStyleClass().add("map-background");
    initContainer(parent);

    scoreView = new ScoreView();

    manager = new GridDisplayManager(view, controller);

    gridContainer.getChildren().add(manager.getGrid());
    gridContainer.getChildren().add(scoreView.render());
  }

  public void initListeners(Pane parent) {
    parent.widthProperty().addListener(obs -> updateWidths(parent.getWidth()));
    parent.heightProperty().addListener(obs -> updateHeights(parent.getHeight()));
  }

  public StackPane getRoot() {
    return this.gridContainer;
  }

  private void initContainer(Pane parent) {
    this.gridContainer.minWidthProperty().bind(parent.widthProperty());
    this.gridContainer.minHeightProperty().bind(parent.heightProperty());

    this.gridContainer.maxWidthProperty().bind(parent.widthProperty());
    this.gridContainer.maxHeightProperty().bind(parent.heightProperty());

    this.gridContainer.prefWidthProperty().bind(parent.widthProperty());
    this.gridContainer.prefHeightProperty().bind(parent.heightProperty());
  }

  private void updateWidths(double width) {
    manager.updateWidth(width, true);
  }

  private void updateHeights(double height) {
    manager.updateHeight(height, true);
  }

  public void setViewPoint(Point point) {
    ViewPoint viewPoint = new ViewPoint(point);
    manager.setFocusPoint(viewPoint);
  }

  public void setXScrollDirection(XScrollDirection direction) {
    this.xDirection = direction;
  }

  public void setYScrollDirection(YScrollDirection direction) {
    this.yDirection = direction;
  }

  public void periodic() {
    if (xDirection != XScrollDirection.NONE) {
      manager.translateX(xDirection.getDirection() * SCROLL_SPEED);
    }
    if (yDirection != YScrollDirection.NONE) {
      manager.translateY(yDirection.getDirection() * SCROLL_SPEED);
    }
  }

  @Override
  public void updateGameRecord(GameRecord gameRecord) {
    manager.updateGameRecord(gameRecord);
    scoreView.updateGameRecord(gameRecord);
  }

  @Override
  public Parent render() {
    return gridContainer;
  }
}