diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2012-01-24 20:19:19 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2012-01-24 20:19:19 (GMT) |
| commit | 97b35bc47059315d4138c8e0827842d2c03de152 (patch) | |
| tree | feaf7a8c018982ba9d7ca1b8e6e15294abfdfc84 /src/login | |
| parent | 04488081d3fa0cd3dfb2939e5d902bc894df150d (diff) | |
| download | powder-97b35bc47059315d4138c8e0827842d2c03de152.zip powder-97b35bc47059315d4138c8e0827842d2c03de152.tar.gz | |
Various
Diffstat (limited to 'src/login')
| -rw-r--r-- | src/login/LoginController.cpp | 28 | ||||
| -rw-r--r-- | src/login/LoginController.h | 30 | ||||
| -rw-r--r-- | src/login/LoginModel.cpp | 69 | ||||
| -rw-r--r-- | src/login/LoginModel.h | 33 | ||||
| -rw-r--r-- | src/login/LoginView.cpp | 72 | ||||
| -rw-r--r-- | src/login/LoginView.h | 37 |
6 files changed, 269 insertions, 0 deletions
diff --git a/src/login/LoginController.cpp b/src/login/LoginController.cpp new file mode 100644 index 0000000..3172398 --- /dev/null +++ b/src/login/LoginController.cpp @@ -0,0 +1,28 @@ +/* + * LoginController.cpp + * + * Created on: Jan 24, 2012 + * Author: Simon + */ + +#include "LoginController.h" + +LoginController::LoginController() { + // TODO Auto-generated constructor stub + loginView = new LoginView(); + loginModel = new LoginModel(); + + loginView->AttachController(this); + loginModel->AddObserver(loginView); + +} + +void LoginController::Login(string username, string password) +{ + loginModel->Login(username, password); +} + +LoginController::~LoginController() { + // TODO Auto-generated destructor stub +} + diff --git a/src/login/LoginController.h b/src/login/LoginController.h new file mode 100644 index 0000000..ecd30a3 --- /dev/null +++ b/src/login/LoginController.h @@ -0,0 +1,30 @@ +/* + * LoginController.h + * + * Created on: Jan 24, 2012 + * Author: Simon + */ + +#ifndef LOGINCONTROLLER_H_ +#define LOGINCONTROLLER_H_ + +#include <string> +#include "LoginView.h" +#include "LoginModel.h" + +using namespace std; + +class LoginView; +class LoginModel; +class LoginController { + LoginView * loginView; + LoginModel * loginModel; +public: + LoginController(); + void Login(string username, string password); + LoginView * GetView() { return loginView; } + + virtual ~LoginController(); +}; + +#endif /* LOGINCONTROLLER_H_ */ diff --git a/src/login/LoginModel.cpp b/src/login/LoginModel.cpp new file mode 100644 index 0000000..7940cf0 --- /dev/null +++ b/src/login/LoginModel.cpp @@ -0,0 +1,69 @@ +/* + * LoginModel.cpp + * + * Created on: Jan 24, 2012 + * Author: Simon + */ + +#include "LoginModel.h" + +LoginModel::LoginModel() { + // TODO Auto-generated constructor stub + +} + +void LoginModel::Login(string username, string password) +{ + statusText = "Logging in..."; + loginStatus = false; + notifyStatusChanged(); + LoginStatus status = Client::Ref().Login(username, password); + switch(status) + { + case LoginOkay: + statusText = "Logged in"; + loginStatus = true; + break; + case LoginPasswordInvalid: + statusText = "Username or Password incorrect"; + break; + case LoginUsernameInvalid: + statusText = "Username incorrect"; + break; + case LoginBanned: + statusText = "Banned: " + Client::Ref().GetLastError(); + break; + default: + statusText = "Error"; + break; + } + notifyStatusChanged(); +} + +void LoginModel::AddObserver(LoginView * observer) +{ + observers.push_back(observer); +} + +string LoginModel::GetStatusText() +{ + return statusText; +} + +bool LoginModel::GetStatus() +{ + return loginStatus; +} + +void LoginModel::notifyStatusChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyStatusChanged(this); + } +} + +LoginModel::~LoginModel() { + // TODO Auto-generated destructor stub +} + diff --git a/src/login/LoginModel.h b/src/login/LoginModel.h new file mode 100644 index 0000000..cd10a5d --- /dev/null +++ b/src/login/LoginModel.h @@ -0,0 +1,33 @@ +/* + * LoginModel.h + * + * Created on: Jan 24, 2012 + * Author: Simon + */ + +#ifndef LOGINMODEL_H_ +#define LOGINMODEL_H_ + +#include <vector> +#include <string> +#include "LoginView.h" +#include "client/Client.h" + +using namespace std; + +class LoginView; +class LoginModel { + vector<LoginView*> observers; + string statusText; + bool loginStatus; + void notifyStatusChanged(); +public: + LoginModel(); + void Login(string username, string password); + void AddObserver(LoginView * observer); + string GetStatusText(); + bool GetStatus(); + virtual ~LoginModel(); +}; + +#endif /* LOGINMODEL_H_ */ diff --git a/src/login/LoginView.cpp b/src/login/LoginView.cpp new file mode 100644 index 0000000..bb88c15 --- /dev/null +++ b/src/login/LoginView.cpp @@ -0,0 +1,72 @@ +/* + * LoginView.cpp + * + * Created on: Jan 24, 2012 + * Author: Simon + */ + +#include "LoginView.h" + +class LoginView::LoginAction : public ui::ButtonAction +{ + LoginView * v; +public: + LoginAction(LoginView * _v) { v = _v; } + void ActionCallback(ui::Button * sender) + { + v->c->Login(v->usernameField->GetText(), v->passwordField->GetText()); + } +}; + +LoginView::LoginView(): + ui::Window(ui::Point(-1, -1), ui::Point(200, 100)), + loginButton(new ui::Button(ui::Point(200-50, 100-16), ui::Point(50, 16), "Login")), + cancelButton(new ui::Button(ui::Point(0, 100-16), ui::Point(50, 16), "Cancel")), + titleLabel(new ui::Label(ui::Point(4, 2), ui::Point(200-16, 16), "Server login")), + usernameField(new ui::Textbox(ui::Point(8, 20), ui::Point(200-16, 16), "")), + passwordField(new ui::Textbox(ui::Point(8, 40), ui::Point(200-16, 16), "")), + infoLabel(new ui::Label(ui::Point(8, 60), ui::Point(200-16, 16), "")) +{ + AddComponent(loginButton); + loginButton->SetAlignment(AlignCentre, AlignBottom); + loginButton->SetActionCallback(new LoginAction(this)); + AddComponent(cancelButton); + cancelButton->SetAlignment(AlignCentre, AlignBottom); + AddComponent(titleLabel); + titleLabel->SetAlignment(AlignLeft, AlignBottom); + AddComponent(usernameField); + usernameField->SetAlignment(AlignLeft, AlignBottom); + AddComponent(passwordField); + passwordField->SetAlignment(AlignLeft, AlignBottom); + passwordField->SetHidden(true); + infoLabel->SetAlignment(AlignCentre, AlignBottom); + AddComponent(infoLabel); +} + +void LoginView::NotifyStatusChanged(LoginModel * sender) +{ + infoLabel->SetText(sender->GetStatusText()); +} + +void LoginView::OnDraw() +{ + Graphics * g = ui::Engine::Ref().g; + g->clearrect(Position.X-2, Position.Y-2, Size.X+3, Size.Y+3); + g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 255, 255, 255, 255); +} + +LoginView::~LoginView() { + RemoveComponent(titleLabel); + RemoveComponent(loginButton); + RemoveComponent(cancelButton); + RemoveComponent(usernameField); + RemoveComponent(passwordField); + RemoveComponent(infoLabel); + delete cancelButton; + delete loginButton; + delete titleLabel; + delete usernameField; + delete passwordField; + delete infoLabel; +} + diff --git a/src/login/LoginView.h b/src/login/LoginView.h new file mode 100644 index 0000000..2646e99 --- /dev/null +++ b/src/login/LoginView.h @@ -0,0 +1,37 @@ +/* + * LoginView.h + * + * Created on: Jan 24, 2012 + * Author: Simon + */ + +#ifndef LOGINVIEW_H_ +#define LOGINVIEW_H_ + +#include "interface/Button.h" +#include "interface/Window.h" +#include "interface/Label.h" +#include "interface/Textbox.h" +#include "LoginController.h" +#include "LoginModel.h" + +class LoginController; +class LoginMode; +class LoginView: public ui::Window { + LoginController * c; + ui::Button * loginButton; + ui::Button * cancelButton; + ui::Label * titleLabel; + ui::Label * infoLabel; + ui::Textbox * usernameField; + ui::Textbox * passwordField; +public: + class LoginAction; + LoginView(); + void AttachController(LoginController * c_) { c = c_; } + void NotifyStatusChanged(LoginModel * sender); + virtual void OnDraw(); + virtual ~LoginView(); +}; + +#endif /* LOGINVIEW_H_ */ |
