diff --git a/app/src/main/java/org/toop/Main.java b/app/src/main/java/org/toop/Main.java index 05a34a4..8456819 100644 --- a/app/src/main/java/org/toop/Main.java +++ b/app/src/main/java/org/toop/Main.java @@ -8,14 +8,14 @@ import org.toop.framework.networking.NetworkingClientManager; import org.toop.framework.networking.NetworkingInitializationException; public final class Main { - public static void main(String[] args) { - initSystems(); - App.run(args); - } + public static void main(String[] args) { + initSystems(); + App.run(args); + } - private static void initSystems() throws NetworkingInitializationException { - ResourceManager.loadAssets(new ResourceLoader("app/src/main/resources/assets")); - new Thread(NetworkingClientManager::new).start(); - new Thread(SoundManager::new).start(); - } -} \ No newline at end of file + private static void initSystems() throws NetworkingInitializationException { + ResourceManager.loadAssets(new ResourceLoader("app/src/main/resources/assets")); + new Thread(NetworkingClientManager::new).start(); + new Thread(SoundManager::new).start(); + } +} diff --git a/app/src/main/java/org/toop/app/App.java b/app/src/main/java/org/toop/app/App.java index 38a4873..c4c9251 100644 --- a/app/src/main/java/org/toop/app/App.java +++ b/app/src/main/java/org/toop/app/App.java @@ -1,6 +1,11 @@ package org.toop.app; +import java.util.Stack; +import javafx.application.Application; import javafx.application.Platform; +import javafx.scene.Scene; +import javafx.scene.layout.StackPane; +import javafx.stage.Stage; import org.toop.app.layer.Layer; import org.toop.app.layer.layers.MainLayer; import org.toop.app.layer.layers.QuitPopup; @@ -9,155 +14,154 @@ import org.toop.framework.asset.resources.CssAsset; import org.toop.framework.audio.events.AudioEvents; import org.toop.framework.eventbus.EventFlow; import org.toop.local.AppContext; - -import javafx.application.Application; -import javafx.scene.Scene; -import javafx.scene.layout.StackPane; -import javafx.stage.Stage; import org.toop.local.AppSettings; -import java.util.Stack; - public final class App extends Application { - private static Stage stage; - private static Scene scene; - private static StackPane root; + private static Stage stage; + private static Scene scene; + private static StackPane root; - private static Stack stack; + private static Stack stack; private static int height; private static int width; - private static boolean isQuitting; + private static boolean isQuitting; - public static void run(String[] args) { - launch(args); - } + public static void run(String[] args) { + launch(args); + } - @Override - public void start(Stage stage) throws Exception { + @Override + public void start(Stage stage) throws Exception { final StackPane root = new StackPane(); - final Scene scene = new Scene(root); + final Scene scene = new Scene(root); - stage.setTitle(AppContext.getString("appTitle")); - stage.setWidth(1080); - stage.setHeight(720); + stage.setTitle(AppContext.getString("appTitle")); + stage.setWidth(1080); + stage.setHeight(720); - stage.setOnCloseRequest(event -> { - event.consume(); + stage.setOnCloseRequest( + event -> { + event.consume(); - if (!isQuitting) { - quitPopup(); - } - }); + if (!isQuitting) { + quitPopup(); + } + }); - stage.setScene(scene); - stage.setResizable(false); + stage.setScene(scene); + stage.setResizable(false); - stage.show(); + stage.show(); - App.stage = stage; - App.scene = scene; - App.root = root; + App.stage = stage; + App.scene = scene; + App.root = root; - App.stack = new Stack<>(); + App.stack = new Stack<>(); - App.width = (int) stage.getWidth(); - App.height = (int) stage.getHeight(); + App.width = (int) stage.getWidth(); + App.height = (int) stage.getHeight(); - App.isQuitting = false; + App.isQuitting = false; - final AppSettings settings = new AppSettings(); - settings.applySettings(); + final AppSettings settings = new AppSettings(); + settings.applySettings(); - new EventFlow().addPostEvent(new AudioEvents.StartBackgroundMusic()).asyncPostEvent(); - activate(new MainLayer()); - } + new EventFlow().addPostEvent(new AudioEvents.StartBackgroundMusic()).asyncPostEvent(); + activate(new MainLayer()); + } - public static void activate(Layer layer) { - Platform.runLater(() -> { - popAll(); - push(layer); - }); - } + public static void activate(Layer layer) { + Platform.runLater( + () -> { + popAll(); + push(layer); + }); + } - public static void push(Layer layer) { - Platform.runLater(() -> { - root.getChildren().addLast(layer.getLayer()); - stack.push(layer); - }); - } + public static void push(Layer layer) { + Platform.runLater( + () -> { + root.getChildren().addLast(layer.getLayer()); + stack.push(layer); + }); + } - public static void pop() { - Platform.runLater(() -> { - root.getChildren().removeLast(); - stack.pop(); + public static void pop() { + Platform.runLater( + () -> { + root.getChildren().removeLast(); + stack.pop(); - isQuitting = false; - }); - } + isQuitting = false; + }); + } - public static void popAll() { - Platform.runLater(() -> { - final int childrenCount = root.getChildren().size(); + public static void popAll() { + Platform.runLater( + () -> { + final int childrenCount = root.getChildren().size(); - for (int i = 0; i < childrenCount; i++) { - try { - root.getChildren().removeLast(); - } catch (Exception e) { - IO.println(e); - } - } + for (int i = 0; i < childrenCount; i++) { + try { + root.getChildren().removeLast(); + } catch (Exception e) { + IO.println(e); + } + } - stack.removeAllElements(); - }); - } + stack.removeAllElements(); + }); + } - public static void quitPopup() { - Platform.runLater(() -> { - push(new QuitPopup()); - isQuitting = true; - }); - } + public static void quitPopup() { + Platform.runLater( + () -> { + push(new QuitPopup()); + isQuitting = true; + }); + } - public static void quit() { - stage.close(); - } + public static void quit() { + stage.close(); + } - public static void reloadAll() { - stage.setTitle(AppContext.getString("appTitle")); + public static void reloadAll() { + stage.setTitle(AppContext.getString("appTitle")); - for (final Layer layer : stack) { - layer.reload(); - } - } + for (final Layer layer : stack) { + layer.reload(); + } + } - public static void setFullscreen(boolean fullscreen) { - stage.setFullScreen(fullscreen); + public static void setFullscreen(boolean fullscreen) { + stage.setFullScreen(fullscreen); - width = (int) stage.getWidth(); - height = (int) stage.getHeight(); + width = (int) stage.getWidth(); + height = (int) stage.getHeight(); - reloadAll(); - } + reloadAll(); + } - public static void setStyle(String theme, String layoutSize) { - final int stylesCount = scene.getStylesheets().size(); + public static void setStyle(String theme, String layoutSize) { + final int stylesCount = scene.getStylesheets().size(); - for (int i = 0; i < stylesCount; i++) { - scene.getStylesheets().removeLast(); - } + for (int i = 0; i < stylesCount; i++) { + scene.getStylesheets().removeLast(); + } - scene.getStylesheets().add(ResourceManager.get(theme + ".css").getUrl()); - scene.getStylesheets().add(ResourceManager.get(layoutSize + ".css").getUrl()); + scene.getStylesheets().add(ResourceManager.get(theme + ".css").getUrl()); + scene.getStylesheets().add(ResourceManager.get(layoutSize + ".css").getUrl()); - reloadAll(); - } + reloadAll(); + } - public static int getWidth() { - return width; - } + public static int getWidth() { + return width; + } - public static int getHeight() { - return height; - } -} \ No newline at end of file + public static int getHeight() { + return height; + } +} diff --git a/app/src/main/java/org/toop/app/GameInformation.java b/app/src/main/java/org/toop/app/GameInformation.java index fd7c56e..2a3e56f 100644 --- a/app/src/main/java/org/toop/app/GameInformation.java +++ b/app/src/main/java/org/toop/app/GameInformation.java @@ -1,6 +1,10 @@ package org.toop.app; -public record GameInformation(String[] playerName, boolean[] isPlayerHuman, - int[] computerDifficulty, int[] computerThinkTime, - boolean isConnectionLocal, String serverIP, String serverPort) { -} +public record GameInformation( + String[] playerName, + boolean[] isPlayerHuman, + int[] computerDifficulty, + int[] computerThinkTime, + boolean isConnectionLocal, + String serverIP, + String serverPort) {} diff --git a/app/src/main/java/org/toop/app/canvas/GameCanvas.java b/app/src/main/java/org/toop/app/canvas/GameCanvas.java index 5d4b56d..5ed5775 100644 --- a/app/src/main/java/org/toop/app/canvas/GameCanvas.java +++ b/app/src/main/java/org/toop/app/canvas/GameCanvas.java @@ -1,123 +1,130 @@ package org.toop.app.canvas; +import java.util.function.Consumer; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.input.MouseButton; import javafx.scene.paint.Color; -import java.util.function.Consumer; - public abstract class GameCanvas { - protected record Cell(float x, float y, float width, float height) { - } + protected record Cell(float x, float y, float width, float height) {} - protected final Canvas canvas; - protected final GraphicsContext graphics; + protected final Canvas canvas; + protected final GraphicsContext graphics; - protected final Color color; + protected final Color color; - protected int width; - protected int height; + protected int width; + protected int height; - protected final int rows; - protected final int columns; + protected final int rows; + protected final int columns; - protected final int gapSize; - protected final boolean edges; + protected final int gapSize; + protected final boolean edges; - protected final Cell[] cells; + protected final Cell[] cells; - protected GameCanvas(Color color, int width, int height, int rows, int columns, int gapSize, boolean edges, Consumer onCellClicked) { - canvas = new Canvas(width, height); - graphics = canvas.getGraphicsContext2D(); + protected GameCanvas( + Color color, + int width, + int height, + int rows, + int columns, + int gapSize, + boolean edges, + Consumer onCellClicked) { + canvas = new Canvas(width, height); + graphics = canvas.getGraphicsContext2D(); - this.color = color; + this.color = color; - this.width = width; - this.height = height; + this.width = width; + this.height = height; - this.rows = rows; - this.columns = columns; + this.rows = rows; + this.columns = columns; - this.gapSize = gapSize; - this.edges = edges; + this.gapSize = gapSize; + this.edges = edges; - cells = new Cell[rows * columns]; + cells = new Cell[rows * columns]; - final float cellWidth = ((float) width - (rows - 1) * gapSize) / rows; - final float cellHeight = ((float) height - (columns - 1) * gapSize) / columns; + final float cellWidth = ((float) width - (rows - 1) * gapSize) / rows; + final float cellHeight = ((float) height - (columns - 1) * gapSize) / columns; - for (int y = 0; y < columns; y++) { - final float startY = y * cellHeight + y * gapSize; + for (int y = 0; y < columns; y++) { + final float startY = y * cellHeight + y * gapSize; - for (int x = 0; x < rows; x++) { - final float startX = x * cellWidth + x * gapSize; - cells[y * rows + x] = new Cell(startX, startY, cellWidth, cellHeight); - } - } + for (int x = 0; x < rows; x++) { + final float startX = x * cellWidth + x * gapSize; + cells[y * rows + x] = new Cell(startX, startY, cellWidth, cellHeight); + } + } - canvas.setOnMouseClicked(event -> { - if (event.getButton() != MouseButton.PRIMARY) { - return; - } + canvas.setOnMouseClicked( + event -> { + if (event.getButton() != MouseButton.PRIMARY) { + return; + } - final int column = (int) ((event.getX() / width) * rows); - final int row = (int) ((event.getY() / height) * columns); + final int column = (int) ((event.getX() / width) * rows); + final int row = (int) ((event.getY() / height) * columns); - event.consume(); - onCellClicked.accept(row * rows + column); - }); + event.consume(); + onCellClicked.accept(row * rows + column); + }); - render(); - } + render(); + } - public void clear() { - graphics.clearRect(0, 0, width, height); - } + public void clear() { + graphics.clearRect(0, 0, width, height); + } - public void render() { - graphics.setFill(color); + public void render() { + graphics.setFill(color); - for (int x = 1; x < rows; x++) { - graphics.fillRect(cells[x].x() - gapSize, 0, gapSize, height); - } + for (int x = 1; x < rows; x++) { + graphics.fillRect(cells[x].x() - gapSize, 0, gapSize, height); + } - for (int y = 1; y < columns; y++) { - graphics.fillRect(0, cells[y * rows].y() - gapSize, width, gapSize); - } + for (int y = 1; y < columns; y++) { + graphics.fillRect(0, cells[y * rows].y() - gapSize, width, gapSize); + } - if (edges) { - graphics.fillRect(-gapSize, 0, gapSize, height); - graphics.fillRect(0, -gapSize, width, gapSize); + if (edges) { + graphics.fillRect(-gapSize, 0, gapSize, height); + graphics.fillRect(0, -gapSize, width, gapSize); - graphics.fillRect(width - gapSize, 0, gapSize, height); - graphics.fillRect(0, height - gapSize, width, gapSize); - } - } + graphics.fillRect(width - gapSize, 0, gapSize, height); + graphics.fillRect(0, height - gapSize, width, gapSize); + } + } - public void draw(Color color, int cell) { - final float x = cells[cell].x() + gapSize; - final float y = cells[cell].y() + gapSize; + public void draw(Color color, int cell) { + final float x = cells[cell].x() + gapSize; + final float y = cells[cell].y() + gapSize; - final float width = cells[cell].width() - gapSize * 2; - final float height = cells[cell].height() - gapSize * 2; + final float width = cells[cell].width() - gapSize * 2; + final float height = cells[cell].height() - gapSize * 2; - graphics.setFill(color); - graphics.fillRect(x, y, width, height); - } + graphics.setFill(color); + graphics.fillRect(x, y, width, height); + } - public void resize(int width, int height) { - canvas.setWidth(width); - canvas.setHeight(height); + public void resize(int width, int height) { + canvas.setWidth(width); + canvas.setHeight(height); - this.width = width; - this.height = height; + this.width = width; + this.height = height; - clear(); - render(); - } + clear(); + render(); + } - public Canvas getCanvas() { - return canvas; - } -} \ No newline at end of file + public Canvas getCanvas() { + return canvas; + } +} diff --git a/app/src/main/java/org/toop/app/canvas/TicTacToeCanvas.java b/app/src/main/java/org/toop/app/canvas/TicTacToeCanvas.java index 0f7cbb9..1838335 100644 --- a/app/src/main/java/org/toop/app/canvas/TicTacToeCanvas.java +++ b/app/src/main/java/org/toop/app/canvas/TicTacToeCanvas.java @@ -1,38 +1,37 @@ package org.toop.app.canvas; +import java.util.function.Consumer; import javafx.scene.paint.Color; -import java.util.function.Consumer; - public class TicTacToeCanvas extends GameCanvas { - public TicTacToeCanvas(Color color, int width, int height, Consumer onCellClicked) { - super(color, width, height, 3, 3, 10, false, onCellClicked); - } + public TicTacToeCanvas(Color color, int width, int height, Consumer onCellClicked) { + super(color, width, height, 3, 3, 10, false, onCellClicked); + } - public void drawX(Color color, int cell) { - graphics.setStroke(color); - graphics.setLineWidth(gapSize); + public void drawX(Color color, int cell) { + graphics.setStroke(color); + graphics.setLineWidth(gapSize); - final float x = cells[cell].x() + gapSize; - final float y = cells[cell].y() + gapSize; + final float x = cells[cell].x() + gapSize; + final float y = cells[cell].y() + gapSize; - final float width = cells[cell].width() - gapSize * 2; - final float height = cells[cell].height() - gapSize * 2; + final float width = cells[cell].width() - gapSize * 2; + final float height = cells[cell].height() - gapSize * 2; - graphics.strokeLine(x, y, x + width, y + height); - graphics.strokeLine(x + width, y, x, y + height); - } + graphics.strokeLine(x, y, x + width, y + height); + graphics.strokeLine(x + width, y, x, y + height); + } - public void drawO(Color color, int cell) { - graphics.setStroke(color); - graphics.setLineWidth(gapSize); + public void drawO(Color color, int cell) { + graphics.setStroke(color); + graphics.setLineWidth(gapSize); - final float x = cells[cell].x() + gapSize; - final float y = cells[cell].y() + gapSize; + final float x = cells[cell].x() + gapSize; + final float y = cells[cell].y() + gapSize; - final float width = cells[cell].width() - gapSize * 2; - final float height = cells[cell].height() - gapSize * 2; + final float width = cells[cell].width() - gapSize * 2; + final float height = cells[cell].height() - gapSize * 2; - graphics.strokeOval(x, y, width, height); - } -} \ No newline at end of file + graphics.strokeOval(x, y, width, height); + } +} diff --git a/app/src/main/java/org/toop/app/layer/Container.java b/app/src/main/java/org/toop/app/layer/Container.java index 409c0eb..89e6436 100644 --- a/app/src/main/java/org/toop/app/layer/Container.java +++ b/app/src/main/java/org/toop/app/layer/Container.java @@ -4,8 +4,9 @@ import javafx.scene.Node; import javafx.scene.layout.Region; public abstract class Container { - public abstract Region getContainer(); + public abstract Region getContainer(); - public abstract void addNodes(Node... nodes); - public abstract void addContainer(Container container, boolean fill); -} \ No newline at end of file + public abstract void addNodes(Node... nodes); + + public abstract void addContainer(Container container, boolean fill); +} diff --git a/app/src/main/java/org/toop/app/layer/Layer.java b/app/src/main/java/org/toop/app/layer/Layer.java index 35034c9..d357200 100644 --- a/app/src/main/java/org/toop/app/layer/Layer.java +++ b/app/src/main/java/org/toop/app/layer/Layer.java @@ -1,81 +1,86 @@ package org.toop.app.layer; -import org.toop.app.App; -import org.toop.app.canvas.GameCanvas; - import javafx.geometry.Pos; import javafx.scene.layout.Region; import javafx.scene.layout.StackPane; +import org.toop.app.App; +import org.toop.app.canvas.GameCanvas; public abstract class Layer { - protected StackPane layer; - protected Region background; + protected StackPane layer; + protected Region background; - protected Layer(String... backgroundStyles) { - layer = new StackPane(); + protected Layer(String... backgroundStyles) { + layer = new StackPane(); - background = new Region(); - background.getStyleClass().addAll(backgroundStyles); - background.setPrefSize(Double.MAX_VALUE, Double.MAX_VALUE); + background = new Region(); + background.getStyleClass().addAll(backgroundStyles); + background.setPrefSize(Double.MAX_VALUE, Double.MAX_VALUE); - layer.getChildren().addLast(background); - } + layer.getChildren().addLast(background); + } - protected void addContainer(Container container, Pos position, int xOffset, int yOffset, int widthPercent, int heightPercent) { - StackPane.setAlignment(container.getContainer(), position); + protected void addContainer( + Container container, + Pos position, + int xOffset, + int yOffset, + int widthPercent, + int heightPercent) { + StackPane.setAlignment(container.getContainer(), position); - final double widthUnit = App.getWidth() / 100.0; - final double heightUnit = App.getHeight() / 100.0; + final double widthUnit = App.getWidth() / 100.0; + final double heightUnit = App.getHeight() / 100.0; - if (widthPercent > 0) { - container.getContainer().setMaxWidth(widthPercent * widthUnit); - } else { - container.getContainer().setMaxWidth(Region.USE_PREF_SIZE); - } + if (widthPercent > 0) { + container.getContainer().setMaxWidth(widthPercent * widthUnit); + } else { + container.getContainer().setMaxWidth(Region.USE_PREF_SIZE); + } - if (heightPercent > 0) { - container.getContainer().setMaxHeight(heightPercent * heightUnit); - } else { - container.getContainer().setMaxHeight(Region.USE_PREF_SIZE); - } + if (heightPercent > 0) { + container.getContainer().setMaxHeight(heightPercent * heightUnit); + } else { + container.getContainer().setMaxHeight(Region.USE_PREF_SIZE); + } - container.getContainer().setTranslateX(xOffset * widthUnit); - container.getContainer().setTranslateY(yOffset * heightUnit); + container.getContainer().setTranslateX(xOffset * widthUnit); + container.getContainer().setTranslateY(yOffset * heightUnit); - layer.getChildren().addLast(container.getContainer()); - } + layer.getChildren().addLast(container.getContainer()); + } - protected void addGameCanvas(GameCanvas canvas, Pos position, int xOffset, int yOffset) { - StackPane.setAlignment(canvas.getCanvas(), position); + protected void addGameCanvas(GameCanvas canvas, Pos position, int xOffset, int yOffset) { + StackPane.setAlignment(canvas.getCanvas(), position); - final double widthUnit = App.getWidth() / 100.0; - final double heightUnit = App.getHeight() / 100.0; + final double widthUnit = App.getWidth() / 100.0; + final double heightUnit = App.getHeight() / 100.0; - canvas.getCanvas().setTranslateX(xOffset * widthUnit); - canvas.getCanvas().setTranslateY(yOffset * heightUnit); + canvas.getCanvas().setTranslateX(xOffset * widthUnit); + canvas.getCanvas().setTranslateY(yOffset * heightUnit); - layer.getChildren().addLast(canvas.getCanvas()); - } + layer.getChildren().addLast(canvas.getCanvas()); + } - protected void pop() { - if (layer.getChildren().size() <= 1) { - return; - } + protected void pop() { + if (layer.getChildren().size() <= 1) { + return; + } - layer.getChildren().removeLast(); - } + layer.getChildren().removeLast(); + } - protected void popAll() { - final int containers = layer.getChildren().size(); + protected void popAll() { + final int containers = layer.getChildren().size(); - for (int i = 1; i < containers; i++) { - layer.getChildren().removeLast(); - } - } + for (int i = 1; i < containers; i++) { + layer.getChildren().removeLast(); + } + } - public StackPane getLayer() { - return layer; - } + public StackPane getLayer() { + return layer; + } - public abstract void reload(); -} \ No newline at end of file + public abstract void reload(); +} diff --git a/app/src/main/java/org/toop/app/layer/NodeBuilder.java b/app/src/main/java/org/toop/app/layer/NodeBuilder.java index a0f2996..b55a70d 100644 --- a/app/src/main/java/org/toop/app/layer/NodeBuilder.java +++ b/app/src/main/java/org/toop/app/layer/NodeBuilder.java @@ -1,131 +1,140 @@ package org.toop.app.layer; -import org.toop.framework.audio.events.AudioEvents; -import org.toop.framework.eventbus.EventFlow; - +import java.util.function.Consumer; import javafx.beans.property.BooleanProperty; import javafx.beans.property.SimpleBooleanProperty; import javafx.geometry.Orientation; import javafx.scene.Node; import javafx.scene.control.*; import javafx.scene.text.Text; - -import java.util.function.Consumer; +import org.toop.framework.audio.events.AudioEvents; +import org.toop.framework.eventbus.EventFlow; public final class NodeBuilder { - public static void addCss(Node node, String... cssClasses) { - node.getStyleClass().addAll(cssClasses); - } + public static void addCss(Node node, String... cssClasses) { + node.getStyleClass().addAll(cssClasses); + } - public static void setCss(Node node, String... cssClasses) { - node.getStyleClass().removeAll(); - node.getStyleClass().addAll(cssClasses); - } + public static void setCss(Node node, String... cssClasses) { + node.getStyleClass().removeAll(); + node.getStyleClass().addAll(cssClasses); + } - public static Text header(String x) { - final Text element = new Text(x); - setCss(element, "text-primary", "text-header"); + public static Text header(String x) { + final Text element = new Text(x); + setCss(element, "text-primary", "text-header"); - return element; - } + return element; + } - public static Text text(String x) { - final Text element = new Text(x); - setCss(element, "text-secondary", "text-normal"); + public static Text text(String x) { + final Text element = new Text(x); + setCss(element, "text-secondary", "text-normal"); - return element; - } + return element; + } - public static Label button(String x, Runnable runnable) { - final Label element = new Label(x); - setCss(element, "button", "text-normal"); + public static Label button(String x, Runnable runnable) { + final Label element = new Label(x); + setCss(element, "button", "text-normal"); - element.setOnMouseClicked(_ -> { - new EventFlow().addPostEvent(new AudioEvents.ClickButton()).asyncPostEvent(); - runnable.run(); - }); + element.setOnMouseClicked( + _ -> { + new EventFlow().addPostEvent(new AudioEvents.ClickButton()).asyncPostEvent(); + runnable.run(); + }); - return element; - } + return element; + } - public static Label toggle(String x1, String x2, boolean toggled, Consumer consumer) { - final Label element = new Label(toggled ? x2 : x1); - setCss(element, "toggle", "text-normal"); + public static Label toggle(String x1, String x2, boolean toggled, Consumer consumer) { + final Label element = new Label(toggled ? x2 : x1); + setCss(element, "toggle", "text-normal"); - final BooleanProperty checked = new SimpleBooleanProperty(toggled); + final BooleanProperty checked = new SimpleBooleanProperty(toggled); - element.setOnMouseClicked(_ -> { - new EventFlow().addPostEvent(new AudioEvents.ClickButton()).asyncPostEvent(); - checked.set(!checked.get()); + element.setOnMouseClicked( + _ -> { + new EventFlow().addPostEvent(new AudioEvents.ClickButton()).asyncPostEvent(); + checked.set(!checked.get()); - if (checked.get()) { - element.setText(x1); - } else { - element.setText(x2); - } + if (checked.get()) { + element.setText(x1); + } else { + element.setText(x2); + } - consumer.accept(checked.get()); - }); + consumer.accept(checked.get()); + }); - return element; - } + return element; + } - public static Slider slider(int max, int initial, Consumer consumer) { - final Slider element = new Slider(0, max, initial); - setCss(element, "bg-slider-track"); + public static Slider slider(int max, int initial, Consumer consumer) { + final Slider element = new Slider(0, max, initial); + setCss(element, "bg-slider-track"); - element.setMinorTickCount(0); - element.setMajorTickUnit(1); - element.setBlockIncrement(1); + element.setMinorTickCount(0); + element.setMajorTickUnit(1); + element.setBlockIncrement(1); - element.setSnapToTicks(true); - element.setShowTickLabels(true); + element.setSnapToTicks(true); + element.setShowTickLabels(true); - element.setOnMouseClicked(_ -> { - new EventFlow().addPostEvent(new AudioEvents.ClickButton()).asyncPostEvent(); - }); + element.setOnMouseClicked( + _ -> { + new EventFlow().addPostEvent(new AudioEvents.ClickButton()).asyncPostEvent(); + }); - element.valueProperty().addListener((_, _, newValue) -> { - consumer.accept(newValue.intValue()); - }); + element.valueProperty() + .addListener( + (_, _, newValue) -> { + consumer.accept(newValue.intValue()); + }); - return element; - } + return element; + } - public static TextField input(String x, Consumer consumer) { - final TextField element = new TextField(x); - setCss(element, "input", "text-normal"); + public static TextField input(String x, Consumer consumer) { + final TextField element = new TextField(x); + setCss(element, "input", "text-normal"); - element.setOnMouseClicked(_ -> { - new EventFlow().addPostEvent(new AudioEvents.ClickButton()).asyncPostEvent(); - }); + element.setOnMouseClicked( + _ -> { + new EventFlow().addPostEvent(new AudioEvents.ClickButton()).asyncPostEvent(); + }); - element.textProperty().addListener((_, _, newValue) -> { - consumer.accept(newValue); - }); + element.textProperty() + .addListener( + (_, _, newValue) -> { + consumer.accept(newValue); + }); - return element; - } + return element; + } - public static ChoiceBox choiceBox(Consumer consumer) { - final ChoiceBox element = new ChoiceBox<>(); - setCss(element, "choice-box", "text-normal"); + public static ChoiceBox choiceBox(Consumer consumer) { + final ChoiceBox element = new ChoiceBox<>(); + setCss(element, "choice-box", "text-normal"); - element.setOnMouseClicked(_ -> { - new EventFlow().addPostEvent(new AudioEvents.ClickButton()).asyncPostEvent(); - }); + element.setOnMouseClicked( + _ -> { + new EventFlow().addPostEvent(new AudioEvents.ClickButton()).asyncPostEvent(); + }); - element.valueProperty().addListener((_, _, newValue) -> { - consumer.accept(newValue); - }); + element.valueProperty() + .addListener( + (_, _, newValue) -> { + consumer.accept(newValue); + }); - return element; - } + return element; + } - public static Separator separator() { - final Separator element = new Separator(Orientation.HORIZONTAL); - setCss(element, "separator"); + public static Separator separator() { + final Separator element = new Separator(Orientation.HORIZONTAL); + setCss(element, "separator"); - return element; - } -} \ No newline at end of file + return element; + } +} diff --git a/app/src/main/java/org/toop/app/layer/Popup.java b/app/src/main/java/org/toop/app/layer/Popup.java index 6a54bec..7e498df 100644 --- a/app/src/main/java/org/toop/app/layer/Popup.java +++ b/app/src/main/java/org/toop/app/layer/Popup.java @@ -3,17 +3,18 @@ package org.toop.app.layer; import org.toop.app.App; public abstract class Popup extends Layer { - protected Popup(boolean popOnBackground, String... backgroundStyles) { - super(backgroundStyles); + protected Popup(boolean popOnBackground, String... backgroundStyles) { + super(backgroundStyles); - if (popOnBackground) { - background.setOnMouseClicked(_ -> { - App.pop(); - }); - } - } + if (popOnBackground) { + background.setOnMouseClicked( + _ -> { + App.pop(); + }); + } + } - protected Popup(boolean popOnBackground) { - this(popOnBackground, "bg-popup"); - } -} \ No newline at end of file + protected Popup(boolean popOnBackground) { + this(popOnBackground, "bg-popup"); + } +} diff --git a/app/src/main/java/org/toop/app/layer/containers/HorizontalContainer.java b/app/src/main/java/org/toop/app/layer/containers/HorizontalContainer.java index b3f00a5..2350216 100644 --- a/app/src/main/java/org/toop/app/layer/containers/HorizontalContainer.java +++ b/app/src/main/java/org/toop/app/layer/containers/HorizontalContainer.java @@ -1,60 +1,59 @@ package org.toop.app.layer.containers; -import org.toop.app.layer.Container; - import javafx.collections.ObservableList; import javafx.scene.Node; import javafx.scene.layout.HBox; import javafx.scene.layout.Priority; import javafx.scene.layout.Region; +import org.toop.app.layer.Container; public final class HorizontalContainer extends Container { - private final HBox container; + private final HBox container; - public HorizontalContainer(int spacing, String... cssClasses) { - container = new HBox(spacing); - container.getStyleClass().addAll(cssClasses); - } + public HorizontalContainer(int spacing, String... cssClasses) { + container = new HBox(spacing); + container.getStyleClass().addAll(cssClasses); + } - public HorizontalContainer(int spacing) { - this(spacing, "container"); - } + public HorizontalContainer(int spacing) { + this(spacing, "container"); + } - @Override - public Region getContainer() { - return container; - } + @Override + public Region getContainer() { + return container; + } - @Override - public void addNodes(Node... nodes) { - container.getChildren().addAll(nodes); - } + @Override + public void addNodes(Node... nodes) { + container.getChildren().addAll(nodes); + } - @Override - public void addContainer(Container container, boolean fill) { - if (fill) { - container.getContainer().setMinSize(0, 0); - container.getContainer().setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE); - HBox.setHgrow(container.getContainer(), Priority.ALWAYS); - } else { - container.getContainer().setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE); - } + @Override + public void addContainer(Container container, boolean fill) { + if (fill) { + container.getContainer().setMinSize(0, 0); + container.getContainer().setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE); + HBox.setHgrow(container.getContainer(), Priority.ALWAYS); + } else { + container.getContainer().setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE); + } - this.container.getChildren().add(container.getContainer()); + this.container.getChildren().add(container.getContainer()); - if (fill) { - balanceChildWidths(); - } - } + if (fill) { + balanceChildWidths(); + } + } - private void balanceChildWidths() { - final ObservableList children = container.getChildren(); - final double widthPerChild = container.getWidth() / children.size(); + private void balanceChildWidths() { + final ObservableList children = container.getChildren(); + final double widthPerChild = container.getWidth() / children.size(); - for (final Node child : children) { - if (child instanceof Region) { - ((Region) child).setPrefWidth(widthPerChild); - } - } - } -} \ No newline at end of file + for (final Node child : children) { + if (child instanceof Region) { + ((Region) child).setPrefWidth(widthPerChild); + } + } + } +} diff --git a/app/src/main/java/org/toop/app/layer/containers/VerticalContainer.java b/app/src/main/java/org/toop/app/layer/containers/VerticalContainer.java index a8fb74d..56d610c 100644 --- a/app/src/main/java/org/toop/app/layer/containers/VerticalContainer.java +++ b/app/src/main/java/org/toop/app/layer/containers/VerticalContainer.java @@ -1,60 +1,59 @@ package org.toop.app.layer.containers; -import org.toop.app.layer.Container; - import javafx.collections.ObservableList; import javafx.scene.Node; import javafx.scene.layout.Priority; import javafx.scene.layout.Region; import javafx.scene.layout.VBox; +import org.toop.app.layer.Container; public final class VerticalContainer extends Container { - private final VBox container; + private final VBox container; - public VerticalContainer(int spacing, String... cssClasses) { - container = new VBox(spacing); - container.getStyleClass().addAll(cssClasses); - } + public VerticalContainer(int spacing, String... cssClasses) { + container = new VBox(spacing); + container.getStyleClass().addAll(cssClasses); + } - public VerticalContainer(int spacing) { - this(spacing, "container"); - } + public VerticalContainer(int spacing) { + this(spacing, "container"); + } - @Override - public Region getContainer() { - return container; - } + @Override + public Region getContainer() { + return container; + } - @Override - public void addNodes(Node... nodes) { - container.getChildren().addAll(nodes); - } + @Override + public void addNodes(Node... nodes) { + container.getChildren().addAll(nodes); + } - @Override - public void addContainer(Container container, boolean fill) { - if (fill) { - container.getContainer().setMinSize(0, 0); - container.getContainer().setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE); - VBox.setVgrow(container.getContainer(), Priority.ALWAYS); - } else { - container.getContainer().setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE); - } + @Override + public void addContainer(Container container, boolean fill) { + if (fill) { + container.getContainer().setMinSize(0, 0); + container.getContainer().setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE); + VBox.setVgrow(container.getContainer(), Priority.ALWAYS); + } else { + container.getContainer().setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE); + } - this.container.getChildren().add(container.getContainer()); + this.container.getChildren().add(container.getContainer()); - if (fill) { - balanceChildHeights(); - } - } + if (fill) { + balanceChildHeights(); + } + } - private void balanceChildHeights() { - final ObservableList children = container.getChildren(); - final double heightPerChild = container.getHeight() / children.size(); + private void balanceChildHeights() { + final ObservableList children = container.getChildren(); + final double heightPerChild = container.getHeight() / children.size(); - for (final Node child : children) { - if (child instanceof Region) { - ((Region) child).setPrefHeight(heightPerChild); - } - } - } -} \ No newline at end of file + for (final Node child : children) { + if (child instanceof Region) { + ((Region) child).setPrefHeight(heightPerChild); + } + } + } +} diff --git a/app/src/main/java/org/toop/app/layer/layers/ConnectedLayer.java b/app/src/main/java/org/toop/app/layer/layers/ConnectedLayer.java index ff9e7ce..b255c3d 100644 --- a/app/src/main/java/org/toop/app/layer/layers/ConnectedLayer.java +++ b/app/src/main/java/org/toop/app/layer/layers/ConnectedLayer.java @@ -1,6 +1,14 @@ package org.toop.app.layer.layers; +import java.util.List; +import java.util.Timer; +import java.util.TimerTask; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.atomic.AtomicInteger; import javafx.application.Platform; +import javafx.geometry.Pos; +import javafx.scene.control.Label; +import javafx.scene.control.ListView; import org.toop.app.App; import org.toop.app.GameInformation; import org.toop.app.layer.Container; @@ -14,169 +22,214 @@ import org.toop.framework.eventbus.EventFlow; import org.toop.framework.networking.events.NetworkEvents; import org.toop.local.AppContext; -import javafx.geometry.Pos; -import javafx.scene.control.Label; -import javafx.scene.control.ListView; - -import java.util.List; -import java.util.Timer; -import java.util.TimerTask; -import java.util.concurrent.CopyOnWriteArrayList; -import java.util.concurrent.atomic.AtomicInteger; - public final class ConnectedLayer extends Layer { - private static Timer pollTimer = new Timer(); + private static Timer pollTimer = new Timer(); - private static class ChallengePopup extends Popup { - private final GameInformation information; + private static class ChallengePopup extends Popup { + private final GameInformation information; - private final String challenger; - private final String game; + private final String challenger; + private final String game; - private final long clientID; - private final int challengeID; + private final long clientID; + private final int challengeID; - public ChallengePopup(GameInformation information, String challenger, String game, long clientID, String challengeID) { - super(false, "bg-popup"); + public ChallengePopup( + GameInformation information, + String challenger, + String game, + long clientID, + String challengeID) { + super(false, "bg-popup"); - this.information = information; + this.information = information; - this.challenger = challenger; - this.game = game; + this.challenger = challenger; + this.game = game; - this.clientID = clientID; - this.challengeID = Integer.parseInt(challengeID.substring(18, challengeID.length() - 2)); + this.clientID = clientID; + this.challengeID = + Integer.parseInt(challengeID.substring(18, challengeID.length() - 2)); - reload(); - } + reload(); + } - @Override - public void reload() { - popAll(); + @Override + public void reload() { + popAll(); - final var challengeText = NodeBuilder.header(AppContext.getString("challengeText")); - final var challengerNameText = NodeBuilder.header(challenger); + final var challengeText = NodeBuilder.header(AppContext.getString("challengeText")); + final var challengerNameText = NodeBuilder.header(challenger); - final var gameText = NodeBuilder.text(AppContext.getString("gameIsText")); - final var gameNameText = NodeBuilder.text(game); + final var gameText = NodeBuilder.text(AppContext.getString("gameIsText")); + final var gameNameText = NodeBuilder.text(game); - final var acceptButton = NodeBuilder.button(AppContext.getString("accept"), () -> { - pollTimer.cancel(); + final var acceptButton = + NodeBuilder.button( + AppContext.getString("accept"), + () -> { + pollTimer.cancel(); - new EventFlow().addPostEvent(new NetworkEvents.SendAcceptChallenge(clientID, challengeID)).postEvent(); - App.activate(new TicTacToeLayer(information, clientID)); - }); + new EventFlow() + .addPostEvent( + new NetworkEvents.SendAcceptChallenge( + clientID, challengeID)) + .postEvent(); + App.activate(new TicTacToeLayer(information, clientID)); + }); - final var denyButton = NodeBuilder.button(AppContext.getString("deny"), () -> { - App.pop(); - }); + final var denyButton = + NodeBuilder.button( + AppContext.getString("deny"), + () -> { + App.pop(); + }); - final Container controlContainer = new HorizontalContainer(30); - controlContainer.addNodes(acceptButton, denyButton); + final Container controlContainer = new HorizontalContainer(30); + controlContainer.addNodes(acceptButton, denyButton); - final Container mainContainer = new VerticalContainer(30); - mainContainer.addNodes(challengeText, challengerNameText); - mainContainer.addNodes(gameText, gameNameText); + final Container mainContainer = new VerticalContainer(30); + mainContainer.addNodes(challengeText, challengerNameText); + mainContainer.addNodes(gameText, gameNameText); - mainContainer.addContainer(controlContainer, false); + mainContainer.addContainer(controlContainer, false); - addContainer(mainContainer, Pos.CENTER, 0, 0, 30, 30); - } - } + addContainer(mainContainer, Pos.CENTER, 0, 0, 30, 30); + } + } - GameInformation information; - long clientId; - String user; - List onlinePlayers = new CopyOnWriteArrayList<>(); + GameInformation information; + long clientId; + String user; + List onlinePlayers = new CopyOnWriteArrayList<>(); - public ConnectedLayer(GameInformation information) { - super("bg-primary"); + public ConnectedLayer(GameInformation information) { + super("bg-primary"); - this.information = information; + this.information = information; - new EventFlow() - .addPostEvent(NetworkEvents.StartClient.class, information.serverIP(), Integer.parseInt(information.serverPort())) - .onResponse(NetworkEvents.StartClientResponse.class, e -> { - clientId = e.clientId(); - user = information.playerName()[0].replaceAll("\\s+", ""); + new EventFlow() + .addPostEvent( + NetworkEvents.StartClient.class, + information.serverIP(), + Integer.parseInt(information.serverPort())) + .onResponse( + NetworkEvents.StartClientResponse.class, + e -> { + clientId = e.clientId(); + user = information.playerName()[0].replaceAll("\\s+", ""); - new EventFlow().addPostEvent(new NetworkEvents.SendLogin(this.clientId, this.user)).postEvent(); + new EventFlow() + .addPostEvent( + new NetworkEvents.SendLogin(this.clientId, this.user)) + .postEvent(); - Thread popThread = new Thread(this::populatePlayerList); - popThread.setDaemon(false); - popThread.start(); - }).postEvent(); + Thread popThread = new Thread(this::populatePlayerList); + popThread.setDaemon(false); + popThread.start(); + }) + .postEvent(); - new EventFlow().listen(this::handleReceivedChallenge); + new EventFlow().listen(this::handleReceivedChallenge); reload(); - } + } - private void populatePlayerList() { - EventFlow sendGetPlayerList = new EventFlow().addPostEvent(new NetworkEvents.SendGetPlayerlist(this.clientId)); - new EventFlow().listen(NetworkEvents.PlayerlistResponse.class, e -> { - if (e.clientId() == this.clientId) { - List playerList = new java.util.ArrayList<>(List.of(e.playerlist())); // TODO: Garbage, but works - playerList.removeIf(name -> name.equalsIgnoreCase(user)); - if (this.onlinePlayers != playerList) { - this.onlinePlayers.clear(); - this.onlinePlayers.addAll(playerList); - } - } - }); + private void populatePlayerList() { + EventFlow sendGetPlayerList = + new EventFlow().addPostEvent(new NetworkEvents.SendGetPlayerlist(this.clientId)); + new EventFlow() + .listen( + NetworkEvents.PlayerlistResponse.class, + e -> { + if (e.clientId() == this.clientId) { + List playerList = + new java.util.ArrayList<>( + List.of(e.playerlist())); // TODO: Garbage, + // but works + playerList.removeIf(name -> name.equalsIgnoreCase(user)); + if (this.onlinePlayers != playerList) { + this.onlinePlayers.clear(); + this.onlinePlayers.addAll(playerList); + } + } + }); - TimerTask task = new TimerTask() { - public void run() { - sendGetPlayerList.postEvent(); - Platform.runLater(() -> reload()); - } - }; + TimerTask task = + new TimerTask() { + public void run() { + sendGetPlayerList.postEvent(); + Platform.runLater(() -> reload()); + } + }; - pollTimer.schedule(task, 0L, 5000L); // TODO: Block app exit, fix later - } + pollTimer.schedule(task, 0L, 5000L); // TODO: Block app exit, fix later + } - private void sendChallenge(String oppUsername, String gameType) { - final AtomicInteger challengeId = new AtomicInteger(-1); + private void sendChallenge(String oppUsername, String gameType) { + final AtomicInteger challengeId = new AtomicInteger(-1); - if (onlinePlayers.contains(oppUsername)) { - new EventFlow().addPostEvent(new NetworkEvents.SendChallenge(this.clientId, oppUsername, gameType)) - .listen(NetworkEvents.ChallengeResponse.class, e -> { - challengeId.set(Integer.parseInt(e.challengeId().substring(18, e.challengeId().length() - 2))); - }) - .listen(NetworkEvents.GameMatchResponse.class, e -> { - if (e.clientId() == this.clientId) { - pollTimer.cancel(); - App.activate(new TicTacToeLayer(information, this.clientId)); - } - }, false).postEvent(); - // ^ - // | - // | - // | - } - } + if (onlinePlayers.contains(oppUsername)) { + new EventFlow() + .addPostEvent( + new NetworkEvents.SendChallenge(this.clientId, oppUsername, gameType)) + .listen( + NetworkEvents.ChallengeResponse.class, + e -> { + challengeId.set( + Integer.parseInt( + e.challengeId() + .substring( + 18, e.challengeId().length() - 2))); + }) + .listen( + NetworkEvents.GameMatchResponse.class, + e -> { + if (e.clientId() == this.clientId) { + pollTimer.cancel(); + App.activate(new TicTacToeLayer(information, this.clientId)); + } + }, + false) + .postEvent(); + // ^ + // | + // | + // | + } + } - private void handleReceivedChallenge(NetworkEvents.ChallengeResponse response) { - App.push(new ChallengePopup(information, response.challengerName(), response.gameType(), clientId, response.challengeId())); - } + private void handleReceivedChallenge(NetworkEvents.ChallengeResponse response) { + App.push( + new ChallengePopup( + information, + response.challengerName(), + response.gameType(), + clientId, + response.challengeId())); + } - @Override - public void reload() { - popAll(); + @Override + public void reload() { + popAll(); - ListView