Use new Gui API for GuiLoginPrompt and GuiRegister

This commit is contained in:
johni0702
2015-07-08 16:18:46 +02:00
parent db8b5a9be0
commit d4e04feaeb
5 changed files with 215 additions and 373 deletions

View File

@@ -1,204 +1,117 @@
package eu.crushedpixel.replaymod.gui.online;
import eu.crushedpixel.replaymod.gui.GuiConstants;
import eu.crushedpixel.replaymod.gui.PasswordTextField;
import de.johni0702.minecraft.gui.container.AbstractGuiScreen;
import de.johni0702.minecraft.gui.element.GuiButton;
import de.johni0702.minecraft.gui.element.GuiLabel;
import de.johni0702.minecraft.gui.element.GuiPasswordField;
import de.johni0702.minecraft.gui.element.GuiTextField;
import de.johni0702.minecraft.gui.layout.CustomLayout;
import eu.crushedpixel.replaymod.online.authentication.AuthenticationHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.resources.I18n;
import org.lwjgl.input.Keyboard;
import java.awt.*;
import java.io.IOException;
import java.util.List;
public class GuiLoginPrompt extends AbstractGuiScreen<GuiLoginPrompt> {
public class GuiLoginPrompt extends GuiScreen {
private final net.minecraft.client.gui.GuiScreen parent, successScreen;
private static final int EMPTY = 0;
private static final int LOGGING_IN = 1;
private static final int INVALID_LOGIN = 2;
private static final int NO_CONNECTION = 3;
private static Minecraft mc = Minecraft.getMinecraft();
private GuiScreen parent, successScreen;
private int textState = 0;
private GuiTextField username;
private PasswordTextField password;
private GuiButton loginButton;
private GuiButton cancelButton;
private GuiButton registerButton;
private GuiLabel usernameLabel = new GuiLabel(this).setI18nText("replaymod.gui.username");
private GuiLabel passwordLabel = new GuiLabel(this).setI18nText("replaymod.gui.password");
private GuiLabel noAccountLabel = new GuiLabel(this).setI18nText("replaymod.gui.login.noacc");
private GuiLabel statusLabel = new GuiLabel(this);
private GuiButton loginButton = new GuiButton(this).setI18nLabel("replaymod.gui.login").setWidth(150).setEnabled(false);
private GuiButton cancelButton = new GuiButton(this).setI18nLabel("replaymod.gui.cancel").setWidth(150);
private GuiButton registerButton = new GuiButton(this).setI18nLabel("replaymod.gui.register").setWidth(150);
private GuiTextField username = new GuiTextField(this).setSize(145, 20).setMaxLength(16).setFocused(true);
private GuiPasswordField password = new GuiPasswordField(this).setSize(145, 20).setNext(username).setPrevious(username);
private String noacc;
private int strwidth;
private boolean initialized = false;
public GuiScreen getSuccessScreen() {
return successScreen;
}
public GuiLoginPrompt(GuiScreen parent, GuiScreen successScreen) {
this.parent = parent;
this.successScreen = successScreen;
}
@Override
public void initGui() {
Keyboard.enableRepeatEvents(true);
if(!initialized) {
username = new GuiTextField(GuiConstants.LOGIN_USERNAME_FIELD, fontRendererObj, this.width / 2 - 45, 30, 145, 20);
username.setEnabled(true);
username.setFocused(true);
username.setMaxStringLength(16);
password = new PasswordTextField(GuiConstants.LOGIN_PASSWORD_FIELD, fontRendererObj, this.width / 2 - 45, 60, 145, 20);
password.setEnabled(true);
password.setFocused(false);
loginButton = new GuiButton(GuiConstants.LOGIN_OKAY_BUTTON, this.width / 2 - 150 - 2, 110, I18n.format("replaymod.gui.login"));
loginButton.enabled = false;
loginButton.width = 150;
cancelButton = new GuiButton(GuiConstants.LOGIN_CANCEL_BUTTON, this.width / 2 + 2, 110, I18n.format("replaymod.gui.cancel"));
cancelButton.width = 150;
registerButton = new GuiButton(GuiConstants.LOGIN_REGISTER_BUTTON, 0, this.height-30, I18n.format("replaymod.gui.register"));
registerButton.width = 150;
} else {
username.xPosition = password.xPosition = this.width / 2 - 45;
loginButton.xPosition = this.width / 2 - 150 - 2;
cancelButton.xPosition = this.width / 2 + 2;
registerButton.yPosition = this.height-30;
}
noacc = I18n.format("replaymod.gui.login.noacc");
strwidth = fontRendererObj.getStringWidth(noacc);
int tw = 150+5+strwidth;
registerButton.xPosition = (width/2) - (tw/2) + strwidth+5;
@SuppressWarnings("unchecked")
List<GuiButton> buttonList = this.buttonList;
buttonList.add(loginButton);
buttonList.add(cancelButton);
buttonList.add(registerButton);
initialized = true;
}
@Override
protected void actionPerformed(GuiButton button) throws IOException {
if(button.id == GuiConstants.LOGIN_OKAY_BUTTON) {
if(button.enabled) {
//Authenticate
textState = LOGGING_IN;
{
Runnable doLogin = new Runnable() {
@Override
public void run() {
if (!loginButton.isEnabled()) return;
statusLabel.setI18nText("replaymod.gui.login.logging");
new Thread(new Runnable() {
@Override
public void run() {
switch(AuthenticationHandler.authenticate(username.getText(), password.getText())) {
case AuthenticationHandler.SUCCESS:
textState = EMPTY;
mc.addScheduledTask(new Runnable() {
statusLabel.setText("");
getMinecraft().addScheduledTask(new Runnable() {
@Override
public void run() {
mc.displayGuiScreen(successScreen);
getMinecraft().displayGuiScreen(successScreen);
}
});
break;
case AuthenticationHandler.INVALID:
textState = INVALID_LOGIN;
statusLabel.setI18nText("replaymod.gui.login.incorrect");
break;
case AuthenticationHandler.NO_CONNECTION:
textState = NO_CONNECTION;
statusLabel.setI18nText("replaymod.gui.login.connectionerror");
break;
}
}
}, "replaymod-auth").start();
}
} else if(button.id == GuiConstants.LOGIN_CANCEL_BUTTON) {
mc.displayGuiScreen(parent);
} else if(button.id == GuiConstants.LOGIN_REGISTER_BUTTON) {
mc.displayGuiScreen(new GuiRegister(this));
}
}
private String usernameLabel = I18n.format("replaymod.gui.username");
private String passwordLabel = I18n.format("replaymod.gui.password");
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
this.drawDefaultBackground();
drawCenteredString(fontRendererObj, I18n.format("replaymod.gui.login.title"), this.width / 2, 10, Color.WHITE.getRGB());
drawString(fontRendererObj, usernameLabel, username.xPosition-10-fontRendererObj.getStringWidth(usernameLabel),
37, Color.WHITE.getRGB());
username.drawTextBox();
drawString(fontRendererObj, passwordLabel, password.xPosition-10-fontRendererObj.getStringWidth(passwordLabel),
67, Color.WHITE.getRGB());
password.drawTextBox();
switch(textState) {
case INVALID_LOGIN:
drawCenteredString(fontRendererObj, I18n.format("replaymod.gui.login.incorrect"), this.width / 2, 92, Color.RED.getRGB());
break;
case LOGGING_IN:
drawCenteredString(fontRendererObj, I18n.format("replaymod.gui.login.logging"), this.width / 2, 92, Color.WHITE.getRGB());
break;
case NO_CONNECTION:
drawCenteredString(fontRendererObj, I18n.format("replaymod.gui.login.connectionerror"), this.width / 2, 92, Color.RED.getRGB());
break;
}
int tw = 150+5+strwidth;
drawString(fontRendererObj, noacc, this.width / 2 - (tw/2), this.height-22, Color.WHITE.getRGB());
super.drawScreen(mouseX, mouseY, partialTicks);
}
@Override
public void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
super.mouseClicked(mouseX, mouseY, mouseButton);
username.mouseClicked(mouseX, mouseY, mouseButton);
password.mouseClicked(mouseX, mouseY, mouseButton);
}
@Override
public void updateScreen() {
username.updateCursorCounter();
password.updateCursorCounter();
}
@Override
public void onGuiClosed() {
Keyboard.enableRepeatEvents(false);
}
@Override
protected void keyTyped(char typedChar, int keyCode) throws IOException {
if(keyCode == Keyboard.KEY_TAB) {
if(password.isFocused()) {
password.setFocused(false);
username.setFocused(true);
} else {
username.setFocused(false);
password.setFocused(true);
};
username.onEnter(doLogin);
password.onEnter(doLogin);
loginButton.onClick(doLogin);
cancelButton.onClick(new Runnable() {
@Override
public void run() {
getMinecraft().displayGuiScreen(parent);
}
return;
} else if(keyCode == Keyboard.KEY_RETURN) {
actionPerformed(loginButton);
return;
}
if(username.isFocused()) {
username.textboxKeyTyped(typedChar, keyCode);
} else if(password.isFocused()) {
password.textboxKeyTyped(typedChar, keyCode);
}
loginButton.enabled = username.getText().length() > 0 && password.getText().length() > 0;
});
registerButton.onClick(new Runnable() {
@Override
public void run() {
new GuiRegister(GuiLoginPrompt.this).display();
}
});
Runnable contentValidation = new Runnable() {
@Override
public void run() {
loginButton.setEnabled(!username.getText().isEmpty() && !password.getText().isEmpty());
}
};
username.onTextChanged(contentValidation);
password.onTextChanged(contentValidation);
}
super.keyTyped(typedChar, keyCode);
public GuiLoginPrompt(net.minecraft.client.gui.GuiScreen parent, net.minecraft.client.gui.GuiScreen successScreen) {
this.parent = parent;
this.successScreen = successScreen;
setLayout(new CustomLayout<GuiLoginPrompt>() {
@Override
protected void layout(GuiLoginPrompt container, int width, int height) {
pos(username, width / 2 - 45, 30);
pos(password, width / 2 - 45, 60);
pos(loginButton, width / 2 - 152, 110);
pos(cancelButton, width / 2 + 2, 110);
pos(usernameLabel, x(username) - 10 - usernameLabel.getMinSize().getWidth(), y(username) + 7);
pos(passwordLabel, x(password) - 10 - passwordLabel.getMinSize().getWidth(), y(password) + 7);
pos(statusLabel, width / 2 - statusLabel.getMinSize().getWidth() / 2, 92);
int labelWidth = noAccountLabel.getMinSize().getWidth();
int buttonWidth = registerButton.getPreferredSize().getWidth();
int lineWidth = labelWidth + 5 + buttonWidth;
int lineStart = width / 2 - lineWidth / 2;
pos(noAccountLabel, lineStart, height - 22);
pos(registerButton, lineStart + labelWidth + 5, height - 30);
}
});
setTitle(new GuiLabel().setI18nText("replaymod.gui.login.title"));
}
public net.minecraft.client.gui.GuiScreen getSuccessScreen() {
return successScreen;
}
@Override
protected GuiLoginPrompt getThis() {
return this;
}
}