Adding Wrapper Classes for Controlls and corresponding EventHandlers. added different fxml views.

This commit is contained in:
Sebastian Lenzlinger 2022-04-28 12:04:08 +02:00
parent 26dffdbbdc
commit 2bc79b53a7
17 changed files with 229 additions and 57 deletions

View File

@ -0,0 +1,5 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui;
public class ClientListViewController {
}

View File

@ -0,0 +1,5 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui;
public class LobbySceneViewController {
}

View File

@ -0,0 +1,5 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui;
public class ServerMessageViewController {
}

View File

@ -0,0 +1,7 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.buttons;
import javafx.scene.control.Button;
public class ChangeNameButton extends Button {
}

View File

@ -0,0 +1,7 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.buttons;
import java.awt.Button;
public class JoinGameButton extends Button {
}

View File

@ -0,0 +1,7 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.buttons;
import javafx.scene.control.Button;
public class LeaveServerButton extends Button {
}

View File

@ -0,0 +1,7 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.buttons;
import javafx.scene.control.Button;
public class NewGameButton extends Button {
}

View File

@ -4,6 +4,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.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;
@ -15,16 +17,22 @@ import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Group;
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.TextFlow;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -33,6 +41,12 @@ public class ChatController implements Initializable {
public static final Logger LOGGER = LogManager.getLogger(ChatController.class);
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
@FXML
private Group vboxGroup;
@FXML
private GridPane vBoxGridPane;
@FXML
private ScrollPane ChatScrollPane;
@FXML
private VBox vBoxServerMessage;
@FXML
@ -103,32 +117,23 @@ public class ChatController implements Initializable {
public void changed(ObservableValue<? extends Number> observable, Number oldValue,
Number newValue) {
vBoxChatMessages.setMaxHeight(newValue.doubleValue());
ChatScrollPane.setMaxHeight(newValue.doubleValue()*2);
}
});
/**
* Initialize what happens when the send button is pressed
*/
sendButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
String msg = chatMsgField.getText().split("\\R")[0]; //cut off extra lines, if present.
if (!msg.isEmpty()) {
client.getClient().sendMsgToServer(cmd.toString() + msg);
LOGGER.info("Message trying to send is: " + cmd.toString() + msg);
Label l;
if (cmd.startsWith(whisper)) {
l = new Label("You whispered to " + whisperTargetSelectField.getText() + ": " + msg);
l.setBackground(Background.fill(Color.LAVENDERBLUSH));
} else {
l = new Label(client.getUsername() + " (you): " + msg);
l.setBackground(Background.fill(Color.LAVENDER));
}
vBoxChatMessages.getChildren().add(l);
chatMsgField.clear();
} else {
LOGGER.debug("Trying to send an empty message.");
}
sendChatMsg();
}
});
chatMsgField.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
sendChatMsg();
}
});
@ -158,6 +163,29 @@ public class ChatController implements Initializable {
});
}
private void sendChatMsg() {
String msg = chatMsgField.getText();//.split("\\R")[0]; //cut off extra lines, if present.
if (!msg.isEmpty()) {
client.getClient().sendMsgToServer(cmd.toString() + msg);
LOGGER.info("Message trying to send is: " + cmd.toString() + msg);
Label l;
if (cmd.startsWith(whisper)) {
l = new Label("You whispered to " + whisperTargetSelectField.getText() + ": " + msg);
l.setBackground(Background.fill(Color.LAVENDERBLUSH));
} else {
l = new Label(client.getUsername() + " (you): " + msg);
l.setBackground(Background.fill(Color.LAVENDER));
l.setWrapText(true);
l.setMaxHeight(Double.MAX_VALUE);
l.setScaleShape(true);
}
vBoxChatMessages.getChildren().add(l);
chatMsgField.clear();
} else {
LOGGER.debug("Trying to send an empty message.");
}
}
/**
* @return the ClientModel whose chat controller this is
*/
@ -185,6 +213,8 @@ public class ChatController implements Initializable {
*/
public void addChatMsgToView(String msg) {
Label l = new Label(msg);
l.setWrapText(true);
l.setMaxHeight(Double.MAX_VALUE);
if (msg.contains("whispers")) {
l.setBackground(Background.fill(Color.SLATEBLUE));
} else {

View File

@ -0,0 +1,17 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.events;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public class ChangeNameButtonPressedEventHandler implements EventHandler<ActionEvent> {
/**
* Invoked when a specific event of the type for which this handler is registered happens.
*
* @param event the event which occurred
*/
@Override
public void handle(ActionEvent event) {
}
}

View File

@ -0,0 +1,17 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.events;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public class JoinGameButtonPressedEventHandler implements EventHandler<ActionEvent> {
/**
* Invoked when a specific event of the type for which this handler is registered happens.
*
* @param event the event which occurred
*/
@Override
public void handle(ActionEvent event) {
}
}

View File

@ -0,0 +1,17 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.events;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public class LeaveServerButtonPressedEventHandler implements EventHandler<ActionEvent> {
/**
* Invoked when a specific event of the type for which this handler is registered happens.
*
* @param event the event which occurred
*/
@Override
public void handle(ActionEvent event) {
}
}

View File

@ -0,0 +1,17 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.events;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public class NewGameButtonPressedEventHandler implements EventHandler<ActionEvent> {
/**
* Invoked when a specific event of the type for which this handler is registered happens.
*
* @param event the event which occurred
*/
@Override
public void handle(ActionEvent event) {
}
}

View File

@ -0,0 +1,5 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.lounge;
public class LoungeSceneViewController {
}

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ClientListViewController"
prefHeight="400.0" prefWidth="600.0">
</AnchorPane>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ServerMessageViewController"
prefHeight="400.0" prefWidth="600.0">
</AnchorPane>

View File

@ -2,6 +2,7 @@
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ScrollBar?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
@ -9,10 +10,11 @@
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane fx:id="chatPaneRoot" prefHeight="251.0" prefWidth="343.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.chat.ChatController">
<children>
<GridPane alignment="CENTER" layoutY="149.0" prefHeight="326.0" prefWidth="360.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<GridPane alignment="CENTER" layoutY="149.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="251.0" prefWidth="343.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" percentWidth="100.0" />
</columnConstraints>
@ -21,45 +23,18 @@
<RowConstraints maxHeight="-Infinity" minHeight="10.0" percentHeight="30.0" valignment="CENTER" vgrow="ALWAYS" />
</rowConstraints>
<children>
<AnchorPane>
<AnchorPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308">
<children>
<ScrollPane layoutX="149.0" layoutY="-18.0" prefHeight="194.0" prefWidth="360.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<ScrollPane fx:id="ChatScrollPane" fitToHeight="true" fitToWidth="true" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="176.0" prefWidth="343.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<content>
<AnchorPane prefHeight="235.0" prefWidth="312.0">
<children>
<GridPane alignment="CENTER" prefHeight="235.0" prefWidth="312.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" percentWidth="50.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="1.7976931348623157E308" minWidth="10.0" percentWidth="50.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<VBox fx:id="vBoxChatMessages" spacing="2.0">
<GridPane.margin>
<Insets right="2.0" />
</GridPane.margin>
</VBox>
<VBox fx:id="vBoxServerMessage" alignment="TOP_RIGHT" spacing="2.0" GridPane.columnIndex="1">
<GridPane.margin>
<Insets left="2.0" />
</GridPane.margin>
</VBox>
</children>
</GridPane>
</children>
</AnchorPane>
<VBox fx:id="vBoxChatMessages" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minWidth="-Infinity" prefWidth="170.0" spacing="2.0" />
</content>
</ScrollPane>
</children>
<padding>
<Insets bottom="2.0" left="5.0" right="5.0" top="5.0" />
</padding>
</AnchorPane>
<AnchorPane GridPane.rowIndex="1">
<AnchorPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" GridPane.rowIndex="1">
<children>
<GridPane alignment="CENTER" layoutX="10.0" layoutY="20.0" prefHeight="74.0" prefWidth="343.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<GridPane alignment="CENTER" layoutX="10.0" layoutY="20.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="75.0" prefWidth="343.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" percentWidth="7.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
@ -69,25 +44,28 @@
<RowConstraints minHeight="10.0" percentHeight="90.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<AnchorPane prefHeight="92.0" prefWidth="98.0" GridPane.halignment="CENTER" GridPane.hgrow="ALWAYS" GridPane.valignment="CENTER" GridPane.vgrow="ALWAYS">
<AnchorPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="92.0" prefWidth="98.0" GridPane.halignment="CENTER" GridPane.hgrow="ALWAYS" GridPane.valignment="CENTER" GridPane.vgrow="ALWAYS">
<children>
<Button fx:id="sendButton" mnemonicParsing="false" prefHeight="100.0" prefWidth="120.0" text="Send" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
<Button fx:id="sendButton" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="157.0" prefWidth="223.5" text="Send" textOverrun="CENTER_WORD_ELLIPSIS" wrapText="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<font>
<Font size="10.0" />
</font></Button>
</children>
<GridPane.margin>
<Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
</GridPane.margin>
</AnchorPane>
<AnchorPane prefHeight="92.0" prefWidth="85.0" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.hgrow="ALWAYS" GridPane.valignment="CENTER" GridPane.vgrow="ALWAYS">
<AnchorPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="92.0" prefWidth="85.0" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.hgrow="ALWAYS" GridPane.valignment="CENTER" GridPane.vgrow="ALWAYS">
<children>
<TextField fx:id="whisperTargetSelectField" prefHeight="92.0" prefWidth="160.0" promptText="whisper..." AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
<TextField fx:id="whisperTargetSelectField" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="50.0" prefWidth="79.0" promptText="whisper..." AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
<GridPane.margin>
<Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
</GridPane.margin>
</AnchorPane>
<AnchorPane prefHeight="92.0" prefWidth="366.0" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.hgrow="ALWAYS" GridPane.valignment="CENTER" GridPane.vgrow="ALWAYS">
<AnchorPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="92.0" prefWidth="366.0" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.hgrow="ALWAYS" GridPane.valignment="CENTER" GridPane.vgrow="ALWAYS">
<children>
<TextField fx:id="chatMsgField" prefHeight="100.0" prefWidth="273.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
<TextField fx:id="chatMsgField" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="50.0" prefWidth="213.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
<GridPane.margin>
<Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
@ -108,5 +86,19 @@
</AnchorPane>
</children>
</GridPane>
<GridPane fx:id="vBoxGridPane" alignment="CENTER" disable="true" layoutX="-14.0" layoutY="-235.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="167.0" prefWidth="331.0">
<columnConstraints>
<ColumnConstraints hgrow="ALWAYS" maxWidth="1.7976931348623157E308" minWidth="10.0" percentWidth="73.0" prefWidth="100.0" />
<ColumnConstraints hgrow="ALWAYS" maxWidth="1.7976931348623157E308" minWidth="10.0" percentWidth="66.0" prefWidth="100.0" />
<ColumnConstraints hgrow="ALWAYS" maxWidth="1.7976931348623157E308" minWidth="10.0" percentWidth="7.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints fillHeight="false" maxHeight="1.7976931348623157E308" minHeight="10.0" percentHeight="100.0" vgrow="ALWAYS" />
</rowConstraints>
<children>
<ScrollBar orientation="VERTICAL" prefHeight="196.0" prefWidth="16.0" GridPane.columnIndex="2" />
<VBox fx:id="vBoxServerMessage" alignment="TOP_RIGHT" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="184.0" prefWidth="212.0" spacing="2.0" GridPane.columnIndex="1" />
</children>
</GridPane>
</children>
</AnchorPane>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="1080.0" prefWidth="1920.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" />