From e62424afd58a555bce236747838559b4c7cdef7c Mon Sep 17 00:00:00 2001 From: fdai7736 Date: Tue, 6 Feb 2024 22:49:11 +0100 Subject: [PATCH] add LastPlayedCard PropertyChangeListener --- .../onses/controllers/GameController.java | 22 ++++++++++++++++++- .../de/hsfulda/onses/views/game.fxml | 3 +-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/hsfulda/onses/controllers/GameController.java b/src/main/java/de/hsfulda/onses/controllers/GameController.java index 37f616f..2da4cb3 100644 --- a/src/main/java/de/hsfulda/onses/controllers/GameController.java +++ b/src/main/java/de/hsfulda/onses/controllers/GameController.java @@ -2,9 +2,11 @@ package de.hsfulda.onses.controllers; import de.hsfulda.onses.Main; import de.hsfulda.onses.models.Card; +import de.hsfulda.onses.models.Game; import de.hsfulda.onses.services.GameService; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; +import javafx.scene.control.Button; import javafx.scene.layout.Pane; import java.io.IOException; @@ -12,15 +14,33 @@ import java.util.Objects; public class GameController implements Controller { private final GameService gameService; + private final Game game; public GameController(GameService gameService) { this.gameService = gameService; + this.game = gameService.getGame(); } @Override public Parent render() throws IOException { final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/game.fxml"))); + final Pane lastPlayedCardPane = (Pane) parent.lookup("#lastPlayedCardPane"); + + final Button playButton = (Button) parent.lookup("#playCardBtn"); CardController lastPlayedCardController = new CardController(new Card().setValue(Card.Value.FIVE).setColor(Card.Color.BLUE)); - Pane lastPlayedCardPane = (Pane) parent.lookup("#lastPlayedCardPane"); + game.listeners().addPropertyChangeListener(Game.PROPERTY_LAST_PLAYED_CARD, e -> { + lastPlayedCardPane.getChildren().removeAll(); + try { + lastPlayedCardPane.getChildren().add(new CardController((Card) e.getNewValue()).render()); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + }); + + playButton.setOnAction(e -> { + gameService.playCard(new Card().setColor(Card.Color.GREEN).setValue(Card.Value.ONE)); + }); + + lastPlayedCardPane.getChildren().add(lastPlayedCardController.render()); return parent; diff --git a/src/main/resources/de/hsfulda/onses/views/game.fxml b/src/main/resources/de/hsfulda/onses/views/game.fxml index ca16664..5ca0e51 100644 --- a/src/main/resources/de/hsfulda/onses/views/game.fxml +++ b/src/main/resources/de/hsfulda/onses/views/game.fxml @@ -6,7 +6,6 @@ - -