From 2832c97f5c04ad0f92a8edc9e473e462385b2417 Mon Sep 17 00:00:00 2001 From: EspenTinius Date: Mon, 11 May 2026 11:35:32 +0200 Subject: [PATCH] play game i have added so when u push play button in the main menu it actually goes to next page --- .../ntnu/idi/idatt2003/g40/mappe/Main.java | 24 +- .../view/mainmenu/MainMenuController.java | 26 ++- .../view/playgame/PlayGameController.java | 55 +++++ .../g40/mappe/view/playgame/PlayGameView.java | 212 ++++++++++++++++++ 4 files changed, 302 insertions(+), 15 deletions(-) create mode 100644 src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/playgame/PlayGameController.java create mode 100644 src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/playgame/PlayGameView.java diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Main.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Main.java index f7876a5..a4a757c 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Main.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Main.java @@ -9,8 +9,9 @@ import edu.ntnu.idi.idatt2003.g40.mappe.view.ingame.InGameView; import edu.ntnu.idi.idatt2003.g40.mappe.view.mainmenu.MainMenuController; 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 javafx.application.Application; -import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.stage.Stage; @@ -20,6 +21,7 @@ import java.util.Objects; public class Main extends Application { + static void main() { FileParser parser1 = new FileParser("src/main/resources/dummydata.txt"); FileConverter converter1 = new FileConverter(); @@ -36,22 +38,36 @@ static void main() { public void start(final Stage stage) throws Exception { Scene scene = new Scene(new Pane()); scene.getStylesheets() - .add(Objects.requireNonNull(getClass().getResource("/styles.css")).toExternalForm()); + .add(Objects.requireNonNull(getClass().getResource("/styles.css")).toExternalForm()); stage.setScene(scene); stage.setWidth(ConfigValues.VIEWPORT_WIDTH.getValue()); stage.setHeight(ConfigValues.VIEWPORT_HEIGHT.getValue()); + EventManager eventManager = new EventManager(); ViewManager viewManager = new ViewManager(stage, eventManager); + // Main menu MainMenuView mainMenuView = new MainMenuView(); new MainMenuController(mainMenuView, eventManager); - InGameView inGameView = new InGameView(); + // Play game (mellom hovedmeny og spillet) + 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))); + // In-game + InGameView inGameView = new InGameView(); + + // Registrer alle views og start på hovedmenyen viewManager.addView(mainMenuView); + viewManager.addView(playGameView); viewManager.addView(inGameView); viewManager.setScene(mainMenuView); + stage.show(); } -} +} \ No newline at end of file diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/mainmenu/MainMenuController.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/mainmenu/MainMenuController.java index a1c0dff..51c287d 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/mainmenu/MainMenuController.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/mainmenu/MainMenuController.java @@ -9,33 +9,37 @@ /** * Controller for the {@link MainMenuView}. * - *

Extends {@link ViewController}

- * */ + *

+ * Extends {@link ViewController} + *

+ */ public class MainMenuController extends ViewController { /** * Constructor. * - * @param view The {@link MainMenuView} object to attach this controller to. + * @param view The {@link MainMenuView} object to attach this controller + * to. * @param eventManager the active {@link EventManager}. - * */ + */ public MainMenuController(final MainMenuView view, - final EventManager eventManager) { + final EventManager eventManager) { super(view, eventManager); } /** * {@inheritDoc} * - *

Sets play game button functionality

- * */ + *

+ * Sets play game button functionality + *

+ */ @Override protected void initInteractions() { getViewElement().getToGameButton().setOnAction(e -> { - ViewData viewData = new ViewData("InGameView"); - EventData eventData = - new EventData<>(EventType.SCENE_CHANGE, viewData); + ViewData viewData = new ViewData("PlayGameView"); + EventData eventData = new EventData<>(EventType.SCENE_CHANGE, viewData); getEventManager().invokeEvent(eventData); }); } -} +} \ No newline at end of file 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 new file mode 100644 index 0000000..73176be --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/playgame/PlayGameController.java @@ -0,0 +1,55 @@ +package edu.ntnu.idi.idatt2003.g40.mappe.view.playgame; + +import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventData; +import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventManager; +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; + +/** + * Controller for {@link PlayGameView}. + * + *

+ * Wires up the two buttons and the save-row click handler. Publishes + * scene-change events through the {@link EventManager}. + *

+ */ +public class PlayGameController extends ViewController { + + /** + * Constructor. + * + * @param view the {@link PlayGameView} this controller is attached to. + * @param eventManager the active {@link EventManager}. + */ + public PlayGameController(final PlayGameView view, + final EventManager eventManager) { + super(view, eventManager); + } + + /** {@inheritDoc} */ + @Override + protected void initInteractions() { + // Create new game → go to in-game view (placeholder; later: name picker). + getViewElement().getCreateNewGameButton().setOnAction(e -> changeScene("InGameView")); + + // Back → return to main menu. + getViewElement().getBackButton().setOnAction(e -> changeScene("MainMenu")); + + // 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"); + }); + } + + /** + * Helper that fires a SCENE_CHANGE event toward the given scene name. + */ + private void changeScene(final String sceneName) { + ViewData viewData = new ViewData(sceneName); + EventData eventData = new EventData<>(EventType.SCENE_CHANGE, viewData); + getEventManager().invokeEvent(eventData); + } +} \ No newline at end of file diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/playgame/PlayGameView.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/playgame/PlayGameView.java new file mode 100644 index 0000000..99004a5 --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/playgame/PlayGameView.java @@ -0,0 +1,212 @@ +package edu.ntnu.idi.idatt2003.g40.mappe.view.playgame; + +import edu.ntnu.idi.idatt2003.g40.mappe.view.ViewElement; +import javafx.geometry.Insets; +import javafx.geometry.Pos; +import javafx.scene.Cursor; +import javafx.scene.control.Button; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; +import javafx.scene.layout.HBox; +import javafx.scene.layout.Priority; +import javafx.scene.layout.Region; +import javafx.scene.layout.StackPane; +import javafx.scene.layout.VBox; +import javafx.scene.text.Text; + +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.function.Consumer; + +/** + * View shown after the player clicks "Play" on the main menu. + * + *

+ * Displays a list of existing save games and two action buttons: + * "Create new game" and "Back". + *

+ * + *

+ * Extends {@link ViewElement} with a {@link StackPane} root so a + * background image can sit behind the central panel. + *

+ */ +public class PlayGameView extends ViewElement { + + /** "Create new game" button. */ + private Button createNewGameButton; + + /** "Back" button. */ + private Button backButton; + + /** Vertical container holding the save rows. Rebuilt on every update. */ + private VBox saveListContainer; + + /** Bottom row with the two action buttons. */ + private HBox bottomButtonRow; + + /** Centered panel: save list + bottom buttons stacked. */ + private VBox mainPanel; + + /** Background image displayed behind the panel. */ + private ImageView backgroundImage; + + /** Current saves to display. Replaced via {@link #setSaves}. */ + private List saves = new ArrayList<>(); + + /** Callback invoked when a save row is clicked. */ + private Consumer onSaveSelected = save -> { + }; + + /** + * Constructor. + * + *

+ * Constructs with name "PlayGameView" + *

+ */ + public PlayGameView() { + super(new StackPane(), "PlayGameView"); + } + + public Button getCreateNewGameButton() { + return createNewGameButton; + } + + public Button getBackButton() { + return backButton; + } + + /** + * Sets the list of saves to display and rebuilds the rows. + * + * @param saves saves to display. + */ + public void setSaves(final List saves) { + this.saves = new ArrayList<>(saves); + rebuildSaveList(); + } + + /** + * Sets the handler called when a save row is clicked. + * + * @param handler callback receiving the selected entry. + */ + public void setOnSaveSelected(final Consumer handler) { + this.onSaveSelected = (handler != null) ? handler : save -> { + }; + } + + /** {@inheritDoc} */ + @Override + protected void initLayout() { + backgroundImage = new ImageView(new Image(Objects.requireNonNull( + getClass().getResourceAsStream("/millionsbackground.png")))); + backgroundImage.setPreserveRatio(false); + + saveListContainer = new VBox(8); + saveListContainer.setAlignment(Pos.TOP_CENTER); + saveListContainer.setPadding(new Insets(20)); + + createNewGameButton = new Button("Create new game"); + backButton = new Button("Back"); + + bottomButtonRow = new HBox(40, createNewGameButton, backButton); + bottomButtonRow.setAlignment(Pos.CENTER); + + mainPanel = new VBox(30, saveListContainer, bottomButtonRow); + mainPanel.setAlignment(Pos.CENTER); + mainPanel.setPadding(new Insets(40)); + mainPanel.setMaxWidth(700); + + getRootPane().getChildren().addAll(backgroundImage, mainPanel); + + // Make the background fill the root pane. + backgroundImage.fitWidthProperty().bind(getRootPane().widthProperty()); + backgroundImage.fitHeightProperty().bind(getRootPane().heightProperty()); + + // NB: Vi kaller IKKE rebuildSaveList() her. Feltinitialisererne + // (saves = new ArrayList<>()) har ikke kjørt ennå — vi er fortsatt + // inne i superkonstruktørens kall til initLayout. Tom liste uansett. + } + + /** {@inheritDoc} */ + @Override + protected void initStyling() { + getRootPane().getStyleClass().add("main-menu-bg"); + saveListContainer.getStyleClass().add("save-list"); + createNewGameButton.getStyleClass().add("menu-button"); + backButton.getStyleClass().add("menu-button"); + } + + /** {@inheritDoc} */ + @Override + public void onUpdate() { + rebuildSaveList(); + } + + /** + * Clears and recreates all save rows from the {@link #saves} list. + * + *

+ * Null-safe — kan kalles før feltinitialisererne har kjørt. + *

+ */ + private void rebuildSaveList() { + if (saveListContainer == null || saves == null) { + return; + } + saveListContainer.getChildren().clear(); + for (SaveEntry save : saves) { + saveListContainer.getChildren().add(buildSaveRow(save)); + } + } + + /** + * Builds a single row displaying the save name on the left and the + * balance (with sign and currency suffix) on the right. + */ + private HBox buildSaveRow(final SaveEntry save) { + Text nameText = new Text(save.name()); + nameText.getStyleClass().add("save-name"); + + Region spacer = new Region(); + HBox.setHgrow(spacer, Priority.ALWAYS); + + Text balanceText = new Text(formatBalance(save.balance())); + balanceText.getStyleClass().add( + save.balance() >= 0 ? "save-balance-positive" : "save-balance-negative"); + + HBox row = new HBox(20, nameText, spacer, balanceText); + row.setAlignment(Pos.CENTER_LEFT); + row.setPadding(new Insets(12, 24, 12, 24)); + row.getStyleClass().add("save-row"); + row.setCursor(Cursor.HAND); + row.setOnMouseClicked(e -> onSaveSelected.accept(save)); + return row; + } + + /** + * Formats a balance value as Norwegian currency, e.g. "+1.000.650.901,43kr" + * or "-1.000.000.000,00kr". + */ + private String formatBalance(final double balance) { + DecimalFormatSymbols symbols = new DecimalFormatSymbols(); + symbols.setGroupingSeparator('.'); + symbols.setDecimalSeparator(','); + DecimalFormat df = new DecimalFormat("+#,##0.00;-#,##0.00", symbols); + return df.format(balance) + "kr"; + } + + /** + * Simple data record for one save entry. + * + * @param name display name of the save. + * @param balance current balance value. + */ + public record SaveEntry(String name, double balance) { + } +} \ No newline at end of file