summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2007-03-22 00:47:41 +0000
committerBjørn Lindeijer <bjorn@lindeijer.nl>2007-03-22 00:47:41 +0000
commitcacb143040c018a24aed00c3ce1f513b75ec7f99 (patch)
tree22afb23dc50f2bad06abb0730f88745b09ff654a /src
parent823aa330987205b251d9f662cfdd4c39149c6ec7 (diff)
downloadmana-cacb143040c018a24aed00c3ce1f513b75ec7f99.tar.gz
mana-cacb143040c018a24aed00c3ce1f513b75ec7f99.tar.bz2
mana-cacb143040c018a24aed00c3ce1f513b75ec7f99.tar.xz
mana-cacb143040c018a24aed00c3ce1f513b75ec7f99.zip
Only enable login button when a username, password and server are filled in.
Pass username, password and server on to the register dialog. Go back to login dialog when canceling registration. Fixed a crash caused by deleting an OkDialog twice (it also deletes itself). Made the register dialog a bit wider. Register dialog no longer clears username field when it is invalid.
Diffstat (limited to 'src')
-rw-r--r--src/gui/login.cpp75
-rw-r--r--src/gui/login.h30
-rw-r--r--src/gui/register.cpp90
-rw-r--r--src/gui/register.h23
4 files changed, 125 insertions, 93 deletions
diff --git a/src/gui/login.cpp b/src/gui/login.cpp
index 27623442..00b99e72 100644
--- a/src/gui/login.cpp
+++ b/src/gui/login.cpp
@@ -36,24 +36,6 @@
#include "passwordfield.h"
#include "textfield.h"
-void
-WrongDataNoticeListener::setTarget(gcn::TextField *textField)
-{
- mTarget = textField;
-}
-
-void
-WrongDataNoticeListener::action(const gcn::ActionEvent &event)
-{
- if (event.getId() == "ok")
- {
- // Reset the field
- mTarget->setText("");
- mTarget->setCaretPosition(0);
- mTarget->requestFocus();
- }
-}
-
LoginDialog::LoginDialog(LoginData *loginData):
Window("Login"), mLoginData(loginData)
{
@@ -99,6 +81,9 @@ LoginDialog::LoginDialog(LoginData *loginData):
mPassField->setActionEventId("ok");
mServerField->setActionEventId("ok");
+ mUserField->addKeyListener(this);
+ mPassField->addKeyListener(this);
+ mServerField->addKeyListener(this);
mUserField->addActionListener(this);
mPassField->addActionListener(this);
mServerField->addActionListener(this);
@@ -124,39 +109,27 @@ LoginDialog::LoginDialog(LoginData *loginData):
mPassField->requestFocus();
}
- mWrongDataNoticeListener = new WrongDataNoticeListener();
+ mOkButton->setEnabled(canSubmit());
}
LoginDialog::~LoginDialog()
{
- delete mWrongDataNoticeListener;
}
void
LoginDialog::action(const gcn::ActionEvent &event)
{
- if (event.getId() == "ok")
+ if (event.getId() == "ok" && canSubmit())
{
- // Check login
- if (mUserField->getText().empty())
- {
- mWrongDataNoticeListener->setTarget(mPassField);
- OkDialog *dlg = new OkDialog("Error", "Enter your username first");
- dlg->addActionListener(mWrongDataNoticeListener);
- }
- else
- {
- mLoginData->hostname = mServerField->getText();
- mLoginData->username = mUserField->getText();
- mLoginData->password = mPassField->getText();
- mLoginData->remember = mKeepCheck->isMarked();
-
- mOkButton->setEnabled(false);
- //mCancelButton->setEnabled(false);
- mRegisterButton->setEnabled(false);
-
- state = ACCOUNT_STATE;
- }
+ mLoginData->hostname = mServerField->getText();
+ mLoginData->username = mUserField->getText();
+ mLoginData->password = mPassField->getText();
+ mLoginData->remember = mKeepCheck->isMarked();
+
+ mOkButton->setEnabled(false);
+ mRegisterButton->setEnabled(false);
+
+ state = ACCOUNT_STATE;
}
else if (event.getId() == "cancel")
{
@@ -164,6 +137,26 @@ LoginDialog::action(const gcn::ActionEvent &event)
}
else if (event.getId() == "register")
{
+ // Transfer these fields on to the register dialog
+ mLoginData->hostname = mServerField->getText();
+ mLoginData->username = mUserField->getText();
+ mLoginData->password = mPassField->getText();
+
state = REGISTER_STATE;
}
}
+
+void
+LoginDialog::keyPressed(gcn::KeyEvent& keyEvent)
+{
+ mOkButton->setEnabled(canSubmit());
+}
+
+bool
+LoginDialog::canSubmit()
+{
+ return !mUserField->getText().empty() &&
+ !mPassField->getText().empty() &&
+ !mServerField->getText().empty() &&
+ state == LOGIN_STATE;
+}
diff --git a/src/gui/login.h b/src/gui/login.h
index 79e74e13..981665d5 100644
--- a/src/gui/login.h
+++ b/src/gui/login.h
@@ -26,6 +26,7 @@
#include <iosfwd>
#include <guichan/actionlistener.hpp>
+#include <guichan/keylistener.hpp>
#include "window.h"
#include "../guichanfwd.h"
@@ -33,22 +34,13 @@
class LoginData;
/**
- * Listener used for handling wrong data.
- */
-class WrongDataNoticeListener : public gcn::ActionListener {
- public:
- void setTarget(gcn::TextField *textField);
- void action(const gcn::ActionEvent &event);
- private:
- gcn::TextField *mTarget;
-};
-
-/**
* The login dialog.
*
* \ingroup Interface
*/
-class LoginDialog : public Window, public gcn::ActionListener {
+class LoginDialog : public Window, public gcn::ActionListener,
+ public gcn::KeyListener
+{
public:
/**
* Constructor
@@ -67,7 +59,19 @@ class LoginDialog : public Window, public gcn::ActionListener {
*/
void action(const gcn::ActionEvent &event);
+ /**
+ * Called when a key is pressed in one of the text fields.
+ */
+ void keyPressed(gcn::KeyEvent& keyEvent);
+
private:
+ /**
+ * Returns whether submit can be enabled. This is true in the login
+ * state, when all necessary fields have some text.
+ */
+ bool
+ canSubmit();
+
gcn::TextField *mUserField;
gcn::TextField *mPassField;
gcn::TextField *mServerField;
@@ -76,8 +80,6 @@ class LoginDialog : public Window, public gcn::ActionListener {
gcn::Button *mCancelButton;
gcn::Button *mRegisterButton;
- WrongDataNoticeListener *mWrongDataNoticeListener;
-
LoginData *mLoginData;
};
diff --git a/src/gui/register.cpp b/src/gui/register.cpp
index c8f3c1e1..4a8a32d9 100644
--- a/src/gui/register.cpp
+++ b/src/gui/register.cpp
@@ -41,55 +41,86 @@
#include "textfield.h"
#include "ok_dialog.h"
+void
+WrongDataNoticeListener::setTarget(gcn::TextField *textField)
+{
+ mTarget = textField;
+}
+
+void
+WrongDataNoticeListener::action(const gcn::ActionEvent &event)
+{
+ if (event.getId() == "ok")
+ {
+ mTarget->requestFocus();
+ }
+}
+
RegisterDialog::RegisterDialog(LoginData *loginData):
Window("Register"),
mWrongDataNoticeListener(new WrongDataNoticeListener()),
- mWrongRegisterNotice(0),
mLoginData(loginData)
{
gcn::Label *userLabel = new gcn::Label("Name:");
gcn::Label *passwordLabel = new gcn::Label("Password:");
gcn::Label *confirmLabel = new gcn::Label("Confirm:");
gcn::Label *serverLabel = new gcn::Label("Server:");
- mUserField = new TextField("player");
- mPasswordField = new PasswordField();
+ mUserField = new TextField(loginData->username);
+ mPasswordField = new PasswordField(loginData->password);
mConfirmField = new PasswordField();
- mServerField = new TextField();
+ mServerField = new TextField(loginData->hostname);
mMaleButton = new RadioButton("Male", "sex", true);
mFemaleButton = new RadioButton("Female", "sex", false);
mRegisterButton = new Button("Register", "register", this);
mCancelButton = new Button("Cancel", "cancel", this);
- int width = 200;
- int height = 150;
+ const int width = 220;
+ const int height = 150;
setContentSize(width, height);
mUserField->setPosition(65, 5);
- mUserField->setWidth(130);
+ mUserField->setWidth(width - 70);
mPasswordField->setPosition(
65, mUserField->getY() + mUserField->getHeight() + 7);
- mPasswordField->setWidth(130);
+ mPasswordField->setWidth(mUserField->getWidth());
mConfirmField->setPosition(
65, mPasswordField->getY() + mPasswordField->getHeight() + 7);
- mConfirmField->setWidth(130);
+ mConfirmField->setWidth(mUserField->getWidth());
mServerField->setPosition(
65, 23 + mConfirmField->getY() + mConfirmField->getHeight() + 7);
- mServerField->setWidth(130);
+ mServerField->setWidth(mUserField->getWidth());
userLabel->setPosition(5, mUserField->getY() + 1);
passwordLabel->setPosition(5, mPasswordField->getY() + 1);
confirmLabel->setPosition(5, mConfirmField->getY() + 1);
serverLabel->setPosition(5, mServerField->getY() + 1);
- mFemaleButton->setPosition(width - mFemaleButton->getWidth() - 10,
- mConfirmField->getY() + mConfirmField->getHeight() + 7);
mMaleButton->setPosition(
- mFemaleButton->getX() - mMaleButton->getWidth() - 5,
- mFemaleButton->getY());
-
- mRegisterButton->setPosition(5, height - mRegisterButton->getHeight() - 5);
- mCancelButton->setPosition(10 + mRegisterButton->getWidth(),
- mRegisterButton->getY());
+ 70, mConfirmField->getY() + mConfirmField->getHeight() + 7);
+ mFemaleButton->setPosition(
+ 70 + 10 + mMaleButton->getWidth(),
+ mMaleButton->getY());
+
+ mCancelButton->setPosition(
+ width - mCancelButton->getWidth() - 5,
+ height - mCancelButton->getHeight() - 5);
+ mRegisterButton->setPosition(
+ mCancelButton->getX() - mRegisterButton->getWidth() - 5,
+ height - mRegisterButton->getHeight() - 5);
+
+ /* TODO:
+ * This is a quick and dirty way to respond to the ENTER key, regardless of
+ * which text field is selected. There may be a better way now with the new
+ * input system of Guichan 0.6.0. See also the login dialog.
+ */
+ mUserField->setActionEventId("register");
+ mPasswordField->setActionEventId("register");
+ mConfirmField->setActionEventId("register");
+ mServerField->setActionEventId("register");
+ mUserField->addActionListener(this);
+ mPasswordField->addActionListener(this);
+ mConfirmField->addActionListener(this);
+ mServerField->addActionListener(this);
add(userLabel);
add(passwordLabel);
@@ -108,13 +139,11 @@ RegisterDialog::RegisterDialog(LoginData *loginData):
setVisible(true);
mUserField->requestFocus();
mUserField->setCaretPosition(mUserField->getText().length());
-
- mServerField->setText(config.getValue("host", ""));
}
RegisterDialog::~RegisterDialog()
{
- delete mWrongRegisterNotice;
+ delete mWrongDataNoticeListener;
}
void
@@ -122,7 +151,7 @@ RegisterDialog::action(const gcn::ActionEvent &event)
{
if (event.getId() == "cancel")
{
- state = EXIT_STATE;
+ state = LOGIN_STATE;
}
else if (event.getId() == "register")
{
@@ -132,14 +161,7 @@ RegisterDialog::action(const gcn::ActionEvent &event)
std::stringstream errorMsg;
int error = 0;
- // Check login
- if (user.empty())
- {
- // No username
- errorMsg << "Enter your username first.";
- error = 1;
- }
- else if (user.length() < LEN_MIN_USERNAME)
+ if (user.length() < LEN_MIN_USERNAME)
{
// Name too short
errorMsg << "The username needs to be at least "
@@ -186,13 +208,15 @@ RegisterDialog::action(const gcn::ActionEvent &event)
}
else if (error == 2)
{
- mWrongDataNoticeListener->setTarget(this->mPasswordField);
// Reset password confirmation
+ mPasswordField->setText("");
mConfirmField->setText("");
+
+ mWrongDataNoticeListener->setTarget(this->mPasswordField);
}
- delete mWrongRegisterNotice;
- mWrongRegisterNotice = new OkDialog("Error", errorMsg.str());
+ OkDialog *mWrongRegisterNotice = new OkDialog("Error",
+ errorMsg.str());
mWrongRegisterNotice->addActionListener(mWrongDataNoticeListener);
}
else
diff --git a/src/gui/register.h b/src/gui/register.h
index 6176aef7..6b23c97f 100644
--- a/src/gui/register.h
+++ b/src/gui/register.h
@@ -32,17 +32,31 @@
class LoginData;
class OkDialog;
-class WrongDataNoticeListener;
/**
- * The login dialog.
+ * Listener used while dealing with wrong data. It is used to direct the focus
+ * to the field which contained wrong data when the Ok button was pressed on
+ * the error notice.
+ */
+class WrongDataNoticeListener : public gcn::ActionListener {
+ public:
+ void setTarget(gcn::TextField *textField);
+ void action(const gcn::ActionEvent &event);
+ private:
+ gcn::TextField *mTarget;
+};
+
+/**
+ * The registration dialog.
*
* \ingroup Interface
*/
-class RegisterDialog : public Window, public gcn::ActionListener {
+class RegisterDialog : public Window, public gcn::ActionListener
+{
public:
/**
- * Constructor
+ * Constructor. Name, password and server fields will be initialized to
+ * the information already present in the LoginData instance.
*
* @see Window::Window
*/
@@ -73,7 +87,6 @@ class RegisterDialog : public Window, public gcn::ActionListener {
gcn::RadioButton *mFemaleButton;
WrongDataNoticeListener *mWrongDataNoticeListener;
- OkDialog *mWrongRegisterNotice;
LoginData *mLoginData;
};