This commit is contained in:
@@ -12,7 +12,7 @@ public abstract class BaseView {
|
||||
|
||||
public abstract void render();
|
||||
|
||||
public void onKill() {}
|
||||
public void cleanResources() {}
|
||||
|
||||
public void closeMenu() {
|
||||
this.stateMachine.popState();
|
||||
|
||||
60
app/src/main/java/gui/menu/ConnexionStatusView.java
Normal file
60
app/src/main/java/gui/menu/ConnexionStatusView.java
Normal file
@@ -0,0 +1,60 @@
|
||||
package gui.menu;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import imgui.ImGui;
|
||||
import network.client.Client;
|
||||
import network.server.Server;
|
||||
|
||||
public class ConnexionStatusView extends BaseView {
|
||||
|
||||
private Client client;
|
||||
private Server server;
|
||||
|
||||
public ConnexionStatusView(StateMachine stateMachine, String address, short port) throws UnknownHostException, IOException {
|
||||
this(stateMachine, null, new Client(address, port));
|
||||
}
|
||||
|
||||
public ConnexionStatusView(StateMachine stateMachine, short port) throws UnknownHostException, IOException {
|
||||
this(stateMachine, new Server(port), new Client("localhost", port));
|
||||
}
|
||||
|
||||
private ConnexionStatusView(StateMachine stateMachine, Server server, Client client) {
|
||||
super(stateMachine);
|
||||
this.client = client;
|
||||
this.client.onConnect.connect(this::onConnect);
|
||||
this.client.onClosed.connect(this::onLeave);
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
public void onConnect() {
|
||||
// System.out.println("Connecté");
|
||||
this.stateMachine.pushState(new MultiPlayerView(stateMachine, client));
|
||||
}
|
||||
|
||||
public void onLeave() {
|
||||
// System.out.println("Quitté !");
|
||||
this.client.onDisconnect.clear();
|
||||
this.client = null;
|
||||
// on passe le menu de la connexion
|
||||
this.closeMenu();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
ImGui.text("Connecting ...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanResources() {
|
||||
// System.out.println("Bye bye !");
|
||||
if (this.server != null) {
|
||||
this.server.stop();
|
||||
}
|
||||
if (this.client != null) {
|
||||
this.client.stop();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,7 +22,7 @@ public class MultiMenu extends BaseView {
|
||||
ImGui.inputInt("Port", port);
|
||||
if (ImGui.button("Créer")) {
|
||||
try {
|
||||
this.stateMachine.pushState(new MultiPlayerView(stateMachine, (short) port.get()));
|
||||
this.stateMachine.pushState(new ConnexionStatusView(stateMachine, (short) port.get()));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -37,7 +37,7 @@ public class MultiMenu extends BaseView {
|
||||
ImGui.inputInt("Port", port);
|
||||
if (ImGui.button("Rejoindre")) {
|
||||
try {
|
||||
this.stateMachine.pushState(new MultiPlayerView(stateMachine, address.get(), (short) port.get()));
|
||||
this.stateMachine.pushState(new ConnexionStatusView(stateMachine, address.get(), (short) port.get()));
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -1,53 +1,28 @@
|
||||
package gui.menu;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import imgui.ImGui;
|
||||
import network.client.Client;
|
||||
import network.server.Server;
|
||||
|
||||
public class MultiPlayerView extends BaseView {
|
||||
|
||||
private final Client client;
|
||||
|
||||
private Client client;
|
||||
private Server server;
|
||||
|
||||
/**
|
||||
* Client
|
||||
*
|
||||
* @param stateMachine
|
||||
* @param address
|
||||
* @param port
|
||||
* @throws IOException
|
||||
* @throws UnknownHostException
|
||||
*/
|
||||
public MultiPlayerView(StateMachine stateMachine, String address, short port)
|
||||
throws UnknownHostException, IOException {
|
||||
public MultiPlayerView(StateMachine stateMachine, Client client) {
|
||||
super(stateMachine);
|
||||
this.client = new Client(address, port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Server
|
||||
*
|
||||
* @param stateMachine
|
||||
* @param port
|
||||
* @throws IOException
|
||||
*/
|
||||
public MultiPlayerView(StateMachine stateMachine, short port) throws IOException {
|
||||
super(stateMachine);
|
||||
this.server = new Server(port);
|
||||
this.client = new Client("localhost", port);
|
||||
this.client = client;
|
||||
this.client.onDisconnect.connect(this::onDisconnect);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onKill() {
|
||||
if (this.server != null) {
|
||||
this.server.stop();
|
||||
}
|
||||
if (this.client != null) {
|
||||
this.client.stop();
|
||||
}
|
||||
public void closeMenu() {
|
||||
this.client.forceDisconnect();
|
||||
super.closeMenu();
|
||||
}
|
||||
|
||||
public void onDisconnect() {
|
||||
// System.out.println("ohohohohohohoho");
|
||||
this.stateMachine.popState();
|
||||
this.client.onDisconnect.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -17,7 +17,7 @@ public class StateMachine {
|
||||
|
||||
public void clear() {
|
||||
for (BaseView view : menus) {
|
||||
view.onKill();
|
||||
view.cleanResources();
|
||||
}
|
||||
menus.clear();
|
||||
}
|
||||
@@ -27,13 +27,13 @@ public class StateMachine {
|
||||
}
|
||||
|
||||
public void popState() {
|
||||
menus.get(menus.size() - 1).onKill();
|
||||
menus.getLast().cleanResources();
|
||||
menus.pop();
|
||||
}
|
||||
|
||||
private void checkEscape() {
|
||||
if (ImGui.isKeyPressed(ImGuiKey.Escape) && menus.size() > 1) {
|
||||
popState();
|
||||
menus.getLast().closeMenu();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user