diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/playgame/PlayGameActions.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/playgame/PlayGameActions.java index 03053e3..aa52832 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/playgame/PlayGameActions.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/playgame/PlayGameActions.java @@ -3,4 +3,5 @@ public enum PlayGameActions { NEW_GAME, BACK, + UPLOAD_SAVE, } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/playgame/PlayGameController.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/playgame/PlayGameController.java index 22d9db4..4ca725c 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/playgame/PlayGameController.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/playgame/PlayGameController.java @@ -1,13 +1,29 @@ package edu.ntnu.idi.idatt2003.g40.mappe.view.playgame; +import edu.ntnu.idi.idatt2003.g40.mappe.model.SaveGame; +import edu.ntnu.idi.idatt2003.g40.mappe.service.SaveGameService; import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventManager; import edu.ntnu.idi.idatt2003.g40.mappe.view.ViewController; import edu.ntnu.idi.idatt2003.g40.mappe.view.ViewEnum; +import java.io.File; +import java.util.List; + +import javafx.scene.Scene; +import javafx.scene.control.Alert; +import javafx.scene.control.Alert.AlertType; +import javafx.stage.FileChooser; +import javafx.stage.Window; + /** * Controller for the {@link PlayGameView}. * *
Extends {@link ViewController}
+ * + *+ * Handles three user actions: starting a new game, going back to the + * main menu, and uploading a custom save file from disk. + *
*/ public class PlayGameController extends ViewControllerSets create new game, back, and save-row click functionality.
+ *Sets create new game, upload save, back, and save-row click + * functionality.
*/ @Override protected void initInteractions() { getViewElement().setOnAction(PlayGameActions.NEW_GAME, () -> - changeScene(ViewEnum.IN_GAME)); + changeScene(ViewEnum.IN_GAME)); getViewElement().setOnAction(PlayGameActions.BACK, () -> - changeScene(ViewEnum.MAIN_MENU)); + changeScene(ViewEnum.MAIN_MENU)); + + getViewElement().setOnAction(PlayGameActions.UPLOAD_SAVE, + this::handleUploadSave); getViewElement().setOnSaveSelected(save -> - changeScene(ViewEnum.IN_GAME)); + changeScene(ViewEnum.IN_GAME)); + } + + /** + * Opens a {@link FileChooser} for the user to pick a save file from + * disk. Parses it using {@link SaveGameService} and appends the + * loaded saves to the view's current list. + * + *+ * Shows an alert if the user picks a file that contains no valid + * save entries. + *
+ */ + private void handleUploadSave() { + FileChooser fileChooser = new FileChooser(); + fileChooser.setTitle("Choose save file"); + fileChooser.getExtensionFilters().addAll( + new FileChooser.ExtensionFilter("Save files (*.txt)", "*.txt"), + new FileChooser.ExtensionFilter("All files", "*.*") + ); + + Window window = getOwnerWindow(); + File selectedFile = fileChooser.showOpenDialog(window); + if (selectedFile == null) { + // User cancelled the dialog. + return; + } + + SaveGameService service = + new SaveGameService(selectedFile.getAbsolutePath()); + List- * Displays a list of existing save games and two action buttons: - * "Create new game" and "Back". + * Displays a list of existing save games and three action buttons: + * "Create new game", "Upload save" and "Back". *
* *
@@ -41,13 +41,16 @@ public class PlayGameView extends ViewElement Replaces any saves currently displayed.
+ * Used by the controller after the user uploads a save file from disk.
+ * Existing saves are kept; the new ones are appended at the bottom.
+ *