Skip to content

refactor cleaned up the controller and such in popup and desktop/logi… #94

Merged
merged 1 commit into from
May 13, 2026
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package edu.ntnu.idi.idatt2003.gruppe42.Controller;

import edu.ntnu.idi.idatt2003.gruppe42.Model.App;
import edu.ntnu.idi.idatt2003.gruppe42.View.Popup;
import java.util.List;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;

/**
* Manages popup visibility, dragging, and bounds-clamping.
*/
public final class PopupsController {

private final List<Popup> popups;

public PopupsController(List<Popup> popups) {
this.popups = popups;
popups.forEach(this::initPopup);
}

private void initPopup(Popup popup) {
makeDraggable(popup);
popup.getCloseButton().setOnAction(e -> hide(popup));
}

public boolean show(App type) {
if (type == null) {
return false;
}
popups.stream()
.filter(popup -> popup.getType().equals(type))
.findFirst()
.ifPresent(popup -> {
popup.getRoot().setVisible(true);
popup.getRoot().autosize();
popup.getRoot().toFront();
});
return true;
}

public boolean hide(Popup popup) {
if (popup == null) {
return false;
}
popup.getRoot().setVisible(false);
return true;
}

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

private void makeDraggable(Popup popup) {
BorderPane root = popup.getRoot();
double[] offset = new double[2]; // [x, y]

popup.getHeader().setOnMousePressed(event -> {
offset[0] = event.getSceneX() - root.getLayoutX();
offset[1] = event.getSceneY() - root.getLayoutY();
});

popup.getHeader().setOnMouseDragged(event -> {
root.setLayoutX(event.getSceneX() - offset[0]);
root.setLayoutY(event.getSceneY() - offset[1]);
clampToBounds(popup);
});
}

private void clampToBounds(Popup popup) {
BorderPane root = popup.getRoot();
if (!(root.getParent() instanceof Pane parent)) {
return;
}

double maxX = parent.getWidth() - popup.getWidth();
double maxY = parent.getHeight() - popup.getHeight();

root.setLayoutX(Math.max(0, Math.min(root.getLayoutX(), maxX)));
root.setLayoutY(Math.max(0, Math.min(root.getLayoutY(), maxY)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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;
import edu.ntnu.idi.idatt2003.gruppe42.Controller.PopupController;
import edu.ntnu.idi.idatt2003.gruppe42.Controller.PopupsController;
import edu.ntnu.idi.idatt2003.gruppe42.Controller.TimeAndWeatherController;
import edu.ntnu.idi.idatt2003.gruppe42.Model.App;
import edu.ntnu.idi.idatt2003.gruppe42.Model.Player;
Expand Down Expand Up @@ -33,7 +33,7 @@
* Handles app button creation, resizing, click events, and drag-and-drop.
*/
public final class DesktopViewController {
private final PopupController popupController;
private final PopupsController popupsController;
private final DesktopView desktopView;
private final Player player;
private final TimeAndWeatherController timeAndWeatherController;
Expand Down Expand Up @@ -79,17 +79,17 @@ public DesktopViewController(
popups.add(newsApp);
popups.add(bankApp);

this.popupController = new PopupController(popups);
this.popupsController = new PopupsController(popups);

bankApp.setOnShareSelected(share -> {
popupController.show(App.STOCK);
popupsController.show(App.STOCK);
stockApp.openStockPage(share.getStock());
});

this.desktopView = new DesktopView(this);

this.desktopView.getRoot().getChildren().addAll(
popupController.getPopups().stream()
popupsController.getPopups().stream()
.map(Popup::getRoot).toArray(Pane[]::new)
);
}
Expand Down Expand Up @@ -183,7 +183,7 @@ public void handleSettings() {
* @param type the app type.
*/
private void openPopup(final App type) {
popupController.show(type);
popupsController.show(type);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Millions.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class Millions extends Application {
private Scene scene;
private Stage stage;
private DesktopView desktopView;
private DesktopViewController desktopViewController;
private Player player;
private GameController gameController;

Expand All @@ -32,7 +33,7 @@ public void start(Stage stage) throws Exception {
stage.setTitle("Millions");
stage.setScene(scene);
stage.setFullScreen(true);
stage.setFullScreenExitHint("");
stage.setFullScreenExitHint("Press 'EXIT' to ENTER the matrix");
stage.show();
}

Expand All @@ -44,13 +45,14 @@ public void initGame(String username, Difficulty difficulty) {
}

public void switchToStartView() {
stop();
StartView startView = new StartView();
scene.setRoot(startView.getRoot());
stage.setScene(scene);
}

public void switchToDesktopView() {
DesktopViewController desktopViewController = new DesktopViewController(player, gameController);
desktopViewController = new DesktopViewController(player, gameController);
desktopView = desktopViewController.getDesktopView();
scene.setRoot(desktopView.getRoot());
stage.setScene(scene);
Expand Down
Loading