Skip to content

Commit

Permalink
- added ability to resize window
Browse files Browse the repository at this point in the history
- added code to fit window to screen on load
- refactored javafx to support window resizing
- added stylesheet for javafx
- added reconnection overlay
- removed check on server IP input
- added code to prevent users from having same username
  • Loading branch information
NLucas0 committed Jul 31, 2023
1 parent 74c648d commit f2c3297
Show file tree
Hide file tree
Showing 13 changed files with 317 additions and 125 deletions.
17 changes: 8 additions & 9 deletions src/main/java/com/wordhunter/client/logic/ClientListening.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ public ClientListening(Socket aSock, ClientMain aParent) {
messageToCallback.put("playerDisconnect", ClientListening::playerDisconnect);
messageToCallback.put("error", ClientListening::error);
messageToCallback.put("", ClientListening::heartBeatAck);

messageToCallback.put("startTimer", ClientListening::updateStartTimer);
messageToCallback.put("gameStart", ClientListening::displayGameScreen);
messageToCallback.put("gameOver", ClientListening::endGameScreen);

messageToCallback.put("addNewWord", ClientListening::processNewWord);
messageToCallback.put("removeWord", ClientListening::handleCompletedWord);
messageToCallback.put("reserveWord", ClientListening::handleReserveWord);
messageToCallback.put("reopenWord", ClientListening::handleReopenWord);
messageToCallback.put("startTimer", ClientListening::updateStartTimer);

SceneController.getInstance().toggleReconnectionOverlay(false); // TODO: move to reconnect handle when implemented
}

/**
Expand Down Expand Up @@ -100,12 +104,12 @@ public void run() {

/**
* disconnect()
* TODO: test reconnection state saving
* disconnect with server (if theres any cleanup or reconnect attempt, start from here)
* disconnect with server and attempt reconnection if max retries not reached
*/
public void disconnect() throws IOException {
sock.close();
if (ClientMain.reconnectAttempts < ClientMain.reconnectMaxAttempt) {
SceneController.getInstance().toggleReconnectionOverlay(true);
System.out.println("reconnection attempt " + ClientMain.reconnectAttempts);
parent.connectServer(true);
}
Expand Down Expand Up @@ -133,13 +137,9 @@ public void handleServerMessage(String msg) {
}
}

// message event functions
// TODO: add other keyword functions (see message_formats.txt)

