Skip to content

drag optimising #96

Merged
merged 1 commit into from
May 13, 2026
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public void addAppController(final AppController appController) {
* Starts the game simulation timer.
*/
public void startGame() {
timer = new Timer(true); // Set as daemon thread to allow JVM to exit

timer = new Timer(true);
final int delay = 0;
final int period = 1000;

Expand All @@ -51,6 +50,7 @@ public void run() {
}
}
}, delay, period);

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public PopupsController(List<Popup> popups) {

private void initPopup(Popup popup) {
makeDraggable(popup);
popup.getCloseButton().setOnAction(e -> hide(popup));
popup.getCloseButton().setOnAction(event -> hide(popup));
}

public boolean show(App type) {
Expand Down Expand Up @@ -52,30 +52,24 @@ public List<Popup> getPopups() {

private void makeDraggable(Popup popup) {
BorderPane root = popup.getRoot();
double[] offset = new double[2]; // [x, y]
double[] offset = new double[2];
double[] parentBounds = new double[2]; // cache here

popup.getHeader().setOnMousePressed(event -> {
offset[0] = event.getSceneX() - root.getLayoutX();
offset[1] = event.getSceneY() - root.getLayoutY();
// snapshot bounds once on press, not on every drag tick
if (root.getParent() instanceof Pane parent) {
parentBounds[0] = parent.getWidth() - popup.getWidth();
parentBounds[1] = parent.getHeight() - popup.getHeight();
}
});

popup.getHeader().setOnMouseDragged(event -> {
root.setLayoutX(event.getSceneX() - offset[0]);
root.setLayoutY(event.getSceneY() - offset[1]);
clampToBounds(popup);
root.setLayoutX(Math.max(0, Math.min(
event.getSceneX() - offset[0], parentBounds[0])));
root.setLayoutY(Math.max(0, Math.min(
event.getSceneY() - offset[1], parentBounds[1])));
});
}

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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ public class TimeAndWeatherController implements AppController {
private final IntegerProperty temperature = new SimpleIntegerProperty(15);
private final StringProperty weather = new SimpleStringProperty("Sunny");
private final Random random = new Random();
private final GameController gameController;

private static final String[] DAYS = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};

public TimeAndWeatherController() {
public TimeAndWeatherController(GameController gameController) {
this.gameController = gameController;
updateWeather();
}

Expand All @@ -38,6 +40,11 @@ public void nextTick() {
} else {
hour.set(nextHour);
}

if (dayIndex.get() > 5) {
gameController.stopGame();

}
}

private void updateWeather() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public DesktopViewController(
final GameController gameController
) {
this.player = player;
this.timeAndWeatherController = new TimeAndWeatherController();
this.timeAndWeatherController = new TimeAndWeatherController(gameController);
gameController.addAppController(timeAndWeatherController);

List<Popup> popups = new ArrayList<>();
Expand Down
9 changes: 8 additions & 1 deletion src/main/resources/css/popup.css
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
.popup-wrapper {
-fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.2), 10, 0, 0, 5);
-fx-background-color: transparent;
}

.popup-root {
-fx-background-color: white;
-fx-background-radius: 10;
-fx-border-color: #d1d1d1;
-fx-border-width: 1;
-fx-border-radius: 10;
-fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.2), 10, 0, 0, 5);
-fx-background-insets: 0;
-fx-border-insets: 0;
}

.popup-header {
Expand Down