Skip to content

Commit

Permalink
feat: Gui JoinScene LobbyScene prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
LapisBerry committed May 19, 2024
1 parent 8f0cc1c commit fbc8bc7
Show file tree
Hide file tree
Showing 20 changed files with 390 additions and 14 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ plugins {
id 'application'
id 'org.openjfx.javafxplugin' version '0.1.0'
}

/*
group = 'com.lapisberry'
version = '1.0-SNAPSHOT'
version = '1.0-SNAPSHOT'*/

repositories {
mavenCentral()
Expand Down
27 changes: 15 additions & 12 deletions src/main/java/com/lapisberry/Main.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
package com.lapisberry;

import com.lapisberry.gui.scenes.JoinScene;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application {
private static Stage primaryStage;

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage stage) {
String javaVersion = System.getProperty("java.version");
String javafxVersion = System.getProperty("javafx.version");
Label l = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
Scene scene = new Scene(new StackPane(l), 640, 480);
stage.setScene(scene);
stage.show();
}
primaryStage = stage;
primaryStage.setScene(new JoinScene());

public static void main(String[] args) {
launch();
primaryStage.show();

// This line will make the username field unfocused when started.
primaryStage.getScene().getRoot().requestFocus();
}

public static Stage getPrimaryStage() {
return primaryStage;
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/lapisberry/gui/FontPreloader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.lapisberry.gui;

public final class FontPreloader {
public static final String Inter_Black = ClassLoader.getSystemResource("fonts/Inter/Inter-Black.ttf").toString();
public static final String Inter_ExtraBold = ClassLoader.getSystemResource("fonts/Inter/Inter-ExtraBold.ttf").toString();
public static final String Inter_Bold = ClassLoader.getSystemResource("fonts/Inter/Inter-Bold.ttf").toString();
public static final String Inter_SemiBold = ClassLoader.getSystemResource("fonts/Inter/Inter-SemiBold.ttf").toString();
public static final String Inter_Medium = ClassLoader.getSystemResource("fonts/Inter/Inter-Medium.ttf").toString();
public static final String Inter_Regular = ClassLoader.getSystemResource("fonts/Inter/Inter-Regular.ttf").toString();
public static final String Inter_Light = ClassLoader.getSystemResource("fonts/Inter/Inter-Light.ttf").toString();
public static final String Inter_ExtraLight = ClassLoader.getSystemResource("fonts/Inter/Inter-ExtraLight.ttf").toString();
public static final String Inter_Thin = ClassLoader.getSystemResource("fonts/Inter/Inter-Thin.ttf").toString();
}
51 changes: 51 additions & 0 deletions src/main/java/com/lapisberry/gui/MediaController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.lapisberry.gui;

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.util.Duration;

/**
* The {@code MediaController} class is used to trigger the media player
* <p>
* Example of usage:
* <pre>
* import application.MediaController;
* MediaController.play(MediaController.monkeysSpinningMonkeysMusic);
*/
public final class MediaController {
// Preload all musics
// Music should play forever

// Set volume for music
static {

}
// preload all sound fxs
// sound fx should play one time
public static final MediaPlayer buttonClickSound = loadRes("audios/button-click.mp3");
// Set volume for sound fx
static {
buttonClickSound.setVolume(0.8);
}


// Methods
public static void playMediaOnce(MediaPlayer mediaPlayer) {
mediaPlayer.seek(Duration.ZERO);
mediaPlayer.play();
}

public static void playMediaForever(MediaPlayer mediaPlayer) {
mediaPlayer.seek(Duration.ZERO);
mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
mediaPlayer.play();
}

public static void stop(MediaPlayer mediaPlayer) {
mediaPlayer.stop();
}

private static MediaPlayer loadRes(String path) {
return new MediaPlayer(new Media(ClassLoader.getSystemResource(path).toString()));
}
}
79 changes: 79 additions & 0 deletions src/main/java/com/lapisberry/gui/scenes/JoinScene.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.lapisberry.gui.scenes;

import com.lapisberry.Main;
import com.lapisberry.gui.MediaController;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;

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

public class JoinScene extends Scene {
public JoinScene() {
super(new StackPane(new Container()), 1280, 720);
}

private static class Container extends VBox {
private Container() {
super();
Title title = new Title("MaKillMai");
InputField username = new InputField("username");
InputField ipAddress = new InputField("ip-address");
JoinButton joinButton = new JoinButton("Join");

getChildren().addAll(title, username, ipAddress, joinButton);
setMaxWidth(400);
setMaxHeight(200);
setAlignment(Pos.CENTER);
setSpacing(17);
}
}

private static class Title extends Text {
private Title(String text) {
super(text);
setFont(Font.loadFont(Inter_Black, 128));
setTextAlignment(TextAlignment.CENTER);
}
}

private static class InputField extends TextField {
private InputField(String promptText) {
super();
setPromptText(promptText);
setFont(Font.loadFont(Inter_ExtraLight, 40));
setBackground(new Background(new BackgroundFill(Color.valueOf("D9D9D9"), new CornerRadii(10), null)));
setMaxWidth(400);
setMinHeight(80);
setAlignment(Pos.CENTER);
}
}

private static class JoinButton extends Button {
private JoinButton(String text) {
super(text);
setFont(Font.loadFont(Inter_SemiBold, 40));
setBackground(new Background(new BackgroundFill(Color.valueOf("00C2FF"), new CornerRadii(40), null)));
setMaxWidth(274);
setMinHeight(80);
setAlignment(Pos.CENTER);
setOnMouseEntered(e -> {
setBackground(new Background(new BackgroundFill(Color.valueOf("00A6D1"), new CornerRadii(40), null)));
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))));
setOnAction(e -> {
Main.getPrimaryStage().setScene(new LobbyScene());
MediaController.playMediaOnce(MediaController.buttonClickSound);
});
}
}
}
115 changes: 115 additions & 0 deletions src/main/java/com/lapisberry/gui/scenes/LobbyScene.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.lapisberry.gui.scenes;

