HitoriMitMaven/src/main/java/GUI/HitoriDialogManager.java

116 lines
4.3 KiB
Java

package GUI;
import domain.HitoriBoardLoader;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Window;
import java.util.Optional;
public class HitoriDialogManager {
private final Window owner;
public HitoriDialogManager(Window owner) {
this.owner = owner;
}
public Optional<String> showBoardSelectionDialog(HitoriBoardLoader boardLoader) {
Dialog<String> dialog = new Dialog<>();
dialog.setTitle("Select Hitori Board");
dialog.setHeaderText("Choose a board to play:");
if (owner != null && owner.getScene() != null) {
dialog.initOwner(owner);
dialog.initModality(Modality.APPLICATION_MODAL);
}
ButtonType selectButtonType = new ButtonType("Play", ButtonBar.ButtonData.OK_DONE);
ButtonType randomButtonType = new ButtonType("Random", ButtonBar.ButtonData.OTHER);
dialog.getDialogPane().getButtonTypes().addAll(selectButtonType, randomButtonType, ButtonType.CANCEL);
ChoiceBox<String> boardChoice = new ChoiceBox<>();
boardChoice.getItems().addAll(boardLoader.getAvailableBoardNames());
if (!boardChoice.getItems().isEmpty()) {
boardChoice.setValue(boardChoice.getItems().get(0));
}
VBox content = new VBox(10);
content.getChildren().add(boardChoice);
dialog.getDialogPane().setContent(content);
dialog.setResultConverter(dialogButton -> {
if (dialogButton == selectButtonType) {
return boardChoice.getValue();
} else if (dialogButton == randomButtonType) {
int random = (int) (Math.random() * boardChoice.getItems().size());
return boardChoice.getItems().get(random);
}
return null;
});
return dialog.showAndWait();
}
public void showAlert(String title, String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.initOwner(owner);
alert.showAndWait();
}
public Optional<String> askForPlayerName() {
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle("High Score");
dialog.setHeaderText("Congratulations! Enter your name:");
dialog.setContentText("Name:");
dialog.initOwner(owner);
return dialog.showAndWait();
}
public boolean confirmDeleteHighScores() {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Delete High Scores");
alert.setHeaderText("Are you sure?");
alert.setContentText("This will permanently delete all high scores.");
alert.initOwner(owner);
Optional<ButtonType> result = alert.showAndWait();
return result.isPresent() && result.get() == ButtonType.OK;
}
public boolean confirmNewGame() {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Game Complete");
alert.setHeaderText("Would you like to start a new game?");
alert.setContentText("Choose whether to start a new game or continue viewing this one.");
ButtonType buttonTypeNew = new ButtonType("New Game");
ButtonType buttonTypeStay = new ButtonType("Stay Here", ButtonBar.ButtonData.CANCEL_CLOSE);
alert.getButtonTypes().setAll(buttonTypeNew, buttonTypeStay);
alert.initOwner(owner);
Optional<ButtonType> result = alert.showAndWait();
return result.isPresent() && result.get() == buttonTypeNew;
}
public boolean confirmLoadSavedGame() {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Saved Game Found");
alert.setHeaderText("Would you like to continue your saved game?");
alert.setContentText("Choose whether to load the saved game or start a new one.");
ButtonType buttonTypeYes = new ButtonType("Load Saved Game");
ButtonType buttonTypeNo = new ButtonType("Start New Game");
alert.getButtonTypes().setAll(buttonTypeYes, buttonTypeNo);
alert.initOwner(owner);
Optional<ButtonType> result = alert.showAndWait();
return result.isPresent() && result.get() == buttonTypeYes;
}
}