Browse Source

add remove propertyChange listeners at destroy of controller

main^2
fdai7736 11 months ago
parent
commit
e3a96ec193
  1. 3
      README.md
  2. 2
      src/main/java/de/hsfulda/onses/App.java
  3. 9
      src/main/java/de/hsfulda/onses/controllers/AppController.java
  4. 12
      src/main/java/de/hsfulda/onses/controllers/CardController.java
  5. 11
      src/main/java/de/hsfulda/onses/controllers/GameController.java
  6. 9
      src/main/java/de/hsfulda/onses/controllers/PlayerController.java

3
README.md

@ -1,5 +1,8 @@
# Onses - Uno Game
## Disclaimer for Tests
**!!Do not move the mouse while tests are running!!**
## git setup after clone
```shell
git config user.name "<FD-Nummer>"

2
src/main/java/de/hsfulda/onses/App.java

@ -20,8 +20,8 @@ public class App extends Application {
this.stage = stage;
final AppController appController = new AppController(this, new GameService());
stage.setTitle("Onses - Uno");
stage.setScene(new Scene(new Label("Loading...")));
stage.setOnCloseRequest(e -> controller.destroy());
show(appController);
stage.show();

9
src/main/java/de/hsfulda/onses/controllers/AppController.java

@ -8,7 +8,6 @@ import javafx.scene.Parent;
import javafx.scene.control.Button;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects;
public class AppController implements Controller {
@ -16,16 +15,14 @@ public class AppController implements Controller {
private final GameService gameService;
private final App app;
private final ArrayList<Controller> controllers = new ArrayList<>();
public AppController(App app, GameService gameService) {
this.app = app;
this.gameService = gameService;
}
@Override
public Parent render() throws IOException {
GameController gameController = new GameController(app, gameService);
controllers.add(gameController);
final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/app.fxml")));
Button button = (Button) parent.lookup("#startGameBtn");
@ -42,8 +39,6 @@ public class AppController implements Controller {
@Override
public void destroy() {
for (Controller controller : controllers) {
controller.destroy();
}
}
}

12
src/main/java/de/hsfulda/onses/controllers/CardController.java

@ -8,6 +8,7 @@ import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.util.Objects;
@ -16,10 +17,13 @@ public class CardController implements Controller {
private final Card card;
private final Player player;
private PropertyChangeListener cardSelectedChangeListener;
public CardController(Card card, Player player) {
this.card = card;
this.player = player;
}
@Override
public Parent render() throws IOException {
final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/card.fxml")));
@ -68,7 +72,7 @@ public class CardController implements Controller {
});
}
card.listeners().addPropertyChangeListener(Card.PROPERTY_SELECTED, e -> {
cardSelectedChangeListener = e -> {
boolean oldValue = (boolean) e.getOldValue();
if(oldValue) {
mainPane.setStyle(addStyle(mainPane.getStyle(), "-fx-border-color: black"));
@ -76,8 +80,8 @@ public class CardController implements Controller {
mainPane.setStyle(addStyle(mainPane.getStyle(), "-fx-border-color: pink"));
}
});
};
card.listeners().addPropertyChangeListener(Card.PROPERTY_SELECTED, cardSelectedChangeListener);
return parent;
}
@ -93,6 +97,6 @@ public class CardController implements Controller {
@Override
public void destroy() {
card.listeners().removePropertyChangeListener(Card.PROPERTY_SELECTED, cardSelectedChangeListener);
}
}

11
src/main/java/de/hsfulda/onses/controllers/GameController.java

@ -10,7 +10,7 @@ import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects;
@ -20,7 +20,7 @@ public class GameController implements Controller {
private final Game game;
private final App app;
private PropertyChangeEvent lastPlayedCardPropertyChangeEvent;
private PropertyChangeListener lastPlayedCardPropertyChangeListener;
private final ArrayList<Controller> controllers = new ArrayList<>();
public GameController(App app, GameService gameService) {
@ -28,6 +28,7 @@ public class GameController implements Controller {
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")));
@ -46,7 +47,7 @@ public class GameController implements Controller {
controllers.add(playerController);
controllers.add(enemyController);
game.listeners().addPropertyChangeListener(Game.PROPERTY_LAST_PLAYED_CARD, e -> {
lastPlayedCardPropertyChangeListener = e -> {
lastPlayedCardPane.getChildren().removeAll();
try {
CardController tmp = new CardController((Card) e.getNewValue(), null);
@ -55,7 +56,8 @@ public class GameController implements Controller {
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
};
game.listeners().addPropertyChangeListener(Game.PROPERTY_LAST_PLAYED_CARD, lastPlayedCardPropertyChangeListener);
exitGameButton.setOnAction(e -> {
app.show(new AppController(app, new GameService()));
@ -93,5 +95,6 @@ public class GameController implements Controller {
for (Controller controller : controllers) {
controller.destroy();
}
game.listeners().removePropertyChangeListener(Game.PROPERTY_LAST_PLAYED_CARD, lastPlayedCardPropertyChangeListener);
}
}

9
src/main/java/de/hsfulda/onses/controllers/PlayerController.java

@ -8,6 +8,7 @@ import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects;
@ -16,10 +17,12 @@ public class PlayerController implements Controller {
private final Player player;
private final ArrayList<Controller> controllers = new ArrayList<>();
private PropertyChangeListener playerDeckChangeListener;
public PlayerController(Player player) {
this.player = player;
}
@Override
public Parent render() throws IOException {
final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/player.fxml")));
@ -32,7 +35,7 @@ public class PlayerController implements Controller {
cards.getChildren().add(newCardController.render());
}
player.listeners().addPropertyChangeListener(Player.PROPERTY_PLAYER_DECK, e -> {
playerDeckChangeListener = e -> {
cards.getChildren().clear();
for(Card card : player.getPlayerDeck()) {
try {
@ -41,7 +44,8 @@ public class PlayerController implements Controller {
throw new RuntimeException(ex);
}
}
});
};
player.listeners().addPropertyChangeListener(Player.PROPERTY_PLAYER_DECK, playerDeckChangeListener);
playerNameLabel.setText(player.getPlayerName());
@ -58,5 +62,6 @@ public class PlayerController implements Controller {
for (Controller controller : controllers) {
controller.destroy();
}
player.listeners().removePropertyChangeListener(Player.PROPERTY_PLAYER_DECK, playerDeckChangeListener);
}
}
Loading…
Cancel
Save