import com.lapisberry.Main;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;

import static com.lapisberry.gui.FontPreloader.Inter_Black;
import static com.lapisberry.gui.FontPreloader.Inter_SemiBold;

public class LobbyScene extends Scene {
public LobbyScene() {
super(new StackPane(new Container()), Main.getPrimaryStage().getScene().getWidth(), Main.getPrimaryStage().getScene().getHeight());
}

private static class Container extends VBox {
private Container() {
super();
Title title = new Title("Lobby");
PlayerPanel playerPanel = new PlayerPanel();
StartButton startButton = new StartButton("Start Game");

//VBox vbox = new VBox();
getChildren().addAll(title, playerPanel, startButton);
setMaxWidth(400);
setMaxHeight(200);
setAlignment(Pos.CENTER);
setSpacing(17);
}
}

private static class Title extends Text {
private Title(String text) {
super(text);
setFont(Font.loadFont(Inter_Black, 128));
setTextAlignment(TextAlignment.CENTER);
}
}

private static class PlayerPanel extends VBox {
private PlayerPanel() {
super();
SubTitle subTitle = new SubTitle("Players in this lobby");
PlayerList playerList = new PlayerList();
setAlignment(Pos.TOP_CENTER);
setBackground(new Background(new BackgroundFill(Color.valueOf("D9D9D9"), new CornerRadii(20), null)));
setMinWidth(420);
setMinHeight(450);
setPadding(new Insets(15, 20, 15, 20));
setSpacing(20);
getChildren().addAll(subTitle, playerList);
}

private static class SubTitle extends Text {
private SubTitle(String text) {
super(text);
setFont(Font.loadFont(Inter_SemiBold, 36));
setTextAlignment(TextAlignment.CENTER);
}
}

private static class PlayerList extends ScrollPane {
private PlayerList() {
super();
VBox vbox = new VBox();
vbox.getChildren().addAll(new PlayerItem("Player 1"), new PlayerItem("Player 2"), new PlayerItem("Player 1"), new PlayerItem("Player 2"), new PlayerItem("Player 1"), new PlayerItem("Player 2"), new PlayerItem("Player 1"), new PlayerItem("Player 2"));
vbox.setMinWidth(336);
vbox.setBackground(new Background(new BackgroundFill(Color.valueOf("D9D9D9"), new CornerRadii(0), null)));
vbox.setAlignment(Pos.TOP_CENTER);
vbox.setSpacing(8);
setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
setMaxWidth(338);
setBackground(new Background(new BackgroundFill(Color.TRANSPARENT, new CornerRadii(0), null)));
setContent(vbox);
}
}

private static class PlayerItem extends Label {
private PlayerItem(String text) {
super(text);
setBackground(new Background(new BackgroundFill(Color.valueOf("32CEFF"), new CornerRadii(10), null)));
setFont(Font.loadFont(Inter_SemiBold, 20));
setAlignment(Pos.CENTER);
setMinWidth(300);
setMinHeight(55);
}
}
}

private static class StartButton extends Button {
private StartButton(String text) {
super(text);
setFont(Font.loadFont(Inter_SemiBold, 40));
setBackground(new Background(new BackgroundFill(Color.valueOf("44FF02"), new CornerRadii(40), null)));
setMaxWidth(274);
setMinHeight(80);
setAlignment(Pos.CENTER);
setOnMouseEntered(e -> {
setBackground(new Background(new BackgroundFill(Color.valueOf("3cbd0f"), new CornerRadii(40), null)));
setCursor(javafx.scene.Cursor.HAND);
});
setOnMouseExited(e -> setBackground(new Background(new BackgroundFill(Color.valueOf("44FF02"), new CornerRadii(40), null))));
setOnMousePressed(e -> setBackground(new Background(new BackgroundFill(Color.valueOf("2b850c"), new CornerRadii(40), null))));
setOnMouseReleased(e -> setBackground(new Background(new BackgroundFill(Color.valueOf("44FF02"), new CornerRadii(40), null))));
}
}
}
31 changes: 31 additions & 0 deletions src/main/java/com/lapisberry/net/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.lapisberry.net;

import com.lapisberry.utils.Config;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class Client implements Runnable {
// Fields
private final Socket socket;
private final ObjectInputStream inputStream;
private final ObjectOutputStream outputStream;

// Constructors
public Client(final String host) {
try {
this.socket = new Socket(host, Config.PORT);
this.outputStream = new ObjectOutputStream(socket.getOutputStream());
this.inputStream = new ObjectInputStream(socket.getInputStream());
} catch (IOException e) {
throw new RuntimeException("Client cannot be created.");
}
}

// Methods
@Override
public void run() {
}
}
33 changes: 33 additions & 0 deletions src/main/java/com/lapisberry/net/ClientHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.lapisberry.net;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class ClientHandler implements Runnable {
// Fields
private Server server;
private Socket socket;
private ObjectOutputStream outputStream;
private ObjectInputStream inputStream;


// Constructors
public ClientHandler(Server server, Socket socket) {
this.server = server;
this.socket = socket;
try {
outputStream = new ObjectOutputStream(socket.getOutputStream());
inputStream = new ObjectInputStream(socket.getInputStream());
} catch (IOException e) {
System.out.println("ClientHandler cannot be created.");
}
}


// Methods
@Override
public void run() {
}
}
Loading

0 comments on commit fbc8bc7

Please sign in to comment.