-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor cleaned up the controller and such in popup and desktop/logi…
…n view
- Loading branch information
Showing
9 changed files
with
253 additions
and
409 deletions.
There are no files selected for viewing
78 changes: 0 additions & 78 deletions
78
src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/PopupController.java
This file was deleted.
Oops, something went wrong.
81 changes: 81 additions & 0 deletions
81
src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/PopupsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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))); | ||
| } | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.