From 48bb5b132bf463938b500a63a8482792a790d87c Mon Sep 17 00:00:00 2001 From: co012 Date: Thu, 19 May 2022 12:51:18 +0200 Subject: [PATCH 1/2] feat: done, but ... --- .../java/io/rpg/controller/Controller.java | 10 ++++++ .../io/rpg/model/actions/GameEndAction.java | 11 ++++++ src/main/java/io/rpg/view/GameEndView.java | 29 ++++++++++++++++ .../io/rpg/viewmodel/GameEndViewModel.java | 34 +++++++++++++++++++ .../io/rpg/viewmodel/game-end-view.fxml | 25 ++++++++++++++ 5 files changed, 109 insertions(+) create mode 100644 src/main/java/io/rpg/model/actions/GameEndAction.java create mode 100644 src/main/java/io/rpg/view/GameEndView.java create mode 100644 src/main/java/io/rpg/viewmodel/GameEndViewModel.java create mode 100644 src/main/resources/io/rpg/viewmodel/game-end-view.fxml diff --git a/src/main/java/io/rpg/controller/Controller.java b/src/main/java/io/rpg/controller/Controller.java index df5dd70f..160899c5 100644 --- a/src/main/java/io/rpg/controller/Controller.java +++ b/src/main/java/io/rpg/controller/Controller.java @@ -2,6 +2,7 @@ import io.rpg.model.actions.Action; import io.rpg.model.actions.ActionConsumer; +import io.rpg.model.actions.GameEndAction; import io.rpg.model.actions.LocationChangeAction; import io.rpg.model.data.KeyboardEvent; import io.rpg.model.data.MouseClickedEvent; @@ -9,6 +10,7 @@ import io.rpg.model.location.LocationModel; import io.rpg.model.object.*; import io.rpg.util.Result; +import io.rpg.view.GameEndView; import io.rpg.view.GameObjectView; import io.rpg.view.LocationView; import java.lang.reflect.InvocationTargetException; @@ -17,6 +19,7 @@ import javafx.scene.Scene; import javafx.scene.input.KeyEvent; import javafx.scene.input.MouseButton; +import javafx.scene.layout.Pane; import javafx.stage.Stage; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -104,6 +107,12 @@ private void onAction(LocationChangeAction action) { mainStage.setScene(nextView); } + private void onAction(GameEndAction action) { + GameEndView view = GameEndView.load(); + view.setDescription(action.description); + mainStage.setScene(view); + } + public Scene getView() { return currentView; } @@ -149,6 +158,7 @@ public void onKeyboardEvent(KeyboardEvent event) { case G -> popupController.openTextPopup("Hello!", getWindowCenterX(), getWindowCenterY()); case Q -> popupController.openQuestionPopup(new Question("How many bits are there in one byte?", new String[]{"1/8", "1024", "8", "256"}, 'C'), getWindowCenterX(), getWindowCenterY()); case L -> consumeAction((Action) new LocationChangeAction("location-2", new Position(1, 2))); + case U -> consumeAction(new GameEndAction("You have pressed the forbidden button")); } } // } else if (payload.getEventType() == KeyEvent.KEY_RELEASED) { diff --git a/src/main/java/io/rpg/model/actions/GameEndAction.java b/src/main/java/io/rpg/model/actions/GameEndAction.java new file mode 100644 index 00000000..b8a6fb26 --- /dev/null +++ b/src/main/java/io/rpg/model/actions/GameEndAction.java @@ -0,0 +1,11 @@ +package io.rpg.model.actions; + +public class GameEndAction implements Action { + public final String description; + + public GameEndAction(String description) { + this.description = description; + } + + +} diff --git a/src/main/java/io/rpg/view/GameEndView.java b/src/main/java/io/rpg/view/GameEndView.java new file mode 100644 index 00000000..eeaca984 --- /dev/null +++ b/src/main/java/io/rpg/view/GameEndView.java @@ -0,0 +1,29 @@ +package io.rpg.view; + +import io.rpg.viewmodel.GameEndViewModel; +import java.io.IOException; +import javafx.scene.Parent; +import javafx.scene.Scene; + +public class GameEndView extends Scene { + private GameEndViewModel viewModel; + private GameEndView(Parent parent) { + super(parent); + } + + public static GameEndView load() { + GameEndViewModel viewModel; + try { + viewModel = GameEndViewModel.load(); + } catch (IOException e) { + throw new RuntimeException(e); + } + GameEndView view = new GameEndView(viewModel.getParent()); + view.viewModel = viewModel; + return view; + } + + public void setDescription(String description) { + viewModel.setDescription(description); + } +} diff --git a/src/main/java/io/rpg/viewmodel/GameEndViewModel.java b/src/main/java/io/rpg/viewmodel/GameEndViewModel.java new file mode 100644 index 00000000..588a4b1f --- /dev/null +++ b/src/main/java/io/rpg/viewmodel/GameEndViewModel.java @@ -0,0 +1,34 @@ +package io.rpg.viewmodel; + +import java.io.IOException; +import java.net.URL; +import java.util.Objects; +import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.control.Label; + +public class GameEndViewModel { + public static final String GAME_END_VIEW_FXML = "game-end-view.fxml"; + + private Parent parent; + @FXML + private Label descriptionLabel; + + public static GameEndViewModel load() throws IOException { + URL url = Objects.requireNonNull(GameEndViewModel.class.getResource(GAME_END_VIEW_FXML)); + FXMLLoader loader = new FXMLLoader(url); + Parent root = loader.load(); + GameEndViewModel viewModel = loader.getController(); + viewModel.parent = root; + return viewModel; + } + + public Parent getParent() { + return parent; + } + + public void setDescription(String description) { + descriptionLabel.setText(description); + } +} diff --git a/src/main/resources/io/rpg/viewmodel/game-end-view.fxml b/src/main/resources/io/rpg/viewmodel/game-end-view.fxml new file mode 100644 index 00000000..a16a8125 --- /dev/null +++ b/src/main/resources/io/rpg/viewmodel/game-end-view.fxml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + From c07e031be0fd35714db121fc3c4c1c2435b25ede Mon Sep 17 00:00:00 2001 From: co012 Date: Thu, 19 May 2022 21:07:29 +0200 Subject: [PATCH 2/2] chore: made it retain size --- src/main/java/io/rpg/controller/Controller.java | 15 +++++++++------ src/main/java/io/rpg/view/GameEndView.java | 1 + .../java/io/rpg/viewmodel/GameEndViewModel.java | 4 +--- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/io/rpg/controller/Controller.java b/src/main/java/io/rpg/controller/Controller.java index 160899c5..e06802a3 100644 --- a/src/main/java/io/rpg/controller/Controller.java +++ b/src/main/java/io/rpg/controller/Controller.java @@ -8,26 +8,25 @@ import io.rpg.model.data.MouseClickedEvent; import io.rpg.model.data.Position; import io.rpg.model.location.LocationModel; -import io.rpg.model.object.*; +import io.rpg.model.object.GameObject; +import io.rpg.model.object.Question; import io.rpg.util.Result; import io.rpg.view.GameEndView; import io.rpg.view.GameObjectView; import io.rpg.view.LocationView; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.LinkedHashMap; +import java.util.List; import javafx.geometry.Point2D; import javafx.scene.Scene; import javafx.scene.input.KeyEvent; import javafx.scene.input.MouseButton; -import javafx.scene.layout.Pane; import javafx.stage.Stage; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.NotNull; -import java.util.LinkedHashMap; -import java.util.List; - public class Controller implements KeyboardEvent.Observer, MouseClickedEvent.Observer, ActionConsumer { private Scene currentView; private LinkedHashMap tagToLocationModelMap; @@ -110,7 +109,11 @@ private void onAction(LocationChangeAction action) { private void onAction(GameEndAction action) { GameEndView view = GameEndView.load(); view.setDescription(action.description); + double prevWidth = mainStage.getWidth(); + double prevHeight = mainStage.getHeight(); mainStage.setScene(view); + mainStage.setWidth(prevWidth); + mainStage.setHeight(prevHeight); } public Scene getView() { @@ -157,7 +160,7 @@ public void onKeyboardEvent(KeyboardEvent event) { case F -> popupController.openPointsPopup(5, getWindowCenterX(), getWindowCenterY()); case G -> popupController.openTextPopup("Hello!", getWindowCenterX(), getWindowCenterY()); case Q -> popupController.openQuestionPopup(new Question("How many bits are there in one byte?", new String[]{"1/8", "1024", "8", "256"}, 'C'), getWindowCenterX(), getWindowCenterY()); - case L -> consumeAction((Action) new LocationChangeAction("location-2", new Position(1, 2))); + case L -> consumeAction(new LocationChangeAction("location-2", new Position(1, 2))); case U -> consumeAction(new GameEndAction("You have pressed the forbidden button")); } } diff --git a/src/main/java/io/rpg/view/GameEndView.java b/src/main/java/io/rpg/view/GameEndView.java index eeaca984..ed68725d 100644 --- a/src/main/java/io/rpg/view/GameEndView.java +++ b/src/main/java/io/rpg/view/GameEndView.java @@ -7,6 +7,7 @@ public class GameEndView extends Scene { private GameEndViewModel viewModel; + private GameEndView(Parent parent) { super(parent); } diff --git a/src/main/java/io/rpg/viewmodel/GameEndViewModel.java b/src/main/java/io/rpg/viewmodel/GameEndViewModel.java index 588a4b1f..40ba380f 100644 --- a/src/main/java/io/rpg/viewmodel/GameEndViewModel.java +++ b/src/main/java/io/rpg/viewmodel/GameEndViewModel.java @@ -10,10 +10,8 @@ public class GameEndViewModel { public static final String GAME_END_VIEW_FXML = "game-end-view.fxml"; - private Parent parent; - @FXML - private Label descriptionLabel; + @FXML private Label descriptionLabel; public static GameEndViewModel load() throws IOException { URL url = Objects.requireNonNull(GameEndViewModel.class.getResource(GAME_END_VIEW_FXML));