diff --git a/src/main/java/edu/ntnu/idatt2001/group_30/paths/PathsSingleton.java b/src/main/java/edu/ntnu/idatt2001/group_30/paths/PathsSingleton.java index 1c98afb3100fed235f8265f4132a6f4918cab934..1604b3eef4c86e16da6dff6b020a51530dc21a22 100644 --- a/src/main/java/edu/ntnu/idatt2001/group_30/paths/PathsSingleton.java +++ b/src/main/java/edu/ntnu/idatt2001/group_30/paths/PathsSingleton.java @@ -3,6 +3,8 @@ package edu.ntnu.idatt2001.group_30.paths; import edu.ntnu.idatt2001.group_30.paths.model.Player; import edu.ntnu.idatt2001.group_30.paths.model.Story; import edu.ntnu.idatt2001.group_30.paths.model.goals.*; + +import java.io.File; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; @@ -20,6 +22,7 @@ public enum PathsSingleton { INSTANCE; private Story story; + private File storyFile; private Player player = new Player("Default", 100, 100, 100); private boolean passageMoving = false; private HealthGoal healthGoal; @@ -44,18 +47,52 @@ public enum PathsSingleton { this.story = story; } + /** + * This method retrieves the file to the story. + * @return The file of the story, given as a File object. + */ + public File getStoryFile() { + return storyFile; + } + + /** + * This method changes the file of the story. + * @param storyFile New file, given as a File object. + */ + public void setStoryFile(File storyFile) { + this.storyFile = storyFile; + } + + /** + * This method retrieves the player. + * @return Current player of game, given as a Player object. + */ public Player getPlayer() { return player; } + /** + * This method sets the player to a new player. + * @param player New player, given as a Player object. + */ public void setPlayer(Player player) { this.player = player; } + /** + * This method allows a goal to be changed, given the goal type. + * @param newGoal New goal, given as a Goal implemented Object + */ public void changeGoal(Goal<?> newGoal) { setGoal(GoalType.getGoalType(newGoal.getClass().getSimpleName()), newGoal); } + /** + * This method sets a given goal to a new goal by the goal type. + * @param goalType Type of goal, given as a GoalType enum. + * @param goal New goal, given as a Goal object. + * @param <T> The type of Goal. + */ public <T> void setGoal(GoalType goalType, Goal<?> goal) { switch (goalType) { case HEALTH_GOAL -> healthGoal = (HealthGoal) goal; @@ -66,22 +103,44 @@ public enum PathsSingleton { } } + /** + * This method retrieves the health goal. + * @return Health goal, given as a HealthGoal object. + */ public HealthGoal getHealthGoal() { return healthGoal; } + /** + * This method retrieves the score goal. + * @return Score goal, given as a ScoreGoal object. + */ public ScoreGoal getScoreGoal() { return scoreGoal; } + /** + * This method retrieves the inventory goal. + * @return Inventory goal, given as a InventoryGoal object. + */ public InventoryGoal getInventoryGoal() { return inventoryGoal; } + /** + * This method retrieves the gold goal. + * @return Gold goal, given as a GoldGoal object. + */ public GoldGoal getGoldGoal() { return goldGoal; } + /** + * This method gets a goal variable given the GoalType. + * @param goalType Type of goal, given as a GoalType enum. + * @return The goal variable. + * @param <T> Type of goal. + */ public <T> Goal<?> getGoal(GoalType goalType) { return switch (goalType) { case HEALTH_GOAL -> healthGoal; @@ -91,10 +150,18 @@ public enum PathsSingleton { }; } + /** + * This method retrieves the character load out image. + * @return Image of the character created, given as an ImageView object. + */ public ImageView getCharacterImageView() { return characterImageView; } + /** + * This method sets the character load out. + * @param characterImageView New character image, given as an ImageView object. + */ public void setCharacterImageView(ImageView characterImageView) { this.characterImageView = characterImageView; } diff --git a/src/main/java/edu/ntnu/idatt2001/group_30/paths/controller/NewGameController.java b/src/main/java/edu/ntnu/idatt2001/group_30/paths/controller/NewGameController.java index eabf7f2c5fda315f0ef06fce95f797f0250293d7..e92647caf318e0310961557fd40f7d086403c501 100644 --- a/src/main/java/edu/ntnu/idatt2001/group_30/paths/controller/NewGameController.java +++ b/src/main/java/edu/ntnu/idatt2001/group_30/paths/controller/NewGameController.java @@ -18,6 +18,7 @@ public class NewGameController extends Controller { StoryFileHandler storyFileHandler = new StoryFileHandler(); try { INSTANCE.setStory(storyFileHandler.readStoryFromFile(storyFile)); + INSTANCE.setStoryFile(storyFile); } catch (IOException | InstantiationException ex) { throw new RuntimeException(ex); } diff --git a/src/main/java/edu/ntnu/idatt2001/group_30/paths/controller/NewStoryController.java b/src/main/java/edu/ntnu/idatt2001/group_30/paths/controller/NewStoryController.java index 6ab5ce9e8c5584dc1a868b9d8667d105abe73771..1d1a7eee8568f7c1b78c77479f2caacaf6187308 100644 --- a/src/main/java/edu/ntnu/idatt2001/group_30/paths/controller/NewStoryController.java +++ b/src/main/java/edu/ntnu/idatt2001/group_30/paths/controller/NewStoryController.java @@ -1,10 +1,43 @@ package edu.ntnu.idatt2001.group_30.paths.controller; +import edu.ntnu.idatt2001.group_30.paths.model.Passage; +import edu.ntnu.idatt2001.group_30.paths.model.Story; +import edu.ntnu.idatt2001.group_30.paths.model.filehandling.StoryFileHandler; +import edu.ntnu.idatt2001.group_30.paths.view.components.pop_up.AlertDialog; import edu.ntnu.idatt2001.group_30.paths.view.views.NewStoryView; +import javafx.stage.FileChooser; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +import static edu.ntnu.idatt2001.group_30.paths.PathsSingleton.INSTANCE; public class NewStoryController extends Controller{ public NewStoryController() { super(NewStoryView.class); } + + public void addStory(String title, List<Passage> passages) throws IOException { + Story story = new Story(title, passages.isEmpty() ? null: passages.get(0)); + passages.forEach(story::addPassage); + INSTANCE.setStory(story); + + saveStory(story); + } + + public void saveStory(Story story) throws IOException { + FileChooser fileChooser = new FileChooser(); + fileChooser.setInitialDirectory(new File("./src/main/resources/story-files")); + fileChooser.getExtensionFilters().add( + new FileChooser.ExtensionFilter("Paths files", "*.paths") + ); + File selectedFile = fileChooser.showSaveDialog(null); + + if (selectedFile != null) { + StoryFileHandler fileHandler = new StoryFileHandler(); + fileHandler.createStoryFile(story, selectedFile); + } + } } diff --git a/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/views/LoadGameView.java b/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/views/LoadGameView.java index bb2b445cc2a9d2278fb20dd14c4dbdedecfdb73e..8131982eb317ffeae4bffeaa46a78966f492d937 100644 --- a/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/views/LoadGameView.java +++ b/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/views/LoadGameView.java @@ -4,7 +4,7 @@ import static edu.ntnu.idatt2001.group_30.paths.PathsSingleton.INSTANCE; import edu.ntnu.idatt2001.group_30.paths.controller.NewGameController; import edu.ntnu.idatt2001.group_30.paths.controller.StageManager; -import edu.ntnu.idatt2001.group_30.paths.view.StoryDisplay; +import edu.ntnu.idatt2001.group_30.paths.view.components.StoryDisplay; import edu.ntnu.idatt2001.group_30.paths.view.components.common.DefaultButton; import edu.ntnu.idatt2001.group_30.paths.view.components.pop_up.AlertDialog; import java.io.File; @@ -32,6 +32,9 @@ public class LoadGameView extends View<BorderPane> { private BorderPane titlePane; private VBox buttonVBox; private Button startButton; + private Button loadButton; + private Button newButton; + private HBox buttonBox; /** * The constructor of the View class. @@ -88,10 +91,10 @@ public class LoadGameView extends View<BorderPane> { titlePane.setTop(title); BorderPane.setAlignment(title, Pos.TOP_CENTER); - Button loadButton = new Button("Load"); - Button newButton = new Button("New"); + loadButton = new Button("Load"); + newButton = new Button("New"); - HBox buttonBox = new HBox(10, loadButton, newButton); + buttonBox = new HBox(10, loadButton, newButton); buttonBox.setAlignment(Pos.CENTER); buttonVBox = new VBox(buttonBox); buttonVBox.setAlignment(Pos.CENTER); @@ -108,29 +111,7 @@ public class LoadGameView extends View<BorderPane> { if (selectedFile != null) { try { newGameController.setStory(selectedFile); - VBox storyVBox = new StoryDisplay.Builder(INSTANCE.getStory()) - .addStoryName() - .addFileInfo(selectedFile) - .build(); - storyVBox.setAlignment(Pos.CENTER); - - Button pencilButton = createIconButton("/images/pencil.png", 16, 16); - Button xButton = createIconButton("/images/remove.png", 16, 16); - - HBox buttonIcons = new HBox(10, pencilButton, xButton); - buttonIcons.setAlignment(Pos.CENTER); - - VBox storyContainer = new VBox(storyVBox, buttonIcons); - storyContainer.setAlignment(Pos.CENTER); - - xButton.setOnAction(event -> { - titlePane.getChildren().remove(storyContainer); - titlePane.setCenter(buttonBox); - startButton.setVisible(false); - }); - - titlePane.setCenter(storyContainer); - startButton.setVisible(true); + addStoryPane(); } catch (RuntimeException runtimeException) { AlertDialog.showError(runtimeException.getMessage()); } catch (IOException ex) { @@ -141,6 +122,7 @@ public class LoadGameView extends View<BorderPane> { newButton.setOnAction(newGameController.goTo(NewStoryView.class)); + this.titlePane = titlePane; return titlePane; } @@ -157,4 +139,36 @@ public class LoadGameView extends View<BorderPane> { } return button; } + + private void addStoryPane() throws IOException { + VBox storyVBox = new StoryDisplay.Builder(INSTANCE.getStory()) + .addStoryName() + .addFileInfo(INSTANCE.getStoryFile()) + .build(); + storyVBox.setAlignment(Pos.CENTER); + + Button pencilButton = createIconButton("/images/pencil.png", 16, 16); + Button xButton = createIconButton("/images/remove.png", 16, 16); + + HBox buttonIcons = new HBox(10, pencilButton, xButton); + buttonIcons.setAlignment(Pos.CENTER); + + VBox storyContainer = new VBox(storyVBox, buttonIcons); + storyContainer.setAlignment(Pos.CENTER); + + pencilButton.setOnAction(event -> { + StageManager.getInstance().setCurrentView(new NewStoryView()); + }); + + xButton.setOnAction(event -> { + titlePane.getChildren().remove(storyContainer); + titlePane.setCenter(buttonBox); + startButton.setVisible(false); + + INSTANCE.setStory(null); + }); + + titlePane.setCenter(storyContainer); + startButton.setVisible(true); + } } diff --git a/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/views/NewStoryView.java b/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/views/NewStoryView.java index f7fabdf58af9091c8803032110fc33aea100f198..f84736ff170ad7c4a8677174e0d876416a79e35e 100644 --- a/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/views/NewStoryView.java +++ b/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/views/NewStoryView.java @@ -1,6 +1,7 @@ package edu.ntnu.idatt2001.group_30.paths.view.views; import edu.ntnu.idatt2001.group_30.paths.controller.NewStoryController; +import edu.ntnu.idatt2001.group_30.paths.controller.StageManager; import edu.ntnu.idatt2001.group_30.paths.model.Link; import edu.ntnu.idatt2001.group_30.paths.model.Passage; import edu.ntnu.idatt2001.group_30.paths.model.Story; @@ -36,7 +37,7 @@ import java.util.stream.Collectors; public class NewStoryView extends View<BorderPane> { private final NewStoryController newStoryController; - private String title; + private String title = ""; private Story story; @@ -50,14 +51,18 @@ public class NewStoryView extends View<BorderPane> { story = INSTANCE.getStory(); } - passages = story == null ? FXCollections.observableArrayList() : (ObservableList<Passage>) story.getPassages(); + if(story != null) title = story.getTitle(); + + passages = story == null ? FXCollections.observableArrayList() : + FXCollections.observableArrayList(story.getPassages()); Text titleText = new Text("Create a new/edit a Story"); Text labelText = new Text("Story Title: "); - TextField textField = new TextField(story == null ? "" : story.getTitle()); + TextField textField = new TextField(title); textField.setPromptText("Enter story title"); + HBox titleBox = new HBox(labelText, textField); textField.setOnKeyTyped(event -> { @@ -96,33 +101,15 @@ public class NewStoryView extends View<BorderPane> { saveButton.setOnAction(event -> { try { - //TODO: add this logic to the controller instead of here //TODO: if everything goes right, give a little feedback and // then take back to load with story selected // Add passage with links - story = new Story(title, passages.isEmpty() ? null: passages.get(0)); - passages.forEach(story::addPassage); - INSTANCE.setStory(story); - - FileChooser fileChooser = new FileChooser(); - fileChooser.setInitialDirectory(new File("./src/main/resources/story-files")); - fileChooser.getExtensionFilters().add( - new FileChooser.ExtensionFilter("Paths files", "*.paths") - ); - File selectedFile = fileChooser.showSaveDialog(null); - - if (selectedFile != null) { - StoryFileHandler fileHandler = new StoryFileHandler(); - try { - fileHandler.createStoryFile(story, selectedFile); - } catch (IOException ex) { - ex.printStackTrace(); - AlertDialog.showWarning("An error occurred while saving the file."); - } - } + newStoryController.addStory(title, passages); + } catch (Exception ex) { AlertDialog.showWarning(ex.getMessage()); } + StageManager.getInstance().setCurrentView(new LoadGameView()); }); VBox display = new VBox(titleText, titleBox, passageTable, addPassageButton, saveButton);