From 3eeb661e871aa9cc1870fa2cd9ec24a87d837f04 Mon Sep 17 00:00:00 2001 From: Per Eric Trapnes Date: Mon, 13 Apr 2026 23:09:52 +0200 Subject: [PATCH] add popup functionality: keep in bounds & bring to front --- .../gruppe42/Controller/PopupController.java | 64 +++++++++++++++++-- .../idatt2003/gruppe42/View/DesktopView.java | 7 +- .../idatt2003/gruppe42/View/Popups/Popup.java | 47 +++++++++++--- .../gruppe42/View/Popups/SimplePopup.java | 11 ++++ 4 files changed, 112 insertions(+), 17 deletions(-) diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/PopupController.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/PopupController.java index 4b837d8..be1164d 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/PopupController.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/PopupController.java @@ -1,32 +1,47 @@ package edu.ntnu.idi.idatt2003.gruppe42.Controller; -import edu.ntnu.idi.idatt2003.gruppe42.View.DesktopView; import edu.ntnu.idi.idatt2003.gruppe42.View.Popups.Popup; import javafx.scene.layout.Pane; - import java.util.ArrayList; import java.util.List; +/** + * Controller for managing popups in a pane. + * Handles adding, removing, and keeping popups within bounds. + */ public class PopupController { - private List popups; + private final List popups; + /** + * Constructs a new popup controller. + */ public PopupController() { popups = new ArrayList<>(); } - public List getPopups() { - return popups; - } - + /** + * Adds a popup to the controller and sets up its events. + * + * @param popup the popup to add + * @return true if added, false otherwise + */ public boolean add(Popup popup) { if (popup == null || popups.contains(popup)) { return false; } popup.getCloseButton().setOnAction(e -> remove(popup)); + popup.getRoot().setOnMousePressed(e -> bringToFront(popup)); + popup.getContent().setOnMousePressed(e -> bringToFront(popup)); return popups.add(popup); } + /** + * Removes a popup from its parent and the controller. + * + * @param popup the popup to remove + * @return true if removed, false otherwise + */ public boolean remove(Popup popup) { if (popup == null || !popups.contains(popup)) { return false; @@ -36,4 +51,39 @@ public boolean remove(Popup popup) { } return popups.remove(popup); } + + /** + * Brings the specified popup to the front of all others. + * + * @param popup the popup to bring to front + */ + public void bringToFront(Popup popup) { + if (popup.getRoot().getParent() instanceof Pane parent) { + popup.getRoot().toFront(); + } + } + + /** + * Checks if a popup is out of bounds and moves it back if necessary. + * + * @param popup the popup to check + */ + public void keepInBounds(Popup popup) { + if (popup.getRoot().getParent() instanceof Pane parent) { + double screenWidth = parent.getWidth(); + double screenHeight = parent.getHeight(); + + if (popup.getX() < 0) { + popup.getRoot().setLayoutX(0); + } else if (popup.getX() + popup.getWidth() > screenWidth) { + popup.getRoot().setLayoutX(screenWidth - popup.getWidth()); + } + + if (popup.getY() < 0) { + popup.getRoot().setLayoutY(0); + } else if (popup.getY() + popup.getHeight() > screenHeight) { + popup.getRoot().setLayoutY(screenHeight - popup.getHeight()); + } + } + } } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/DesktopView.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/DesktopView.java index e68babc..52735a7 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/DesktopView.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/DesktopView.java @@ -29,8 +29,13 @@ public BorderPane getRoot() { int x = random.nextInt(1000); int y = random.nextInt(700); Popup popup = new SimplePopup(width, height, x, y); - popupController.add(popup); center.getChildren().add(popup.getRoot()); + popupController.add(popup); + popupController.keepInBounds(popup); + + popup.getRoot().setOnMouseDragged(event -> { + popupController.keepInBounds(popup); + }); }); center.getChildren().add(button); diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Popups/Popup.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Popups/Popup.java index b3b44cb..3a7bf5c 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Popups/Popup.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Popups/Popup.java @@ -7,12 +7,14 @@ import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; +/** + * An abstract class for all popups. + * Handles dragging, layout, and common components. + */ public abstract class Popup { protected int width; protected int height; - protected int x; - protected int y; protected double dx; protected double dy; @@ -22,11 +24,17 @@ public abstract class Popup { protected VBox content; protected Button closeButton; - public Popup(int width, int height, int x, int y) { + /** + * Constructs a new popup. + * + * @param width width of the popup + * @param height height of the popup + * @param x x-position of the popup + * @param y y-position of the popup + */ + protected Popup(int width, int height, int x, int y) { this.width = width; this.height = height; - this.x = x; - this.y = y; root = new BorderPane(); scrollPane = new ScrollPane(); @@ -53,30 +61,51 @@ public Popup(int width, int height, int x, int y) { makeDraggable(); } + /** + * Returns the width of the popup. + */ public int getWidth() { return width; } + /** + * Returns the height of the popup. + */ public int getHeight() { return height; } - public int getX() { - return x; + /** + * Returns the x-position of the popup. + */ + public double getX() { + return root.getLayoutX(); } - public int getY() { - return y; + /** + * Returns the y-position of the popup. + */ + public double getY() { + return root.getLayoutY(); } + /** + * Returns the root node of the popup. + */ public BorderPane getRoot() { return root; } + /** + * Returns the content node of the popup. + */ public VBox getContent() { return content; } + /** + * Returns the close button of the popup. + */ public Button getCloseButton() { return closeButton; } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Popups/SimplePopup.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Popups/SimplePopup.java index 1e6503c..7ee86d7 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Popups/SimplePopup.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Popups/SimplePopup.java @@ -1,7 +1,18 @@ package edu.ntnu.idi.idatt2003.gruppe42.View.Popups; +/** + * A simple implementation of a popup. + */ public class SimplePopup extends Popup { + /** + * Constructs a new simple popup. + * + * @param width width of the popup + * @param height height of the popup + * @param x x-position of the popup + * @param y y-position of the popup + */ public SimplePopup(int width, int height, int x, int y) { super(width, height, x, y); getContent().setStyle("-fx-background-color: blue;");