Skip to content

Commit

Permalink
fix: user can create server manually by starting thread
Browse files Browse the repository at this point in the history
  • Loading branch information
LapisBerry committed May 22, 2024
1 parent a46e97e commit d1dea6b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/lapisberry/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public void start(Stage stage) {

public static void createServer() {
server = new Server();
new Thread(server, "Server thread").start();
}

public static void closeServer() {
Expand Down
49 changes: 36 additions & 13 deletions src/main/java/com/lapisberry/gui/scenes/JoinScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.lapisberry.Main;
import com.lapisberry.gui.MediaController;
import com.lapisberry.net.Server;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
Expand All @@ -13,6 +12,8 @@
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;

import java.util.concurrent.atomic.AtomicReference;

import static com.lapisberry.gui.FontPreloader.*;

public class JoinScene extends Scene {
Expand Down Expand Up @@ -60,18 +61,22 @@ private InputField(String promptText) {
private static class JoinButton extends Button {
private JoinButton(String text) {
super(text);
final Background regularBackground = new Background(new BackgroundFill(Color.valueOf("00C2FF"), new CornerRadii(40), null));
final Background hoverBackground = new Background(new BackgroundFill(Color.valueOf("00A6D1"), new CornerRadii(40), null));
final Background pressedBackground = new Background(new BackgroundFill(Color.valueOf("0089A9"), new CornerRadii(40), null));

setFont(Font.loadFont(Inter_SemiBold, 40));
setBackground(new Background(new BackgroundFill(Color.valueOf("00C2FF"), new CornerRadii(40), null)));
setBackground(regularBackground);
setMaxWidth(274);
setMinHeight(80);
setAlignment(Pos.CENTER);
setOnMouseEntered(e -> {
setBackground(new Background(new BackgroundFill(Color.valueOf("00A6D1"), new CornerRadii(40), null)));
setBackground(hoverBackground);
setCursor(javafx.scene.Cursor.HAND);
});
setOnMouseExited(e -> setBackground(new Background(new BackgroundFill(Color.valueOf("00C2FF"), new CornerRadii(40), null))));
setOnMousePressed(e -> setBackground(new Background(new BackgroundFill(Color.valueOf("0089A9"), new CornerRadii(40), null))));
setOnMouseReleased(e -> setBackground(new Background(new BackgroundFill(Color.valueOf("00A6D1"), new CornerRadii(40), null))));
setOnMouseExited(e -> setBackground(regularBackground));
setOnMousePressed(e -> setBackground(pressedBackground));
setOnMouseReleased(e -> setBackground(hoverBackground));
setOnAction(e -> {
Main.getPrimaryStage().setScene(new LobbyScene());
MediaController.playMediaOnce(MediaController.buttonClickSound);
Expand All @@ -84,25 +89,43 @@ private static class CreateServerButton extends Button {

private CreateServerButton() {
super("Create Server");
final Background blueRegularBackground = new Background(new BackgroundFill(Color.valueOf("00C2FF"), new CornerRadii(40), null));
final Background blueHoverBackground = new Background(new BackgroundFill(Color.valueOf("00A6D1"), new CornerRadii(40), null));
final Background bluePressedBackground = new Background(new BackgroundFill(Color.valueOf("0089A9"), new CornerRadii(40), null));

final Background redRegularBackground = new Background(new BackgroundFill(Color.valueOf("FF0000"), new CornerRadii(40), null));
final Background redHoverBackground = new Background(new BackgroundFill(Color.valueOf("D10000"), new CornerRadii(40), null));
final Background redPressedBackground = new Background(new BackgroundFill(Color.valueOf("890000"), new CornerRadii(40), null));

AtomicReference<Background> regularBackground = new AtomicReference<>(blueRegularBackground);
AtomicReference<Background> hoverBackground = new AtomicReference<>(blueHoverBackground);
AtomicReference<Background> pressedBackground = new AtomicReference<>(bluePressedBackground);

setFont(Font.loadFont(Inter_SemiBold, 18));
setBackground(new Background(new BackgroundFill(Color.valueOf("00C2FF"), new CornerRadii(20), null)));
setBackground(regularBackground.get());
setMaxWidth(180);
setMinHeight(40);
setAlignment(Pos.CENTER);
setOnMouseEntered(e -> {
setBackground(new Background(new BackgroundFill(Color.valueOf("00A6D1"), new CornerRadii(20), null)));
setBackground(hoverBackground.get());
setCursor(javafx.scene.Cursor.HAND);
});
setOnMouseExited(e -> setBackground(new Background(new BackgroundFill(Color.valueOf("00C2FF"), new CornerRadii(20), null))));
setOnMousePressed(e -> setBackground(new Background(new BackgroundFill(Color.valueOf("0089A9"), new CornerRadii(20), null))));
setOnMouseReleased(e -> setBackground(new Background(new BackgroundFill(Color.valueOf("00A6D1"), new CornerRadii(20), null))));
setOnMouseExited(e -> setBackground(regularBackground.get()));
setOnMousePressed(e -> setBackground(pressedBackground.get()));
setOnMouseReleased(e -> setBackground(hoverBackground.get()));
setOnAction(e -> {
if (isServerCreated) {
setText("Create Server");
Main.closeServer();
setText("Create Server");
regularBackground.set(blueRegularBackground);
hoverBackground.set(blueHoverBackground);
pressedBackground.set(bluePressedBackground);
} else {
setText("Close Server");
Main.createServer();
setText("Close Server");
regularBackground.set(redRegularBackground);
hoverBackground.set(redHoverBackground);
pressedBackground.set(redPressedBackground);
}
isServerCreated = !isServerCreated;
});
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/lapisberry/net/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.lapisberry.utils.Config;

import java.io.Closeable;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
Expand Down

0 comments on commit d1dea6b

Please sign in to comment.