Skip to content

Commit

Permalink
Merge pull request #69 from einaskoi/einar/popups
Browse files Browse the repository at this point in the history
Einar/popups
  • Loading branch information
peretr authored Apr 13, 2026
2 parents 056aaa8 + 6d0cf2a commit f478bd2
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<Popup> popups;

public PopupController() {
popups = new ArrayList<>();
}

public List<Popup> 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);
}
}
3 changes: 2 additions & 1 deletion src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Millions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -36,7 +37,7 @@ public void switchToStartView() {
}

public void switchToDesktopView() {
DesktopView desktopView = new DesktopView();
desktopView = new DesktopView();
scene.setRoot(desktopView.getRoot());
stage.setScene(scene);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
});
}
}
Original file line number Diff line number Diff line change
@@ -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;");
}
}

0 comments on commit f478bd2

Please sign in to comment.