Skip to content

Commit

Permalink
I forgot to pull before working on the startview
Browse files Browse the repository at this point in the history
  • Loading branch information
peretr committed May 15, 2026
2 parents 9fa7194 + ebadfba commit fcab19d
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers;

import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.SettingsApp;
import java.nio.file.Path;

public class SettingsAppController implements AppController {

private SettingsApp settingsApp;
private Path userSelectedPath;
private Runnable onUserSelection;

public SettingsAppController(SettingsApp settingsApp) {
this.settingsApp = settingsApp;

if (!settingsApp.isLoggedIn()) {
settingsApp.getOpenFileChooserButton().setOnAction(event -> {
userSelectedPath = settingsApp.getFileChooser().showOpenDialog(settingsApp.getRoot().getScene().getWindow()).toPath();
System.out.println(userSelectedPath.toString());
onUserSelection.run();
});
}
}

public Path getUserSelectedPath() {
return userSelectedPath;
}

public void setOnUserPathSelected(Runnable callback) {
onUserSelection = callback;
}

@Override
public void nextTick() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ public final class MarketController {
/**
* Constructs a new MarketController and starts the market.
*/
public MarketController() {
public MarketController(Path userSelectedPath) {
this.stockResolution = 120;

System.out.println(userSelectedPath);
try {
Path path = Path.of(Objects.requireNonNull(getClass().getClassLoader()
.getResource("stocks.csv")).toURI());
.getResource("stocks.csv")).toURI());
if (userSelectedPath != null) {
path = userSelectedPath;
System.out.println(path);
}
exchange = new Exchange(StockFileHandler.readFromFile(path));

startMarket();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import edu.ntnu.idi.idatt2003.gruppe42.Model.App;
import edu.ntnu.idi.idatt2003.gruppe42.View.Popup;
import java.util.List;
import java.util.Optional;

import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
Expand Down Expand Up @@ -52,10 +54,6 @@ public boolean hide(Popup popup) {
return true;
}

public List<Popup> getPopups() {
return popups;
}

public void bindOpenButton(Button button, App type) {
button.setOnAction(event -> show(type));
}
Expand All @@ -79,4 +77,22 @@ private void makeDraggable(Popup popup) {
root.setLayoutY(Math.max(0, Math.min(event.getSceneY() - offset[1], maxXY[1])));
});
}

public void addPopups(List<Popup> popups) {
popups.forEach(this::addPopup);
}

public void addPopup(Popup popup) {
initPopup(popup);
this.popups.add(popup);
}

public Popup getPopup(App type) {
Optional<Popup> match = popups.stream().filter(popup -> popup.getType().equals(type)).findFirst();
return match.orElse(null);
}

public List<Popup> getPopups() {
return popups;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers.AppStoreController;
import edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers.BankAppController;
import edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers.SettingsAppController;
import edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers.StockAppController;
import edu.ntnu.idi.idatt2003.gruppe42.Controller.GameController;
import edu.ntnu.idi.idatt2003.gruppe42.Controller.MarketController;
Expand All @@ -13,19 +14,12 @@
import edu.ntnu.idi.idatt2003.gruppe42.Model.Transaction.Purchase;
import edu.ntnu.idi.idatt2003.gruppe42.Model.Transaction.Sale;
import edu.ntnu.idi.idatt2003.gruppe42.Model.Transaction.Transaction;
import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.AppStoreApp;
import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.BankApp;
import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.GameOverApp;
import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.HustlersApp;
import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.MailApp;
import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.NewsApp;
import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.StockApp;
import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.WarningApp;
import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.WeekendRapportApp;
import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.*;
import edu.ntnu.idi.idatt2003.gruppe42.View.Popup;
import edu.ntnu.idi.idatt2003.gruppe42.View.Views.DesktopView;
import edu.ntnu.idi.idatt2003.gruppe42.View.Views.DesktopView.ModalLayer;
import java.math.BigDecimal;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Platform;
Expand Down Expand Up @@ -61,12 +55,12 @@ public final class DesktopViewController {

private Runnable onGameOver;

public DesktopViewController(final Player player, final GameController gameController) {
public DesktopViewController(final Player player, final GameController gameController, final Path userSelectedPath, final PopupsController popupsController) {
this.player = player;
this.timeAndWeatherController = new TimeAndWeatherController(gameController);
gameController.addAppController(timeAndWeatherController);

this.marketController = new MarketController();
this.marketController = new MarketController(userSelectedPath);


AppStoreApp appStoreApp = new AppStoreApp();
Expand All @@ -84,6 +78,9 @@ public DesktopViewController(final Player player, final GameController gameContr
BankApp bankApp = new BankApp();
gameController.addAppController(new BankAppController(bankApp, player));

SettingsApp settingsApp = (SettingsApp) popupsController.getPopup(App.SETTINGS);
settingsApp.setLoggedIn(true);

List<Popup> popups = new ArrayList<>();
popups.add(appStoreApp);
popups.add(hustlersApp);
Expand All @@ -92,7 +89,8 @@ public DesktopViewController(final Player player, final GameController gameContr
popups.add(newsApp);
popups.add(bankApp);

this.popupsController = new PopupsController(popups);
this.popupsController = popupsController;
this.popupsController.addPopups(popups);

bankApp.setOnShareSelected(share -> {
popupsController.show(App.STOCK);
Expand Down Expand Up @@ -132,6 +130,7 @@ public DesktopViewController(final Player player, final GameController gameContr
wireGameOver();

desktopView.getNextWeekButton().setOnAction(event -> handleAdvanceWeek());
desktopView.getSettingsButton().setOnAction(event -> popupsController.show(App.SETTINGS));

timeAndWeatherController.dayIndexProperty().addListener((obs, oldDay, newDay) -> {
int day = newDay.intValue();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package edu.ntnu.idi.idatt2003.gruppe42.Controller.ViewControllers;

import edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers.SettingsAppController;
import edu.ntnu.idi.idatt2003.gruppe42.Controller.PopupsController;
import edu.ntnu.idi.idatt2003.gruppe42.Millions;
import edu.ntnu.idi.idatt2003.gruppe42.Model.App;
import edu.ntnu.idi.idatt2003.gruppe42.Model.Difficulty;
import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.SettingsApp;
import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.WarningApp;
import edu.ntnu.idi.idatt2003.gruppe42.View.Popup;
import edu.ntnu.idi.idatt2003.gruppe42.View.Views.StartView;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

Expand All @@ -18,6 +26,9 @@ public class StartViewController {

private final StartView startView;
private final Millions application;
private final PopupsController popupsController;
private Path userSelectedPath;
private SettingsAppController settingsAppController;

private static final List<String> TATE_NAMES = List.of(
"Top G",
Expand All @@ -42,32 +53,48 @@ public StartViewController(final Millions application, final StartView startView
this.application = application;
this.startView = startView;

List<Popup> popups = new ArrayList<>();

SettingsApp settingsApp = new SettingsApp(false);
settingsAppController = new SettingsAppController(settingsApp);

WarningApp warningApp = new WarningApp();
warningApp.centerInParent();
startView.getRoot().getChildren().add(warningApp.getRoot());

startView.getRoot().getChildren().addAll(settingsApp.getRoot(), warningApp.getRoot());

settingsAppController.setOnUserPathSelected(this::updateUserSelectedPath);

warningApp.configure(
"⚠",
"Difficulty Required",
"Choose how much starting money you want before jumping in.",
"Got it"
);
warningApp.getPrimaryButton().setOnAction(e ->
warningApp.getRoot().setVisible(false)
);

popups.add(settingsApp);
popups.add(warningApp);
popupsController = new PopupsController(popups);

warningApp.getPrimaryButton().setOnAction(e -> {
popupsController.hide(warningApp);
});

startView.getUsernameField().textProperty().addListener(
(obs, old, newValue) -> username = newValue
);

startView.getLoginButton().setOnAction(event -> {
if (startView.getSelectedMode() == null) {
warningApp.show();
warningApp.getRoot().toFront();
popupsController.show(App.WARNING);
return;
}
handleStart();
});

startView.getSettingsButton().setOnAction(actionEvent -> {
popupsController.show(App.SETTINGS);
});
}

private void handleStart() {
Expand All @@ -83,7 +110,7 @@ private void handleStart() {
}
}

application.initGame(resolvedName, difficulty);
application.initGame(resolvedName, difficulty, userSelectedPath, popupsController);
}

private String resolveUsername(final String input) {
Expand All @@ -96,4 +123,8 @@ private String resolveUsername(final String input) {
private boolean isValidName(final String value) {
return value.matches("[a-zA-Z0-9]+");
}

private void updateUserSelectedPath() {
userSelectedPath = settingsAppController.getUserSelectedPath();
}
}
7 changes: 5 additions & 2 deletions src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Millions.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.ntnu.idi.idatt2003.gruppe42;

import edu.ntnu.idi.idatt2003.gruppe42.Controller.GameController;
import edu.ntnu.idi.idatt2003.gruppe42.Controller.PopupsController;
import edu.ntnu.idi.idatt2003.gruppe42.Controller.ViewControllers.DesktopViewController;
import edu.ntnu.idi.idatt2003.gruppe42.Controller.ViewControllers.StartViewController;
import edu.ntnu.idi.idatt2003.gruppe42.Model.Difficulty;
Expand All @@ -12,6 +13,8 @@
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.nio.file.Path;

/**
* JavaFX application entry point.
*
Expand Down Expand Up @@ -47,12 +50,12 @@ public void start(final Stage stage) {
* Begins a brand-new game session. Called by the start screen once the
* user has chosen a difficulty.
*/
public void initGame(final String username, final Difficulty difficulty) {
public void initGame(final String username, final Difficulty difficulty, Path userSelectedPath, PopupsController popupsController) {
player = GameFactory.createPlayer(username, difficulty);
gameController = new GameController();
gameController.startGame();

desktopViewController = new DesktopViewController(player, gameController);
desktopViewController = new DesktopViewController(player, gameController, userSelectedPath, popupsController);
desktopViewController.setOnGameOver(this::resetGame);
desktopView = desktopViewController.getDesktopView();
scene.setRoot(desktopView.getRoot());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public enum App {
MAIL("Mail"),
NEWS("News"),
BANK("Bank"),
SETTINGS("Settings"),
WEEKENDRAPPORT("Weekend Rapport"),
WARNING("Warning"),
GAMEOVER("Game Over");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package edu.ntnu.idi.idatt2003.gruppe42.View.Apps;

import edu.ntnu.idi.idatt2003.gruppe42.Model.App;
import edu.ntnu.idi.idatt2003.gruppe42.View.Popup;
import javafx.scene.control.Button;
import javafx.stage.FileChooser;

public class SettingsApp extends Popup {

private Button openFileChooserButton;
private FileChooser fileChooser;
private boolean isLoggedIn;

public SettingsApp(boolean isLoggedIn) {
super(720, 480, 80, 60, App.SETTINGS);
this.isLoggedIn = isLoggedIn;
updateContent();
}

public Button getOpenFileChooserButton() {
return openFileChooserButton;
}

public FileChooser getFileChooser() {
return fileChooser;
}

public boolean isLoggedIn() {
return isLoggedIn;
}

public void setLoggedIn(boolean status) {
this.isLoggedIn = status;
updateContent();
}

public void updateContent() {
if (!isLoggedIn) {
openFileChooserButton = new Button("Import stock file");
fileChooser = new FileChooser();
fileChooser.setTitle("Select .csv stock file");
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("CSV files","*.csv"));

content.getChildren().setAll(openFileChooserButton);
} else {
//TODO: settings in desktopView
content.getChildren().setAll();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public enum ModalLayer { RAPPORT, WARNING, GAME_OVER }
private final EnumSet<ModalLayer> activeLayers = EnumSet.noneOf(ModalLayer.class);

private Button nextWeekButton;
private Button settingsButton;

public DesktopView(final DesktopViewController controller) {
this.controller = controller;
Expand Down Expand Up @@ -298,7 +299,7 @@ private static boolean isDesktopApp(final App app) {
// Bottom bar

private HBox createBottomBar() {
Button settingsButton = new Button("⚙");
settingsButton = new Button("⚙");
settingsButton.getStyleClass().add("desktop-settings-button");
settingsButton.setOnAction(e -> controller.handleSettings());

Expand Down Expand Up @@ -413,4 +414,8 @@ private String resource(final String path) {
public Button getNextWeekButton() {
return nextWeekButton;
}

public Button getSettingsButton() {
return settingsButton;
}
}
Loading

0 comments on commit fcab19d

Please sign in to comment.