tcr/java/src/game/gui/GuiConnect.java
2025-03-12 18:13:11 +01:00

107 lines
3.2 KiB
Java

package game.gui;
import game.color.TextColor;
import game.gui.element.ActButton;
import game.gui.element.Label;
import game.gui.element.Textbox;
import game.gui.element.Textbox.Action;
import game.init.Config;
import game.network.NetHandlerPlayServer;
public class GuiConnect extends Gui implements Textbox.Callback {
public static final GuiConnect INSTANCE = new GuiConnect();
private GuiConnect() {
}
private Textbox addrBox;
private Textbox portBox;
private Textbox userBox;
private Textbox passBox;
private Textbox accBox;
private Label addrLabel;
private Label portLabel;
private Label userLabel;
private Label passLabel;
private Label accLabel;
public void init(int width, int height) {
this.addrBox = this.add(new Textbox(0, 20, 410, 24, 128, true, this, ""));
this.portBox = this.add(new Textbox(414, 20, 66, 24, 5, true, this, ""));
this.userBox = this.add(new Textbox(0, 70, 220, 24, NetHandlerPlayServer.MAX_USER_LENGTH, true, this, NetHandlerPlayServer.VALID_USER, ""));
this.passBox = this.add(new Textbox(0, 120, 480, 24, NetHandlerPlayServer.MAX_PASS_LENGTH, true, this, ""));
this.accBox = this.add(new Textbox(0, 170, 480, 24, NetHandlerPlayServer.MAX_PASS_LENGTH, true, this, ""));
this.add(new ActButton(0, 220, 480, 24, new ActButton.Callback() {
public void use(ActButton elem, ActButton.Mode action) {
GuiConnect.this.connect();
}
}, "Verbinden"));
this.add(new ActButton(0, 250, 480, 24, new ActButton.Callback() {
public void use(ActButton elem, ActButton.Mode action) {
GuiConnect.this.gm.displayGuiScreen(GuiMenu.INSTANCE);
}
}, "Zurück"));
this.addrLabel = this.add(new Label(0, 0, 410, 20, "Adresse", true));
this.portLabel = this.add(new Label(414, 0, 66, 20, "Port", true));
this.userLabel = this.add(new Label(0, 50, 220, 20, "Nutzer", true));
this.passLabel = this.add(new Label(0, 100, 480, 20, "Passwort", true));
this.accLabel = this.add(new Label(0, 150, 480, 20, "Zugang", true));
this.shift();
}
public String getTitle() {
return "Mit Server verbinden";
}
private void connect() {
if(this.gm.theWorld != null)
return;
String addr = this.addrBox.getText();
if(addr.isEmpty()) {
this.addrLabel.setText(TextColor.RED + "Adresse");
return;
}
int port = -1;
if(this.portBox.getText().isEmpty()) {
port = Config.PORT;
}
else {
try {
port = Integer.parseInt(this.portBox.getText());
}
catch(NumberFormatException e) {
}
if(port < 0 || port > 65535) {
this.portLabel.setText(TextColor.RED + "Port");
return;
}
}
String user = this.userBox.getText();
if(user.isEmpty()) {
this.userLabel.setText(TextColor.RED + "Nutzer");
return;
}
String pass = this.passBox.getText();
String acc = this.accBox.getText();
this.gm.connect(addr, port, user, pass, acc);
}
public void use(Textbox elem, Action value) {
if(value == Action.SEND) {
elem.setDeselected();
this.connect();
}
else if(value == Action.FOCUS) {
if(elem == this.addrBox)
this.addrLabel.setText("Adresse");
else if(elem == this.portBox)
this.portLabel.setText("Port");
else if(elem == this.userBox)
this.userLabel.setText("Nutzer");
}
}
// public void updateScreen() {
//
// }
}