/**
* newPlayerJoin()
* TODO: add code to extract own colorId from here too
*
* extracts own colorId if needed, updates player list
* @param input message from server
*/
public void newPlayerJoin(String input) {
Expand All @@ -149,7 +149,6 @@ public void newPlayerJoin(String input) {
// get own color id
if (parent.colorId.isEmpty()) {
parent.colorId = players.elementAt(players.size() - 1).getColor();
//System.out.println("got color id:" + parent.colorId);
}

Platform.runLater(() -> {
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/wordhunter/client/ui/ReconnectionOverlay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.wordhunter.client.ui;

import javafx.fxml.FXMLLoader;
import javafx.scene.layout.VBox;

import java.io.IOException;

public class ReconnectionOverlay extends VBox
{

public ReconnectionOverlay() {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("ReconnectionOverlay.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(ReconnectionOverlay.this);

try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
}
79 changes: 69 additions & 10 deletions src/main/java/com/wordhunter/client/ui/SceneController.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,50 @@
package com.wordhunter.client.ui;

import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Screen;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.Objects;

public class SceneController {

// components
private Stage stage;
private Scene scene;
private ReconnectionOverlay reconnectionOverlay;

// device screen size
private double maxHeight;
private double maxWidth;

// min size
private double minHeight = 400;
private double minWidth = 600;

// controllers
private static SceneController sceneController;

public static SceneController getInstance(){
if (sceneController == null){
sceneController = new SceneController();
}
return sceneController;
}
private SceneController(){

/**
* SceneController()
* constructor. initializes reconnection overlay and max width/height
*/
private SceneController()
{
reconnectionOverlay = new ReconnectionOverlay();

maxWidth = Screen.getPrimary().getVisualBounds().getWidth();
maxHeight = Screen.getPrimary().getVisualBounds().getHeight();
}

public void showStartPage() {
Expand All @@ -36,9 +60,6 @@ public void showStartPage() {
stage.setTitle("WordHunter");
stage.setScene(scene);
stage.show();

// Make not resizable
stage.setResizable(false);
}

public void showWaitingPage() {
Expand All @@ -53,8 +74,6 @@ public void showWaitingPage() {

stage.setScene(scene);
stage.show();

stage.setResizable(false);
}

public WordHunterController showGamePage() {
Expand All @@ -74,17 +93,57 @@ public WordHunterController showGamePage() {
stage.show();
WordHunterController.getInstance();

stage.setResizable(false);

return fxmlLoader.getController();
}

public void setStage(Stage stage){
/**
* setStage()
* set stage, resize window to fit screen, bind close button to close entire app
* @param stage stage
*/
public void setStage(Stage stage)
{
this.stage = stage;

// set min/max sizes
this.stage.setMinWidth(minWidth);
this.stage.setMinHeight(minHeight);

// fit to screen
this.stage.setWidth(maxWidth*.7);
this.stage.setHeight((maxWidth*.7)/1.5);

// make sure program closes on window close
stage.setOnCloseRequest(t -> {
Platform.exit();
System.exit(0);
});
}

public void closeStage() {
stage.close();
}

/**
* toggleReconnectionOverlay()
* show/hide reconnection overlay
* @param show boolean
*/
public void toggleReconnectionOverlay(boolean show)
{
Platform.runLater(() -> {
try
{
StackPane container = (StackPane) scene.getRoot();
if (show) {
container.getChildren().add(reconnectionOverlay);
// move focus away from all components (mainly text boxes)
reconnectionOverlay.requestFocus();
} else {
container.getChildren().remove(reconnectionOverlay);
}
}
catch (ClassCastException ignored){}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,7 @@ public void joinButtonClicked(ActionEvent e){
clientMain.setUsername(this.username);

String address = ipAddressField.getText();
if ((address.length() <= 11 && isNumeric(address) || address.length() == 0)) {
clientMain.setAddress(address);
} else {
this.label.setText("Address must be numeric!");
this.label.setStyle("-fx-text-fill: #ff0000; ");
return;
}
clientMain.setAddress(address);
try {
clientMain.connectServer(false);
SceneController.getInstance().showWaitingPage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;

import java.io.IOException;
import java.util.Vector;

public class WordHunterController {
public ProgressBar healthBarBar;
@FXML
private GridPane grids;

Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/wordhunter/client/ui/WordPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public class WordPane extends StackPane {
// Constant
// Word font
private static final Font FONT = new Font("Arial", 30);
private static final Font FONT = new Font("Arial", 20);
public static final String BORDER = "-fx-border-color: #000000; -fx-border-width: 2px;";
public static final Color DEFAULT_COLOR = Color.WHITE;

Expand Down Expand Up @@ -70,7 +70,11 @@ public void initAnimation(long interval){
}

public void closeAnimation() {
animation.stop();
animation = null;
try
{
animation.stop();
animation = null;
}
catch(NullPointerException ignored){}
}
}
13 changes: 11 additions & 2 deletions src/main/java/com/wordhunter/server/ServerAcceptClients.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,17 @@ else if (ServerMain.clientLimit <= ServerMain.playerList.size()) {
+ "maximum clients reached");
in.close();
client.close();
} else {
}
else {
// check if username already taken
Optional<Player> foundPlayer = ServerMain.playerList.stream()
.filter(player -> player.getName().equals(msg[1]))
.findFirst();
if(foundPlayer.isPresent())
{
ServerMain.sendMessageToClient(client, "error" + ServerMain.messageDelimiter
+ "username taken");
}
// create player obj and store socket in there
newPlayerJoinHandle(msg[1], client);
}
Expand Down Expand Up @@ -166,7 +176,6 @@ private void newPlayerJoinHandle(String username, Socket client) {
new Player(username, colorIds.remove(0), client)
);


PlayerThread newPlayerThread = new PlayerThread(this, client, playerCount - 1);
newPlayerThread.start();

Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/wordhunter/server/ServerMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,3 @@ public static void sendMessageToClient(Socket sock, String message)
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--overlay when player reconnecting. stops all mouse clicks but doesn't pause any animations-->

<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ProgressIndicator?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<fx:root alignment="CENTER"
style="-fx-background-color: #000000cc;"
type="javafx.scene.layout.VBox"
xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml/1"
stylesheets="@WordHunterStyleSheet.css">
<!--spinner-->
<ProgressIndicator prefHeight="46.0" prefWidth="600.0"/>
<!--message-->
<Label alignment="CENTER" blendMode="ADD" text="Reconnecting..." textFill="WHITE">
<font>
<Font size="26.0"/>
</font>
</Label>
</fx:root>
Loading

0 comments on commit f2c3297

Please sign in to comment.