This commit is contained in:
@@ -12,30 +12,68 @@ 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));
|
||||
private String displayText = "Connecting ...";
|
||||
|
||||
public ConnexionStatusView(StateMachine stateMachine, String address, short port)
|
||||
throws UnknownHostException, IOException {
|
||||
super(stateMachine);
|
||||
Thread t = new Thread(() -> {
|
||||
try {
|
||||
this.client = new Client(address, port);
|
||||
bindListeners();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
onDisconnect();
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
}
|
||||
|
||||
public ConnexionStatusView(StateMachine stateMachine, short port) throws UnknownHostException, IOException {
|
||||
this(stateMachine, new Server(port), new Client("localhost", port));
|
||||
super(stateMachine);
|
||||
Thread t = new Thread(() -> {
|
||||
try {
|
||||
this.server = new Server(port);
|
||||
this.client = new Client("localhost", port);
|
||||
bindListeners();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
onDisconnect();
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
}
|
||||
|
||||
private void bindListeners() {
|
||||
this.client.onConnect.connect(this::onConnect);
|
||||
this.client.onClosed.connect(this::onLeave);
|
||||
this.client.onDisconnect.connect(this::onDisconnect);
|
||||
}
|
||||
|
||||
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 onDisconnect() {
|
||||
if (client != null) {
|
||||
String reason = client.getDisconnectReason();
|
||||
if (reason == null)
|
||||
displayText = "Le serveur a fermé la connexion !";
|
||||
else
|
||||
displayText = "Vous avez été déconnecté ! Raison : " + client.getDisconnectReason();
|
||||
} else {
|
||||
displayText = "La connexion a échoué !";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void onLeave() {
|
||||
// System.out.println("Quitté !");
|
||||
this.client.onDisconnect.clear();
|
||||
this.client = null;
|
||||
// on passe le menu de la connexion
|
||||
this.closeMenu();
|
||||
@@ -43,7 +81,14 @@ public class ConnexionStatusView extends BaseView {
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
ImGui.text("Connecting ...");
|
||||
ImGui.text(displayText);
|
||||
renderReturnButton();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeMenu() {
|
||||
super.closeMenu();
|
||||
cleanResources();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -14,6 +14,7 @@ public class MultiMenu extends BaseView {
|
||||
|
||||
public MultiMenu(StateMachine stateMachine) {
|
||||
super(stateMachine);
|
||||
address.resize(20);
|
||||
}
|
||||
|
||||
private void renderCreate() {
|
||||
|
||||
@@ -20,9 +20,7 @@ public class MultiPlayerView extends BaseView {
|
||||
}
|
||||
|
||||
public void onDisconnect() {
|
||||
// System.out.println("ohohohohohohoho");
|
||||
this.stateMachine.popState();
|
||||
this.client.onDisconnect.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user