diff --git a/assets/button-image-2.png b/assets/button-image-2.png new file mode 100644 index 00000000..25c4b984 Binary files /dev/null and b/assets/button-image-2.png differ diff --git a/assets/button-image.png b/assets/button-image.png new file mode 100644 index 00000000..c5f008d1 Binary files /dev/null and b/assets/button-image.png differ diff --git a/assets/coin.png b/assets/coin.png new file mode 100644 index 00000000..4aa6e5d5 Binary files /dev/null and b/assets/coin.png differ diff --git a/assets/point-popup-bg.png b/assets/point-popup-bg.png deleted file mode 100644 index 53115469..00000000 Binary files a/assets/point-popup-bg.png and /dev/null differ diff --git a/assets/popup-background-2.png b/assets/popup-background-2.png new file mode 100644 index 00000000..1ef7d4ed Binary files /dev/null and b/assets/popup-background-2.png differ diff --git a/assets/popup-background.png b/assets/popup-background.png new file mode 100644 index 00000000..71b25286 Binary files /dev/null and b/assets/popup-background.png differ diff --git a/src/main/java/io/rpg/controller/Controller.java b/src/main/java/io/rpg/controller/Controller.java index 2d706202..d99f3a6a 100644 --- a/src/main/java/io/rpg/controller/Controller.java +++ b/src/main/java/io/rpg/controller/Controller.java @@ -110,6 +110,7 @@ public void onKeyboardEvent(KeyboardEvent event) { if (payload.getEventType() == KeyEvent.KEY_PRESSED){ switch (payload.getCode()) { case F -> popupController.openPointsPopup(5, getWindowCenterX(), getWindowCenterY()); + case G -> popupController.openTextPopup("Hello!", getWindowCenterX(), getWindowCenterY()); case A -> currentModel.getPlayer().setLeftPressed(true); case D -> currentModel.getPlayer().setRightPressed(true); case S -> currentModel.getPlayer().setDownPressed(true); @@ -145,7 +146,7 @@ public void onMouseClickedEvent(MouseClickedEvent event) { } if (object instanceof CollectibleGameObject) { - popupController.openPointsPopup(5, getWindowCenterX(), getWindowCenterY()); + popupController.openTextImagePopup("Picked up an item!", objectView.getImage(), getWindowCenterX(), getWindowCenterY()); objectView.setVisible(false); } } diff --git a/src/main/java/io/rpg/controller/PopupController.java b/src/main/java/io/rpg/controller/PopupController.java index 670a4c9b..53dc0b01 100644 --- a/src/main/java/io/rpg/controller/PopupController.java +++ b/src/main/java/io/rpg/controller/PopupController.java @@ -1,41 +1,55 @@ package io.rpg.controller; -import io.rpg.view.popups.PointsEarnedPopup; +import io.rpg.view.popups.TextImagePopup; +import io.rpg.view.popups.TextPopup; +import javafx.scene.image.Image; 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; + private final Image coinImage = new Image("file:assets/coin.png"); 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(); } }); + } + + public void openTextPopup(String text, int x, int y){ + TextPopup popupScene = new TextPopup(text); + popupStage.setScene(popupScene); popupStage.show(); - popupStage.setX(x - pointsPopupScene.getWidth() / 2); - popupStage.setY(y - pointsPopupScene.getHeight() / 2); + popupStage.setX(x - popupScene.getWidth() / 2); + popupStage.setY(y - popupScene.getHeight() / 2); + + popupScene.setButtonCallback(event -> popupStage.hide()); } + public void openTextImagePopup(String text, Image image, int x, int y){ + TextImagePopup popupScene = new TextImagePopup(text, image); + popupStage.setScene(popupScene); + + popupStage.show(); + + popupStage.setX(x - popupScene.getWidth() / 2); + popupStage.setY(y - popupScene.getHeight() / 2); + + popupScene.setButtonCallback(event -> popupStage.hide()); + } + + public void openPointsPopup(int pointsCount, int x, int y) { + openTextImagePopup("You earned " + pointsCount + " points!", coinImage, x, y); + } + + 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 deleted file mode 100644 index 40820c9d..00000000 --- a/src/main/java/io/rpg/view/popups/PointsEarnedPopup.java +++ /dev/null @@ -1,34 +0,0 @@ -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; - -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 void setPointsCount(int pointsCount){ - controller.setPointsCount(pointsCount); - } -} diff --git a/src/main/java/io/rpg/view/popups/TextImagePopup.java b/src/main/java/io/rpg/view/popups/TextImagePopup.java new file mode 100644 index 00000000..d75dc6f7 --- /dev/null +++ b/src/main/java/io/rpg/view/popups/TextImagePopup.java @@ -0,0 +1,50 @@ +package io.rpg.view.popups; + +import io.rpg.viewmodel.TextImagePopupViewModel; +import io.rpg.viewmodel.TextPopupViewModel; +import javafx.event.EventHandler; +import javafx.fxml.FXMLLoader; +import javafx.scene.Group; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.scene.image.Image; +import javafx.scene.input.MouseEvent; +import javafx.scene.paint.Color; + +import java.io.IOException; +import java.util.Objects; + +public class TextImagePopup extends Scene { + + private final TextImagePopupViewModel viewModel; + + public TextImagePopup(String text, Image image, String backgroundPath, String buttonPath) { + this(text, image); + viewModel.setBackgroundImage(backgroundPath); + viewModel.setOkButtonImage(buttonPath); + } + + public TextImagePopup(String text, Image image) { + super(new Group(), Color.TRANSPARENT); + + FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(TextPopupViewModel.class.getResource("text-image-popup-view.fxml"))); + Parent root = null; + + try { + root = loader.load(); + } catch (IOException e) { + e.printStackTrace(); + } + + this.setRoot(root); + + viewModel = loader.getController(); + viewModel.setText(text); + viewModel.setImage(image); + this.setFill(Color.TRANSPARENT); + } + + public void setButtonCallback(EventHandler callback) { + this.viewModel.setButtonOnClick(callback); + } +} diff --git a/src/main/java/io/rpg/view/popups/TextPopup.java b/src/main/java/io/rpg/view/popups/TextPopup.java new file mode 100644 index 00000000..55bed3a2 --- /dev/null +++ b/src/main/java/io/rpg/view/popups/TextPopup.java @@ -0,0 +1,46 @@ +package io.rpg.view.popups; + +import io.rpg.viewmodel.TextPopupViewModel; +import javafx.event.EventHandler; +import javafx.fxml.FXMLLoader; +import javafx.scene.Group; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.scene.input.MouseEvent; +import javafx.scene.paint.Color; + +import java.io.IOException; +import java.util.Objects; + +public class TextPopup extends Scene { + + private final TextPopupViewModel viewModel; + + public TextPopup(String text, String backgroundPath, String buttonPath) { + this(text); + viewModel.setBackgroundImage(backgroundPath); + viewModel.setOkButtonImage(buttonPath); + } + + public TextPopup(String text) { + super(new Group(), Color.TRANSPARENT); + + FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(TextPopupViewModel.class.getResource("text-popup-view.fxml"))); + Parent root = null; + + try { + root = loader.load(); + } catch (IOException e) { + e.printStackTrace(); + } + this.setRoot(root); + + viewModel = loader.getController(); + viewModel.setText(text); + this.setFill(Color.TRANSPARENT); + } + + public void setButtonCallback(EventHandler callback) { + this.viewModel.setButtonOnClick(callback); + } +} diff --git a/src/main/java/io/rpg/viewmodel/PointsPopupViewModel.java b/src/main/java/io/rpg/viewmodel/PointsPopupViewModel.java deleted file mode 100644 index f98155c0..00000000 --- a/src/main/java/io/rpg/viewmodel/PointsPopupViewModel.java +++ /dev/null @@ -1,29 +0,0 @@ -package io.rpg.viewmodel; - -import javafx.fxml.FXML; -import javafx.scene.control.Label; -import javafx.scene.image.Image; -import javafx.scene.layout.*; -import javafx.util.Pair; - -public class PointsPopupViewModel { - - @FXML - private Label label; - @FXML - private Pane background; - - public void setPointsCount(int pointsCount) { - label.setText("Earned " + pointsCount + " points!"); - } - - 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)); - } -} diff --git a/src/main/java/io/rpg/viewmodel/TextImagePopupViewModel.java b/src/main/java/io/rpg/viewmodel/TextImagePopupViewModel.java new file mode 100644 index 00000000..a0a93704 --- /dev/null +++ b/src/main/java/io/rpg/viewmodel/TextImagePopupViewModel.java @@ -0,0 +1,50 @@ +package io.rpg.viewmodel; + +import javafx.event.EventHandler; +import javafx.fxml.FXML; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; +import javafx.scene.input.MouseEvent; +import javafx.scene.layout.Pane; + +public class TextImagePopupViewModel { + + @FXML private Label label; + @FXML private Pane backgroundPane; + @FXML private ImageView backgroundImage; + @FXML private Button okButton; + @FXML private ImageView imageView; + + + public void setText(String text) { + if (text.length() < 40) setTextSize(25); + else if (text.length() < 120) setTextSize(19); + else setTextSize(13); + label.setText(text); + } + + public void setImage(Image image) { + imageView.setImage(image); + } + + public void setTextSize(int size) { + label.setStyle("-fx-font-family: Monospaced; -fx-text-fill: white; -fx-font-weight: bold; -fx-font-size: " + size); + } + + public void setBackgroundImage(String url) { + Image image = new Image(url); + backgroundImage.setImage(image); + } + + public void setOkButtonImage(String url) { + ImageView imageView = new ImageView(url); + okButton.setGraphic(imageView); + } + + public void setButtonOnClick(EventHandler callback) { + okButton.setOnMouseClicked(callback); + } + +} diff --git a/src/main/java/io/rpg/viewmodel/TextPopupViewModel.java b/src/main/java/io/rpg/viewmodel/TextPopupViewModel.java new file mode 100644 index 00000000..8bdf21e3 --- /dev/null +++ b/src/main/java/io/rpg/viewmodel/TextPopupViewModel.java @@ -0,0 +1,44 @@ +package io.rpg.viewmodel; + +import javafx.event.EventHandler; +import javafx.fxml.FXML; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; +import javafx.scene.input.MouseEvent; +import javafx.scene.layout.*; + +public class TextPopupViewModel { + + @FXML private Label label; + @FXML private Pane backgroundPane; + @FXML private ImageView backgroundImage; + @FXML private Button okButton; + + + public void setText(String text) { + if (text.length() < 50) setTextSize(25); + else if (text.length() < 190) setTextSize(19); + else setTextSize(13); + label.setText(text); + } + + public void setTextSize(int size) { + label.setStyle("-fx-font-family: Monospaced; -fx-text-fill: white; -fx-font-weight: bold; -fx-font-size: " + size); + } + + public void setBackgroundImage(String url) { + Image image = new Image(url); + backgroundImage.setImage(image); + } + + public void setOkButtonImage(String url) { + ImageView imageView = new ImageView(url); + okButton.setGraphic(imageView); + } + + public void setButtonOnClick(EventHandler callback) { + okButton.setOnMouseClicked(callback); + } +} diff --git a/src/main/resources/css/styles.css b/src/main/resources/css/styles.css new file mode 100644 index 00000000..ad42b5f0 --- /dev/null +++ b/src/main/resources/css/styles.css @@ -0,0 +1,3 @@ +.button:hover { + -fx-text-fill: white; +} \ No newline at end of file diff --git a/src/main/resources/io/rpg/viewmodel/points-earned-view.fxml b/src/main/resources/io/rpg/viewmodel/points-earned-view.fxml deleted file mode 100644 index 7aec9976..00000000 --- a/src/main/resources/io/rpg/viewmodel/points-earned-view.fxml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - - - - diff --git a/src/main/resources/io/rpg/viewmodel/text-image-popup-view.fxml b/src/main/resources/io/rpg/viewmodel/text-image-popup-view.fxml new file mode 100644 index 00000000..2b65b0c1 --- /dev/null +++ b/src/main/resources/io/rpg/viewmodel/text-image-popup-view.fxml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/io/rpg/viewmodel/text-popup-view.fxml b/src/main/resources/io/rpg/viewmodel/text-popup-view.fxml new file mode 100644 index 00000000..8feb1cef --- /dev/null +++ b/src/main/resources/io/rpg/viewmodel/text-popup-view.fxml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + +