Skip to content
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
15 changes: 13 additions & 2 deletions src/main/java/io/rpg/controller/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,6 +18,7 @@ public class Controller implements KeyboardEvent.Observer, MouseClickedEvent.Obs
private LocationModel currentModel;
private LinkedHashMap<String, LocationView> tagToLocationViewMap;
private Logger logger;
private final PopupController popupController = new PopupController();


public Controller() {
Expand Down Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/io/rpg/controller/PopupController.java
Original file line number Diff line number Diff line change
@@ -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();
}

}

59 changes: 17 additions & 42 deletions src/main/java/io/rpg/view/popups/PointsEarnedPopup.java
Original file line number Diff line number Diff line change
@@ -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<Double, Double> 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;
}
}
3 changes: 1 addition & 2 deletions src/main/java/io/rpg/viewmodel/PointsPopupViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ public void setPointsCount(int pointsCount) {
label.setText("Earned " + pointsCount + " points!");
}

public Pair<Double, Double> setBackgroundImage(String url) {
public void setBackgroundImage(String url) {
BackgroundImage backgroundImg = new BackgroundImage(
new Image(url),
BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT,
BackgroundPosition.DEFAULT,
BackgroundSize.DEFAULT
);
background.setBackground(new Background(backgroundImg));
return new Pair<>(background.getPrefWidth(), background.getPrefHeight());
}
}
2 changes: 1 addition & 1 deletion src/main/resources/io/rpg/viewmodel/location-view.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Pane?>

<HBox fx:id="parent" alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="640.0" prefWidth="640.0" style="-fx-background-color: #cb2f2f;" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="io.rpg.viewmodel.LocationViewModel">
<HBox fx:id="parent" alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="640.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="io.rpg.viewmodel.LocationViewModel">
<children>
<Pane fx:id="contentPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="640.0" prefWidth="640.0">
<children>
Expand Down