-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
i have added so when u push play button in the main menu it actually goes to next page
- Loading branch information
EspenTinius
committed
May 11, 2026
1 parent
b309fd9
commit 2832c97
Showing
4 changed files
with
302 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/playgame/PlayGameController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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}. | ||
| * | ||
| * <p> | ||
| * Wires up the two buttons and the save-row click handler. Publishes | ||
| * scene-change events through the {@link EventManager}. | ||
| * </p> | ||
| */ | ||
| public class PlayGameController extends ViewController<PlayGameView> { | ||
|
|
||
| /** | ||
| * 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<ViewData> eventData = new EventData<>(EventType.SCENE_CHANGE, viewData); | ||
| getEventManager().invokeEvent(eventData); | ||
| } | ||
| } |
212 changes: 212 additions & 0 deletions
212
src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/playgame/PlayGameView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. | ||
| * | ||
| * <p> | ||
| * Displays a list of existing save games and two action buttons: | ||
| * "Create new game" and "Back". | ||
| * </p> | ||
| * | ||
| * <p> | ||
| * Extends {@link ViewElement} with a {@link StackPane} root so a | ||
| * background image can sit behind the central panel. | ||
| * </p> | ||
| */ | ||
| public class PlayGameView extends ViewElement<StackPane> { | ||
|
|
||
| /** "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<SaveEntry> saves = new ArrayList<>(); | ||
|
|
||
| /** Callback invoked when a save row is clicked. */ | ||
| private Consumer<SaveEntry> onSaveSelected = save -> { | ||
| }; | ||
|
|
||
| /** | ||
| * Constructor. | ||
| * | ||
| * <p> | ||
| * Constructs with name "PlayGameView" | ||
| * </p> | ||
| */ | ||
| 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<SaveEntry> 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<SaveEntry> 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. | ||
| * | ||
| * <p> | ||
| * Null-safe — kan kalles før feltinitialisererne har kjørt. | ||
| * </p> | ||
| */ | ||
| 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) { | ||
| } | ||
| } |