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 new file mode 100644 index 0000000..4b837d8 --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/PopupController.java @@ -0,0 +1,39 @@ +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; + +public class PopupController { + + private List popups; + + public PopupController() { + popups = new ArrayList<>(); + } + + public List getPopups() { + return popups; + } + + public boolean add(Popup popup) { + if (popup == null || popups.contains(popup)) { + return false; + } + popup.getCloseButton().setOnAction(e -> remove(popup)); + return popups.add(popup); + } + + public boolean remove(Popup popup) { + if (popup == null || !popups.contains(popup)) { + return false; + } + if (popup.getRoot().getParent() instanceof Pane parent) { + parent.getChildren().remove(popup.getRoot()); + } + return popups.remove(popup); + } +} diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Millions.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Millions.java index 287be2d..44185a8 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Millions.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Millions.java @@ -11,6 +11,7 @@ public class Millions extends Application { private Scene scene; private Stage stage; + private DesktopView desktopView; @Override public void start(Stage stage) throws Exception { @@ -36,7 +37,7 @@ public void switchToStartView() { } public void switchToDesktopView() { - DesktopView desktopView = new DesktopView(); + desktopView = new DesktopView(); scene.setRoot(desktopView.getRoot()); stage.setScene(scene); } 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 7ec67f9..e68babc 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 @@ -1,12 +1,41 @@ package edu.ntnu.idi.idatt2003.gruppe42.View; +import edu.ntnu.idi.idatt2003.gruppe42.Controller.PopupController; +import edu.ntnu.idi.idatt2003.gruppe42.View.Popups.Popup; +import edu.ntnu.idi.idatt2003.gruppe42.View.Popups.SimplePopup; +import javafx.scene.control.Button; import javafx.scene.layout.BorderPane; +import javafx.scene.layout.Pane; +import java.util.Random; public class DesktopView { + private PopupController popupController; + + public DesktopView() { + popupController = new PopupController(); + } + public BorderPane getRoot() { BorderPane root = new BorderPane(); + Pane center = new Pane(); + + Button button = new Button("Add"); + Random random = new Random(); + button.setOnAction(e -> { + int width = random.nextInt(400) + 100; + int height = random.nextInt(400) + 100; + 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()); + }); + + center.getChildren().add(button); + //center.getChildren().addAll(popupController.getPopups().stream().map(Popup::getRoot).toList()); + root.setCenter(center); return root; } 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 new file mode 100644 index 0000000..b3b44cb --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Popups/Popup.java @@ -0,0 +1,95 @@ +package edu.ntnu.idi.idatt2003.gruppe42.View.Popups; + +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.control.ScrollPane; +import javafx.scene.layout.BorderPane; +import javafx.scene.layout.HBox; +import javafx.scene.layout.VBox; + +public abstract class Popup { + + protected int width; + protected int height; + protected int x; + protected int y; + protected double dx; + protected double dy; + + protected BorderPane root; + protected ScrollPane scrollPane; + protected HBox header; + protected VBox content; + protected Button closeButton; + + public 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(); + scrollPane.setStyle("-fx-hbar-policy: never; -fx-vbar-policy: AS_NEEDED; -fx-background-insets: 0; -fx-padding: 0;"); + + header = new HBox(); + content = new VBox(); + + Label titleLabel = new Label("Title"); + closeButton = new Button("Exit"); + header.getChildren().addAll(titleLabel, closeButton); + header.setStyle("-fx-background-color: red;"); + + content.setPrefSize(width, height); + scrollPane.setMinSize(width, height); + + root.setLayoutX(x); + root.setLayoutY(y); + + scrollPane.setContent(content); + root.setTop(header); + root.setCenter(scrollPane); + + makeDraggable(); + } + + public int getWidth() { + return width; + } + + public int getHeight() { + return height; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } + + public BorderPane getRoot() { + return root; + } + + public VBox getContent() { + return content; + } + + public Button getCloseButton() { + return closeButton; + } + + private void makeDraggable() { + header.setOnMousePressed(e -> { + dx = e.getSceneX() - root.getLayoutX(); + dy = e.getSceneY() - root.getLayoutY(); + }); + + header.setOnMouseDragged(e -> { + root.setLayoutX(e.getSceneX() - dx); + root.setLayoutY(e.getSceneY() - dy); + }); + } +} 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 new file mode 100644 index 0000000..1e6503c --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Popups/SimplePopup.java @@ -0,0 +1,9 @@ +package edu.ntnu.idi.idatt2003.gruppe42.View.Popups; + +public class SimplePopup extends Popup { + + public SimplePopup(int width, int height, int x, int y) { + super(width, height, x, y); + getContent().setStyle("-fx-background-color: blue;"); + } +}