diff --git a/src/main/java/io/rpg/controller/Controller.java b/src/main/java/io/rpg/controller/Controller.java index 96200780..a9732259 100644 --- a/src/main/java/io/rpg/controller/Controller.java +++ b/src/main/java/io/rpg/controller/Controller.java @@ -4,10 +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 javafx.scene.Scene; -import javafx.scene.input.KeyEvent; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.NotNull; @@ -20,6 +18,7 @@ public class Controller implements KeyboardEvent.Observer, MouseClickedEvent.Obs private LocationModel currentModel; private LinkedHashMap tagToLocationViewMap; private Logger logger; + private final PopupController popupController = new PopupController(); public Controller() { @@ -90,6 +89,18 @@ 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 -> 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 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..670a4c9b --- /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() { + 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 b818e8e6..40820c9d 100644 --- a/src/main/java/io/rpg/view/popups/PointsEarnedPopup.java +++ b/src/main/java/io/rpg/view/popups/PointsEarnedPopup.java @@ -1,59 +1,34 @@ 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.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; -import java.io.IOException; -import java.util.Objects; +public class PointsEarnedPopup extends Scene { + + private final PointsPopupViewModel controller; -public class PointsEarnedPopup { + public PointsEarnedPopup(String backgroundPath) throws IOException { + super(new Group(), Color.TRANSPARENT); - private final FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(PointsEarnedPopup.class.getResource("points-earned-view.fxml"))); - private Parent root; - private PointsPopupViewModel controller; - private final Scene popupScene; + FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(PointsPopupViewModel.class.getResource("points-earned-view.fxml"))); + Parent root = loader.load(); + this.setRoot(root); - public PointsEarnedPopup() { - // read FXML view - try { - root = loader.load(); - } catch (IOException e) { - e.printStackTrace(); - } + controller = loader.getController(); + controller.setBackgroundImage(backgroundPath); + } - popupScene = new Scene(root, Color.TRANSPARENT); + 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 @@ - +