Skip to content
Snippets Groups Projects
Commit 043ddf1c authored by Trym Hamer Gudvangen's avatar Trym Hamer Gudvangen
Browse files

feat: connect creation of new story to loading of story

parent b9f5d700
No related branches found
No related tags found
2 merge requests!34Feat/create story gui,!7Feat/part three
......@@ -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;
}
......
......@@ -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);
}
......
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);
}
}
}
......@@ -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);
}
}
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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment