Skip to content

Commit

Permalink
Merge pull request #81 from einaskoi/per/bugs
Browse files Browse the repository at this point in the history
bug fixes:constrain popups to window bounds and enforce unique type
  • Loading branch information
einaskoi authored Apr 17, 2026
2 parents 9f84d65 + e45cea4 commit 806e51e
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package edu.ntnu.idi.idatt2003.gruppe42.Controller;

import edu.ntnu.idi.idatt2003.gruppe42.Model.Apps;
import edu.ntnu.idi.idatt2003.gruppe42.Model.App;
import edu.ntnu.idi.idatt2003.gruppe42.View.Popups.AppStorePopup;
import edu.ntnu.idi.idatt2003.gruppe42.View.Popups.BankPopup;
import edu.ntnu.idi.idatt2003.gruppe42.View.Popups.HustlersPopup;
Expand Down Expand Up @@ -42,7 +42,7 @@ public AppController(PopupController popupController, Pane parent) {
* @param type the type of the app.
* @return the app button.
*/
public Button createAppButton(Apps type) {
public Button createAppButton(App type) {
Button button = new Button(type.toString());
button.setMinSize(0, 0);

Expand Down Expand Up @@ -86,7 +86,7 @@ public Button createAppButton(Apps type) {
*
* @param type the app type.
*/
private void openPopup(Apps type) {
private void openPopup(App type) {
Popup popup = switch (type) {
case APPSTORE -> new AppStorePopup(400, 300, 100, 100);
case HUSTLERS -> new HustlersPopup(400, 300, 140, 140);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public PopupController() {
* @return true if added, false otherwise
*/
public boolean add(Popup popup) {
if (popup == null || popups.contains(popup)) {
if (popup == null) {
return false;
}else if (popups.stream().anyMatch(oldPopup -> oldPopup.getType().equals(popup.getType()))) {
return false;
}
popup.getCloseButton().setOnAction(event -> remove(popup));
Expand Down Expand Up @@ -62,27 +64,4 @@ public void bringToFront(Popup popup) {
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());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,56 @@
import edu.ntnu.idi.idatt2003.gruppe42.Model.Player;
import edu.ntnu.idi.idatt2003.gruppe42.View.StartView;
import java.math.BigDecimal;
import java.util.List;
import java.util.Random;
import javafx.scene.control.Alert;

public class StartViewController {

private String username = "";
private final StartView startView;
private final Millions application;
private static final List<String> TATE_NAMES = List.of(
"Top G",
"Cobra Tate",
"Tristan's Brother",
"Emory's Son",
"Matrix Escaper",
"Bugatti Owner",
"Hustler G",
"Chin Checker",
"Sparkling Water Enthusiast",
"The Talisman"
);

public StartViewController(Millions application, StartView startView) {

this.application = application;
this.startView = startView;

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

startView.getLoginButton().setOnAction(event -> handleStart());
}

private void handleStart() {
if (startView.getSelectedMode() == null) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Difficulty missing");
alert.setHeaderText(null);
alert.setContentText("how much money you got?");
alert.showAndWait();
return;
}

BigDecimal money = getStartingMoney();

if (username == null || username.trim().isEmpty() || !isValidName(username)) {
username = TATE_NAMES.get(new Random().nextInt(TATE_NAMES.size()));
}

Player player = new Player(username, money);
System.out.println(player.getMoney());
application.switchToDesktopView();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package edu.ntnu.idi.idatt2003.gruppe42.Model;

public enum Apps {
public enum App {
APPSTORE,
HUSTLERS,
STOCK,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import edu.ntnu.idi.idatt2003.gruppe42.Controller.AppController;
import edu.ntnu.idi.idatt2003.gruppe42.Controller.PopupController;
import edu.ntnu.idi.idatt2003.gruppe42.Model.Apps;
import edu.ntnu.idi.idatt2003.gruppe42.Model.App;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
Expand Down Expand Up @@ -72,8 +71,8 @@ private GridPane createGrid() {
grid.add(cell, col, row);

// Add initial buttons to the first row based on the Apps enum
if (row == 0 && col < Apps.values().length) {
cell.getChildren().add(appController.createAppButton(Apps.values()[col]));
if (row == 0 && col < App.values().length) {
cell.getChildren().add(appController.createAppButton(App.values()[col]));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package edu.ntnu.idi.idatt2003.gruppe42.View.Popups;

import edu.ntnu.idi.idatt2003.gruppe42.Model.App;

/**
* A popup for the AppStore app.
*/
Expand All @@ -14,7 +16,7 @@ public class AppStorePopup extends Popup {
* @param y y-position of the popup
*/
public AppStorePopup(int width, int height, int x, int y) {
super(width, height, x, y);
super(width, height, x, y, App.APPSTORE);
// Add AppStore specific content here
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package edu.ntnu.idi.idatt2003.gruppe42.View.Popups;

import edu.ntnu.idi.idatt2003.gruppe42.Model.App;

/**
* A popup for the Bank app.
*/
Expand All @@ -14,7 +16,7 @@ public class BankPopup extends Popup {
* @param y y-position of the popup
*/
public BankPopup(int width, int height, int x, int y) {
super(width, height, x, y);
super(width, height, x, y, App.BANK);
// Add Bank specific content here
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package edu.ntnu.idi.idatt2003.gruppe42.View.Popups;

import edu.ntnu.idi.idatt2003.gruppe42.Model.App;

/**
* A popup for the Hustlers app.
*/
Expand All @@ -14,7 +16,7 @@ public class HustlersPopup extends Popup {
* @param y y-position of the popup
*/
public HustlersPopup(int width, int height, int x, int y) {
super(width, height, x, y);
super(width, height, x, y, App.HUSTLERS);
// Add Hustlers specific content here
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package edu.ntnu.idi.idatt2003.gruppe42.View.Popups;

import edu.ntnu.idi.idatt2003.gruppe42.Model.App;

/**
* A popup for the Mail app.
*/
Expand All @@ -14,7 +16,7 @@ public class MailPopup extends Popup {
* @param y y-position of the popup
*/
public MailPopup(int width, int height, int x, int y) {
super(width, height, x, y);
super(width, height, x, y, App.MAIL);
// Add Mail specific content here
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package edu.ntnu.idi.idatt2003.gruppe42.View.Popups;

import edu.ntnu.idi.idatt2003.gruppe42.Model.App;

/**
* A popup for the News app.
*/
Expand All @@ -14,7 +16,7 @@ public class NewsPopup extends Popup {
* @param y y-position of the popup
*/
public NewsPopup(int width, int height, int x, int y) {
super(width, height, x, y);
super(width, height, x, y, App.NEWS);
// Add News specific content here
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package edu.ntnu.idi.idatt2003.gruppe42.View.Popups;

import edu.ntnu.idi.idatt2003.gruppe42.Model.App;
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.Pane;
import javafx.scene.layout.VBox;

/**
Expand All @@ -17,6 +19,7 @@ public abstract class Popup {
protected int height;
protected double dx;
protected double dy;
protected App type;

protected BorderPane root;
protected ScrollPane scrollPane;
Expand All @@ -32,9 +35,10 @@ public abstract class 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) {
protected Popup(int width, int height, int x, int y, App type) {
this.width = width;
this.height = height;
this.type = type;

root = new BorderPane();
scrollPane = new ScrollPane();
Expand All @@ -43,10 +47,10 @@ protected Popup(int width, int height, int x, int y) {
header = new HBox();
content = new VBox();

Label titleLabel = new Label("Title");
Label titleLabel = new Label(type.toString());
closeButton = new Button("Exit");
header.getChildren().addAll(titleLabel, closeButton);
header.setStyle("-fx-background-color: red;");
header.setStyle("-fx-background-color: lightgray;");

content.setPrefSize(width, height);
scrollPane.setMinSize(width, height);
Expand Down Expand Up @@ -110,6 +114,12 @@ public Button getCloseButton() {
return closeButton;
}

/**
* Returns the type of the popup.
*/
public App getType() {return type;
}

private void makeDraggable() {
header.setOnMousePressed(e -> {
dx = e.getSceneX() - root.getLayoutX();
Expand All @@ -119,6 +129,29 @@ private void makeDraggable() {
header.setOnMouseDragged(e -> {
root.setLayoutX(e.getSceneX() - dx);
root.setLayoutY(e.getSceneY() - dy);
keepInBounds();
});
}

/**
* Checks if a popup is out of bounds and moves it back if necessary
*/
public void keepInBounds() {
if (getRoot().getParent() instanceof Pane parent) {
double screenWidth = parent.getWidth();
double screenHeight = parent.getHeight();

if (getX() < 0) {
getRoot().setLayoutX(0);
} else if (getX() + getWidth() > screenWidth) {
getRoot().setLayoutX(screenWidth - getWidth());
}

if (getY() < 0) {
getRoot().setLayoutY(0);
} else if (getY() + getHeight() > screenHeight) {
getRoot().setLayoutY(screenHeight - getHeight());
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package edu.ntnu.idi.idatt2003.gruppe42.View.Popups;

import edu.ntnu.idi.idatt2003.gruppe42.Model.App;

/**
* A popup for the Stock app.
*/
Expand All @@ -14,7 +16,7 @@ public class StockPopup extends Popup {
* @param y y-position of the popup
*/
public StockPopup(int width, int height, int x, int y) {
super(width, height, x, y);
super(width, height, x, y, App.STOCK);
// Add Stock specific content here
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public TextField getUsernameField() {
}
public String getSelectedMode() {
RadioButton selectedMode = (RadioButton) startingMoneyGroup.getSelectedToggle();
if (selectedMode == null) {
return null;
}
return selectedMode.getText();
}
public Button getLoginButton() {
Expand Down

0 comments on commit 806e51e

Please sign in to comment.