From 7d14e31b6c1aa6d364c9621dabd7241486af37a6 Mon Sep 17 00:00:00 2001 From: mhawryluk Date: Wed, 20 Apr 2022 13:39:56 +0200 Subject: [PATCH 1/3] feat: Add showing points popup on key press (F) --- src/main/java/io/rpg/controller/Controller.java | 11 +++++++++++ .../java/io/rpg/view/popups/PointsEarnedPopup.java | 7 +++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/rpg/controller/Controller.java b/src/main/java/io/rpg/controller/Controller.java index 96200780..f11584d9 100644 --- a/src/main/java/io/rpg/controller/Controller.java +++ b/src/main/java/io/rpg/controller/Controller.java @@ -6,8 +6,10 @@ import io.rpg.util.Result; import io.rpg.view.GameObjectView; import io.rpg.view.LocationView; +import io.rpg.view.popups.PointsEarnedPopup; import javafx.scene.Scene; import javafx.scene.input.KeyEvent; +import javafx.stage.Stage; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.NotNull; @@ -21,6 +23,8 @@ public class Controller implements KeyboardEvent.Observer, MouseClickedEvent.Obs private LinkedHashMap tagToLocationViewMap; private Logger logger; + private final PointsEarnedPopup pointsPopup = new PointsEarnedPopup(); + public Controller() { logger = LogManager.getLogger(Controller.class); @@ -90,6 +94,13 @@ else if (currentModel == null) public void onKeyboardEvent(KeyboardEvent event) { // TODO: implement event handling logger.info("Controller notified on key pressed from " + event.source()); + + switch (event.payload().getCode()) { + case F -> { + Stage popup = pointsPopup.getPopup(5, currentView); + popup.show(); + } + } } @Override diff --git a/src/main/java/io/rpg/view/popups/PointsEarnedPopup.java b/src/main/java/io/rpg/view/popups/PointsEarnedPopup.java index b818e8e6..f5c43e9e 100644 --- a/src/main/java/io/rpg/view/popups/PointsEarnedPopup.java +++ b/src/main/java/io/rpg/view/popups/PointsEarnedPopup.java @@ -1,6 +1,8 @@ package io.rpg.view.popups; import io.rpg.viewmodel.PointsPopupViewModel; +import java.io.IOException; +import java.util.Objects; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; @@ -10,12 +12,9 @@ import javafx.stage.Window; import javafx.util.Pair; -import java.io.IOException; -import java.util.Objects; - public class PointsEarnedPopup { - private final FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(PointsEarnedPopup.class.getResource("points-earned-view.fxml"))); + private final FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(PointsPopupViewModel.class.getResource("points-earned-view.fxml"))); private Parent root; private PointsPopupViewModel controller; private final Scene popupScene; From db0ea20bec4f54549ad0c46f2eb8164fdd6a20d3 Mon Sep 17 00:00:00 2001 From: mhawryluk Date: Wed, 20 Apr 2022 23:48:37 +0200 Subject: [PATCH 2/3] refactor: Create PopupController - class responsible for creating and controlling popups, currently only handles PointsEarnedPopup --- .../java/io/rpg/controller/Controller.java | 20 +++--- .../io/rpg/controller/PopupController.java | 44 +++++++++++++ .../io/rpg/view/popups/PointsEarnedPopup.java | 64 ++++++------------- .../rpg/viewmodel/PointsPopupViewModel.java | 3 +- .../io/rpg/viewmodel/location-view.fxml | 2 +- 5 files changed, 76 insertions(+), 57 deletions(-) create mode 100644 src/main/java/io/rpg/controller/PopupController.java diff --git a/src/main/java/io/rpg/controller/Controller.java b/src/main/java/io/rpg/controller/Controller.java index f11584d9..55329018 100644 --- a/src/main/java/io/rpg/controller/Controller.java +++ b/src/main/java/io/rpg/controller/Controller.java @@ -4,12 +4,8 @@ import io.rpg.model.data.MouseClickedEvent; import io.rpg.model.location.LocationModel; import io.rpg.util.Result; -import io.rpg.view.GameObjectView; import io.rpg.view.LocationView; -import io.rpg.view.popups.PointsEarnedPopup; import javafx.scene.Scene; -import javafx.scene.input.KeyEvent; -import javafx.stage.Stage; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.NotNull; @@ -22,8 +18,7 @@ public class Controller implements KeyboardEvent.Observer, MouseClickedEvent.Obs private LocationModel currentModel; private LinkedHashMap tagToLocationViewMap; private Logger logger; - - private final PointsEarnedPopup pointsPopup = new PointsEarnedPopup(); + private final PopupController popupController = new PopupController(); public Controller() { @@ -96,13 +91,18 @@ public void onKeyboardEvent(KeyboardEvent event) { logger.info("Controller notified on key pressed from " + event.source()); switch (event.payload().getCode()) { - case F -> { - Stage popup = pointsPopup.getPopup(5, currentView); - popup.show(); - } + case F -> popupController.openPointsPopup(5, getWindowCenterX(), getWindowCenterY()); } } + private int getWindowCenterX(){ + return (int) (currentView.getWindow().getX() + currentView.getWindow().getWidth()/2); + } + + private int getWindowCenterY(){ + return (int) (currentView.getWindow().getY() + currentView.getWindow().getHeight()/2); + } + @Override public void onMouseClickedEvent(MouseClickedEvent event) { // TODO: implement event handling diff --git a/src/main/java/io/rpg/controller/PopupController.java b/src/main/java/io/rpg/controller/PopupController.java new file mode 100644 index 00000000..639cd6a5 --- /dev/null +++ b/src/main/java/io/rpg/controller/PopupController.java @@ -0,0 +1,44 @@ +package io.rpg.controller; + +import io.rpg.view.popups.PointsEarnedPopup; +import javafx.stage.Stage; +import javafx.stage.StageStyle; + +import java.io.IOException; + +public class PopupController { + + private final Stage popupStage = new Stage(StageStyle.TRANSPARENT); + private PointsEarnedPopup pointsPopupScene; + + public PopupController() { + try { + pointsPopupScene = new PointsEarnedPopup(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void openPointsPopup(int pointsCount, int x, int y) { + pointsPopupScene.setPointsCount(pointsCount); + popupStage.setScene(pointsPopupScene); + + // close popup after clicking aside + popupStage.focusedProperty().addListener((obs, wasFocused, isNowFocused) -> { + if (!isNowFocused) { + popupStage.close(); + } + }); + + popupStage.show(); + + popupStage.setX(x - pointsPopupScene.getWidth() / 2); + popupStage.setY(y - pointsPopupScene.getHeight() / 2); + } + + public void hidePopup() { + this.popupStage.hide(); + } + +} + diff --git a/src/main/java/io/rpg/view/popups/PointsEarnedPopup.java b/src/main/java/io/rpg/view/popups/PointsEarnedPopup.java index f5c43e9e..40820c9d 100644 --- a/src/main/java/io/rpg/view/popups/PointsEarnedPopup.java +++ b/src/main/java/io/rpg/view/popups/PointsEarnedPopup.java @@ -4,55 +4,31 @@ import java.io.IOException; import java.util.Objects; import javafx.fxml.FXMLLoader; +import javafx.scene.Group; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.paint.Color; -import javafx.stage.Stage; -import javafx.stage.StageStyle; -import javafx.stage.Window; -import javafx.util.Pair; - -public class PointsEarnedPopup { - - private final FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(PointsPopupViewModel.class.getResource("points-earned-view.fxml"))); - private Parent root; - private PointsPopupViewModel controller; - private final Scene popupScene; - - public PointsEarnedPopup() { - // read FXML view - try { - root = loader.load(); - } catch (IOException e) { - e.printStackTrace(); - } - - popupScene = new Scene(root, Color.TRANSPARENT); + +public class PointsEarnedPopup extends Scene { + + private final PointsPopupViewModel controller; + + public PointsEarnedPopup(String backgroundPath) throws IOException { + super(new Group(), Color.TRANSPARENT); + + FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(PointsPopupViewModel.class.getResource("points-earned-view.fxml"))); + Parent root = loader.load(); + this.setRoot(root); + + controller = loader.getController(); + controller.setBackgroundImage(backgroundPath); + } + + public PointsEarnedPopup() throws IOException { + this("file:assets/point-popup-bg.png"); } - public Stage getPopup(int pointsCount, Scene scene) { - // fill dynamic view components - if (controller == null) controller = loader.getController(); + public void setPointsCount(int pointsCount){ controller.setPointsCount(pointsCount); - Pair backgroundDims = controller.setBackgroundImage("file:assets/point-popup-bg.png"); - - // create popup stage - Stage popupStage = new Stage(StageStyle.TRANSPARENT); - Window window = scene.getWindow(); - popupStage.initOwner(window); - - // add and center popupScene on popup stage - popupStage.setScene(popupScene); - popupStage.setX(window.getX() + window.getWidth() / 2 - backgroundDims.getKey() / 2); - popupStage.setY(window.getY() + window.getHeight() / 2 - backgroundDims.getValue() / 2); - - // close popup after clicking aside - popupStage.focusedProperty().addListener((obs, wasFocused, isNowFocused) -> { - if (!isNowFocused) { - popupStage.close(); - } - }); - - return popupStage; } } diff --git a/src/main/java/io/rpg/viewmodel/PointsPopupViewModel.java b/src/main/java/io/rpg/viewmodel/PointsPopupViewModel.java index 6523df37..f98155c0 100644 --- a/src/main/java/io/rpg/viewmodel/PointsPopupViewModel.java +++ b/src/main/java/io/rpg/viewmodel/PointsPopupViewModel.java @@ -17,7 +17,7 @@ public void setPointsCount(int pointsCount) { label.setText("Earned " + pointsCount + " points!"); } - public Pair setBackgroundImage(String url) { + public void setBackgroundImage(String url) { BackgroundImage backgroundImg = new BackgroundImage( new Image(url), BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, @@ -25,6 +25,5 @@ public Pair setBackgroundImage(String url) { BackgroundSize.DEFAULT ); background.setBackground(new Background(backgroundImg)); - return new Pair<>(background.getPrefWidth(), background.getPrefHeight()); } } diff --git a/src/main/resources/io/rpg/viewmodel/location-view.fxml b/src/main/resources/io/rpg/viewmodel/location-view.fxml index 2f55ae9a..c567fec8 100644 --- a/src/main/resources/io/rpg/viewmodel/location-view.fxml +++ b/src/main/resources/io/rpg/viewmodel/location-view.fxml @@ -5,7 +5,7 @@ - + From 7c2cebe6f3873324642331cab4c4f767f080bce8 Mon Sep 17 00:00:00 2001 From: Marcin Hawryluk <70582973+mhawryluk@users.noreply.github.com> Date: Sat, 23 Apr 2022 20:36:54 +0200 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Kacper Kafara --- src/main/java/io/rpg/controller/Controller.java | 8 ++++---- src/main/java/io/rpg/controller/PopupController.java | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/rpg/controller/Controller.java b/src/main/java/io/rpg/controller/Controller.java index 55329018..a9732259 100644 --- a/src/main/java/io/rpg/controller/Controller.java +++ b/src/main/java/io/rpg/controller/Controller.java @@ -95,12 +95,12 @@ public void onKeyboardEvent(KeyboardEvent event) { } } - private int getWindowCenterX(){ - return (int) (currentView.getWindow().getX() + currentView.getWindow().getWidth()/2); + private int getWindowCenterX() { + return (int) (currentView.getWindow().getX() + currentView.getWindow().getWidth() / 2); } - private int getWindowCenterY(){ - return (int) (currentView.getWindow().getY() + currentView.getWindow().getHeight()/2); + private int getWindowCenterY() { + return (int) (currentView.getWindow().getY() + currentView.getWindow().getHeight() / 2); } @Override diff --git a/src/main/java/io/rpg/controller/PopupController.java b/src/main/java/io/rpg/controller/PopupController.java index 639cd6a5..670a4c9b 100644 --- a/src/main/java/io/rpg/controller/PopupController.java +++ b/src/main/java/io/rpg/controller/PopupController.java @@ -37,7 +37,7 @@ public void openPointsPopup(int pointsCount, int x, int y) { } public void hidePopup() { - this.popupStage.hide(); + popupStage.hide(); } }