Browse Source

add LastPlayedCard PropertyChangeListener

main
fdai7736 11 months ago
parent
commit
e62424afd5
  1. 22
      src/main/java/de/hsfulda/onses/controllers/GameController.java
  2. 3
      src/main/resources/de/hsfulda/onses/views/game.fxml

22
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;

3
src/main/resources/de/hsfulda/onses/views/game.fxml

@ -6,7 +6,6 @@
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="690.0" prefWidth="1254.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button fx:id="btnWishBlue" layoutX="891.0" layoutY="558.0" mnemonicParsing="false" style="-fx-background-color: blue; -fx-pref-width: 30px; -fx-pref-height: 30px;" AnchorPane.bottomAnchor="100.0" AnchorPane.rightAnchor="280.0">
@ -29,7 +28,7 @@
<Cursor fx:constant="HAND" />
</cursor>
</Button>
<Button layoutX="1085.0" layoutY="586.0" mnemonicParsing="false" text="Play Card">
<Button fx:id="playCardBtn" layoutX="1085.0" layoutY="586.0" mnemonicParsing="false" text="Play Card">
<font>
<Font size="18.0" />
</font>

Loading…
Cancel
Save