Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.cyberdeck.client.state;
- import java.util.prefs.Preferences;
- import org.cyberdeck.client.GameClient;
- import org.cyberdeck.client.SessionInfo;
- import org.cyberdeck.client.net.XmlRpcClient;
- import org.cyberdeck.client.ui.GameGUI;
- import org.cyberdeck.client.ui.layout.CancelAccept;
- import org.cyberdeck.client.ui.layout.VideoSettings;
- import org.cyberdeck.client.ui.widget.LoginPane;
- import org.cyberdeck.common.interfaces.Authenticator;
- import org.cyberdeck.common.util.Password;
- import org.cyberdeck.common.util.XmlRpcStatus;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.DisplayMode;
- import org.lwjgl.input.Keyboard;
- import org.newdawn.slick.GameContainer;
- import org.newdawn.slick.Graphics;
- import org.newdawn.slick.SlickException;
- import org.newdawn.slick.state.BasicGameState;
- import org.newdawn.slick.state.StateBasedGame;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import redstone.xmlrpc.XmlRpcException;
- import redstone.xmlrpc.XmlRpcFault;
- import de.matthiasmann.twl.Button;
- import de.matthiasmann.twl.CallbackWithReason;
- import de.matthiasmann.twl.DialogLayout;
- import de.matthiasmann.twl.EditField;
- import de.matthiasmann.twl.Label;
- import de.matthiasmann.twl.PopupWindow;
- import de.matthiasmann.twl.DialogLayout.Group;
- import de.matthiasmann.twl.EditField.Callback;
- public class LoginState extends BasicGameState {
- private static Logger log = LoggerFactory.getLogger(LoginState.class);
- int stateID = -1;
- private GameGUI gui;
- private LoginPane root = new LoginPane();
- private Preferences prefs;
- protected final DisplayMode desktopMode;
- protected VideoSettings.CallbackReason vidDlgCloseReason;
- protected boolean closeRequested;
- private boolean loginRequested = false;
- private boolean focusRequested = false;
- private boolean forceLogin = false;
- private boolean openLoginPopup = false;
- private EditField cUsername;
- private EditField cPassword;
- private Button bLogin;
- private PopupWindow forceLoginDlg;
- public LoginState(int stateID, GameGUI gui) {
- this.stateID = stateID;
- this.gui = gui;
- desktopMode = Display.getDesktopDisplayMode();
- prefs = Preferences.userNodeForPackage(GameClient.class);
- }
- @Override
- public int getID() {
- return this.stateID;
- }
- @Override
- public void init(GameContainer gc, StateBasedGame game)
- throws SlickException {
- final PopupWindow settingsDlg = new PopupWindow(root);
- final VideoSettings settings = new VideoSettings(
- Preferences.userNodeForPackage(GameClient.class),
- desktopMode);
- settingsDlg.setTheme("settingdialog");
- settingsDlg.add(settings);
- settingsDlg.setCloseOnClickedOutside(false);
- settings.setTheme("settings");
- settings.addCallback(new CallbackWithReason<VideoSettings.CallbackReason>() {
- public void callback(VideoSettings.CallbackReason reason) {
- vidDlgCloseReason = reason;
- settingsDlg.closePopup();
- if (reason == VideoSettings.CallbackReason.ACCEPT) {
- settings.storeSettings();
- }
- }
- });
- root.addButton("Settings", "Opens a dialog which might be used to change video settings", new Runnable() {
- public void run() {
- settings.readSettings();
- settingsDlg.openPopupCentered();
- }
- });
- root.addButton("Exit", new Runnable() {
- public void run() {
- closeRequested = true;
- }
- });
- Label lUsername = new Label("Username");
- lUsername.setTheme("/loginpane.fpscounter");
- Label lPassword = new Label("Password");
- lPassword.setTheme("/loginpane.fpscounter");
- cUsername = new EditField();
- cUsername.setText(prefs.get("prevUsername", ""));
- cPassword = new EditField();
- cPassword.setPasswordMasking(true);
- bLogin = new Button("Login");
- bLogin.setTheme("/button");
- bLogin.addCallback(new Runnable() {
- public void run() {
- loginRequested = true;
- }
- });
- cUsername.addCallback(new Callback() {
- public void callback(int key) {
- if (key == Keyboard.KEY_RETURN) {
- if (cPassword.getTextLength()>0) {
- loginRequested = true;
- } else {
- cPassword.requestKeyboardFocus();
- }
- }
- }
- });
- cPassword.addCallback(new Callback() {
- public void callback(int key) {
- if (key == Keyboard.KEY_RETURN) {
- if (cPassword.getTextLength()>0) {
- loginRequested = true;
- }
- }
- }
- });
- bLogin.setTooltipContent("Login to the game");
- forceLoginDlg = new PopupWindow(root);
- final CancelAccept cxlAccept = new CancelAccept("This account is already logged in.\n\nDo you wish to disconnect the existing session and login?");
- forceLoginDlg.setTheme("settingdialog");
- forceLoginDlg.add(cxlAccept);
- forceLoginDlg.setCloseOnClickedOutside(false);
- cxlAccept.setTheme("settings");
- cxlAccept.addCallback(new CallbackWithReason<CancelAccept.CallbackReason>() {
- public void callback(CancelAccept.CallbackReason reason) {
- log.info("in cxlAccept callback. CallbackReason = "+reason.toString());
- forceLoginDlg.closePopup();
- if (reason == CancelAccept.CallbackReason.ACCEPT) {
- forceLogin = true;
- loginRequested = true;
- } else {
- forceLogin = false;
- loginRequested = false;
- cPassword.setText("");
- cPassword.requestKeyboardFocus();
- cUsername.setEnabled(true);
- cPassword.setEnabled(true);
- bLogin.setEnabled(true);
- }
- }
- });
- root.setStatus("Please enter your username and password to login");
- DialogLayout layout = root.getLayout();
- Group ghLabels = layout.createParallelGroup().addWidget(lUsername).addWidget(lPassword);
- Group ghControls = layout.createParallelGroup().addWidget(cUsername).addWidget(cPassword).addGap(100);
- Group vRowA = layout.createParallelGroup().addWidget(lUsername).addWidget(cUsername);
- Group vRowB = layout.createParallelGroup().addWidget(lPassword).addWidget(cPassword);
- layout.setHorizontalGroup(layout.createParallelGroup().
- addGroup(layout.createSequentialGroup().addGroup(ghLabels).addGroup(ghControls)).
- addGroup(layout.createSequentialGroup().addGap().addWidget(bLogin).addGap()));
- layout.setVerticalGroup(layout.createSequentialGroup().
- addGroup(layout.createSequentialGroup().addGroup(vRowA).addGroup(vRowB)).
- addGap(DialogLayout.MEDIUM_GAP, DialogLayout.MEDIUM_GAP, Short.MAX_VALUE).
- addGroup(layout.createParallelGroup().addWidget(bLogin)));
- }
- @Override
- public void render(GameContainer container, StateBasedGame game, Graphics g)
- throws SlickException {
- gui.update();
- if (openLoginPopup) {
- forceLoginDlg.openPopupCentered();
- openLoginPopup = false;
- }
- }
- @Override
- public void update(GameContainer container, StateBasedGame game, int delta)
- throws SlickException {
- // if we've accepted new display settings, apply them
- if (vidDlgCloseReason == VideoSettings.CallbackReason.ACCEPT) {
- gui.reload(container);
- }
- vidDlgCloseReason = null;
- // if the exit button has been clicked, exit...
- if (closeRequested) {
- container.exit();
- }
- if (loginRequested) {
- // ignore login requests if password field is empty
- if (cPassword.getTextLength()<1) {
- loginRequested = false;
- } else {
- cUsername.setEnabled(false);
- cPassword.setEnabled(false);
- bLogin.setEnabled(false);
- prefs.put("prevUsername", cUsername.getText());
- root.setStatus("Logging in to server...");
- Authenticator auth = (Authenticator)XmlRpcClient.get().createProxy(Authenticator.class);
- String result = null;
- try {
- result = auth.login(cUsername.getText(), Password.hash(cPassword.getText()), forceLogin);
- } catch (XmlRpcException e) {
- root.setStatus(e.getMessage());
- cPassword.setText("");
- cPassword.requestKeyboardFocus();
- } catch (XmlRpcFault e) {
- try {
- if (e.getErrorCode() == XmlRpcStatus.SESSIONEXISTS) {
- openLoginPopup = true;
- }
- root.setStatus(XmlRpcStatus.getMessage(e.getErrorCode()));
- } catch (Exception e1) {
- root.setStatus(e1.getMessage());
- }
- if (!openLoginPopup) {
- cPassword.setText("");
- cPassword.requestKeyboardFocus();
- }
- }
- if (result != null) {
- root.setStatus("Authentication accepted, entering game...");
- SessionInfo.get().setSessionId(result);
- game.enterState(GameClient.PLAYSTATE);
- }
- if (!openLoginPopup) {
- cUsername.setEnabled(true);
- cPassword.setEnabled(true);
- bLogin.setEnabled(true);
- }
- loginRequested = false;
- }
- }
- if (!focusRequested) {
- if (cUsername.getTextLength()>0) {
- cPassword.requestKeyboardFocus();
- } else {
- cUsername.requestKeyboardFocus();
- }
- focusRequested = true;
- }
- }
- @Override
- public void enter(GameContainer container, StateBasedGame game) throws SlickException {
- gui.setRoot(root);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement