Merge remote-tracking branch 'origin/master'

This commit is contained in:
Seraina
2022-04-30 16:52:19 +02:00
11 changed files with 337 additions and 96 deletions

View File

@@ -2,6 +2,19 @@ package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.multiplayer.client.Client;
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.lounge.ClientListItem;
import ch.unibas.dmi.dbis.cs108.multiplayer.server.Lobby;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.collections.ObservableSet;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -14,9 +27,17 @@ public class ClientModel {
private Client client;
private String incomingChatMsg;
private ObservableList<ClientListItem> allClients;
private ObservableMap<Integer, SimpleStringProperty> idToNameMap;
private HashSet<String> clientsOnServer;
public ClientModel(String username, Client client) {
this.username = username;
this.client = client;
this.allClients = FXCollections.observableArrayList();
this.idToNameMap = FXCollections.observableHashMap();
}
//private Number;
@@ -36,4 +57,23 @@ public class ClientModel {
public void setUsername(String username) {
this.username = username;
}
public ObservableList<ClientListItem> getAllClients() {
return allClients;
}
public void addClientToList(ClientListItem nameAndId) {
this.allClients.add(nameAndId);
}
public void removeClientFromList(int id){
Iterator<ClientListItem> it = allClients.iterator();
while(it.hasNext()){
int uid = it.next().getId();
if(uid == id){
it.remove();
break;
}
}
}
}

View File

@@ -3,9 +3,8 @@ package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.chat;
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.utils.ChatLabelConfigurator;
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol;
import com.sun.javafx.scene.control.Properties;
import com.sun.javafx.scene.control.inputmap.KeyBinding;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;
@@ -13,8 +12,6 @@ import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ListChangeListener;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Group;
@@ -22,18 +19,13 @@ import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -99,20 +91,14 @@ public class ChatController implements Initializable {
public void initialize(URL location, ResourceBundle resources) {
setClient(ChatApp.getClientModel());
ChatApp.setChatController(this);
vBoxChatMessages.getChildren().addListener(new ListChangeListener<Node>() {
@Override
public void onChanged(Change<? extends Node> c) {
vBoxChatMessages.setMaxHeight(vBoxChatMessages.getMaxHeight() * 2);
}
});
vBoxChatMessages.getChildren().addListener(
(ListChangeListener<Node>) c -> vBoxChatMessages.setMaxHeight(
vBoxChatMessages.getMaxHeight() * 2));
vBoxChatMessages.heightProperty().addListener(new ChangeListener<Number>() {
vBoxChatMessages.heightProperty().addListener(new ChangeListener<>() {
/**
* TODO: implement
* Adjust the hight when new messages come in.
* @param observable
* @param oldValue
* @param newValue
* Adjust the height when new messages come in.
*/
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue,
@@ -124,30 +110,15 @@ public class ChatController implements Initializable {
/**
* Initialize what happens when the send button is pressed
*/
sendButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
sendChatMsg();
}
});
sendButton.setOnAction(event -> sendChatMsg());
chatMsgField.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
sendChatMsg();
}
});
chatMsgField.setOnAction(event -> sendChatMsg());
/**
* Initialize the change of the TextArea field holding potential chat messages
*/
chatMsgField.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue,
String newValue) {
chatMsgField.setText(newValue);
}
});
chatMsgField.textProperty().addListener(
(observable, oldValue, newValue) -> chatMsgField.setText(newValue));
//Possibly now the whisperTargetChosenProperty is obsolete
whisperTargetSelectField.textProperty().addListener(new ChangeListener<String>() {
@@ -171,19 +142,10 @@ public class ChatController implements Initializable {
client.getClient().sendMsgToServer(cmd.toString() + msg);
LOGGER.info("Message trying to send is: " + cmd.toString() + msg);
Text t;
Label l;
if (cmd.startsWith(whisper)) {
t = new Text("You whispered to " + whisperTargetSelectField.getText() + ": " + msg);
l = new Label("You whispered to " + whisperTargetSelectField.getText() + ": " + msg);
l.setBackground(Background.fill(Color.LAVENDERBLUSH));
} else {
t = new Text(client.getUsername() + " (you): " + msg);
l = new Label(client.getUsername() + " (you): " + msg);
l.setBackground(Background.fill(Color.LAVENDER));
l.setWrapText(true);
l.setMaxHeight(Double.MAX_VALUE);
l.setScaleShape(true);
}
//Configure what to put in the Users ChatView
Label l = ChatLabelConfigurator.configure(cmd,msg,whisperTargetSelectField,client);
vBoxChatMessages.getChildren().add(l);
chatMsgField.clear();
} else {
@@ -204,7 +166,7 @@ public class ChatController implements Initializable {
* @param client who's gui controller this should be
*/
public void setClient(ClientModel client) {
this.client = client;
ChatController.client = client;
}
public Pane getChatPaneRoot() {

View File

@@ -0,0 +1,40 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.lounge;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
public class ClientListItem {
private SimpleStringProperty name;
private final SimpleIntegerProperty id;
public ClientListItem(SimpleStringProperty name, SimpleIntegerProperty id) {
this.name = name;
this.id = id;
}
@Override
public String toString(){
return name + " ID: " + id;
}
public String getName() {
return name.get();
}
public SimpleStringProperty nameProperty() {
return name;
}
public void setName(String name) {
this.name.set(name);
}
public int getId() {
return id.get();
}
public SimpleIntegerProperty idProperty() {
return id;
}
}

View File

@@ -1,14 +1,22 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.lounge;
import java.util.List;
import java.util.Set;
import javafx.beans.property.StringProperty;
import javafx.scene.control.Label;
import javafx.scene.control.ToggleButton;
public class LobbyListItem {
private Label lobbyID;
private Label adminName;
private List<String> clientInLobby;
private ToggleButton button;
private final Label lobbyID;
private final Label adminName;
private Set<StringProperty> clientsInLobby;
private final ToggleButton button;
public LobbyListItem(Label lobbyID, Label adminName,
Set<StringProperty> clientsInLobby, ToggleButton button) {
this.lobbyID = lobbyID;
this.adminName = adminName;
this.clientsInLobby = clientsInLobby;
this.button = button;
}
}

View File

@@ -1,5 +1,76 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.lounge;
public class LoungeSceneViewController {
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ClientModel;
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.events.ChangeNameButtonPressedEventHandler;
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.events.LeaveServerButtonPressedEventHandler;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.property.StringProperty;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.ToolBar;
import javafx.scene.control.TreeView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
public class LoungeSceneViewController implements Initializable {
@FXML
private TreeView LobbyTreeView;
@FXML
private ListView ClientListView;
@FXML
private Button ChangeNameButton;
@FXML
private Button LeaveServerButton;
@FXML
private AnchorPane ChatArea;
@FXML
private HBox ChatAreaHBox;
@FXML
private BorderPane LoungeSceneBorderPane;
@FXML
private ToolBar NTtBToolBar;
public static ClientModel client;
/**
* Called to initialize a controller after its root element has been completely processed.
*
* @param location The location used to resolve relative paths for the root object, or {@code
* null} if the location is not known.
* @param resources The resources used to localize the root object, or {@code null} if
*/
@Override
public void initialize(URL location, ResourceBundle resources) {
ChangeNameButton.setOnAction(new ChangeNameButtonPressedEventHandler());
LeaveServerButton.setOnAction(new LeaveServerButtonPressedEventHandler());
}
public void updateLobbyListView(){
}
public void updateClientListView(){
}
public void addClientToList(ClientListItem player) {
ListCell<StringProperty> playerCell = new ListCell<>();
ClientListView.ite
}
/**
* Utility to set the client model for this class
* @param client, the client model
*/
public static void setClient(ClientModel client) {
LoungeSceneViewController.client = client;
}
}

View File

@@ -0,0 +1,36 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.utils;
import static ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol.whisper;
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ClientModel;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.Background;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
/**
* Provides utilities to configure Labels for the ChatView {@see ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.chat.ChatView.fxml}
* within {@link ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.chat.ChatController}
*/
public class ChatLabelConfigurator {
public static Label configure(String cmd, String msg, TextField whisperTargetSelectField, ClientModel client) {
Label l;
if (cmd.startsWith(whisper)) {
//t = new Text("You whispered to " + whisperTargetSelectField.getText() + ": " + msg);
l = new Label("You whispered to " + whisperTargetSelectField.getText() + ": " + msg);
l.setBackground(Background.fill(Color.LAVENDERBLUSH));
} else {
//t = new Text(client.getUsername() + " (you): " + msg);
l = new Label(client.getUsername() + " (you): " + msg);
l.setBackground(Background.fill(Color.LAVENDER));
l.setWrapText(true);
l.setMaxHeight(Double.MAX_VALUE);
l.setScaleShape(true);
}
return l;
}
}