diff --git a/.idea/misc.xml b/.idea/misc.xml index 54267fb..67f7df6 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,10 @@ + + + + + - + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..75d7be8 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,127 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/org/toop/Main.java b/src/main/java/org/toop/Main.java index 8e81a8b..9e23bda 100644 --- a/src/main/java/org/toop/Main.java +++ b/src/main/java/org/toop/Main.java @@ -32,9 +32,6 @@ public class Main { while (running) { window.update(); renderer.clear(); - do { - console.print(); - } while (console.next()); shader.start(); renderer.render(); @@ -45,6 +42,11 @@ public class Main { if (shader != null) shader.cleanup(); if (renderer != null) renderer.cleanup(); if (window != null) window.cleanup(); + + */ + //JFrameWindow window = new JFrameWindow(); + GameSelectorWindow gameSelectorWindow = new GameSelectorWindow(); + } private static void registerEvents() { diff --git a/src/main/java/org/toop/UI/GameSelectorWindow.form b/src/main/java/org/toop/UI/GameSelectorWindow.form new file mode 100644 index 0000000..7a78dab --- /dev/null +++ b/src/main/java/org/toop/UI/GameSelectorWindow.form @@ -0,0 +1,117 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/main/java/org/toop/UI/GameSelectorWindow.java b/src/main/java/org/toop/UI/GameSelectorWindow.java new file mode 100644 index 0000000..8d64e60 --- /dev/null +++ b/src/main/java/org/toop/UI/GameSelectorWindow.java @@ -0,0 +1,57 @@ +package org.toop.UI; +import org.toop.eventbus.GlobalEventBus; + +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.*; + + +public class GameSelectorWindow extends JFrame { + private JPanel mainMenu; + private JTextField nameTextField; + private JTextField ipTextField; + private JTextField portTextField; + private JButton connectButton; + private JComboBox gameSelectorBox; + private JPanel cards; + private JPanel gameSelector; + private JFrame frame; + private JLabel fillAllFields; + + public GameSelectorWindow() { + gameSelectorBox.addItem("Tic Tac Toe"); + gameSelectorBox.addItem("Reversi"); + //todo get supported games from server and add to gameSelectorBox + frame = new JFrame("Game Selector"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setSize(1920, 1080); + frame.setResizable(true); + + init(); + frame.add(mainMenu); + frame.setVisible(true); + //GlobalEventBus.subscribeAndRegister() Todo add game panel to frame when connection succeeds + + } + private void init() { + connectButton.addActionListener(( + ActionEvent e) -> { + if(!nameTextField.getText().equals("") && !ipTextField.getText().equals("") && !portTextField.getText().equals("")) { + System.out.println(gameSelectorBox.getSelectedItem().toString()); //todo attempt connecting to the server with given ip, port and name. + frame.remove(mainMenu); + UIGameBoard ttt = new UIGameBoard(gameSelectorBox.getSelectedItem().toString(),this); + frame.add(ttt.getTTTPanel()); + frame.revalidate(); + frame.repaint(); + }else{ + fillAllFields.setVisible(true); + } + }); + } + public void returnToMainMenu() { + frame.removeAll(); + frame.add(mainMenu); + frame.revalidate(); + frame.repaint(); + } +} diff --git a/src/main/java/org/toop/UI/UIGameBoard.form b/src/main/java/org/toop/UI/UIGameBoard.form new file mode 100644 index 0000000..0d285e4 --- /dev/null +++ b/src/main/java/org/toop/UI/UIGameBoard.form @@ -0,0 +1,55 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/main/java/org/toop/UI/UIGameBoard.java b/src/main/java/org/toop/UI/UIGameBoard.java new file mode 100644 index 0000000..56c10a2 --- /dev/null +++ b/src/main/java/org/toop/UI/UIGameBoard.java @@ -0,0 +1,78 @@ +package org.toop.UI; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.toop.Main; +import org.toop.eventbus.Events; +import org.toop.eventbus.GlobalEventBus; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; + +public class UIGameBoard extends JFrame { + private final int TICTACTOE_SIZE = 3; + private final int REVERSI_SIZE = 8; + private static final Logger logger = LogManager.getLogger(Main.class); + private JLabel name; + private JLabel ip; + private JLabel gameName; + private JPanel tttPanel; + private JPanel cellPanel; + private JButton backToMainMenuButton; + private JButton[] buttons = new JButton[9]; + private JButton[] cells; + public UIGameBoard(String game,GameSelectorWindow gameSelectorWindow) { + + //cellPanel = new JPanel(); + JPanel gamePanel = new JPanel(); + if(game.toLowerCase().equals("tic tac toe")) { + gamePanel = createGridPanel(TICTACTOE_SIZE, TICTACTOE_SIZE); + } + if(game.toLowerCase().equals("reversi")) { + gamePanel = createGridPanel(REVERSI_SIZE, REVERSI_SIZE); + } + + cellPanel.removeAll(); + cellPanel.add(gamePanel); + cellPanel.revalidate(); + cellPanel.repaint(); + //tttPanel.add(cellPanel); + backToMainMenuButton.addActionListener(( + ActionEvent e) -> { + gameSelectorWindow.returnToMainMenu(); + System.out.println("gothere"); + }); + } + //Set the IP, game name and name + public void setIGN(String ip, String gameName, String name) { + this.ip.setText(ip); + this.gameName.setText(gameName); + this.name.setText(name); + } + public JPanel getTTTPanel() { + return tttPanel; + } + //Creates a grid of buttons and adds a global event bus event on click with the index of the button. + private JPanel createGridPanel(int sizeX, int sizeY) { + JPanel cellPanel = new JPanel(new GridLayout(sizeX,sizeY)); + cells = new JButton[sizeX*sizeY]; + for(int i =0; i < sizeX*sizeY; i++){ + cells[i] = new JButton(" "); + cells[i].setPreferredSize(new Dimension(1000/sizeX,1000/sizeY)); + cells[i].setFont(new Font("Arial", Font.BOLD, 480/sizeX)); + cells[i].setFocusPainted(false); + cellPanel.add(cells[i]); + final int index = i; + cells[i].addActionListener((ActionEvent e) -> { + setCell(index,"X");//■ todo get current player + GlobalEventBus.post(new Events.ServerEvents.CellClicked(index)); + logger.info("Grid button {} was clicked.", index); + }); + } + return cellPanel; + } + public void setCell(int cell, String newValue){ + cells[cell].setText(newValue); + } +} diff --git a/src/main/java/org/toop/eventbus/Events.java b/src/main/java/org/toop/eventbus/Events.java index a5fe209..5e3d70b 100644 --- a/src/main/java/org/toop/eventbus/Events.java +++ b/src/main/java/org/toop/eventbus/Events.java @@ -258,6 +258,10 @@ public class Events implements IEvents { */ public record ClosedConnection() {} + /** + * Triggers when a cell is clicked in one of the game boards. + */ + public record CellClicked(int cell) {} } public static class EventBusEvents {