konkon / Cell.java
Cell.java
Raw
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;

public class Cell extends StackPane {
    private int cellID, x, y;
    public Cage cage;
    private ComboBox<Integer> comboBox = new ComboBox<>();
    private Label currentValueLabel;
    private int currentValue = 0;
    public Integer correctAnswer = null;
    private GameManager manager;
    public Background mistakeBG = new Background(new BackgroundFill(Color.rgb(240, 118, 147), CornerRadii.EMPTY, Insets.EMPTY)),
            normalBG = new Background(new BackgroundFill(Color.rgb(202, 181, 214), CornerRadii.EMPTY, Insets.EMPTY)),
            selectedBG = new Background(new BackgroundFill(Color.rgb(135, 95, 157), CornerRadii.EMPTY, Insets.EMPTY));


    public Cell(GameManager manager, int cellID, Cage cage) {
        this.cellID = cellID;
        this.cage = cage;
        this.manager = manager;
        comboBox.setEditable(true);
        comboBox.getEditor().setFont(Font.font("Courier New", 20));
        comboBox.setPromptText("Enter Answer");

        currentValueLabel = new Label("");
        currentValueLabel.setFont(Font.font("Courier New", FontWeight.BOLD, 35));
        currentValueLabel.setAlignment(Pos.CENTER);
        getChildren().add(currentValueLabel);

        this.setMinSize(50, 50);
        this.setAlignment(Pos.CENTER);
        this.setBackground(normalBG);

        setUpEventHandlers();
    }

    // handles updating the label of cells
    public void updateValueLabel() {
        if (this.currentValue == 0) {
            this.currentValueLabel.setText("");
        } else {
            this.currentValueLabel.setText(Integer.toString(currentValue));
        }
    }

    // clears the cell without making it error using runlater
    public void clearCell() {
        this.currentValue = 0;
        this.currentValueLabel.setText("");
        this.setBackground(normalBG);
        Platform.runLater(() -> this.comboBox.getEditor().clear());
    }

    // change graphics when deselecting cell
    public void unSelect() {
        this.getChildren().add(this.currentValueLabel);
        this.getChildren().remove(this.comboBox);
        this.setBackground(normalBG);
        if (manager.isShowMistakes()) {
            manager.checkAllCellsForMistakes();
        }
    }

    // change graphics when selecting a cell
    public void select() {
        this.getChildren().remove(this.currentValueLabel);
        this.getChildren().add(this.comboBox);
        this.setBackground(selectedBG);
    }

    // set up the choice box based on size of board
    public void fillComboBox() {
        for (int i = 0; i < manager.getN(); i++) {
            this.comboBox.getItems().add(i + 1);
        }
    }

    // just putting the event handlers here
    private void setUpEventHandlers() {
        comboBox.setOnAction(event -> {
            try {
                // errors for some reason unless i turn the int into a string then back again
                // definitely not a boxing issue
                currentValue = Integer.parseInt(String.valueOf(comboBox.getValue()));
                manager.checkGameWon();
            } catch (NumberFormatException e) {
                currentValue = 0;
            }
            updateValueLabel();
        });

        comboBox.getEditor().textProperty().addListener((observableValue, s, t1) -> {
            manager.logHistory(this);
            try {
                if (Integer.parseInt(t1) <= manager.getN() && Integer.parseInt(t1) > 0) {
                    currentValue = Integer.parseInt(t1);
                } else {
                    Platform.runLater(() -> comboBox.getEditor().clear());
                }
                manager.checkIfGameOver();
            } catch (NumberFormatException e) {
                currentValue = 0;
            }
            updateValueLabel();
        });

        this.setOnMouseClicked(event -> {
            if (this.manager.getCurrentlySelected() != null) {
                this.manager.getCurrentlySelected().unSelect();
                if (this.manager.getCurrentlySelected() != this) {
                    this.manager.setCurrentlySelected(this);
                    this.select();
                } else {
                    this.manager.setCurrentlySelected(null);
                }
            } else {
                this.manager.setCurrentlySelected(this);
                this.select();
            }
        });
    }

    public int getCellID() {
        return cellID;
    }

    public void setXY(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return this.x;
    }

    public int getY() {
        return this.y;
    }

    public Integer getCurrentValue() {
        return currentValue;
    }

    public void setCurrentValue(int x) {
        this.currentValue = x;
        this.comboBox.getEditor().setText(String.valueOf(x));
        if (currentValue != 0) {
            this.currentValueLabel.setText(Integer.toString(x));
        } else {
            this.currentValueLabel.setText("");
        }
    }

    public void setCorrectAnswer(int x) {
        this.correctAnswer = x;
    }

    public Label getCurrentValueLabel() {
        return currentValueLabel;
    }
}