Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create TextPopup and TextImagePopup #28

Merged
merged 13 commits into from
May 2, 2022
Merged
Binary file added assets/button-image-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/button-image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/coin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed assets/point-popup-bg.png
Binary file not shown.
Binary file added assets/popup-background-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/popup-background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion src/main/java/io/rpg/controller/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
Expand Down
46 changes: 30 additions & 16 deletions src/main/java/io/rpg/controller/PopupController.java
Original file line number Diff line number Diff line change
@@ -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();
}
Expand Down
34 changes: 0 additions & 34 deletions src/main/java/io/rpg/view/popups/PointsEarnedPopup.java

This file was deleted.

50 changes: 50 additions & 0 deletions src/main/java/io/rpg/view/popups/TextImagePopup.java
Original file line number Diff line number Diff line change
@@ -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<? super MouseEvent> callback) {
this.viewModel.setButtonOnClick(callback);
}
}
46 changes: 46 additions & 0 deletions src/main/java/io/rpg/view/popups/TextPopup.java
Original file line number Diff line number Diff line change
@@ -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 {
kkafar marked this conversation as resolved.
Show resolved Hide resolved

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);
kkafar marked this conversation as resolved.
Show resolved Hide resolved

viewModel = loader.getController();
viewModel.setText(text);
this.setFill(Color.TRANSPARENT);
}

public void setButtonCallback(EventHandler<? super MouseEvent> callback) {
this.viewModel.setButtonOnClick(callback);
}
}
29 changes: 0 additions & 29 deletions src/main/java/io/rpg/viewmodel/PointsPopupViewModel.java

This file was deleted.

50 changes: 50 additions & 0 deletions src/main/java/io/rpg/viewmodel/TextImagePopupViewModel.java
Original file line number Diff line number Diff line change
@@ -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<? super MouseEvent> callback) {
okButton.setOnMouseClicked(callback);
}

}
44 changes: 44 additions & 0 deletions src/main/java/io/rpg/viewmodel/TextPopupViewModel.java
Original file line number Diff line number Diff line change
@@ -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<? super MouseEvent> callback) {
okButton.setOnMouseClicked(callback);
}
}
3 changes: 3 additions & 0 deletions src/main/resources/css/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.button:hover {
-fx-text-fill: white;
}
15 changes: 0 additions & 15 deletions src/main/resources/io/rpg/viewmodel/points-earned-view.fxml

This file was deleted.

Loading