Added game view to lounge viwe via anchor pane, currently there is a dedicated button for starting a game, might need to be removed later

This commit is contained in:
Seraina 2022-05-01 12:41:59 +02:00
parent 505577817e
commit 1b30cd6435
3 changed files with 58 additions and 13 deletions

View File

@ -29,6 +29,9 @@ public class ChatApp extends Application {
private static LoungeSceneViewController loungeSceneViewController; private static LoungeSceneViewController loungeSceneViewController;
private LoungeSceneViewController lSVController; private LoungeSceneViewController lSVController;
public Node chat;
public Node game;
public ChatApp() { public ChatApp() {
super(); super();
LOGGER.info("Empty ChatApp constructor got called: "); LOGGER.info("Empty ChatApp constructor got called: ");
@ -122,20 +125,24 @@ public class ChatApp extends Application {
public void start(Stage primaryStage) throws Exception { public void start(Stage primaryStage) throws Exception {
this.setcModel(clientModel); this.setcModel(clientModel);
this.setGameC(gameController); this.setGameC(gameController);
this.setlSVController(loungeSceneViewController);
gameC.setClient(cModel); gameC.setClient(cModel);
gameC.setGameStateModel(GameController.getGameStateModel()); gameC.setGameStateModel(GameController.getGameStateModel());
URL chatResource = ChatApp.class.getResource( try {
"chat/ChatView.fxml"); URL chatResource = ChatApp.class.getResource("chat/ChatView.fxml");
URL gameResource = ChatApp.class.getResource( URL gameResource = ChatApp.class.getResource("game/GameDayAll.fxml");
"game/GameDayAll.fxml"); this.chat = FXMLLoader.load(Objects.requireNonNull(chatResource));
this.game = FXMLLoader.load(Objects.requireNonNull(gameResource));
} catch (Exception e) {
LOGGER.warn(e.getMessage());
}
URL loungeResource = ChatApp.class.getResource( URL loungeResource = ChatApp.class.getResource(
"lounge/LoungeSceneView.fxml"); "lounge/LoungeSceneView.fxml");
try { try {
Parent lounge = FXMLLoader.load( Parent lounge = FXMLLoader.load(
Objects.requireNonNull(loungeResource)); Objects.requireNonNull(loungeResource));
Node chat = FXMLLoader.load(Objects.requireNonNull(chatResource)); this.setlSVController(loungeSceneViewController);
Node game = FXMLLoader.load(Objects.requireNonNull(gameResource)); lSVController.setChatApp(this);
// TODO bin chatController.getChatPaneRoot() border to root border for rezising // TODO bin chatController.getChatPaneRoot() border to root border for rezising
Scene scene = new Scene(lounge); Scene scene = new Scene(lounge);
scene.setRoot(lounge); scene.setRoot(lounge);

View File

@ -1,5 +1,6 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.lounge; package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.lounge;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ClientModel; import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ClientModel;
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ChatApp; import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ChatApp;
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.events.ChangeNameButtonPressedEventHandler; import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.events.ChangeNameButtonPressedEventHandler;
@ -9,8 +10,10 @@ import java.net.URL;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import javafx.application.Application; import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.binding.StringBinding; import javafx.beans.binding.StringBinding;
import javafx.beans.property.SimpleListProperty; import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleMapProperty; import javafx.beans.property.SimpleMapProperty;
@ -23,7 +26,9 @@ import javafx.collections.ObservableMap;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.event.EventHandler; import javafx.event.EventHandler;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.ListCell; import javafx.scene.control.ListCell;
@ -35,14 +40,22 @@ import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox; import javafx.scene.layout.HBox;
import javafx.scene.text.Text; import javafx.scene.text.Text;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class LoungeSceneViewController implements Initializable { public class LoungeSceneViewController implements Initializable {
public static final Logger LOGGER = LogManager.getLogger(LoungeSceneViewController.class);
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
@FXML @FXML
public Button leaveLobbyButton; private Button leaveLobbyButton;
@FXML @FXML
public Button newGameButton; private Button startGame;
@FXML
private Button newGameButton;
@FXML
private AnchorPane gameAnchorPane;
@FXML @FXML
private ListView<HBox> LobbyListView; private ListView<HBox> LobbyListView;
@FXML @FXML
@ -61,7 +74,8 @@ public class LoungeSceneViewController implements Initializable {
private ToolBar NTtBToolBar; private ToolBar NTtBToolBar;
public static ClientModel client; public static ClientModel client;
public static ChatApp chatApp; private static ChatApp chatApp;
private ChatApp cApp;
private ObservableMap<String, ObservableList<String>> lobbyToMemberssMap; private ObservableMap<String, ObservableList<String>> lobbyToMemberssMap;
private HashMap<String, String> clientToLobbyMap; private HashMap<String, String> clientToLobbyMap;
@ -71,6 +85,13 @@ public class LoungeSceneViewController implements Initializable {
lobbyToMemberssMap = FXCollections.observableHashMap(); lobbyToMemberssMap = FXCollections.observableHashMap();
} }
public void setChatApp(ChatApp chatApp) {
LoungeSceneViewController.chatApp = chatApp;
}
public void setcApp(ChatApp cApp) {
this.cApp = cApp;
}
/** /**
* Called to initialize a controller after its root element has been completely processed. * Called to initialize a controller after its root element has been completely processed.
@ -82,6 +103,7 @@ public class LoungeSceneViewController implements Initializable {
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
ChatApp.setLoungeSceneViewController(this); ChatApp.setLoungeSceneViewController(this);
setcApp(chatApp);
ChangeNameButton.setOnAction(event -> changeName()); ChangeNameButton.setOnAction(event -> changeName());
LeaveServerButton.setOnAction(event -> leaveServer()); LeaveServerButton.setOnAction(event -> leaveServer());
newGameButton.setOnAction(event -> newGame()); newGameButton.setOnAction(event -> newGame());
@ -100,6 +122,19 @@ public class LoungeSceneViewController implements Initializable {
}); });
} }
public void addGameView(){
Platform.runLater(new Runnable(){
@Override
public void run() {
try {
gameAnchorPane.getChildren().add(chatApp.game);
} catch (Exception e) {
LOGGER.debug("Not yet initialized");
}
}
});
}
public void updateClientListView(ObservableList<SimpleStringProperty> names) { public void updateClientListView(ObservableList<SimpleStringProperty> names) {
ObservableList<SimpleStringProperty> clientsLeft = ClientListView.getItems(); ObservableList<SimpleStringProperty> clientsLeft = ClientListView.getItems();
clientsLeft.removeAll(names); clientsLeft.removeAll(names);
@ -162,12 +197,13 @@ public class LoungeSceneViewController implements Initializable {
LobbyListView.getItems().add(lobby); LobbyListView.getItems().add(lobby);
} }
private void joinGame(String lobbyID) { public void joinGame(String lobbyID) {
client.getClient().sendMsgToServer(Protocol.joinLobby + "$" + lobbyID); client.getClient().sendMsgToServer(Protocol.joinLobby + "$" + lobbyID);
} }
private void startGame() { public void startGame() {
client.getClient().sendMsgToServer(Protocol.startANewGame); client.getClient().sendMsgToServer(Protocol.startANewGame);
addGameView();
} }
public void leaveLobby() {client.getClient().sendMsgToServer(Protocol.leaveLobby);} public void leaveLobby() {client.getClient().sendMsgToServer(Protocol.leaveLobby);}

View File

@ -22,7 +22,9 @@
<center> <center>
<AnchorPane BorderPane.alignment="CENTER"> <AnchorPane BorderPane.alignment="CENTER">
<children> <children>
<Button fx:id="newGameButton" layoutX="136.0" layoutY="105.0" mnemonicParsing="false" text="New Game" /> <Button fx:id="newGameButton" layoutX="136.0" layoutY="105.0" mnemonicParsing="false" text="New Lobby" />
<AnchorPane fx:id="gameAnchorPane" />
<Button fx:id="startGame" layoutX="136.0" layoutY="141.0" mnemonicParsing="false" onAction="#startGame" text="Start Game" />
</children> </children>
</AnchorPane> </AnchorPane>
</center> </center>