diff --git a/src/client/Client.cpp b/src/client/Client.cpp index 3a46e23dc..d851e55f7 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -211,11 +211,31 @@ void Client::notifyUpdateAvailable() } } +void Client::notifyAuthUserChanged() +{ + for (std::vector::iterator iterator = listeners.begin(), end = listeners.end(); iterator != end; ++iterator) + { + (*iterator)->NotifyAuthUserChanged(this); + } +} + void Client::AddListener(ClientListener * listener) { listeners.push_back(listener); } +void Client::RemoveListener(ClientListener * listener) +{ + for (std::vector::iterator iterator = listeners.begin(), end = listeners.end(); iterator != end; ++iterator) + { + if((*iterator) == listener) + { + listeners.erase(iterator); + return; + } + } +} + void Client::Shutdown() { ClearThumbnailRequests(); @@ -256,6 +276,7 @@ Client::~Client() void Client::SetAuthUser(User user) { authUser = user; + notifyAuthUserChanged(); } User Client::GetAuthUser() diff --git a/src/client/Client.h b/src/client/Client.h index b2822d4d7..adaa4494f 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -67,6 +67,7 @@ private: void updateStamps(); static vector explodePropertyString(std::string property); void notifyUpdateAvailable(); + void notifyAuthUserChanged(); //Config file handle json::Object configDocument; @@ -79,6 +80,7 @@ public: ~Client(); void AddListener(ClientListener * listener); + void RemoveListener(ClientListener * listener); RequestStatus ExecVote(int saveID, int direction); RequestStatus UploadSave(SaveInfo * save); diff --git a/src/client/ClientListener.h b/src/client/ClientListener.h index 308721c2e..4fc2d8921 100644 --- a/src/client/ClientListener.h +++ b/src/client/ClientListener.h @@ -16,6 +16,7 @@ public: virtual ~ClientListener() {} virtual void NotifyUpdateAvailable(Client * sender) {} + virtual void NotifyAuthUserChanged(Client * sender) {} }; diff --git a/src/interface/Textbox.cpp b/src/interface/Textbox.cpp index 12a469839..66af516a9 100644 --- a/src/interface/Textbox.cpp +++ b/src/interface/Textbox.cpp @@ -8,13 +8,15 @@ using namespace ui; -Textbox::Textbox(Point position, Point size, std::string textboxText): +Textbox::Textbox(Point position, Point size, std::string textboxText, std::string textboxPlaceholder): Label(position, size, ""), actionCallback(NULL), masked(false), border(true), mouseDown(false) { + placeHolder = textboxPlaceholder; + SetText(textboxText); cursor = text.length(); } @@ -231,6 +233,10 @@ void Textbox::Draw(const Point& screenPos) } else { + if(!text.length()) + { + g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, placeHolder, textColour.Red, textColour.Green, textColour.Blue, 170); + } if(border) g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, 160, 160, 160, 255); } } diff --git a/src/interface/Textbox.h b/src/interface/Textbox.h index 120194e81..141040519 100644 --- a/src/interface/Textbox.h +++ b/src/interface/Textbox.h @@ -25,8 +25,9 @@ protected: int cursor, cursorPosition; TextboxAction *actionCallback; std::string backingText; + std::string placeHolder; public: - Textbox(Point position, Point size, std::string textboxText); + Textbox(Point position, Point size, std::string textboxText = "", std::string textboxPlaceholder = ""); virtual ~Textbox(); virtual void SetDisplayText(std::string text); diff --git a/src/login/LoginController.cpp b/src/login/LoginController.cpp index a44e9ea13..f27ad5995 100644 --- a/src/login/LoginController.cpp +++ b/src/login/LoginController.cpp @@ -7,6 +7,7 @@ #include "LoginController.h" #include "client/User.h" +#include "client/Client.h" LoginController::LoginController(ControllerCallback * callback): HasExited(false) @@ -40,6 +41,10 @@ void LoginController::Exit() } if(callback) callback->ControllerExit(); + else + { + Client::Ref().SetAuthUser(loginModel->GetUser()); + } HasExited = true; } diff --git a/src/preview/PreviewController.cpp b/src/preview/PreviewController.cpp index 63d37c3a7..d5e1a938f 100644 --- a/src/preview/PreviewController.cpp +++ b/src/preview/PreviewController.cpp @@ -12,11 +12,13 @@ #include "PreviewModel.h" #include "PreviewModelException.h" #include "dialogues/ErrorMessage.h" +#include "login/LoginController.h" #include "Controller.h" PreviewController::PreviewController(int saveID, ControllerCallback * callback): HasExited(false), - saveId(saveID) + saveId(saveID), + loginWindow(NULL) { previewModel = new PreviewModel(); previewView = new PreviewView(); @@ -25,11 +27,24 @@ PreviewController::PreviewController(int saveID, ControllerCallback * callback): previewModel->UpdateSave(saveID, 0); + if(Client::Ref().GetAuthUser().ID) + { + previewModel->SetCommentBoxEnabled(true); + } + + Client::Ref().AddListener(this); + this->callback = callback; } void PreviewController::Update() { + if(loginWindow && loginWindow->HasExited == true) + { + delete loginWindow; + loginWindow = NULL; + } + try { previewModel->Update(); @@ -45,6 +60,17 @@ void PreviewController::Update() } } +void PreviewController::ShowLogin() +{ + loginWindow = new LoginController(); + ui::Engine::Ref().ShowWindow(loginWindow->GetView()); +} + +void PreviewController::NotifyAuthUserChanged(Client * sender) +{ + previewModel->SetCommentBoxEnabled(sender->GetAuthUser().ID); +} + SaveInfo * PreviewController::GetSave() { return previewModel->GetSave(); @@ -114,6 +140,7 @@ PreviewController::~PreviewController() { { ui::Engine::Ref().CloseWindow(); } + Client::Ref().RemoveListener(this); delete previewModel; delete previewView; if(callback) diff --git a/src/preview/PreviewController.h b/src/preview/PreviewController.h index 815ca5db8..81c457b30 100644 --- a/src/preview/PreviewController.h +++ b/src/preview/PreviewController.h @@ -12,21 +12,27 @@ #include "preview/PreviewView.h" #include "Controller.h" #include "client/SaveInfo.h" +#include "client/ClientListener.h" +class LoginController; class PreviewModel; class PreviewView; -class PreviewController { +class PreviewController: public ClientListener { int saveId; PreviewModel * previewModel; PreviewView * previewView; + LoginController * loginWindow; ControllerCallback * callback; public: + virtual void NotifyAuthUserChanged(Client * sender); + bool HasExited; PreviewController(int saveID, ControllerCallback * callback); void Exit(); void DoOpen(); void OpenInBrowser(); void Report(std::string message); + void ShowLogin(); bool GetDoOpen(); SaveInfo * GetSave(); PreviewView * GetView() { return previewView; } diff --git a/src/preview/PreviewModel.cpp b/src/preview/PreviewModel.cpp index f62e3f5c6..59c1c0b9a 100644 --- a/src/preview/PreviewModel.cpp +++ b/src/preview/PreviewModel.cpp @@ -21,7 +21,8 @@ PreviewModel::PreviewModel(): updateSaveCommentsWorking(false), updateSaveCommentsFinished(false), commentsTotal(0), - commentsPageNumber(1) + commentsPageNumber(1), + commentBoxEnabled(false) { // TODO Auto-generated constructor stub @@ -77,6 +78,20 @@ void PreviewModel::SetFavourite(bool favourite) } } +bool PreviewModel::GetCommentBoxEnabled() +{ + return commentBoxEnabled; +} + +void PreviewModel::SetCommentBoxEnabled(bool enabledState) +{ + if(enabledState != commentBoxEnabled) + { + commentBoxEnabled = enabledState; + notifyCommentBoxEnabledChanged(); + } +} + void PreviewModel::UpdateSave(int saveID, int saveDate) { this->tSaveID = saveID; @@ -189,6 +204,14 @@ void PreviewModel::notifySaveChanged() } } +void PreviewModel::notifyCommentBoxEnabledChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyCommentBoxEnabledChanged(this); + } +} + void PreviewModel::notifyCommentsPageChanged() { for(int i = 0; i < observers.size(); i++) @@ -210,6 +233,7 @@ void PreviewModel::AddObserver(PreviewView * observer) { observer->NotifySaveChanged(this); observer->NotifyCommentsChanged(this); observer->NotifyCommentsPageChanged(this); + observer->NotifyCommentBoxEnabledChanged(this); } void PreviewModel::Update() diff --git a/src/preview/PreviewModel.h b/src/preview/PreviewModel.h index f00a4182b..11618a041 100644 --- a/src/preview/PreviewModel.h +++ b/src/preview/PreviewModel.h @@ -27,6 +27,7 @@ struct SaveData class PreviewView; class PreviewModel { bool doOpen; + bool commentBoxEnabled; vector observers; SaveInfo * save; vector saveDataBuffer; @@ -34,6 +35,7 @@ class PreviewModel { void notifySaveChanged(); void notifySaveCommentsChanged(); void notifyCommentsPageChanged(); + void notifyCommentBoxEnabledChanged(); //Background retrieval int tSaveID; @@ -66,6 +68,9 @@ public: SaveInfo * GetSave(); std::vector * GetComments(); + bool GetCommentBoxEnabled(); + void SetCommentBoxEnabled(bool enabledState); + bool GetCommentsLoaded(); int GetCommentsPageNum(); int GetCommentsPageCount(); diff --git a/src/preview/PreviewView.cpp b/src/preview/PreviewView.cpp index b6b2e02b1..0eb29b604 100644 --- a/src/preview/PreviewView.cpp +++ b/src/preview/PreviewView.cpp @@ -13,9 +13,21 @@ #include "simulation/SaveRenderer.h" #include "interface/Point.h" #include "interface/Window.h" +#include "interface/Textbox.h" #include "Style.h" #include "search/Thumbnail.h" +class PreviewView::LoginAction: public ui::ButtonAction +{ + PreviewView * v; +public: + LoginAction(PreviewView * v_){ v = v_; } + virtual void ActionCallback(ui::Button * sender) + { + v->c->ShowLogin(); + } +}; + PreviewView::PreviewView(): ui::Window(ui::Point(-1, -1), ui::Point((XRES/2)+200, (YRES/2)+150)), savePreview(NULL), @@ -24,7 +36,10 @@ PreviewView::PreviewView(): commentsVel(0), maxOffset(0), commentsBegin(true), - commentsEnd(false) + commentsEnd(false), + addCommentBox(NULL), + submitCommentButton(NULL), + commentBoxHeight(20) { class OpenAction: public ui::ButtonAction { @@ -118,8 +133,9 @@ PreviewView::PreviewView(): authorDateLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; authorDateLabel->Appearance.VerticalAlign = ui::Appearance::AlignBottom; AddComponent(authorDateLabel); - pageInfo = new ui::Label(ui::Point((XRES/2) + 5, Size.Y-15), ui::Point(Size.X-((XRES/2) + 10), 15), "Page 1 of 1"); + pageInfo = new ui::Label(ui::Point((XRES/2) + 5, Size.Y+1), ui::Point(Size.X-((XRES/2) + 10), 15), "Page 1 of 1"); pageInfo->Appearance.HorizontalAlign = ui::Appearance::AlignCentre; authorDateLabel->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + AddComponent(pageInfo); } @@ -149,7 +165,7 @@ void PreviewView::OnDraw() g->draw_image(savePreview->Data, (Position.X+1)+(((XRES/2)-savePreview->Size.X)/2), (Position.Y+1)+(((YRES/2)-savePreview->Size.Y)/2), savePreview->Size.X, savePreview->Size.Y, 255); } g->drawrect(Position.X, Position.Y, (XRES/2)+1, (YRES/2)+1, 255, 255, 255, 100); - g->draw_line(Position.X+XRES/2, Position.Y+1, Position.X+XRES/2, Position.Y+Size.Y-2, 200, 200, 200, 255); + g->draw_line(Position.X+1+XRES/2, Position.Y+1, Position.X+XRES/2, Position.Y+Size.Y-2, 200, 200, 200, 255); g->draw_line(Position.X+1, Position.Y+12+YRES/2, Position.X-1+XRES/2, Position.Y+12+YRES/2, 100, 100, 100,255); @@ -289,7 +305,7 @@ void PreviewView::displayComments(int yOffset) tempUsername->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; tempUsername->Appearance.VerticalAlign = ui::Appearance::AlignBottom; currentY += 16; - if(currentY > Size.Y || usernameY < 0) + if(currentY > Size.Y-commentBoxHeight || usernameY < 0) { delete tempUsername; if(currentY > Size.Y) @@ -308,7 +324,7 @@ void PreviewView::displayComments(int yOffset) tempComment->SetTextColour(ui::Colour(180, 180, 180)); currentY += tempComment->Size.Y+4; - if(currentY > Size.Y || commentY < 0) + if(currentY > Size.Y-commentBoxHeight || commentY < 0) { delete tempComment; if(currentY > Size.Y) @@ -323,6 +339,37 @@ void PreviewView::displayComments(int yOffset) } } +void PreviewView::NotifyCommentBoxEnabledChanged(PreviewModel * sender) +{ + if(addCommentBox) + { + RemoveComponent(addCommentBox); + addCommentBox = NULL; + delete addCommentBox; + } + if(submitCommentButton) + { + RemoveComponent(submitCommentButton); + submitCommentButton = NULL; + delete submitCommentButton; + } + if(sender->GetCommentBoxEnabled()) + { + addCommentBox = new ui::Textbox(ui::Point((XRES/2)+4, Size.Y-19), ui::Point(Size.X-(XRES/2)-48, 17), "", "Add Comment"); + addCommentBox->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + AddComponent(addCommentBox); + submitCommentButton = new ui::Button(ui::Point(Size.X-40, Size.Y-19), ui::Point(40, 19), "Submit"); + //submitCommentButton->Enabled = false; + AddComponent(submitCommentButton); + } + else + { + submitCommentButton = new ui::Button(ui::Point(XRES/2, Size.Y-19), ui::Point(Size.X-(XRES/2), 19), "Login to comment"); + submitCommentButton->SetActionCallback(new LoginAction(this)); + AddComponent(submitCommentButton); + } +} + void PreviewView::NotifyCommentsPageChanged(PreviewModel * sender) { std::stringstream pageInfoStream; @@ -361,7 +408,7 @@ void PreviewView::NotifyCommentsChanged(PreviewModel * sender) } - maxOffset = (maxY-Size.Y)+16; + maxOffset = (maxY-(Size.Y-commentBoxHeight))+16; commentsBegin = true; commentsEnd = false; commentsOffset = 0; diff --git a/src/preview/PreviewView.h b/src/preview/PreviewView.h index fbc2adce3..f6be29fb3 100644 --- a/src/preview/PreviewView.h +++ b/src/preview/PreviewView.h @@ -16,16 +16,20 @@ #include "interface/Button.h" #include "search/Thumbnail.h" #include "interface/Label.h" +#include "interface/Textbox.h" class PreviewModel; class PreviewController; class PreviewView: public ui::Window { + class LoginAction; PreviewController * c; Thumbnail * savePreview; ui::Button * openButton; ui::Button * browserOpenButton; ui::Button * favButton; ui::Button * reportButton; + ui::Button * submitCommentButton; + ui::Textbox * addCommentBox; ui::Label * saveNameLabel; ui::Label * authorDateLabel; ui::Label * pageInfo; @@ -43,6 +47,8 @@ class PreviewView: public ui::Window { float commentsOffset; float commentsVel; + int commentBoxHeight; + void displayComments(int yOffset); public: void AttachController(PreviewController * controller) { c = controller;} @@ -50,6 +56,7 @@ public: void NotifySaveChanged(PreviewModel * sender); void NotifyCommentsChanged(PreviewModel * sender); void NotifyCommentsPageChanged(PreviewModel * sender); + void NotifyCommentBoxEnabledChanged(PreviewModel * sender); virtual void OnDraw(); virtual void DoDraw(); virtual void OnTick(float dt);