Skip to content

Commit

Permalink
fikset main menu
Browse files Browse the repository at this point in the history
  • Loading branch information
EspenTinius committed May 12, 2026
1 parent 2832c97 commit 7c4d022
Show file tree
Hide file tree
Showing 10 changed files with 567 additions and 56 deletions.
15 changes: 11 additions & 4 deletions src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import edu.ntnu.idi.idatt2003.g40.mappe.model.Stock;
import edu.ntnu.idi.idatt2003.g40.mappe.service.FileConverter;
import edu.ntnu.idi.idatt2003.g40.mappe.service.FileParser;
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.utils.ConfigValues;
import edu.ntnu.idi.idatt2003.g40.mappe.view.ViewManager;
Expand All @@ -11,6 +12,8 @@
import edu.ntnu.idi.idatt2003.g40.mappe.view.mainmenu.MainMenuView;
import edu.ntnu.idi.idatt2003.g40.mappe.view.playgame.PlayGameController;
import edu.ntnu.idi.idatt2003.g40.mappe.view.playgame.PlayGameView;
import edu.ntnu.idi.idatt2003.g40.mappe.view.settings.SettingsController;
import edu.ntnu.idi.idatt2003.g40.mappe.view.settings.SettingsView;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
Expand Down Expand Up @@ -54,17 +57,21 @@ public void start(final Stage stage) throws Exception {
PlayGameView playGameView = new PlayGameView();
new PlayGameController(playGameView, eventManager);

// Midlertidig dummy-data — bytt ut med ekte saver fra disk senere.
playGameView.setSaves(List.of(
new PlayGameView.SaveEntry("fuck", -1_000_000_000.00),
new PlayGameView.SaveEntry("Halleluja", 1_000_650_901.43)));
// Last lagrede spill fra disk.
SaveGameService saveGameService = new SaveGameService();
playGameView.setSaves(saveGameService.loadSaves());

// Settings
SettingsView settingsView = new SettingsView();
new SettingsController(settingsView, eventManager);

// In-game
InGameView inGameView = new InGameView();

// Registrer alle views og start på hovedmenyen
viewManager.addView(mainMenuView);
viewManager.addView(playGameView);
viewManager.addView(settingsView);
viewManager.addView(inGameView);
viewManager.setScene(mainMenuView);

Expand Down
47 changes: 47 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/SaveGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package edu.ntnu.idi.idatt2003.g40.mappe.model;

/**
* Represents one save game entry.
*
* <p>
* Holds the display name and the current balance for a single
* saved game.
* </p>
*/
public class SaveGame {

/** Display name of the save. */
private final String name;

/** Current balance in the save. */
private final double balance;

/**
* Constructor.
*
* @param name the display name of the save.
* @param balance the current balance value.
*/
public SaveGame(final String name, final double balance) {
this.name = name;
this.balance = balance;
}

/**
* Getter method for the name.
*
* @return the save name.
*/
public String getName() {
return name;
}

/**
* Getter method for the balance.
*
* @return the balance value.
*/
public double getBalance() {
return balance;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package edu.ntnu.idi.idatt2003.g40.mappe.service;

import edu.ntnu.idi.idatt2003.g40.mappe.model.SaveGame;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
* Service for loading and saving {@link SaveGame} entries from disk.
*
* <p>
* Save file format (one entry per line):
* </p>
*
* <pre>
* # Comment lines start with hash
* SaveName, 1234567.89
* </pre>
*
* <p>
* Lines that don't match the expected format are skipped.
* </p>
*/
public class SaveGameService {

/** Default location of the save file. */
private static final String DEFAULT_PATH = "src/main/resources/saves.txt";

/** Path to the save file. */
private final String filePath;

/**
* Constructor with default path.
*/
public SaveGameService() {
this(DEFAULT_PATH);
}

/**
* Constructor with custom path.
*
* @param filePath the path to the save file.
*/
public SaveGameService(final String filePath) {
this.filePath = filePath;
}

/**
* Loads all save games from the file.
*
* <p>
* Returns an empty list if the file cannot be read or is empty.
* </p>
*
* @return the loaded {@link SaveGame} entries.
*/
public List<SaveGame> loadSaves() {
List<SaveGame> saves = new ArrayList<>();
Path path = Paths.get(filePath);
try {
List<String> lines = Files.readAllLines(path);
for (String line : lines) {
SaveGame save = parseLine(line);
if (save != null) {
saves.add(save);
}
}
} catch (IOException e) {
System.err.println("Could not read save file: " + e.getMessage());
}
return saves;
}

/**
* Parses a single line into a {@link SaveGame}.
*
* <p>
* Returns null for invalid lines, comment lines, and blank lines.
* </p>
*
* @param line the raw line from the file.
* @return the parsed {@link SaveGame}, or null if the line is invalid.
*/
private SaveGame parseLine(final String line) {
if (line == null || line.isBlank() || line.trim().startsWith("#")) {
return null;
}
String[] parts = line.split(",");
if (parts.length != 2) {
return null;
}
try {
String name = parts[0].trim();
double balance = Double.parseDouble(parts[1].trim());
if (name.isEmpty()) {
return null;
}
return new SaveGame(name, balance);
} catch (NumberFormatException e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventType;
import edu.ntnu.idi.idatt2003.g40.mappe.view.ViewController;
import edu.ntnu.idi.idatt2003.g40.mappe.view.ViewData;
import javafx.application.Platform;

/**
* Controller for the {@link MainMenuView}.
Expand All @@ -31,7 +32,7 @@ public MainMenuController(final MainMenuView view,
* {@inheritDoc}
*
* <p>
* Sets play game button functionality
* Sets play game, settings, and exit button functionality.
* </p>
*/
@Override
Expand All @@ -41,5 +42,13 @@ protected void initInteractions() {
EventData<ViewData> eventData = new EventData<>(EventType.SCENE_CHANGE, viewData);
getEventManager().invokeEvent(eventData);
});

getViewElement().getSettingsButton().setOnAction(e -> {
ViewData viewData = new ViewData("SettingsView");
EventData<ViewData> eventData = new EventData<>(EventType.SCENE_CHANGE, viewData);
getEventManager().invokeEvent(eventData);
});

getViewElement().getExitButton().setOnAction(e -> Platform.exit());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,51 @@
import edu.ntnu.idi.idatt2003.g40.mappe.view.ViewData;

/**
* Controller for {@link PlayGameView}.
* Controller for the {@link PlayGameView}.
*
* <p>
* Wires up the two buttons and the save-row click handler. Publishes
* scene-change events through the {@link EventManager}.
* Extends {@link ViewController}
* </p>
*/
public class PlayGameController extends ViewController<PlayGameView> {

/**
* Constructor.
*
* @param view the {@link PlayGameView} this controller is attached to.
* @param view The {@link PlayGameView} object to attach this controller
* to.
* @param eventManager the active {@link EventManager}.
*/
public PlayGameController(final PlayGameView view,
final EventManager eventManager) {
super(view, eventManager);
}

/** {@inheritDoc} */
/**
* {@inheritDoc}
*
* <p>
* Sets create new game, back, and save-row click functionality.
* </p>
*/
@Override
protected void initInteractions() {
// Create new game → go to in-game view (placeholder; later: name picker).
getViewElement().getCreateNewGameButton().setOnAction(e -> changeScene("InGameView"));
getViewElement().getCreateNewGameButton().setOnAction(e -> {
ViewData viewData = new ViewData("InGameView");
EventData<ViewData> eventData = new EventData<>(EventType.SCENE_CHANGE, viewData);
getEventManager().invokeEvent(eventData);
});

// Back → return to main menu.
getViewElement().getBackButton().setOnAction(e -> changeScene("MainMenu"));
getViewElement().getBackButton().setOnAction(e -> {
ViewData viewData = new ViewData("MainMenu");
EventData<ViewData> eventData = new EventData<>(EventType.SCENE_CHANGE, viewData);
getEventManager().invokeEvent(eventData);
});

// Click on a save row → load that save and enter the game.
getViewElement().setOnSaveSelected(save -> {
// TODO: load the selected save into the model layer before changing scene.
System.out.println("Loaded save: " + save.name());
changeScene("InGameView");
ViewData viewData = new ViewData("InGameView");
EventData<ViewData> eventData = new EventData<>(EventType.SCENE_CHANGE, viewData);
getEventManager().invokeEvent(eventData);
});
}

/**
* Helper that fires a SCENE_CHANGE event toward the given scene name.
*/
private void changeScene(final String sceneName) {
ViewData viewData = new ViewData(sceneName);
EventData<ViewData> eventData = new EventData<>(EventType.SCENE_CHANGE, viewData);
getEventManager().invokeEvent(eventData);
}
}
Loading

0 comments on commit 7c4d022

Please sign in to comment.