From 23873eae719a1c0a1227a4e108a158a9ec625462 Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Thu, 22 Mar 2012 13:50:43 +0000 Subject: [PATCH] More work on Tags - display tags in Tag window and Tag button --- src/client/Client.cpp | 13 ++++++++++++- src/game/GameController.cpp | 29 +++++++++++++++++++++++++++-- src/game/GameController.h | 1 + src/game/GameView.cpp | 15 +++++++++++++++ src/search/Save.cpp | 17 +++++++++++++---- src/search/Save.h | 8 +++++++- src/tags/TagsController.cpp | 9 ++++++++- src/tags/TagsController.h | 4 +++- src/tags/TagsModel.cpp | 25 ++++++++++++++++++++++++- src/tags/TagsModel.h | 5 +++++ src/tags/TagsView.cpp | 29 ++++++++++++++++++++++++++--- src/tags/TagsView.h | 8 ++++++++ 12 files changed, 149 insertions(+), 14 deletions(-) diff --git a/src/client/Client.cpp b/src/client/Client.cpp index 1fbff93ed..f6c2ecb01 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -359,6 +359,16 @@ Save * Client::GetSave(int saveID, int saveDate) json::String tempDescription = objDocument["Description"]; json::Number tempDate = objDocument["Date"]; json::Boolean tempPublished = objDocument["Published"]; + + json::Array tagsArray = objDocument["Tags"]; + vector tempTags; + + for(int j = 0; j < tagsArray.Size(); j++) + { + json::String tempTag = tagsArray[j]; + tempTags.push_back(tempTag.Value()); + } + return new Save( tempID.Value(), tempDate.Value(), @@ -368,7 +378,8 @@ Save * Client::GetSave(int saveID, int saveDate) tempUsername.Value(), tempName.Value(), tempDescription.Value(), - tempPublished.Value() + tempPublished.Value(), + tempTags ); } catch (json::Exception &e) diff --git a/src/game/GameController.cpp b/src/game/GameController.cpp index 25f02e9b1..80f45ff87 100644 --- a/src/game/GameController.cpp +++ b/src/game/GameController.cpp @@ -66,6 +66,17 @@ public: } }; +class GameController::TagsCallback: public ControllerCallback +{ + GameController * cc; +public: + TagsCallback(GameController * cc_) { cc = cc_; } + virtual void ControllerExit() + { + cc->gameModel->SetSave(new Save(*(cc->tagsWindow->GetSave()))); + } +}; + GameController::GameController(): search(NULL), renderOptions(NULL), @@ -338,8 +349,22 @@ void GameController::OpenLogin() void GameController::OpenTags() { - tagsWindow = new TagsController(NULL); - ui::Engine::Ref().ShowWindow(tagsWindow->GetView()); + if(gameModel->GetUser().ID) + { + if(gameModel->GetSave() && gameModel->GetSave()->GetID()) + { + tagsWindow = new TagsController(new TagsCallback(this), gameModel->GetSave()); + ui::Engine::Ref().ShowWindow(tagsWindow->GetView()); + } + else + { + new ErrorMessage("Error", "No save open"); + } + } + else + { + new ErrorMessage("Error", "You need to login to edit tags."); + } } void GameController::OpenDisplayOptions() diff --git a/src/game/GameController.h b/src/game/GameController.h index 7944dc717..d405051c0 100644 --- a/src/game/GameController.h +++ b/src/game/GameController.h @@ -40,6 +40,7 @@ public: class SearchCallback; class RenderCallback; class SSaveCallback; + class TagsCallback; GameController(); ~GameController(); GameView * GetView(); diff --git a/src/game/GameView.cpp b/src/game/GameView.cpp index 82293e4e4..1fa039c4a 100644 --- a/src/game/GameView.cpp +++ b/src/game/GameView.cpp @@ -1,3 +1,5 @@ +#include + #include "Config.h" #include "GameView.h" #include "interface/Window.h" @@ -426,6 +428,18 @@ void GameView::NotifySaveChanged(GameModel * sender) else downVoteButton->SetBackgroundColour(ui::Colour(0, 0, 0)); tagSimulationButton->Enabled = (sender->GetSave()->GetID() && sender->GetUser().ID); + if(sender->GetSave()->GetID()) + { + std::stringstream tagsStream; + std::vector tags = sender->GetSave()->GetTags(); + for(int i = 0; i < tags.size(); i++) + { + tagsStream << sender->GetSave()->GetTags()[i]; + if(i < tags.size()-1) + tagsStream << " "; + } + tagSimulationButton->SetText(tagsStream.str()); + } } else { @@ -436,6 +450,7 @@ void GameView::NotifySaveChanged(GameModel * sender) downVoteButton->Enabled = false; upVoteButton->SetBackgroundColour(ui::Colour(0, 0, 0)); tagSimulationButton->Enabled = false; + tagSimulationButton->SetText(""); } } diff --git a/src/search/Save.cpp b/src/search/Save.cpp index 407b73942..3c7b1f91e 100644 --- a/src/search/Save.cpp +++ b/src/search/Save.cpp @@ -11,7 +11,7 @@ Save::Save(Save & save) : userName(save.userName), name(save.name), Description(save.Description), date( save.date), Published(save.Published), id(save.id), votesUp( - save.votesUp), votesDown(save.votesDown), data(NULL), vote(save.vote) { + save.votesUp), votesDown(save.votesDown), data(NULL), vote(save.vote), tags(save.tags) { if (save.data) { std::cout << data << " " << save.data << std::endl; data = (unsigned char *) malloc(save.dataLength); @@ -24,14 +24,14 @@ Save::Save(int _id, int _date, int _votesUp, int _votesDown, string _userName, string _name) : id(_id), votesUp(_votesUp), votesDown(_votesDown), userName(_userName), name( _name), Description("No description provided"), date(_date), Published( - true), data(NULL), vote(0) { + true), data(NULL), vote(0), tags() { } Save::Save(int _id, int date_, int _votesUp, int _votesDown, int _vote, string _userName, - string _name, string description_, bool published_) : + string _name, string description_, bool published_, vector tags_) : id(_id), votesUp(_votesUp), votesDown(_votesDown), userName(_userName), name( _name), Description(description_), date(date_), Published( - published_), data(NULL), vote(_vote) { + published_), data(NULL), vote(_vote), tags(tags_) { } Save::~Save() @@ -86,6 +86,15 @@ int Save::GetVotesDown() { return votesDown; } +void Save::SetTags(vector tags) +{ + this->tags = tags; +} +vector Save::GetTags() +{ + return tags; +} + unsigned char * Save::GetData() { if (!data) { data = Client::Ref().GetSaveData(id, date, dataLength); diff --git a/src/search/Save.h b/src/search/Save.h index 74097fde7..00387ae37 100644 --- a/src/search/Save.h +++ b/src/search/Save.h @@ -1,6 +1,7 @@ #ifndef SAVE_H #define SAVE_H +#include #include #include #include @@ -22,7 +23,7 @@ public: Save(int _id, int _date, int _votesUp, int _votesDown, string _userName, string _name); - Save(int _id, int date_, int _votesUp, int _votesDown, int _vote, string _userName, string _name, string description_, bool published_); + Save(int _id, int date_, int _votesUp, int _votesDown, int _vote, string _userName, string _name, string description_, bool published_, vector tags); ~Save(); @@ -31,6 +32,8 @@ public: string Description; + vector tags; + int vote; bool Published; @@ -53,6 +56,9 @@ public: void SetVotesDown(int votesDown); int GetVotesDown(); + void SetTags(vector tags); + vector GetTags(); + unsigned char * GetData(); void SetData(unsigned char * data_, int dataLength); diff --git a/src/tags/TagsController.cpp b/src/tags/TagsController.cpp index 88356c7d3..5a457a1de 100644 --- a/src/tags/TagsController.cpp +++ b/src/tags/TagsController.cpp @@ -11,7 +11,7 @@ #include "TagsModel.h" #include "TagsView.h" -TagsController::TagsController(ControllerCallback * callback): +TagsController::TagsController(ControllerCallback * callback, Save * save): HasDone(false) { tagsModel = new TagsModel(); @@ -19,9 +19,16 @@ TagsController::TagsController(ControllerCallback * callback): tagsView->AttachController(this); tagsModel->AddObserver(tagsView); + tagsModel->SetSave(save); + this->callback = callback; } +Save * TagsController::GetSave() +{ + return tagsModel->GetSave(); +} + void TagsController::Exit() { if(ui::Engine::Ref().GetWindow() == tagsView) diff --git a/src/tags/TagsController.h b/src/tags/TagsController.h index 5c613f0a6..151cb73bc 100644 --- a/src/tags/TagsController.h +++ b/src/tags/TagsController.h @@ -10,6 +10,7 @@ #include "Controller.h" #include "TagsView.h" +#include "search/Save.h" class TagsView; class TagsModel; @@ -19,8 +20,9 @@ class TagsController { TagsModel * tagsModel; public: bool HasDone; - TagsController(ControllerCallback * callback); + TagsController(ControllerCallback * callback, Save * save); TagsView * GetView() {return tagsView;} + Save * GetSave(); void Exit(); virtual ~TagsController(); }; diff --git a/src/tags/TagsModel.cpp b/src/tags/TagsModel.cpp index cfff37120..b623ef781 100644 --- a/src/tags/TagsModel.cpp +++ b/src/tags/TagsModel.cpp @@ -6,15 +6,38 @@ */ #include "TagsModel.h" +#include "TagsView.h" -TagsModel::TagsModel() { +TagsModel::TagsModel(): + save(NULL) +{ // TODO Auto-generated constructor stub } +void TagsModel::SetSave(Save * save) +{ + this->save = save; + notifyTagsChanged(); +} + +Save * TagsModel::GetSave() +{ + return save; +} + void TagsModel::AddObserver(TagsView * observer) { observers.push_back(observer); + observer->NotifyTagsChanged(this); +} + +void TagsModel::notifyTagsChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyTagsChanged(this); + } } TagsModel::~TagsModel() { diff --git a/src/tags/TagsModel.h b/src/tags/TagsModel.h index fe7c0579c..3d633c395 100644 --- a/src/tags/TagsModel.h +++ b/src/tags/TagsModel.h @@ -9,13 +9,18 @@ #define TAGSMODEL_H_ #include +#include "search/Save.h" class TagsView; class TagsModel { + Save * save; std::vector observers; + void notifyTagsChanged(); public: TagsModel(); void AddObserver(TagsView * observer); + void SetSave(Save * save); + Save * GetSave(); virtual ~TagsModel(); }; diff --git a/src/tags/TagsView.cpp b/src/tags/TagsView.cpp index f9eafbc00..b531190f9 100644 --- a/src/tags/TagsView.cpp +++ b/src/tags/TagsView.cpp @@ -11,9 +11,12 @@ #include "TagsModel.h" TagsView::TagsView(): - ui::Window(ui::Point(-1, -1), ui::Point(200, 300)){ - // TODO Auto-generated constructor stub - + ui::Window(ui::Point(-1, -1), ui::Point(200, 300)) +{ + submitButton = new ui::Button(ui::Point(Size.X-56, Size.Y-24), ui::Point(50, 16)); + AddComponent(submitButton); + tagInput = new ui::Textbox(ui::Point(6, Size.Y-24), ui::Point(Size.X-80, 16), ""); + AddComponent(tagInput); } void TagsView::OnDraw() @@ -23,6 +26,26 @@ void TagsView::OnDraw() g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 255, 255, 255, 255); } +void TagsView::NotifyTagsChanged(TagsModel * sender) +{ + for(int i = 0; i < tags.size(); i++) + { + RemoveComponent(tags[i]); + delete tags[i]; + } + tags.clear(); + + if(sender->GetSave()) + { + for(int i = 0; i < sender->GetSave()->GetTags().size(); i++) + { + ui::Label * tempLabel = new ui::Label(ui::Point(5, 10*i), ui::Point(50, 16), sender->GetSave()->GetTags()[i]); + tags.push_back(tempLabel); + AddComponent(tempLabel); + } + } +} + TagsView::~TagsView() { // TODO Auto-generated destructor stub } diff --git a/src/tags/TagsView.h b/src/tags/TagsView.h index 3126e5a6b..e40151635 100644 --- a/src/tags/TagsView.h +++ b/src/tags/TagsView.h @@ -8,16 +8,24 @@ #ifndef TAGSVIEW_H_ #define TAGSVIEW_H_ +#include #include "interface/Window.h" +#include "interface/Button.h" +#include "interface/Textbox.h" +#include "interface/Label.h" class TagsController; class TagsModel; class TagsView: public ui::Window { TagsController * c; + ui::Button * submitButton; + ui::Textbox * tagInput; + std::vector tags; public: TagsView(); virtual void OnDraw(); void AttachController(TagsController * c_) { c = c_; }; + void NotifyTagsChanged(TagsModel * sender); virtual ~TagsView(); };