mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 19:04:49 +00:00
Formatted code to follow google formatting guidelines using the Spotless formatter
This commit is contained in:
@@ -1,38 +1,40 @@
|
||||
package org.toop.frontend;
|
||||
|
||||
import org.toop.eventbus.Events;
|
||||
import org.toop.eventbus.GlobalEventBus;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.toop.eventbus.Events;
|
||||
import org.toop.eventbus.GlobalEventBus;
|
||||
|
||||
public class ConnectionManager {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(ConnectionManager.class);
|
||||
|
||||
/**
|
||||
* Map of serverId -> Server instances
|
||||
*/
|
||||
/** Map of serverId -> Server instances */
|
||||
private final Map<String, ServerConnection> serverConnections = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* Starts a connection manager, to manage, connections.
|
||||
*/
|
||||
/** Starts a connection manager, to manage, connections. */
|
||||
public ConnectionManager() {
|
||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.StartConnectionRequest.class, this::handleStartConnectionRequest);
|
||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.StartConnection.class, this::handleStartConnection);
|
||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.SendCommand.class, this::handleCommand);
|
||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.Reconnect.class, this::handleReconnect);
|
||||
// GlobalEventBus.subscribeAndRegister(Events.ServerEvents.ChangeConnection.class, this::handleChangeConnection);
|
||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.ForceCloseAllConnections.class, _ -> shutdownAll());
|
||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.RequestsAllConnections.class, this::getAllConnections);
|
||||
GlobalEventBus.subscribeAndRegister(
|
||||
Events.ServerEvents.StartConnectionRequest.class,
|
||||
this::handleStartConnectionRequest);
|
||||
GlobalEventBus.subscribeAndRegister(
|
||||
Events.ServerEvents.StartConnection.class, this::handleStartConnection);
|
||||
GlobalEventBus.subscribeAndRegister(
|
||||
Events.ServerEvents.SendCommand.class, this::handleCommand);
|
||||
GlobalEventBus.subscribeAndRegister(
|
||||
Events.ServerEvents.Reconnect.class, this::handleReconnect);
|
||||
// GlobalEventBus.subscribeAndRegister(Events.ServerEvents.ChangeConnection.class,
|
||||
// this::handleChangeConnection);
|
||||
GlobalEventBus.subscribeAndRegister(
|
||||
Events.ServerEvents.ForceCloseAllConnections.class, _ -> shutdownAll());
|
||||
GlobalEventBus.subscribeAndRegister(
|
||||
Events.ServerEvents.RequestsAllConnections.class, this::getAllConnections);
|
||||
}
|
||||
|
||||
private String startConnectionRequest(String ip, String port) {
|
||||
@@ -49,18 +51,24 @@ public class ConnectionManager {
|
||||
}
|
||||
|
||||
private void handleStartConnectionRequest(Events.ServerEvents.StartConnectionRequest request) {
|
||||
request.future().complete(this.startConnectionRequest(request.ip(), request.port())); // TODO: Maybe post ConnectionEstablished event.
|
||||
request.future()
|
||||
.complete(
|
||||
this.startConnectionRequest(
|
||||
request.ip(),
|
||||
request.port())); // TODO: Maybe post ConnectionEstablished event.
|
||||
}
|
||||
|
||||
private void handleStartConnection(Events.ServerEvents.StartConnection event) {
|
||||
GlobalEventBus.post(new Events.ServerEvents.ConnectionEstablished(
|
||||
this.startConnectionRequest(event.ip(), event.port()),
|
||||
event.ip(),
|
||||
event.port()
|
||||
));
|
||||
GlobalEventBus.post(
|
||||
new Events.ServerEvents.ConnectionEstablished(
|
||||
this.startConnectionRequest(event.ip(), event.port()),
|
||||
event.ip(),
|
||||
event.port()));
|
||||
}
|
||||
|
||||
private void handleCommand(Events.ServerEvents.SendCommand event) { // TODO: Move this to ServerConnection class, keep it internal.
|
||||
private void handleCommand(
|
||||
Events.ServerEvents.SendCommand
|
||||
event) { // TODO: Move this to ServerConnection class, keep it internal.
|
||||
ServerConnection serverConnection = this.serverConnections.get(event.connectionId());
|
||||
if (serverConnection != null) {
|
||||
serverConnection.sendCommandByString(event.args());
|
||||
@@ -82,18 +90,21 @@ public class ConnectionManager {
|
||||
}
|
||||
}
|
||||
|
||||
// private void handleChangeConnection(Events.ServerEvents.ChangeConnection event) {
|
||||
// ServerConnection serverConnection = this.serverConnections.get(event.connectionId());
|
||||
// if (serverConnection != null) {
|
||||
// try {
|
||||
// serverConnection.connect(event.ip(), event.port());
|
||||
// logger.info("Server {} changed connection to {}:{}", event.connectionId(), event.ip(), event.port());
|
||||
// } catch (Exception e) {
|
||||
// logger.error("Server {} failed to change connection", event.connectionId(), e);
|
||||
// GlobalEventBus.post(new Events.ServerEvents.CouldNotConnect(event.connectionId()));
|
||||
// }
|
||||
// }
|
||||
// } TODO
|
||||
// private void handleChangeConnection(Events.ServerEvents.ChangeConnection event) {
|
||||
// ServerConnection serverConnection = this.serverConnections.get(event.connectionId());
|
||||
// if (serverConnection != null) {
|
||||
// try {
|
||||
// serverConnection.connect(event.ip(), event.port());
|
||||
// logger.info("Server {} changed connection to {}:{}", event.connectionId(),
|
||||
// event.ip(), event.port());
|
||||
// } catch (Exception e) {
|
||||
// logger.error("Server {} failed to change connection", event.connectionId(),
|
||||
// e);
|
||||
// GlobalEventBus.post(new
|
||||
// Events.ServerEvents.CouldNotConnect(event.connectionId()));
|
||||
// }
|
||||
// }
|
||||
// } TODO
|
||||
|
||||
private void getAllConnections(Events.ServerEvents.RequestsAllConnections request) {
|
||||
List<ServerConnection> a = new ArrayList<>(this.serverConnections.values());
|
||||
@@ -105,4 +116,4 @@ public class ConnectionManager {
|
||||
this.serverConnections.clear();
|
||||
logger.info("All servers shut down");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
package org.toop.frontend;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.toop.eventbus.Events;
|
||||
import org.toop.eventbus.GlobalEventBus;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.util.concurrent.*;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.toop.eventbus.Events;
|
||||
import org.toop.eventbus.GlobalEventBus;
|
||||
|
||||
public final class ServerConnection extends TcpClient implements Runnable {
|
||||
|
||||
@@ -28,21 +27,20 @@ public final class ServerConnection extends TcpClient implements Runnable {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Sends a command to the server.
|
||||
*
|
||||
* @param args The arguments for the command.
|
||||
*/
|
||||
public void sendCommandByString(String... args) {
|
||||
// if (!TicTacToeServerCommand.isValid(command)) {
|
||||
// logger.error("Invalid command: {}", command);
|
||||
// return;
|
||||
// } // TODO: DO I CARE?
|
||||
// if (!TicTacToeServerCommand.isValid(command)) {
|
||||
// logger.error("Invalid command: {}", command);
|
||||
// return;
|
||||
// } // TODO: DO I CARE?
|
||||
|
||||
// if (!this.running) {
|
||||
// logger.warn("Server has been stopped");
|
||||
// return;
|
||||
// } // TODO: Server not running
|
||||
// if (!this.running) {
|
||||
// logger.warn("Server has been stopped");
|
||||
// return;
|
||||
// } // TODO: Server not running
|
||||
|
||||
String command = String.join(" ", args);
|
||||
|
||||
@@ -69,11 +67,11 @@ public final class ServerConnection extends TcpClient implements Runnable {
|
||||
private void stopWorkers() {
|
||||
this.running = false;
|
||||
this.sendQueue.clear();
|
||||
try {
|
||||
this.closeSocket();
|
||||
} catch (IOException e) {
|
||||
logger.warn("Error closing client socket", e); // TODO: Better log
|
||||
}
|
||||
try {
|
||||
this.closeSocket();
|
||||
} catch (IOException e) {
|
||||
logger.warn("Error closing client socket", e); // TODO: Better log
|
||||
}
|
||||
|
||||
this.executor.shutdownNow();
|
||||
}
|
||||
@@ -86,7 +84,9 @@ public final class ServerConnection extends TcpClient implements Runnable {
|
||||
if (received != null) {
|
||||
logger.info("Connection: {} received: '{}'", this.uuid, received);
|
||||
// this.addReceivedMessageToQueue(received); // TODO: Will never go empty
|
||||
GlobalEventBus.post(new Events.ServerEvents.ReceivedMessage(this.uuid, received)); // TODO: mb change
|
||||
GlobalEventBus.post(
|
||||
new Events.ServerEvents.ReceivedMessage(
|
||||
this.uuid, received)); // TODO: mb change
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
@@ -114,7 +114,6 @@ public final class ServerConnection extends TcpClient implements Runnable {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Connect to a new server.
|
||||
*
|
||||
* @param ip The ip to connect to.
|
||||
@@ -132,7 +131,6 @@ public final class ServerConnection extends TcpClient implements Runnable {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Reconnects to previous address.
|
||||
*
|
||||
* @throws IOException wip
|
||||
@@ -141,14 +139,14 @@ public final class ServerConnection extends TcpClient implements Runnable {
|
||||
this.connect(this.serverAddress, this.serverPort);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Close connection to server.
|
||||
*
|
||||
*/
|
||||
/** Close connection to server. */
|
||||
public void closeConnection() {
|
||||
this.stopWorkers();
|
||||
logger.info("Closed connection: {}, to server {}:{}", this.uuid, this.serverAddress, this.serverPort);
|
||||
logger.info(
|
||||
"Closed connection: {}, to server {}:{}",
|
||||
this.uuid,
|
||||
this.serverAddress,
|
||||
this.serverPort);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -164,9 +162,6 @@ public final class ServerConnection extends TcpClient implements Runnable {
|
||||
public String toString() {
|
||||
return String.format(
|
||||
"Server {ip: \"%s\", port: \"%s\", running: %s}",
|
||||
this.serverAddress, this.serverPort, this.running
|
||||
);
|
||||
this.serverAddress, this.serverPort, this.running);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -7,9 +7,7 @@ import java.io.PrintWriter;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
/**
|
||||
* A simple wrapper for creating TCP clients.
|
||||
*/
|
||||
/** A simple wrapper for creating TCP clients. */
|
||||
public abstract class TcpClient {
|
||||
|
||||
InetAddress serverAddress;
|
||||
@@ -61,5 +59,4 @@ public abstract class TcpClient {
|
||||
public void close() throws IOException {
|
||||
this.socket.close();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package org.toop.frontend.UI;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import javax.swing.*;
|
||||
|
||||
public class BackgroundPanel extends JPanel {
|
||||
private Image backgroundImage;
|
||||
@@ -18,4 +18,4 @@ public class BackgroundPanel extends JPanel {
|
||||
g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,11 @@
|
||||
package org.toop.frontend.UI;
|
||||
|
||||
import java.awt.*;
|
||||
import javax.swing.*;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.toop.eventbus.Events;
|
||||
import org.toop.eventbus.GlobalEventBus;
|
||||
import org.toop.frontend.games.LocalTicTacToe;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class LocalGameSelector extends JFrame {
|
||||
private static final Logger logger = LogManager.getLogger(LocalGameSelector.class);
|
||||
|
||||
@@ -20,7 +14,7 @@ public class LocalGameSelector extends JFrame {
|
||||
private JButton startGame;
|
||||
private JComboBox playerTypeSelectionBox;
|
||||
|
||||
private JPanel cards; // CardLayout panel
|
||||
private JPanel cards; // CardLayout panel
|
||||
private CardLayout cardLayout;
|
||||
|
||||
private UIGameBoard tttBoard;
|
||||
@@ -70,14 +64,14 @@ public class LocalGameSelector extends JFrame {
|
||||
|
||||
if (playerTypes.equals("Player vs Player")) {
|
||||
logger.info("Player vs Player");
|
||||
lttt = LocalTicTacToe.createLocal(new boolean[] { false, false });
|
||||
lttt = LocalTicTacToe.createLocal(new boolean[] {false, false});
|
||||
} else {
|
||||
if (playerTypes.equals("Player vs AI")) {
|
||||
logger.info("Player vs AI");
|
||||
lttt = LocalTicTacToe.createLocal(new boolean[] { false, true });
|
||||
lttt = LocalTicTacToe.createLocal(new boolean[] {false, true});
|
||||
} else {
|
||||
logger.info("AI vs Player");
|
||||
lttt = LocalTicTacToe.createLocal(new boolean[] { true, false });
|
||||
lttt = LocalTicTacToe.createLocal(new boolean[] {true, false});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,6 @@ public class LocalServerSelector {
|
||||
|
||||
serverButton.addActionListener(e -> onServerClicked());
|
||||
localButton.addActionListener(e -> onLocalClicked());
|
||||
|
||||
}
|
||||
|
||||
private void onServerClicked() {
|
||||
@@ -30,5 +29,4 @@ public class LocalServerSelector {
|
||||
frame.dispose();
|
||||
new LocalGameSelector();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
package org.toop.frontend.UI;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.toop.eventbus.Events;
|
||||
import org.toop.eventbus.GlobalEventBus;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import javax.swing.*;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.toop.eventbus.Events;
|
||||
import org.toop.eventbus.GlobalEventBus;
|
||||
|
||||
public class RemoteGameSelector {
|
||||
private static final Logger logger = LogManager.getLogger(RemoteGameSelector.class);
|
||||
@@ -29,7 +28,7 @@ public class RemoteGameSelector {
|
||||
public RemoteGameSelector() {
|
||||
gameSelectorBox.addItem("Tic Tac Toe");
|
||||
gameSelectorBox.addItem("Reversi");
|
||||
//todo get supported games from server and add to gameSelectorBox
|
||||
// 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);
|
||||
@@ -38,83 +37,102 @@ public class RemoteGameSelector {
|
||||
init();
|
||||
frame.add(mainMenu);
|
||||
frame.setVisible(true);
|
||||
//GlobalEventBus.subscribeAndRegister() Todo add game panel to frame when connection succeeds
|
||||
// GlobalEventBus.subscribeAndRegister() Todo add game panel to frame when connection
|
||||
// succeeds
|
||||
|
||||
}
|
||||
|
||||
private void init() {
|
||||
connectButton.addActionListener((ActionEvent e) -> {
|
||||
if( !nameTextField.getText().isEmpty() &&
|
||||
!name2TextField.getText().isEmpty() &&
|
||||
!ipTextField.getText().isEmpty() &&
|
||||
!portTextField.getText().isEmpty()) {
|
||||
connectButton.addActionListener(
|
||||
(ActionEvent e) -> {
|
||||
if (!nameTextField.getText().isEmpty()
|
||||
&& !name2TextField.getText().isEmpty()
|
||||
&& !ipTextField.getText().isEmpty()
|
||||
&& !portTextField.getText().isEmpty()) {
|
||||
|
||||
CompletableFuture<String> serverIdFuture = new CompletableFuture<>();
|
||||
GlobalEventBus.post(new Events.ServerEvents.StartServerRequest(
|
||||
portTextField.getText(),
|
||||
Objects.requireNonNull(gameSelectorBox.getSelectedItem()).toString().toLowerCase().replace(" ", ""),
|
||||
serverIdFuture
|
||||
));
|
||||
String serverId;
|
||||
try {
|
||||
serverId = serverIdFuture.get();
|
||||
} catch (InterruptedException | ExecutionException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
} // TODO: Better error handling to not crash the system.
|
||||
CompletableFuture<String> serverIdFuture = new CompletableFuture<>();
|
||||
GlobalEventBus.post(
|
||||
new Events.ServerEvents.StartServerRequest(
|
||||
portTextField.getText(),
|
||||
Objects.requireNonNull(gameSelectorBox.getSelectedItem())
|
||||
.toString()
|
||||
.toLowerCase()
|
||||
.replace(" ", ""),
|
||||
serverIdFuture));
|
||||
String serverId;
|
||||
try {
|
||||
serverId = serverIdFuture.get();
|
||||
} catch (InterruptedException | ExecutionException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
} // TODO: Better error handling to not crash the system.
|
||||
|
||||
CompletableFuture<String> connectionIdFuture = new CompletableFuture<>();
|
||||
GlobalEventBus.post(new Events.ServerEvents.StartConnectionRequest(
|
||||
ipTextField.getText(),
|
||||
portTextField.getText(),
|
||||
connectionIdFuture
|
||||
));
|
||||
String connectionId;
|
||||
try {
|
||||
connectionId = connectionIdFuture.get();
|
||||
} catch (InterruptedException | ExecutionException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
} // TODO: Better error handling to not crash the system.
|
||||
CompletableFuture<String> connectionIdFuture = new CompletableFuture<>();
|
||||
GlobalEventBus.post(
|
||||
new Events.ServerEvents.StartConnectionRequest(
|
||||
ipTextField.getText(),
|
||||
portTextField.getText(),
|
||||
connectionIdFuture));
|
||||
String connectionId;
|
||||
try {
|
||||
connectionId = connectionIdFuture.get();
|
||||
} catch (InterruptedException | ExecutionException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
} // TODO: Better error handling to not crash the system.
|
||||
|
||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.ReceivedMessage.class,
|
||||
event -> {
|
||||
if (event.message().equalsIgnoreCase("ok")) {
|
||||
logger.info("received ok from server.");
|
||||
} else if (event.message().toLowerCase().startsWith("gameid")) {
|
||||
String gameId = event.message().toLowerCase().replace("gameid ", "");
|
||||
GlobalEventBus.post(new Events.ServerEvents.SendCommand("start_game " + gameId));
|
||||
}
|
||||
else {
|
||||
logger.info("{}", event.message());
|
||||
}
|
||||
GlobalEventBus.subscribeAndRegister(
|
||||
Events.ServerEvents.ReceivedMessage.class,
|
||||
event -> {
|
||||
if (event.message().equalsIgnoreCase("ok")) {
|
||||
logger.info("received ok from server.");
|
||||
} else if (event.message().toLowerCase().startsWith("gameid")) {
|
||||
String gameId =
|
||||
event.message()
|
||||
.toLowerCase()
|
||||
.replace("gameid ", "");
|
||||
GlobalEventBus.post(
|
||||
new Events.ServerEvents.SendCommand(
|
||||
"start_game " + gameId));
|
||||
} else {
|
||||
logger.info("{}", event.message());
|
||||
}
|
||||
});
|
||||
|
||||
GlobalEventBus.post(
|
||||
new Events.ServerEvents.SendCommand(
|
||||
connectionId,
|
||||
"create_game",
|
||||
nameTextField.getText(),
|
||||
name2TextField.getText()));
|
||||
|
||||
// CompletableFuture<String> ticTacToeGame = new
|
||||
// CompletableFuture<>();
|
||||
// GlobalEventBus.post(new
|
||||
// Events.ServerEvents.CreateTicTacToeGameRequest( // TODO: Make this happen
|
||||
// through commands send through the connection, instead of an event.
|
||||
// serverId,
|
||||
// nameTextField.getText(),
|
||||
// name2TextField.getText(),
|
||||
// ticTacToeGame
|
||||
// ));
|
||||
// String ticTacToeGameId;
|
||||
// try {
|
||||
// ticTacToeGameId = ticTacToeGame.get();
|
||||
// } catch (InterruptedException | ExecutionException ex) {
|
||||
// throw new RuntimeException(ex);
|
||||
// } // TODO: Better error handling to not crash the system.
|
||||
|
||||
frame.remove(mainMenu);
|
||||
// UIGameBoard ttt = new UIGameBoard("tic tac toe", "test",
|
||||
// "test",this); // TODO: Fix later
|
||||
// frame.add(ttt.getTTTPanel()); // TODO: Fix later
|
||||
frame.revalidate();
|
||||
frame.repaint();
|
||||
} else {
|
||||
fillAllFields.setVisible(true);
|
||||
}
|
||||
);
|
||||
|
||||
GlobalEventBus.post(new Events.ServerEvents.SendCommand(connectionId, "create_game", nameTextField.getText(), name2TextField.getText()));
|
||||
|
||||
// CompletableFuture<String> ticTacToeGame = new CompletableFuture<>();
|
||||
// GlobalEventBus.post(new Events.ServerEvents.CreateTicTacToeGameRequest( // TODO: Make this happen through commands send through the connection, instead of an event.
|
||||
// serverId,
|
||||
// nameTextField.getText(),
|
||||
// name2TextField.getText(),
|
||||
// ticTacToeGame
|
||||
// ));
|
||||
// String ticTacToeGameId;
|
||||
// try {
|
||||
// ticTacToeGameId = ticTacToeGame.get();
|
||||
// } catch (InterruptedException | ExecutionException ex) {
|
||||
// throw new RuntimeException(ex);
|
||||
// } // TODO: Better error handling to not crash the system.
|
||||
|
||||
|
||||
frame.remove(mainMenu);
|
||||
// UIGameBoard ttt = new UIGameBoard("tic tac toe", "test", "test",this); // TODO: Fix later
|
||||
// frame.add(ttt.getTTTPanel()); // TODO: Fix later
|
||||
frame.revalidate();
|
||||
frame.repaint();
|
||||
} else {
|
||||
fillAllFields.setVisible(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public void returnToMainMenu() {
|
||||
frame.removeAll();
|
||||
frame.add(mainMenu);
|
||||
|
||||
@@ -2,5 +2,4 @@ package org.toop.frontend.UI;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
public class Services {
|
||||
}
|
||||
public class Services {}
|
||||
|
||||
@@ -1,18 +1,14 @@
|
||||
package org.toop.frontend.UI;
|
||||
|
||||
import org.toop.eventbus.Events;
|
||||
import org.toop.eventbus.GlobalEventBus;
|
||||
import org.toop.frontend.games.LocalTicTacToe;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.util.Objects;
|
||||
import javax.swing.*;
|
||||
import org.toop.frontend.games.LocalTicTacToe;
|
||||
|
||||
public class UIGameBoard {
|
||||
private static final int TICTACTOE_SIZE = 3;
|
||||
|
||||
private JPanel tttPanel; // Root panel for this game
|
||||
private JPanel tttPanel; // Root panel for this game
|
||||
private JButton backToMainMenuButton;
|
||||
private JButton[] cells;
|
||||
private String currentPlayer = "X";
|
||||
@@ -32,20 +28,20 @@ public class UIGameBoard {
|
||||
// Back button
|
||||
backToMainMenuButton = new JButton("Back to Main Menu");
|
||||
tttPanel.add(backToMainMenuButton, BorderLayout.SOUTH);
|
||||
backToMainMenuButton.addActionListener(e ->
|
||||
// TODO reset game and connections
|
||||
parent.showMainMenu()
|
||||
);
|
||||
backToMainMenuButton.addActionListener(
|
||||
_ ->
|
||||
// TODO reset game and connections
|
||||
parent.showMainMenu());
|
||||
|
||||
// Game grid
|
||||
JPanel gameGrid = createGridPanel(TICTACTOE_SIZE, TICTACTOE_SIZE);
|
||||
tttPanel.add(gameGrid, BorderLayout.CENTER);
|
||||
|
||||
// localTicTacToe.setMoveListener((playerIndex, moveIndex, symbol) -> {
|
||||
// SwingUtilities.invokeLater(() -> {
|
||||
// cells[moveIndex].setText(String.valueOf(symbol));
|
||||
// });
|
||||
// });
|
||||
// localTicTacToe.setMoveListener((playerIndex, moveIndex, symbol) -> {
|
||||
// SwingUtilities.invokeLater(() -> {
|
||||
// cells[moveIndex].setText(String.valueOf(symbol));
|
||||
// });
|
||||
// });
|
||||
|
||||
}
|
||||
|
||||
@@ -59,18 +55,25 @@ public class UIGameBoard {
|
||||
panel.add(cells[i]);
|
||||
|
||||
final int index = i;
|
||||
cells[i].addActionListener((ActionEvent e) -> {
|
||||
int cp = this.localTicTacToe.getCurrentPlayersTurn();
|
||||
if (cp == 0) { this.currentPlayer = "X"; currentPlayerIndex = 0; }
|
||||
else if (cp == 1) { this.currentPlayer = "O"; currentPlayerIndex = 1; }
|
||||
this.localTicTacToe.move(index);
|
||||
cells[index].setText(currentPlayer);
|
||||
});
|
||||
cells[i].addActionListener(
|
||||
(ActionEvent _) -> {
|
||||
int cp = this.localTicTacToe.getCurrentPlayersTurn();
|
||||
if (cp == 0) {
|
||||
this.currentPlayer = "X";
|
||||
currentPlayerIndex = 0;
|
||||
} else if (cp == 1) {
|
||||
this.currentPlayer = "O";
|
||||
currentPlayerIndex = 1;
|
||||
}
|
||||
this.localTicTacToe.move(index);
|
||||
cells[index].setText(currentPlayer);
|
||||
});
|
||||
}
|
||||
|
||||
return panel;
|
||||
}
|
||||
public void setCell(int index, String move){
|
||||
|
||||
public void setCell(int index, String move) {
|
||||
System.out.println(cells[index].getText());
|
||||
cells[index].setText(move);
|
||||
}
|
||||
@@ -78,4 +81,4 @@ public class UIGameBoard {
|
||||
public JPanel getTTTPanel() {
|
||||
return tttPanel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.toop.frontend.games;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.toop.eventbus.Events;
|
||||
@@ -9,12 +10,9 @@ import org.toop.game.GameBase;
|
||||
import org.toop.game.tictactoe.MinMaxTicTacToe;
|
||||
import org.toop.game.tictactoe.TicTacToe;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* A representation of a local tic-tac-toe game.
|
||||
* Calls are made to a server for information about current game state.
|
||||
* MOST OF THIS CODE IS TRASH, THROW IT OUT OF THE WINDOW AFTER DEMO.
|
||||
* A representation of a local tic-tac-toe game. Calls are made to a server for information about
|
||||
* current game state. MOST OF THIS CODE IS TRASH, THROW IT OUT OF THE WINDOW AFTER DEMO.
|
||||
*/
|
||||
public class LocalTicTacToe { // TODO: Implement runnable
|
||||
private static final Logger logger = LogManager.getLogger(LocalTicTacToe.class);
|
||||
@@ -36,38 +34,39 @@ public class LocalTicTacToe { // TODO: Implement runnable
|
||||
private TicTacToe ticTacToe;
|
||||
private UIGameBoard ui;
|
||||
|
||||
|
||||
/**
|
||||
* Is either 0 or 1.
|
||||
*/
|
||||
/** Is either 0 or 1. */
|
||||
private int playersTurn = 0;
|
||||
|
||||
/**
|
||||
* @return The current players turn.
|
||||
*/
|
||||
public int getCurrentPlayersTurn() { return this.playersTurn; }
|
||||
public int getCurrentPlayersTurn() {
|
||||
return this.playersTurn;
|
||||
}
|
||||
|
||||
|
||||
// LocalTicTacToe(String gameId, String connectionId, String serverId) {
|
||||
// this.gameId = gameId;
|
||||
// this.connectionId = connectionId;
|
||||
// this.serverId = serverId;
|
||||
// this.receivedMessageListener = GlobalEventBus.subscribe(Events.ServerEvents.ReceivedMessage.class, this::receiveMessageAction);
|
||||
// GlobalEventBus.register(this.receivedMessageListener);
|
||||
//
|
||||
//
|
||||
// this.executor.submit(this::gameThread);
|
||||
// } TODO: If remote server
|
||||
// LocalTicTacToe(String gameId, String connectionId, String serverId) {
|
||||
// this.gameId = gameId;
|
||||
// this.connectionId = connectionId;
|
||||
// this.serverId = serverId;
|
||||
// this.receivedMessageListener =
|
||||
// GlobalEventBus.subscribe(Events.ServerEvents.ReceivedMessage.class,
|
||||
// this::receiveMessageAction);
|
||||
// GlobalEventBus.register(this.receivedMessageListener);
|
||||
//
|
||||
//
|
||||
// this.executor.submit(this::gameThread);
|
||||
// } TODO: If remote server
|
||||
|
||||
/**
|
||||
*
|
||||
* Starts a connection with a remote server.
|
||||
*
|
||||
* @param ip The IP of the server to connect to.
|
||||
* @param port The port of the server to connect to.
|
||||
*/
|
||||
private LocalTicTacToe(String ip, String port) {
|
||||
this.receivedMessageListener = GlobalEventBus.subscribe(Events.ServerEvents.ReceivedMessage.class, this::receiveMessageAction);
|
||||
this.receivedMessageListener =
|
||||
GlobalEventBus.subscribe(
|
||||
Events.ServerEvents.ReceivedMessage.class, this::receiveMessageAction);
|
||||
GlobalEventBus.register(this.receivedMessageListener);
|
||||
this.connectionId = this.createConnection(ip, port);
|
||||
this.createGame(ip, port);
|
||||
@@ -100,7 +99,8 @@ public class LocalTicTacToe { // TODO: Implement runnable
|
||||
|
||||
private String createServer(String port) {
|
||||
CompletableFuture<String> serverIdFuture = new CompletableFuture<>();
|
||||
GlobalEventBus.post(new Events.ServerEvents.StartServerRequest(port, "tictactoe", serverIdFuture));
|
||||
GlobalEventBus.post(
|
||||
new Events.ServerEvents.StartServerRequest(port, "tictactoe", serverIdFuture));
|
||||
try {
|
||||
return serverIdFuture.get();
|
||||
} catch (Exception e) {
|
||||
@@ -111,7 +111,11 @@ public class LocalTicTacToe { // TODO: Implement runnable
|
||||
|
||||
private String createConnection(String ip, String port) {
|
||||
CompletableFuture<String> connectionIdFuture = new CompletableFuture<>();
|
||||
GlobalEventBus.post(new Events.ServerEvents.StartConnectionRequest(ip, port, connectionIdFuture)); // TODO: what if server couldn't be started with port.
|
||||
GlobalEventBus.post(
|
||||
new Events.ServerEvents.StartConnectionRequest(
|
||||
ip,
|
||||
port,
|
||||
connectionIdFuture)); // TODO: what if server couldn't be started with port.
|
||||
try {
|
||||
return connectionIdFuture.get();
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
@@ -127,7 +131,9 @@ public class LocalTicTacToe { // TODO: Implement runnable
|
||||
}
|
||||
|
||||
private void startGame() {
|
||||
if (this.gameId == null) { return; }
|
||||
if (this.gameId == null) {
|
||||
return;
|
||||
}
|
||||
this.sendCommand("start_game", this.gameId);
|
||||
}
|
||||
|
||||
@@ -176,9 +182,7 @@ public class LocalTicTacToe { // TODO: Implement runnable
|
||||
return ticTacToe.getGrid();
|
||||
}
|
||||
|
||||
/**
|
||||
* End the current game.
|
||||
*/
|
||||
/** End the current game. */
|
||||
public void endGame() {
|
||||
sendCommand("gameid", "end_game"); // TODO: Command is a bit wrong.
|
||||
}
|
||||
@@ -187,14 +191,25 @@ public class LocalTicTacToe { // TODO: Implement runnable
|
||||
* @param moveIndex The index of the move to make.
|
||||
*/
|
||||
public void move(int moveIndex) {
|
||||
this.executor.submit(() -> {
|
||||
try {
|
||||
if (this.playersTurn == 0 && !isAiPlayer[0]) { this.moveQueuePlayerA.put(moveIndex); logger.info("Adding player's {}, move: {}", this.playersTurn, moveIndex); }
|
||||
else if (this.playersTurn == 1 && !isAiPlayer[1]) { this.moveQueuePlayerB.put(moveIndex); logger.info("Adding player's {}, move: {}", this.playersTurn, moveIndex); }
|
||||
} catch (InterruptedException e) {
|
||||
logger.error("Could not add player: {}'s, move {}", this.playersTurn, moveIndex); // TODO: Error handling instead of crash.
|
||||
}
|
||||
});
|
||||
this.executor.submit(
|
||||
() -> {
|
||||
try {
|
||||
if (this.playersTurn == 0 && !isAiPlayer[0]) {
|
||||
this.moveQueuePlayerA.put(moveIndex);
|
||||
logger.info(
|
||||
"Adding player's {}, move: {}", this.playersTurn, moveIndex);
|
||||
} else if (this.playersTurn == 1 && !isAiPlayer[1]) {
|
||||
this.moveQueuePlayerB.put(moveIndex);
|
||||
logger.info(
|
||||
"Adding player's {}, move: {}", this.playersTurn, moveIndex);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
logger.error(
|
||||
"Could not add player: {}'s, move {}",
|
||||
this.playersTurn,
|
||||
moveIndex); // TODO: Error handling instead of crash.
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void endTheGame() {
|
||||
@@ -208,7 +223,11 @@ public class LocalTicTacToe { // TODO: Implement runnable
|
||||
}
|
||||
|
||||
try {
|
||||
logger.info("Received message from " + this.connectionId + ": " + receivedMessage.message());
|
||||
logger.info(
|
||||
"Received message from "
|
||||
+ this.connectionId
|
||||
+ ": "
|
||||
+ receivedMessage.message());
|
||||
this.receivedQueue.put(receivedMessage.message());
|
||||
} catch (InterruptedException e) {
|
||||
logger.error("Error waiting for received Message", e);
|
||||
@@ -226,4 +245,4 @@ public class LocalTicTacToe { // TODO: Implement runnable
|
||||
public void setUIReference(UIGameBoard uiGameBoard) {
|
||||
this.ui = uiGameBoard;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,50 +1,50 @@
|
||||
package org.toop.frontend.graphics;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.toop.frontend.platform.graphics.opengl.OpenglRenderer;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
|
||||
public abstract class Renderer {
|
||||
public enum API {
|
||||
NONE,
|
||||
OPENGL,
|
||||
};
|
||||
public enum API {
|
||||
NONE,
|
||||
OPENGL,
|
||||
};
|
||||
|
||||
protected static final Logger logger = LogManager.getLogger(Renderer.class);
|
||||
|
||||
private static API api = API.NONE;
|
||||
private static Renderer instance = null;
|
||||
private static API api = API.NONE;
|
||||
private static Renderer instance = null;
|
||||
|
||||
public static Renderer setup(API api) {
|
||||
if (instance != null) {
|
||||
logger.warn("Renderer is already setup.");
|
||||
return instance;
|
||||
}
|
||||
public static Renderer setup(API api) {
|
||||
if (instance != null) {
|
||||
logger.warn("Renderer is already setup.");
|
||||
return instance;
|
||||
}
|
||||
|
||||
switch (api) {
|
||||
case OPENGL:
|
||||
instance = new OpenglRenderer();
|
||||
break;
|
||||
switch (api) {
|
||||
case OPENGL:
|
||||
instance = new OpenglRenderer();
|
||||
break;
|
||||
|
||||
default:
|
||||
logger.fatal("No valid renderer api chosen");
|
||||
return null;
|
||||
}
|
||||
default:
|
||||
logger.fatal("No valid renderer api chosen");
|
||||
return null;
|
||||
}
|
||||
|
||||
Renderer.api = api;
|
||||
return instance;
|
||||
}
|
||||
Renderer.api = api;
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static API getApi() {
|
||||
return api;
|
||||
}
|
||||
public static API getApi() {
|
||||
return api;
|
||||
}
|
||||
|
||||
public void cleanup() {
|
||||
instance = null;
|
||||
logger.info("Renderer cleanup.");
|
||||
}
|
||||
public void cleanup() {
|
||||
instance = null;
|
||||
logger.info("Renderer cleanup.");
|
||||
}
|
||||
|
||||
public abstract void clear();
|
||||
public abstract void render();
|
||||
public abstract void clear();
|
||||
|
||||
public abstract void render();
|
||||
}
|
||||
|
||||
@@ -3,24 +3,25 @@ package org.toop.frontend.graphics;
|
||||
import org.toop.frontend.platform.graphics.opengl.OpenglShader;
|
||||
|
||||
public abstract class Shader {
|
||||
public static Shader create(String vertexPath, String fragmentPath) {
|
||||
Shader shader = null;
|
||||
public static Shader create(String vertexPath, String fragmentPath) {
|
||||
Shader shader = null;
|
||||
|
||||
switch (Renderer.getApi()) {
|
||||
case OPENGL:
|
||||
shader = new OpenglShader(vertexPath, fragmentPath);
|
||||
break;
|
||||
switch (Renderer.getApi()) {
|
||||
case OPENGL:
|
||||
shader = new OpenglShader(vertexPath, fragmentPath);
|
||||
break;
|
||||
|
||||
case NONE:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
case NONE:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return shader;
|
||||
}
|
||||
return shader;
|
||||
}
|
||||
|
||||
public abstract void cleanup();
|
||||
public abstract void cleanup();
|
||||
|
||||
public abstract void start();
|
||||
public abstract void stop();
|
||||
public abstract void start();
|
||||
|
||||
public abstract void stop();
|
||||
}
|
||||
|
||||
@@ -4,23 +4,30 @@ import org.toop.core.*;
|
||||
import org.toop.frontend.math.Color;
|
||||
|
||||
public class Button extends Node {
|
||||
ICallable<Boolean> onHover;
|
||||
ICallable<Boolean> onClick;
|
||||
ICallable<Boolean> onHover;
|
||||
ICallable<Boolean> onClick;
|
||||
|
||||
public Button(int x, int y, int width, int height, Color color, ICallable<Boolean> onHover, ICallable<Boolean> onClick) {
|
||||
super(x, y, width, height, color);
|
||||
public Button(
|
||||
int x,
|
||||
int y,
|
||||
int width,
|
||||
int height,
|
||||
Color color,
|
||||
ICallable<Boolean> onHover,
|
||||
ICallable<Boolean> onClick) {
|
||||
super(x, y, width, height, color);
|
||||
|
||||
this.onHover = onHover;
|
||||
this.onClick = onClick;
|
||||
}
|
||||
this.onHover = onHover;
|
||||
this.onClick = onClick;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void hover() {
|
||||
onHover.call();
|
||||
}
|
||||
@Override
|
||||
public void hover() {
|
||||
onHover.call();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void click() {
|
||||
onClick.call();
|
||||
}
|
||||
@Override
|
||||
public void click() {
|
||||
onClick.call();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,18 +4,19 @@ import org.toop.frontend.math.Bounds;
|
||||
import org.toop.frontend.math.Color;
|
||||
|
||||
public abstract class Node {
|
||||
protected Bounds bounds;
|
||||
protected Color color;
|
||||
protected Bounds bounds;
|
||||
protected Color color;
|
||||
|
||||
public Node(int x, int y, int width, int height, Color color) {
|
||||
bounds = new Bounds(x, y, width, height);
|
||||
this.color = color;
|
||||
}
|
||||
public Node(int x, int y, int width, int height, Color color) {
|
||||
bounds = new Bounds(x, y, width, height);
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public boolean check(int x, int y) {
|
||||
return bounds.check(x, y);
|
||||
}
|
||||
public boolean check(int x, int y) {
|
||||
return bounds.check(x, y);
|
||||
}
|
||||
|
||||
public void hover() {}
|
||||
public void click() {}
|
||||
public void hover() {}
|
||||
|
||||
public void click() {}
|
||||
}
|
||||
|
||||
@@ -1,66 +1,67 @@
|
||||
package org.toop.frontend.graphics.node;
|
||||
|
||||
import java.util.*;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.toop.eventbus.*;
|
||||
import org.toop.frontend.graphics.Shader;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
|
||||
public class NodeManager {
|
||||
private static final Logger logger = LogManager.getLogger(NodeManager.class);
|
||||
|
||||
private static NodeManager instance = null;
|
||||
|
||||
public static NodeManager setup() {
|
||||
if (instance != null) {
|
||||
logger.warn("NodeManager is already setup.");
|
||||
return instance;
|
||||
}
|
||||
private static NodeManager instance = null;
|
||||
|
||||
instance = new NodeManager();
|
||||
return instance;
|
||||
}
|
||||
public static NodeManager setup() {
|
||||
if (instance != null) {
|
||||
logger.warn("NodeManager is already setup.");
|
||||
return instance;
|
||||
}
|
||||
|
||||
private Shader shader;
|
||||
private ArrayList<Node> nodes;
|
||||
private Node active;
|
||||
instance = new NodeManager();
|
||||
return instance;
|
||||
}
|
||||
|
||||
private NodeManager() {
|
||||
shader = Shader.create(
|
||||
"src/main/resources/shaders/gui_vertex.glsl",
|
||||
"src/main/resources/shaders/gui_fragment.glsl");
|
||||
private Shader shader;
|
||||
private ArrayList<Node> nodes;
|
||||
private Node active;
|
||||
|
||||
nodes = new ArrayList<Node>();
|
||||
private NodeManager() {
|
||||
shader =
|
||||
Shader.create(
|
||||
"src/main/resources/shaders/gui_vertex.glsl",
|
||||
"src/main/resources/shaders/gui_fragment.glsl");
|
||||
|
||||
GlobalEventBus.subscribeAndRegister(Events.WindowEvents.OnMouseMove.class, event -> {
|
||||
for (int i = 0; i < nodes.size(); i++) {
|
||||
Node node = nodes.get(i);
|
||||
nodes = new ArrayList<Node>();
|
||||
|
||||
if (node.check(event.x(), event.y())) {
|
||||
active = node;
|
||||
node.hover();
|
||||
GlobalEventBus.subscribeAndRegister(
|
||||
Events.WindowEvents.OnMouseMove.class,
|
||||
event -> {
|
||||
for (int i = 0; i < nodes.size(); i++) {
|
||||
Node node = nodes.get(i);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
if (node.check(event.x(), event.y())) {
|
||||
active = node;
|
||||
node.hover();
|
||||
|
||||
GlobalEventBus.subscribeAndRegister(Events.WindowEvents.OnMouseClick.class, event -> {
|
||||
if (active != null) {
|
||||
active.click();
|
||||
}
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
public void cleanup() {
|
||||
}
|
||||
GlobalEventBus.subscribeAndRegister(
|
||||
Events.WindowEvents.OnMouseClick.class,
|
||||
event -> {
|
||||
if (active != null) {
|
||||
active.click();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void add(Node node) {
|
||||
nodes.add(node);
|
||||
}
|
||||
public void cleanup() {}
|
||||
|
||||
public void render() {
|
||||
}
|
||||
public void add(Node node) {
|
||||
nodes.add(node);
|
||||
}
|
||||
|
||||
public void render() {}
|
||||
}
|
||||
|
||||
@@ -1,49 +1,48 @@
|
||||
package org.toop.frontend.graphics.node;
|
||||
|
||||
import java.util.*;
|
||||
import org.toop.frontend.math.Bounds;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Widget {
|
||||
Bounds bounds;
|
||||
private ArrayList<Node> nodes;
|
||||
Bounds bounds;
|
||||
private ArrayList<Node> nodes;
|
||||
|
||||
public Widget(Bounds bounds) {
|
||||
this.bounds = bounds;
|
||||
nodes = new ArrayList<Node>();
|
||||
}
|
||||
public Widget(Bounds bounds) {
|
||||
this.bounds = bounds;
|
||||
nodes = new ArrayList<Node>();
|
||||
}
|
||||
|
||||
public boolean check(int x, int y) {
|
||||
return bounds.check(x, y);
|
||||
}
|
||||
public boolean check(int x, int y) {
|
||||
return bounds.check(x, y);
|
||||
}
|
||||
|
||||
public void add(Node node) {
|
||||
nodes.add(node);
|
||||
}
|
||||
public void add(Node node) {
|
||||
nodes.add(node);
|
||||
}
|
||||
|
||||
public boolean hover(int x, int y) {
|
||||
for (int i = 0; i < nodes.size(); i++) {
|
||||
Node node = nodes.get(i);
|
||||
public boolean hover(int x, int y) {
|
||||
for (int i = 0; i < nodes.size(); i++) {
|
||||
Node node = nodes.get(i);
|
||||
|
||||
if (node.check(x, y)) {
|
||||
node.hover();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (node.check(x, y)) {
|
||||
node.hover();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean click(int x, int y) {
|
||||
for (int i = 0; i < nodes.size(); i++) {
|
||||
Node node = nodes.get(i);
|
||||
public boolean click(int x, int y) {
|
||||
for (int i = 0; i < nodes.size(); i++) {
|
||||
Node node = nodes.get(i);
|
||||
|
||||
if (node.check(x, y)) {
|
||||
node.click();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (node.check(x, y)) {
|
||||
node.click();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,30 +1,39 @@
|
||||
package org.toop.frontend.math;
|
||||
|
||||
public class Bounds {
|
||||
private int x;
|
||||
private int y;
|
||||
private int width;
|
||||
private int height;
|
||||
private int x;
|
||||
private int y;
|
||||
private int width;
|
||||
private int height;
|
||||
|
||||
public Bounds(int x, int y, int width, int height) {
|
||||
set(x, y, width, height);
|
||||
}
|
||||
public Bounds(int x, int y, int width, int height) {
|
||||
set(x, y, width, height);
|
||||
}
|
||||
|
||||
public void set(int x, int y, int width, int height) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
public void set(int x, int y, int width, int height) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public int getX() { return x; }
|
||||
public int getY() { return y; }
|
||||
public int getWidth() { return width; }
|
||||
public int getHeight() { return height; }
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public boolean check(int x, int y) {
|
||||
return
|
||||
x >= this.x && x <= this.x + this.width &&
|
||||
y >= this.y && y <= this.y + this.height;
|
||||
}
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public boolean check(int x, int y) {
|
||||
return x >= this.x && x <= this.x + this.width && y >= this.y && y <= this.y + this.height;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,25 @@
|
||||
package org.toop.frontend.math;
|
||||
|
||||
public class Color {
|
||||
private float r;
|
||||
private float g;
|
||||
private float b;
|
||||
private float r;
|
||||
private float g;
|
||||
private float b;
|
||||
|
||||
public Color(float r, float g, float b) {
|
||||
this.r = r;
|
||||
this.g = g;
|
||||
this.b = b;
|
||||
}
|
||||
public Color(float r, float g, float b) {
|
||||
this.r = r;
|
||||
this.g = g;
|
||||
this.b = b;
|
||||
}
|
||||
|
||||
public float r() { return r; }
|
||||
public float g() { return g; }
|
||||
public float b() { return b; }
|
||||
public float r() {
|
||||
return r;
|
||||
}
|
||||
|
||||
public float g() {
|
||||
return g;
|
||||
}
|
||||
|
||||
public float b() {
|
||||
return b;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,95 +1,108 @@
|
||||
package org.toop.frontend.platform.core.glfw;
|
||||
|
||||
import org.toop.core.*;
|
||||
import org.toop.eventbus.*;
|
||||
|
||||
import org.lwjgl.glfw.*;
|
||||
import org.lwjgl.system.*;
|
||||
import org.toop.core.*;
|
||||
import org.toop.eventbus.*;
|
||||
|
||||
public class GlfwWindow extends Window {
|
||||
private long window;
|
||||
private long window;
|
||||
|
||||
public GlfwWindow(String title, Size size) {
|
||||
if (!GLFW.glfwInit()) {
|
||||
logger.fatal("Failed to initialize glfw");
|
||||
return;
|
||||
}
|
||||
public GlfwWindow(String title, Size size) {
|
||||
if (!GLFW.glfwInit()) {
|
||||
logger.fatal("Failed to initialize glfw");
|
||||
return;
|
||||
}
|
||||
|
||||
GLFW.glfwDefaultWindowHints();
|
||||
GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GLFW.GLFW_FALSE);
|
||||
GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, GLFW.GLFW_TRUE);
|
||||
GLFW.glfwDefaultWindowHints();
|
||||
GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GLFW.GLFW_FALSE);
|
||||
GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, GLFW.GLFW_TRUE);
|
||||
|
||||
GLFWVidMode videoMode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
|
||||
GLFWVidMode videoMode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
|
||||
|
||||
int width = size.width();
|
||||
int height = size.height();
|
||||
int width = size.width();
|
||||
int height = size.height();
|
||||
|
||||
if (width <= 0 || height <= 0 || width > videoMode.width() || height > videoMode.height()) {
|
||||
width = videoMode.width();
|
||||
height = videoMode.height();
|
||||
if (width <= 0 || height <= 0 || width > videoMode.width() || height > videoMode.height()) {
|
||||
width = videoMode.width();
|
||||
height = videoMode.height();
|
||||
|
||||
GLFW.glfwWindowHint(GLFW.GLFW_MAXIMIZED, GLFW.GLFW_TRUE);
|
||||
}
|
||||
GLFW.glfwWindowHint(GLFW.GLFW_MAXIMIZED, GLFW.GLFW_TRUE);
|
||||
}
|
||||
|
||||
long window = GLFW.glfwCreateWindow(width, height, title, MemoryUtil.NULL, MemoryUtil.NULL);
|
||||
long window = GLFW.glfwCreateWindow(width, height, title, MemoryUtil.NULL, MemoryUtil.NULL);
|
||||
|
||||
if (window == MemoryUtil.NULL) {
|
||||
GLFW.glfwTerminate();
|
||||
if (window == MemoryUtil.NULL) {
|
||||
GLFW.glfwTerminate();
|
||||
|
||||
logger.fatal("Failed to create glfw window");
|
||||
return;
|
||||
}
|
||||
logger.fatal("Failed to create glfw window");
|
||||
return;
|
||||
}
|
||||
|
||||
int[] widthBuffer = new int[1];
|
||||
int[] heightBuffer = new int[1];
|
||||
GLFW.glfwGetWindowSize(window, widthBuffer, heightBuffer);
|
||||
int[] widthBuffer = new int[1];
|
||||
int[] heightBuffer = new int[1];
|
||||
GLFW.glfwGetWindowSize(window, widthBuffer, heightBuffer);
|
||||
|
||||
GLFW.glfwMakeContextCurrent(window);
|
||||
GLFW.glfwSwapInterval(1);
|
||||
GLFW.glfwMakeContextCurrent(window);
|
||||
GLFW.glfwSwapInterval(1);
|
||||
|
||||
GLFW.glfwSetWindowCloseCallback(window, (lwindow) -> {
|
||||
GlobalEventBus.post(new Events.WindowEvents.OnQuitRequested());
|
||||
});
|
||||
GLFW.glfwSetWindowCloseCallback(
|
||||
window,
|
||||
(lwindow) -> {
|
||||
GlobalEventBus.post(new Events.WindowEvents.OnQuitRequested());
|
||||
});
|
||||
|
||||
GLFW.glfwSetFramebufferSizeCallback(window, (lwindow, lwidth, lheight) -> {
|
||||
GlobalEventBus.post(new Events.WindowEvents.OnResize(new Size(lwidth, lheight)));
|
||||
});
|
||||
GLFW.glfwSetFramebufferSizeCallback(
|
||||
window,
|
||||
(lwindow, lwidth, lheight) -> {
|
||||
GlobalEventBus.post(
|
||||
new Events.WindowEvents.OnResize(new Size(lwidth, lheight)));
|
||||
});
|
||||
|
||||
GLFW.glfwSetCursorPosCallback(window, (lwindow, lx, ly) -> {
|
||||
GlobalEventBus.post(new Events.WindowEvents.OnMouseMove((int)lx, (int)ly));
|
||||
});
|
||||
GLFW.glfwSetCursorPosCallback(
|
||||
window,
|
||||
(lwindow, lx, ly) -> {
|
||||
GlobalEventBus.post(new Events.WindowEvents.OnMouseMove((int) lx, (int) ly));
|
||||
});
|
||||
|
||||
GLFW.glfwSetMouseButtonCallback(window, (lwindow, lbutton, laction, lmods) -> {
|
||||
switch (laction) {
|
||||
case GLFW.GLFW_PRESS:
|
||||
GlobalEventBus.post(new Events.WindowEvents.OnMouseClick(lbutton));
|
||||
break;
|
||||
GLFW.glfwSetMouseButtonCallback(
|
||||
window,
|
||||
(lwindow, lbutton, laction, lmods) -> {
|
||||
switch (laction) {
|
||||
case GLFW.GLFW_PRESS:
|
||||
GlobalEventBus.post(new Events.WindowEvents.OnMouseClick(lbutton));
|
||||
break;
|
||||
|
||||
case GLFW.GLFW_RELEASE:
|
||||
GlobalEventBus.post(new Events.WindowEvents.OnMouseRelease(lbutton));
|
||||
break;
|
||||
case GLFW.GLFW_RELEASE:
|
||||
GlobalEventBus.post(new Events.WindowEvents.OnMouseRelease(lbutton));
|
||||
break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
});
|
||||
default:
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
this.window = window;
|
||||
GLFW.glfwShowWindow(window);
|
||||
this.window = window;
|
||||
GLFW.glfwShowWindow(window);
|
||||
|
||||
logger.info("Glfw window setup. Title: {}. Width: {}. Height: {}.", title, size.width(), size.height());
|
||||
}
|
||||
logger.info(
|
||||
"Glfw window setup. Title: {}. Width: {}. Height: {}.",
|
||||
title,
|
||||
size.width(),
|
||||
size.height());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanup() {
|
||||
GLFW.glfwDestroyWindow(window);
|
||||
GLFW.glfwTerminate();
|
||||
@Override
|
||||
public void cleanup() {
|
||||
GLFW.glfwDestroyWindow(window);
|
||||
GLFW.glfwTerminate();
|
||||
|
||||
super.cleanup();
|
||||
}
|
||||
super.cleanup();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
GLFW.glfwSwapBuffers(window);
|
||||
GLFW.glfwPollEvents();
|
||||
}
|
||||
@Override
|
||||
public void update() {
|
||||
GLFW.glfwSwapBuffers(window);
|
||||
GLFW.glfwPollEvents();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,76 +1,78 @@
|
||||
package org.toop.frontend.platform.graphics.opengl;
|
||||
|
||||
import org.lwjgl.opengl.*;
|
||||
import org.lwjgl.system.*;
|
||||
import org.toop.eventbus.*;
|
||||
import org.toop.frontend.graphics.Renderer;
|
||||
import org.toop.frontend.graphics.Shader;
|
||||
|
||||
import org.lwjgl.opengl.*;
|
||||
import org.lwjgl.system.*;
|
||||
|
||||
public class OpenglRenderer extends Renderer {
|
||||
private Shader shader;
|
||||
private int vao;
|
||||
private Shader shader;
|
||||
private int vao;
|
||||
|
||||
public OpenglRenderer() {
|
||||
GL.createCapabilities();
|
||||
GL45.glClearColor(0.65f, 0.9f, 0.65f, 1f);
|
||||
|
||||
GlobalEventBus.subscribeAndRegister(Events.WindowEvents.OnResize.class, event -> {
|
||||
GL45.glViewport(0, 0, event.size().width(), event.size().height());
|
||||
});
|
||||
public OpenglRenderer() {
|
||||
GL.createCapabilities();
|
||||
GL45.glClearColor(0.65f, 0.9f, 0.65f, 1f);
|
||||
|
||||
logger.info("Opengl renderer setup.");
|
||||
GlobalEventBus.subscribeAndRegister(
|
||||
Events.WindowEvents.OnResize.class,
|
||||
event -> {
|
||||
GL45.glViewport(0, 0, event.size().width(), event.size().height());
|
||||
});
|
||||
|
||||
// Form here on, everything is temporary
|
||||
float vertices[] = {
|
||||
-0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
|
||||
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, 1.0f, 1.0f, 0.0f,
|
||||
};
|
||||
logger.info("Opengl renderer setup.");
|
||||
|
||||
int indicies[] = {
|
||||
0, 1, 2,
|
||||
2, 3, 0,
|
||||
};
|
||||
// Form here on, everything is temporary
|
||||
float vertices[] = {
|
||||
-0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
|
||||
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, 1.0f, 1.0f, 0.0f,
|
||||
};
|
||||
|
||||
vao = GL45.glCreateVertexArrays();
|
||||
GL45.glBindVertexArray(vao);
|
||||
int indicies[] = {
|
||||
0, 1, 2,
|
||||
2, 3, 0,
|
||||
};
|
||||
|
||||
int vbo = GL45.glCreateBuffers();
|
||||
GL45.glBindBuffer(GL45.GL_ARRAY_BUFFER, vbo);
|
||||
GL45.glBufferData(GL45.GL_ARRAY_BUFFER, vertices, GL45.GL_STATIC_DRAW);
|
||||
vao = GL45.glCreateVertexArrays();
|
||||
GL45.glBindVertexArray(vao);
|
||||
|
||||
GL45.glVertexAttribPointer(0, 2, GL45.GL_FLOAT, false, 5 * 4, 0);
|
||||
GL45.glVertexAttribPointer(1, 3, GL45.GL_FLOAT, false, 5 * 4, 2 * 4);
|
||||
int vbo = GL45.glCreateBuffers();
|
||||
GL45.glBindBuffer(GL45.GL_ARRAY_BUFFER, vbo);
|
||||
GL45.glBufferData(GL45.GL_ARRAY_BUFFER, vertices, GL45.GL_STATIC_DRAW);
|
||||
|
||||
GL45.glEnableVertexAttribArray(0);
|
||||
GL45.glEnableVertexAttribArray(1);
|
||||
GL45.glVertexAttribPointer(0, 2, GL45.GL_FLOAT, false, 5 * 4, 0);
|
||||
GL45.glVertexAttribPointer(1, 3, GL45.GL_FLOAT, false, 5 * 4, 2 * 4);
|
||||
|
||||
int ib = GL45.glCreateBuffers();
|
||||
GL45.glBindBuffer(GL45.GL_ELEMENT_ARRAY_BUFFER, ib);
|
||||
GL45.glBufferData(GL45.GL_ELEMENT_ARRAY_BUFFER, indicies, GL45.GL_STATIC_DRAW);
|
||||
GL45.glEnableVertexAttribArray(0);
|
||||
GL45.glEnableVertexAttribArray(1);
|
||||
|
||||
shader = Shader.create(
|
||||
"src/main/resources/shaders/gui_vertex.glsl",
|
||||
"src/main/resources/shaders/gui_fragment.glsl");
|
||||
}
|
||||
int ib = GL45.glCreateBuffers();
|
||||
GL45.glBindBuffer(GL45.GL_ELEMENT_ARRAY_BUFFER, ib);
|
||||
GL45.glBufferData(GL45.GL_ELEMENT_ARRAY_BUFFER, indicies, GL45.GL_STATIC_DRAW);
|
||||
|
||||
@Override
|
||||
public void cleanup() {
|
||||
super.cleanup();
|
||||
}
|
||||
shader =
|
||||
Shader.create(
|
||||
"src/main/resources/shaders/gui_vertex.glsl",
|
||||
"src/main/resources/shaders/gui_fragment.glsl");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
GL45.glClear(GL45.GL_COLOR_BUFFER_BIT);
|
||||
}
|
||||
@Override
|
||||
public void cleanup() {
|
||||
super.cleanup();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
// temporary
|
||||
// shader.start();
|
||||
GL45.glBindVertexArray(vao);
|
||||
GL45.glDrawElements(GL45.GL_TRIANGLES, 6, GL45.GL_UNSIGNED_INT, MemoryUtil.NULL);
|
||||
}
|
||||
@Override
|
||||
public void clear() {
|
||||
GL45.glClear(GL45.GL_COLOR_BUFFER_BIT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
// temporary
|
||||
// shader.start();
|
||||
GL45.glBindVertexArray(vao);
|
||||
GL45.glDrawElements(GL45.GL_TRIANGLES, 6, GL45.GL_UNSIGNED_INT, MemoryUtil.NULL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,58 +1,57 @@
|
||||
package org.toop.frontend.platform.graphics.opengl;
|
||||
|
||||
import org.lwjgl.opengl.*;
|
||||
import org.toop.core.*;
|
||||
import org.toop.frontend.graphics.Shader;
|
||||
|
||||
import org.lwjgl.opengl.*;
|
||||
|
||||
public class OpenglShader extends Shader {
|
||||
private int programID;
|
||||
private int programID;
|
||||
|
||||
public OpenglShader(String vertexPath, String fragmentPath) {
|
||||
FileSystem.File vertexSource = FileSystem.read(vertexPath);
|
||||
FileSystem.File fragmentSource = FileSystem.read(fragmentPath);
|
||||
public OpenglShader(String vertexPath, String fragmentPath) {
|
||||
FileSystem.File vertexSource = FileSystem.read(vertexPath);
|
||||
FileSystem.File fragmentSource = FileSystem.read(fragmentPath);
|
||||
|
||||
if (vertexSource == null || fragmentPath == null) {
|
||||
return;
|
||||
}
|
||||
if (vertexSource == null || fragmentPath == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
programID = GL45.glCreateProgram();
|
||||
programID = GL45.glCreateProgram();
|
||||
|
||||
int vertexShader = GL45.glCreateShader(GL45.GL_VERTEX_SHADER);
|
||||
int fragmentShader = GL45.glCreateShader(GL45.GL_FRAGMENT_SHADER);
|
||||
int vertexShader = GL45.glCreateShader(GL45.GL_VERTEX_SHADER);
|
||||
int fragmentShader = GL45.glCreateShader(GL45.GL_FRAGMENT_SHADER);
|
||||
|
||||
GL45.glShaderSource(vertexShader, vertexSource.buffer());
|
||||
GL45.glShaderSource(fragmentShader, fragmentSource.buffer());
|
||||
GL45.glShaderSource(vertexShader, vertexSource.buffer());
|
||||
GL45.glShaderSource(fragmentShader, fragmentSource.buffer());
|
||||
|
||||
GL45.glCompileShader(vertexShader);
|
||||
GL45.glCompileShader(fragmentShader);
|
||||
GL45.glCompileShader(vertexShader);
|
||||
GL45.glCompileShader(fragmentShader);
|
||||
|
||||
GL45.glAttachShader(programID, vertexShader);
|
||||
GL45.glAttachShader(programID, fragmentShader);
|
||||
GL45.glAttachShader(programID, vertexShader);
|
||||
GL45.glAttachShader(programID, fragmentShader);
|
||||
|
||||
GL45.glLinkProgram(programID);
|
||||
GL45.glValidateProgram(programID);
|
||||
GL45.glLinkProgram(programID);
|
||||
GL45.glValidateProgram(programID);
|
||||
|
||||
GL45.glDetachShader(programID, vertexShader);
|
||||
GL45.glDetachShader(programID, fragmentShader);
|
||||
GL45.glDetachShader(programID, vertexShader);
|
||||
GL45.glDetachShader(programID, fragmentShader);
|
||||
|
||||
GL45.glDeleteShader(vertexShader);
|
||||
GL45.glDeleteShader(fragmentShader);
|
||||
}
|
||||
GL45.glDeleteShader(vertexShader);
|
||||
GL45.glDeleteShader(fragmentShader);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanup() {
|
||||
stop();
|
||||
GL45.glDeleteProgram(programID);
|
||||
}
|
||||
@Override
|
||||
public void cleanup() {
|
||||
stop();
|
||||
GL45.glDeleteProgram(programID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
GL45.glUseProgram(programID);
|
||||
}
|
||||
@Override
|
||||
public void start() {
|
||||
GL45.glUseProgram(programID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
GL45.glUseProgram(0);
|
||||
}
|
||||
@Override
|
||||
public void stop() {
|
||||
GL45.glUseProgram(0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user