diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2004-12-18 13:40:58 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2004-12-18 13:40:58 +0000 |
commit | 55372882029946d8193ca4414091b65abd64a6ae (patch) | |
tree | 61395d66a6de7a308979acde1551447a85b17025 /src/gui | |
parent | 6f44dd9fd801bf279034f5cebb5dc83c8a504799 (diff) | |
download | mana-client-55372882029946d8193ca4414091b65abd64a6ae.tar.gz mana-client-55372882029946d8193ca4414091b65abd64a6ae.tar.bz2 mana-client-55372882029946d8193ca4414091b65abd64a6ae.tar.xz mana-client-55372882029946d8193ca4414091b65abd64a6ae.zip |
Made login dialog and server select dialog into classes.
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/char_server.cpp | 93 | ||||
-rw-r--r-- | src/gui/char_server.h | 38 | ||||
-rw-r--r-- | src/gui/login.cpp | 139 | ||||
-rw-r--r-- | src/gui/login.h | 25 |
4 files changed, 168 insertions, 127 deletions
diff --git a/src/gui/char_server.cpp b/src/gui/char_server.cpp index df88adc6..e71fdd00 100644 --- a/src/gui/char_server.cpp +++ b/src/gui/char_server.cpp @@ -29,11 +29,53 @@ char server[30]; int showServerList = 1; -ServerListModel *serverListModel; -gcn::ListBox *serverList; +ServerSelectDialog::ServerSelectDialog(): + Window("Select Server") +{ + serverListModel = new ServerListModel(); + serverList = new gcn::ListBox(serverListModel); + scrollArea = new gcn::ScrollArea(serverList); + okButton = new Button("OK"); + cancelButton = new Button("Cancel"); + + setDimension(gcn::Rectangle(300, 200, 200, 100)); + scrollArea->setDimension(gcn::Rectangle(4, 4, 192, 55)); + okButton->setPosition(120, 70); + cancelButton->setPosition(146, 70); + + serverList->setEventId("ok"); + okButton->setEventId("ok"); + cancelButton->setEventId("cancel"); + + serverList->addActionListener(this); + okButton->addActionListener(this); + cancelButton->addActionListener(this); + + add(scrollArea); + add(okButton); + add(cancelButton); + + if (n_server == 0) { + // Disable Ok button + //okButton->char_server_dialog[2].flags |= D_DISABLED; + } else { + // Select first server + serverList->setSelected(1); + } +} + +ServerSelectDialog::~ServerSelectDialog() +{ + delete serverList; + delete serverListModel; + delete scrollArea; + delete okButton; + delete cancelButton; +} -void ServerSelectListener::action(const std::string& eventId) { +void ServerSelectDialog::action(const std::string& eventId) +{ if (eventId == "ok") { server_char_server(serverList->getSelected()); } @@ -56,49 +98,12 @@ std::string ServerListModel::getElementAt(int i) { void char_server() { - // Create dialog - gcn::Container *dialog; - gcn::Button *okButton; - gcn::Button *cancelButton; - gcn::ScrollArea *scrollArea; - - serverListModel = new ServerListModel(); - dialog = new Window("Select Server"); - serverList = new gcn::ListBox(serverListModel); - scrollArea = new gcn::ScrollArea(serverList); - okButton = new Button("OK"); - cancelButton = new Button("Cancel"); - - dialog->setDimension(gcn::Rectangle(300, 200, 200, 100)); - scrollArea->setDimension(gcn::Rectangle(4, 4, 192, 55)); - okButton->setPosition(120, 70); - cancelButton->setPosition(146, 70); - - serverList->setEventId("ok"); - okButton->setEventId("ok"); - cancelButton->setEventId("cancel"); - - ServerSelectListener *actionListener = new ServerSelectListener(); - serverList->addActionListener(actionListener); - okButton->addActionListener(actionListener); - cancelButton->addActionListener(actionListener); - - dialog->add(scrollArea); - dialog->add(okButton); - dialog->add(cancelButton); + ServerSelectDialog *dialog = new ServerSelectDialog(); guiTop->add(dialog); state = LOGIN; - if (n_server == 0) { - // Disable Ok button - //okButton->char_server_dialog[2].flags |= D_DISABLED; - } else { - // Select first server - serverList->setSelected(1); - } - showServerList = 1; while (showServerList) { clear_bitmap(buffer); @@ -108,12 +113,6 @@ void char_server() { } delete dialog; - delete serverList; - delete serverListModel; - delete scrollArea; - delete okButton; - delete cancelButton; - delete actionListener; } void server_char_server(int serverIndex) { diff --git a/src/gui/char_server.h b/src/gui/char_server.h index 2d694ef4..6a11c589 100644 --- a/src/gui/char_server.h +++ b/src/gui/char_server.h @@ -26,31 +26,51 @@ #ifndef _CHAR_SEL_SERVER_H #define _CHAR_SEL_SERVER_H -#include <allegro.h> - #include "../main.h" #include "../net/network.h" #include "gui.h" +#include "window.h" + +#include <allegro.h> + /** - * The action listener for the server select dialog. + * The list model for the server list. * * \ingroup GUI */ -class ServerSelectListener : public gcn::ActionListener { +class ServerListModel : public gcn::ListModel { public: - void action(const std::string& eventId); + int getNumberOfElements(); + std::string getElementAt(int i); }; /** - * The list model for the server list. + * The server select dialog. * * \ingroup GUI */ -class ServerListModel : public gcn::ListModel { +class ServerSelectDialog : public Window, public gcn::ActionListener { public: - int getNumberOfElements(); - std::string getElementAt(int i); + ServerSelectDialog(); + ~ServerSelectDialog(); + + /** + * Initializes the dialog. Should be called after adding it to the GUI. + */ + void init(); + + /** + * Called when receiving actions from the widgets. + */ + void action(const std::string& eventId); + + private: + ServerListModel *serverListModel; + gcn::ListBox *serverList; + gcn::Button *okButton; + gcn::Button *cancelButton; + gcn::ScrollArea *scrollArea; }; void char_server(); diff --git a/src/gui/login.cpp b/src/gui/login.cpp index 32039294..511b45b6 100644 --- a/src/gui/login.cpp +++ b/src/gui/login.cpp @@ -25,53 +25,12 @@ #include "gui.h" #include "button.h" #include "checkbox.h" -#include "window.h" #include "../graphic/graphic.h" -// Dialog parts -gcn::Container *dialog; -gcn::Label *userLabel; -gcn::Label *passLabel; -gcn::TextField *userField; -gcn::TextField *passField; -gcn::CheckBox *keepCheck; -gcn::Button *okButton; -gcn::Button *cancelButton; - -void LoginActionListener::action(const std::string& eventId) -{ - if (eventId == "ok") { - const std::string user = userField->getText(); - log("Network", "Username is %s", user.c_str()); - // Store config settings - set_config_int("login", "remember", keepCheck->isMarked()); - if (keepCheck->isMarked()) { - set_config_string("login", "username", user.c_str()); - } else { - set_config_string("login", "username", ""); - } - - // Check login - if (user.length() == 0) { - ok("Error", "Enter your username first"); - warning("Enter your username first"); - state = LOGIN; - } else { - server_login(user, passField->getText()); - close_session(); - } - } else if (eventId == "cancel") { - state = EXIT; - } -} - -/* - * Display login GUI - */ -void login() { - // Create dialog - dialog = new Window("Login"); +LoginDialog::LoginDialog(): + Window("Login") +{ userLabel = new gcn::Label("Name:"); passLabel = new gcn::Label("Password:"); userField = new gcn::TextField("player"); @@ -80,7 +39,7 @@ void login() { okButton = new Button("OK"); cancelButton = new Button("Cancel"); - dialog->setDimension(gcn::Rectangle(300, 250, 200, 80)); + setDimension(gcn::Rectangle(300, 250, 200, 80)); userLabel->setPosition(4, 11); passLabel->setPosition(4, 31); userField->setPosition(60, 10); @@ -97,23 +56,34 @@ void login() { okButton->setEventId("ok"); cancelButton->setEventId("cancel"); - LoginActionListener *loginActionListener = new LoginActionListener(); - userField->addActionListener(loginActionListener); - passField->addActionListener(loginActionListener); - keepCheck->addActionListener(loginActionListener); - okButton->addActionListener(loginActionListener); - cancelButton->addActionListener(loginActionListener); - - dialog->add(userLabel); - dialog->add(passLabel); - dialog->add(userField); - dialog->add(passField); - dialog->add(keepCheck); - dialog->add(okButton); - dialog->add(cancelButton); + userField->addActionListener(this); + passField->addActionListener(this); + keepCheck->addActionListener(this); + okButton->addActionListener(this); + cancelButton->addActionListener(this); + + add(userLabel); + add(passLabel); + add(userField); + add(passField); + add(keepCheck); + add(okButton); + add(cancelButton); +} - guiTop->add(dialog); +LoginDialog::~LoginDialog() +{ + delete userLabel; + delete passLabel; + delete userField; + delete passField; + delete keepCheck; + delete okButton; + delete cancelButton; +} +void LoginDialog::init() +{ userField->requestFocus(); userField->setCaretPosition(userField->getText().length()); @@ -123,6 +93,43 @@ void login() { passField->requestFocus(); } } +} + +void LoginDialog::action(const std::string& eventId) +{ + if (eventId == "ok") { + const std::string user = userField->getText(); + log("Network", "Username is %s", user.c_str()); + + // Store config settings + set_config_int("login", "remember", keepCheck->isMarked()); + if (keepCheck->isMarked()) { + set_config_string("login", "username", user.c_str()); + } else { + set_config_string("login", "username", ""); + } + + // Check login + if (user.length() == 0) { + ok("Error", "Enter your username first"); + warning("Enter your username first"); + state = LOGIN; + } else { + server_login(user, passField->getText()); + close_session(); + } + } else if (eventId == "cancel") { + state = EXIT; + } +} + +/** + * Display login dialog + */ +void login() { + LoginDialog *dialog = new LoginDialog(); + guiTop->add(dialog); + dialog->init(); while (state == LOGIN) { clear_bitmap(buffer); @@ -135,17 +142,11 @@ void login() { } delete dialog; - delete userLabel; - delete passLabel; - delete userField; - delete passField; - delete keepCheck; - delete okButton; - delete cancelButton; - delete loginActionListener; } -/** Attempt to login to login server */ +/** + * Attempt to login to login server + */ void server_login(const std::string& user, const std::string& pass) { strncpy(username, user.c_str(), LEN_USERNAME); strncpy(password, pass.c_str(), LEN_PASSWORD); diff --git a/src/gui/login.h b/src/gui/login.h index 7cafb47f..725e578c 100644 --- a/src/gui/login.h +++ b/src/gui/login.h @@ -30,19 +30,40 @@ #include "../log.h" #include "../net/network.h" #include "gui.h" +#include "window.h" #include <allegro.h> #ifdef WIN32 #include <winalleg.h> #endif /** - * The action listener for the login dialog. + * The login dialog. * * \ingroup GUI */ -class LoginActionListener : public gcn::ActionListener { +class LoginDialog : public Window, public gcn::ActionListener { public: + LoginDialog(); + ~LoginDialog(); + + /** + * Initializes the dialog. Should be called after adding it to the GUI. + */ + void init(); + + /** + * Called when receiving actions from the widgets. + */ void action(const std::string& eventId); + + private: + gcn::Label *userLabel; + gcn::Label *passLabel; + gcn::TextField *userField; + gcn::TextField *passField; + gcn::CheckBox *keepCheck; + gcn::Button *okButton; + gcn::Button *cancelButton; }; void login(); |