Added GUI with swift.

This commit is contained in:
Ticho Hidding
2025-09-17 17:17:21 +02:00
parent 9538b42cea
commit ef80baae5c
8 changed files with 322 additions and 4 deletions

2
.idea/misc.xml generated
View File

@@ -13,7 +13,7 @@
</list> </list>
</option> </option>
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" project-jdk-name="openjdk-24" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_24" project-jdk-name="openjdk-24" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>

3
.idea/uiDesigner.xml generated
View File

@@ -121,4 +121,7 @@
</item> </item>
</group> </group>
</component> </component>
<component name="uidesigner-configuration">
<option name="DEFAULT_LAYOUT_MANAGER" value="CardLayout" />
</component>
</project> </project>

View File

@@ -1,15 +1,14 @@
package org.toop; package org.toop;
import org.toop.core.*; import org.toop.UI.GameSelectorWindow;
import org.toop.eventbus.*; import org.toop.eventbus.*;
import org.toop.graphics.*;
public class Main { public class Main {
private static boolean running = false; private static boolean running = false;
public static void main(String[] args) { public static void main(String[] args) {
registerEvents(); registerEvents();
/*
Window window = Window.setup(Window.API.GLFW, "Test", new Window.Size(1280, 720)); Window window = Window.setup(Window.API.GLFW, "Test", new Window.Size(1280, 720));
Renderer renderer = Renderer.setup(Renderer.API.OPENGL); Renderer renderer = Renderer.setup(Renderer.API.OPENGL);
@@ -30,6 +29,11 @@ public class Main {
if (shader != null) shader.cleanup(); if (shader != null) shader.cleanup();
if (renderer != null) renderer.cleanup(); if (renderer != null) renderer.cleanup();
if (window != null) window.cleanup(); if (window != null) window.cleanup();
*/
//JFrameWindow window = new JFrameWindow();
GameSelectorWindow gameSelectorWindow = new GameSelectorWindow();
} }
private static void registerEvents() { private static void registerEvents() {

View File

@@ -0,0 +1,117 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.toop.UI.GameSelectorWindow">
<grid id="27dc6" binding="mainMenu" layout-manager="GridLayoutManager" row-count="8" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties>
<opaque value="true"/>
<preferredSize width="800" height="600"/>
</properties>
<border type="none"/>
<children>
<component id="ed019" class="javax.swing.JTextField" binding="nameTextField">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<vspacer id="4e05c">
<constraints>
<grid row="7" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="1d2f1" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Name:"/>
</properties>
</component>
<component id="70d8c" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Welcome to ISY games selector!"/>
</properties>
</component>
<component id="5cc82" class="javax.swing.JTextField" binding="ipTextField">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="26a0" class="javax.swing.JTextField" binding="portTextField">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="3abc3" class="javax.swing.JLabel">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Port:"/>
</properties>
</component>
<component id="514db" class="javax.swing.JLabel">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="IP:"/>
</properties>
</component>
<hspacer id="27f2a">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="a2f0" class="javax.swing.JButton" binding="connectButton" default-binding="true">
<constraints>
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Connect"/>
</properties>
</component>
<component id="4214e" class="javax.swing.JComboBox" binding="gameSelectorBox">
<constraints>
<grid row="4" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model/>
</properties>
</component>
<component id="43a89" class="javax.swing.JLabel">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Select game:"/>
</properties>
</component>
<component id="3874e" class="javax.swing.JLabel" binding="fillAllFields">
<constraints>
<grid row="6" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<enabled value="true"/>
<foreground color="-65536"/>
<text value="Please fill all fields"/>
<visible value="false"/>
</properties>
</component>
</children>
</grid>
</form>

View File

@@ -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();
}
}

View File

@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.toop.UI.UIGameBoard">
<grid id="27dc6" binding="tttPanel" layout-manager="GridLayoutManager" row-count="2" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties>
<preferredSize width="800" height="600"/>
</properties>
<border type="none"/>
<children>
<component id="f23e8" class="javax.swing.JLabel" binding="name">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Label"/>
</properties>
</component>
<component id="4db9d" class="javax.swing.JLabel" binding="ip">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Label"/>
</properties>
</component>
<component id="ac2e0" class="javax.swing.JLabel" binding="gameName">
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Label"/>
</properties>
</component>
<grid id="f1c82" binding="cellPanel" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1">
<constraints>
<grid row="1" column="0" row-span="1" col-span="4" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<component id="e1a78" class="javax.swing.JButton" binding="backToMainMenuButton" default-binding="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="1" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Back to main menu"/>
</properties>
</component>
</children>
</grid>
</form>

View File

@@ -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);
}
}

View File

@@ -110,6 +110,10 @@ public class Events implements IEvents {
*/ */
public record ChangeConnection(String ip, String port) { } public record ChangeConnection(String ip, String port) { }
/**
* Triggers when a cell is clicked in one of the game boards.
*/
public record CellClicked(int cell) {}
} }
public static class EventBusEvents { public static class EventBusEvents {