Skip to content

Commit

Permalink
Move UI into separate entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
logandhillon committed Sep 20, 2024
1 parent 393a08d commit 2f28a7c
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 44 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group 'net.logandhillon'
version '0.9.1-ALPHA'
version '0.9.2-ALPHA'

repositories {
mavenCentral()
Expand Down
1 change: 1 addition & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
requires java.desktop;

exports net.logandhillon.icx;
exports net.logandhillon.icx.ui to javafx.graphics;
}
25 changes: 4 additions & 21 deletions src/main/java/net/logandhillon/icx/ICX.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
package net.logandhillon.icx;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import net.logandhillon.icx.server.ICXServer;
import net.logandhillon.icx.ui.view.LoginView;
import net.logandhillon.icx.ui.UI;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;

public class ICX extends Application {
public class ICX {
private static final Logger LOG = LoggerContext.getContext().getLogger(ICX.class);
public static Stage stage;

@Override
public void start(Stage stage) {
ICX.stage = stage;
LOG.info("Initializing UI views");
Scene scene = new Scene(new LoginView());
stage.setTitle("ICX");
stage.setScene(scene);
stage.show();
}

public static void main(String[] args) {
boolean isServer = false;
Expand All @@ -31,14 +17,11 @@ public static void main(String[] args) {
}

if (isServer) serverMain();
else clientMain();
else UI.startClient();
System.exit(0);
}

private static void clientMain() {
LOG.info("Starting ICX client");
launch();
}


private static void serverMain() {
LOG.info("Starting ICX server");
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/net/logandhillon/icx/client/S2CHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import net.logandhillon.icx.ICX;
import net.logandhillon.icx.common.ICXPacket;
import net.logandhillon.icx.ui.UI;
import net.logandhillon.icx.ui.view.ChatView;
import net.logandhillon.icx.ui.view.LoginView;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -43,18 +43,13 @@ public void run() {
LOG.debug("Incoming: {}", packet);

switch (packet.command()) {
case SRV_KICK -> Platform.runLater(() -> {
ICX.stage.close();

case SRV_KICK -> Platform.runLater(() -> UI.reloadScene(new Scene(new LoginView()), () -> {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Kicked from server");
alert.setHeaderText("You have been kicked from " + ICXClient.getServerAddr());
alert.setContentText("Reason: " + packet.content());
alert.showAndWait();

ICX.stage.setScene(new Scene(new LoginView()));
ICX.stage.show();
});
}));
case SEND -> Platform.runLater(() -> ChatView.postMessage(packet.sender(), packet.content()));
case JOIN -> Platform.runLater(() -> ChatView.postAlert(String.format("Welcome, %s!", packet.sender())));
case EXIT -> Platform.runLater(() -> ChatView.postAlert(String.format("Farewell, %s!", packet.sender())));
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/net/logandhillon/icx/ui/UI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package net.logandhillon.icx.ui;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import net.logandhillon.icx.ui.view.LoginView;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;

public class UI extends Application {
private static final Logger LOG = LoggerContext.getContext().getLogger(UI.class);
public static Stage stage;

@Override
public void start(Stage stage) {
UI.stage = stage;
LOG.info("Initializing UI views");
Scene scene = new Scene(new LoginView());
stage.setTitle("ICX");
stage.setScene(scene);
stage.show();
}

public static void startClient() {
LOG.info("Starting ICX client");
launch();
}

public static void reloadScene(Scene newScene, Runnable silentAction) {
stage.close();
silentAction.run();
stage.setScene(newScene);
stage.show();
}

public static void reloadScene(Scene newScene) {
stage.close();
stage.setScene(newScene);
stage.show();
}
}
14 changes: 4 additions & 10 deletions src/main/java/net/logandhillon/icx/ui/view/ChatView.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import net.logandhillon.icx.ICX;
import net.logandhillon.icx.client.ICXClient;
import net.logandhillon.icx.common.ICXPacket;
import net.logandhillon.icx.ui.UI;
import net.logandhillon.icx.ui.component.MessageAlertComponent;
import net.logandhillon.icx.ui.component.MessageComponent;

Expand Down Expand Up @@ -47,21 +47,15 @@ public ChatView() {

private static HBox getHeader(Label screenName) {
Button leaveBtn = new Button("Exit");
leaveBtn.setOnAction(_e -> {
leaveBtn.setOnAction(_e -> UI.reloadScene(new Scene(new LoginView()), () -> {
try {
ICX.stage.close();

ICXClient.disconnect();
MESSAGES.getChildren().clear();

ICX.stage.setScene(new Scene(new LoginView()));
ICX.stage.show();
} catch (IOException e) {
Alert alert = new Alert(Alert.AlertType.ERROR, "Failed to disconnect: " + e.getMessage());
alert.showAndWait();
}

});
MESSAGES.getChildren().clear();
}));

Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/net/logandhillon/icx/ui/view/LoginView.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import net.logandhillon.icx.ICX;
import net.logandhillon.icx.client.ICXClient;
import net.logandhillon.icx.ui.UI;

import java.io.IOException;
import java.net.InetAddress;
Expand Down Expand Up @@ -48,9 +48,7 @@ private VBox getWrapper() {
return;
}
ICXClient.connect(inpName.getText(), InetAddress.getByName(inpServerAddr.getText()));
ICX.stage.close();
ICX.stage.setScene(new Scene(new ChatView()));
ICX.stage.show();
UI.reloadScene(new Scene(new ChatView()));
} catch (IOException ex) {
status.setText(ex.getMessage());
}
Expand Down

0 comments on commit 2f28a7c

Please sign in to comment.