diff --git a/src/main/java/de/hsfulda/onses/App.java b/src/main/java/de/hsfulda/onses/App.java index bda0c20..fff6bd7 100644 --- a/src/main/java/de/hsfulda/onses/App.java +++ b/src/main/java/de/hsfulda/onses/App.java @@ -1,18 +1,44 @@ package de.hsfulda.onses; import de.hsfulda.onses.controllers.AppController; +import de.hsfulda.onses.controllers.Controller; import de.hsfulda.onses.services.GameService; import javafx.application.Application; +import javafx.scene.Parent; import javafx.scene.Scene; +import javafx.scene.control.Label; import javafx.stage.Stage; +import java.io.IOException; + public class App extends Application { + + private Stage stage; + private Controller controller; @Override - public void start(Stage stage) throws Exception { - final AppController appController = new AppController(new GameService(), stage); + public void start(Stage stage) { + this.stage = stage; + final AppController appController = new AppController(this, new GameService()); stage.setTitle("Onses - Uno"); - stage.setScene(new Scene(appController.render())); + stage.setScene(new Scene(new Label("Loading..."))); + + show(appController); stage.show(); } + + public void show(Controller controller) { + try { + final Parent parent = controller.render(); + stage.getScene().setRoot(parent); + } catch (IOException e) { + throw new RuntimeException(e); + } + + if(this.controller != null) { + this.controller.destroy(); + } + this.controller = controller; + stage.setTitle(controller.getTitle()); + } } diff --git a/src/main/java/de/hsfulda/onses/controllers/AppController.java b/src/main/java/de/hsfulda/onses/controllers/AppController.java index d2e0b80..b5de34e 100644 --- a/src/main/java/de/hsfulda/onses/controllers/AppController.java +++ b/src/main/java/de/hsfulda/onses/controllers/AppController.java @@ -1,13 +1,11 @@ package de.hsfulda.onses.controllers; +import de.hsfulda.onses.App; import de.hsfulda.onses.Main; -import de.hsfulda.onses.models.Game; import de.hsfulda.onses.services.GameService; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; -import javafx.scene.Scene; import javafx.scene.control.Button; -import javafx.stage.Stage; import java.io.IOException; import java.util.Objects; @@ -15,11 +13,11 @@ import java.util.Objects; public class AppController implements Controller { private final GameService gameService; - private final Stage stage; + private final App app; - public AppController(GameService gameService, Stage stage) { + public AppController(App app, GameService gameService) { + this.app = app; this.gameService = gameService; - this.stage = stage; } @Override public Parent render() throws IOException { @@ -28,13 +26,18 @@ public class AppController implements Controller { Button button = (Button) parent.lookup("#startGameBtn"); button.setOnAction(e -> { - try { - stage.setScene(new Scene(gameController.render())); - stage.setTitle("Onses - Uno Game"); - } catch (IOException ex) { - throw new RuntimeException(ex); - } + app.show(gameController); }); return parent; } + + @Override + public String getTitle() { + return "Onses - Uno"; + } + + @Override + public void destroy() { + + } } diff --git a/src/main/java/de/hsfulda/onses/controllers/CardController.java b/src/main/java/de/hsfulda/onses/controllers/CardController.java index 5e2aef6..0df3e65 100644 --- a/src/main/java/de/hsfulda/onses/controllers/CardController.java +++ b/src/main/java/de/hsfulda/onses/controllers/CardController.java @@ -84,4 +84,14 @@ public class CardController implements Controller { private String addStyle(String oldStyle, String newStyle) { return oldStyle + "; " + newStyle; } + + @Override + public String getTitle() { + return "Onses - Uno Game"; + } + + @Override + public void destroy() { + + } } diff --git a/src/main/java/de/hsfulda/onses/controllers/Controller.java b/src/main/java/de/hsfulda/onses/controllers/Controller.java index 3a92766..4ce5b11 100644 --- a/src/main/java/de/hsfulda/onses/controllers/Controller.java +++ b/src/main/java/de/hsfulda/onses/controllers/Controller.java @@ -6,4 +6,8 @@ import java.io.IOException; public interface Controller { Parent render() throws IOException; + + void destroy(); + + String getTitle(); } diff --git a/src/main/java/de/hsfulda/onses/controllers/GameController.java b/src/main/java/de/hsfulda/onses/controllers/GameController.java index 7ab7151..2f7ba5c 100644 --- a/src/main/java/de/hsfulda/onses/controllers/GameController.java +++ b/src/main/java/de/hsfulda/onses/controllers/GameController.java @@ -62,4 +62,14 @@ public class GameController implements Controller { return parent; } + + @Override + public String getTitle() { + return "Onses - Uno Game"; + } + + @Override + public void destroy() { + + } } diff --git a/src/main/java/de/hsfulda/onses/controllers/PlayerController.java b/src/main/java/de/hsfulda/onses/controllers/PlayerController.java index 0779359..6742b09 100644 --- a/src/main/java/de/hsfulda/onses/controllers/PlayerController.java +++ b/src/main/java/de/hsfulda/onses/controllers/PlayerController.java @@ -9,7 +9,6 @@ import javafx.scene.control.Label; import javafx.scene.layout.HBox; import java.io.IOException; -import java.util.ArrayList; import java.util.Objects; public class PlayerController implements Controller { @@ -44,4 +43,14 @@ public class PlayerController implements Controller { return parent; } + + @Override + public String getTitle() { + return "Onses - Uno Game"; + } + + @Override + public void destroy() { + + } } diff --git a/src/main/resources/de/hsfulda/onses/views/app.fxml b/src/main/resources/de/hsfulda/onses/views/app.fxml index 7205ca5..42e2610 100644 --- a/src/main/resources/de/hsfulda/onses/views/app.fxml +++ b/src/main/resources/de/hsfulda/onses/views/app.fxml @@ -5,14 +5,14 @@ - + - diff --git a/src/test/java/de/hsfulda/onses/GuiTest.java b/src/test/java/de/hsfulda/onses/GuiTest.java index 845c004..eec7715 100644 --- a/src/test/java/de/hsfulda/onses/GuiTest.java +++ b/src/test/java/de/hsfulda/onses/GuiTest.java @@ -10,11 +10,13 @@ import static org.testfx.matcher.control.LabeledMatchers.*; public class GuiTest extends ApplicationTest { private Stage stage; + private App app; @Override public void start(Stage stage) throws Exception { this.stage = stage; - new App().start(stage); + app = new App(); + app.start(stage); } @Test