From c4bace95bf97546ecb2fff4343fe82d0540d34ab Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Thu, 19 Apr 2012 15:22:18 +0100 Subject: [PATCH] Allow deleting of stamps --- src/client/Client.cpp | 13 +++++- src/interface/SaveButton.cpp | 83 +++++++++++++++++---------------- src/search/SearchController.cpp | 30 +++++++++++- src/stamps/StampsController.cpp | 66 ++++++++++++++++++++++++++ src/stamps/StampsController.h | 4 ++ src/stamps/StampsModel.cpp | 45 +++++++++++++++++- src/stamps/StampsModel.h | 7 +++ src/stamps/StampsView.cpp | 43 ++++++++++++++++- src/stamps/StampsView.h | 5 +- 9 files changed, 251 insertions(+), 45 deletions(-) diff --git a/src/client/Client.cpp b/src/client/Client.cpp index 9e4732bf6..9a110847e 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #ifdef WIN32 #include @@ -221,7 +222,7 @@ Save * Client::GetStamp(string stampID) stampFile.close(); - Save * tempSave = new Save(0, 0, 0, 0, "", ""); + Save * tempSave = new Save(0, 0, 0, 0, "", stampID); tempSave->SetData(tempData, fileSize); return tempSave; } @@ -233,7 +234,15 @@ Save * Client::GetStamp(string stampID) void Client::DeleteStamp(string stampID) { - return; + for(int i = 0; i < stampIDs.size(); i++) + { + if(stampIDs[i] == stampID) + { + remove(string(STAMPS_DIR PATH_SEP + stampID + ".stm").c_str()); + stampIDs.erase(stampIDs.begin()+i); + return; + } + } } string Client::AddStamp(Save * saveData) diff --git a/src/interface/SaveButton.cpp b/src/interface/SaveButton.cpp index eacf0ef26..6e08e95ac 100644 --- a/src/interface/SaveButton.cpp +++ b/src/interface/SaveButton.cpp @@ -20,21 +20,23 @@ SaveButton::SaveButton(Point position, Point size, Save * save): selectable(false), selected(false) { - if(save->votesUp==0) - voteRatio = 0.0f; - else if(save->votesDown==0) - voteRatio = 1.0f; - else - voteRatio = 1.0f-(float)(((float)(save->votesDown))/((float)(save->votesUp))); - if(voteRatio < 0.0f) - voteRatio = 0.0f; - if(voteRatio > 1.0f) //Not possible, but just in case the server were to give a negative value or something - voteRatio = 1.0f; + if(save) + { + if(save->votesUp==0) + voteRatio = 0.0f; + else if(save->votesDown==0) + voteRatio = 1.0f; + else + voteRatio = 1.0f-(float)(((float)(save->votesDown))/((float)(save->votesUp))); + if(voteRatio < 0.0f) + voteRatio = 0.0f; + if(voteRatio > 1.0f) //Not possible, but just in case the server were to give a negative value or something + voteRatio = 1.0f; - voteColour.Red = (1.0f-voteRatio)*255; - voteColour.Green = voteRatio*255; - + voteColour.Red = (1.0f-voteRatio)*255; + voteColour.Green = voteRatio*255; + } } SaveButton::~SaveButton() @@ -51,7 +53,7 @@ void SaveButton::Tick(float dt) { Thumbnail * tempThumb; float scaleFactorY = 1.0f, scaleFactorX = 1.0f; - if(!thumbnail) + if(!thumbnail && save) { if(save->GetID()) { @@ -112,35 +114,38 @@ void SaveButton::Draw(const Point& screenPos) scaleFactor = (Size.Y-25)/((float)YRES); thumbBoxSize = ui::Point(((float)XRES)*scaleFactor, ((float)YRES)*scaleFactor); } - if(save->id) + if(save) { - if(isMouseInside) - g->drawrect(screenPos.X-3+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, thumbBoxSize.X, thumbBoxSize.Y, 210, 230, 255, 255); - else - g->drawrect(screenPos.X-3+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, thumbBoxSize.X, thumbBoxSize.Y, 180, 180, 180, 255); - g->drawrect(screenPos.X-3+thumbBoxSize.X+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, 6, thumbBoxSize.Y, 180, 180, 180, 255); + if(save->id) + { + if(isMouseInside) + g->drawrect(screenPos.X-3+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, thumbBoxSize.X, thumbBoxSize.Y, 210, 230, 255, 255); + else + g->drawrect(screenPos.X-3+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, thumbBoxSize.X, thumbBoxSize.Y, 180, 180, 180, 255); + g->drawrect(screenPos.X-3+thumbBoxSize.X+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, 6, thumbBoxSize.Y, 180, 180, 180, 255); - int voteBar = max(10.0f, ((float)(thumbBoxSize.Y-2))*voteRatio); - g->fillrect(1+screenPos.X-3+thumbBoxSize.X+(Size.X-thumbBoxSize.X)/2, 1+(screenPos.Y-2)+(thumbBoxSize.Y-voteBar)+(Size.Y-21-thumbBoxSize.Y)/2, 4, voteBar, voteColour.Red, voteColour.Green, voteColour.Blue, 255); - } - else - { - if(isMouseInside) - g->drawrect(screenPos.X+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, thumbBoxSize.X, thumbBoxSize.Y, 210, 230, 255, 255); + int voteBar = max(10.0f, ((float)(thumbBoxSize.Y-2))*voteRatio); + g->fillrect(1+screenPos.X-3+thumbBoxSize.X+(Size.X-thumbBoxSize.X)/2, 1+(screenPos.Y-2)+(thumbBoxSize.Y-voteBar)+(Size.Y-21-thumbBoxSize.Y)/2, 4, voteBar, voteColour.Red, voteColour.Green, voteColour.Blue, 255); + } else - g->drawrect(screenPos.X+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, thumbBoxSize.X, thumbBoxSize.Y, 180, 180, 180, 255); - } + { + if(isMouseInside) + g->drawrect(screenPos.X+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, thumbBoxSize.X, thumbBoxSize.Y, 210, 230, 255, 255); + else + g->drawrect(screenPos.X+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, thumbBoxSize.X, thumbBoxSize.Y, 180, 180, 180, 255); + } - if(isMouseInside) - { - //g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, 255, 255, 255, 255); - g->drawtext(screenPos.X+(Size.X-Graphics::textwidth((char *)save->name.c_str()))/2, screenPos.Y+Size.Y - 21, save->name, 255, 255, 255, 255); - g->drawtext(screenPos.X+(Size.X-Graphics::textwidth((char *)save->userName.c_str()))/2, screenPos.Y+Size.Y - 10, save->userName, 200, 230, 255, 255); - } - else - { - g->drawtext(screenPos.X+(Size.X-Graphics::textwidth((char *)save->name.c_str()))/2, screenPos.Y+Size.Y - 21, save->name, 180, 180, 180, 255); - g->drawtext(screenPos.X+(Size.X-Graphics::textwidth((char *)save->userName.c_str()))/2, screenPos.Y+Size.Y - 10, save->userName, 100, 130, 160, 255); + if(isMouseInside) + { + //g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, 255, 255, 255, 255); + g->drawtext(screenPos.X+(Size.X-Graphics::textwidth((char *)save->name.c_str()))/2, screenPos.Y+Size.Y - 21, save->name, 255, 255, 255, 255); + g->drawtext(screenPos.X+(Size.X-Graphics::textwidth((char *)save->userName.c_str()))/2, screenPos.Y+Size.Y - 10, save->userName, 200, 230, 255, 255); + } + else + { + g->drawtext(screenPos.X+(Size.X-Graphics::textwidth((char *)save->name.c_str()))/2, screenPos.Y+Size.Y - 21, save->name, 180, 180, 180, 255); + g->drawtext(screenPos.X+(Size.X-Graphics::textwidth((char *)save->userName.c_str()))/2, screenPos.Y+Size.Y - 10, save->userName, 100, 130, 160, 255); + } } if(isMouseInside && selectable) diff --git a/src/search/SearchController.cpp b/src/search/SearchController.cpp index b0936effa..ec5f805d3 100644 --- a/src/search/SearchController.cpp +++ b/src/search/SearchController.cpp @@ -220,6 +220,7 @@ void SearchController::removeSelectedC() std::vector selected = searchModel->GetSelected(); new TaskWindow("Removing saves", new RemoveSavesTask(selected)); ClearSelection(); + searchModel->UpdateSaveList(searchModel->GetPageNum(), searchModel->GetLastQuery()); } void SearchController::UnpublishSelected() @@ -272,10 +273,37 @@ void SearchController::unpublishSelectedC() std::vector selected = searchModel->GetSelected(); new TaskWindow("Unpublishing saves", new UnpublishSavesTask(selected)); ClearSelection(); + searchModel->UpdateSaveList(searchModel->GetPageNum(), searchModel->GetLastQuery()); } void SearchController::FavouriteSelected() { - new ErrorMessage("Not impletemented", "Not ermplermerterd"); + class FavouriteSavesTask : public Task + { + std::vector saves; + public: + FavouriteSavesTask(std::vector saves_) { saves = saves_; } + virtual void doWork() + { + for(int i = 0; i < saves.size(); i++) + { + std::stringstream saveID; + saveID << "Favouring save [" << saves[i] << "] ..."; + notifyStatus(saveID.str()); + if(Client::Ref().FavouriteSave(saves[i], true)!=RequestOkay) + { + std::stringstream saveIDF; + saveIDF << "\boFailed to favourite [" << saves[i] << "] ..."; + notifyStatus(saveIDF.str()); + usleep(500*1000); + } + usleep(100*1000); + notifyProgress((float(i+1)/float(saves.size())*100)); + } + } + }; + + std::vector selected = searchModel->GetSelected(); + new TaskWindow("Favouring saves", new FavouriteSavesTask(selected)); ClearSelection(); } diff --git a/src/stamps/StampsController.cpp b/src/stamps/StampsController.cpp index 07c1efd43..fde93a54b 100644 --- a/src/stamps/StampsController.cpp +++ b/src/stamps/StampsController.cpp @@ -4,9 +4,15 @@ * Created on: Mar 29, 2012 * Author: Simon */ +#include +#include +#include "client/Client.h" #include "StampsController.h" #include "interface/Engine.h" +#include "dialogues/ConfirmPrompt.h" +#include "tasks/TaskWindow.h" +#include "tasks/Task.h" #include "StampsModel.h" #include "StampsView.h" @@ -34,6 +40,58 @@ Save * StampsController::GetStamp() return stampsModel->GetStamp(); } +void StampsController::RemoveSelected() +{ + class RemoveSelectedConfirmation: public ConfirmDialogueCallback { + public: + StampsController * c; + RemoveSelectedConfirmation(StampsController * c_) { c = c_; } + virtual void ConfirmCallback(ConfirmPrompt::DialogueResult result) { + if (result == ConfirmPrompt::ResultOkay) + c->removeSelectedC(); + } + virtual ~RemoveSelectedConfirmation() { } + }; + + std::stringstream desc; + desc << "Are you sure you want to delete " << stampsModel->GetSelected().size() << " stamp"; + if(stampsModel->GetSelected().size()>1) + desc << "s"; + new ConfirmPrompt("Delete stamps", desc.str(), new RemoveSelectedConfirmation(this)); +} + +void StampsController::removeSelectedC() +{ + class RemoveSavesTask : public Task + { + std::vector stamps; + public: + RemoveSavesTask(std::vector stamps_) { stamps = stamps_; } + virtual void doWork() + { + for(int i = 0; i < stamps.size(); i++) + { + std::stringstream stampID; + stampID << "Deleting stamp [" << stamps[i] << "] ..."; + notifyStatus(stampID.str()); + Client::Ref().DeleteStamp(stamps[i]); + usleep(100*1000); + notifyProgress((float(i+1)/float(stamps.size())*100)); + } + } + }; + + std::vector selected = stampsModel->GetSelected(); + new TaskWindow("Removing saves", new RemoveSavesTask(selected)); + ClearSelection(); + stampsModel->UpdateStampsList(stampsModel->GetPageNum()); +} + +void StampsController::ClearSelection() +{ + stampsModel->ClearSelected(); +} + void StampsController::NextPage() { if(stampsModel->GetPageNum()>1) @@ -54,6 +112,14 @@ void StampsController::Update() } } +void StampsController::Selected(std::string stampID, bool selected) +{ + if(selected) + stampsModel->SelectStamp(stampID); + else + stampsModel->DeselectStamp(stampID); +} + void StampsController::Exit() { if(ui::Engine::Ref().GetWindow() == stampsView) diff --git a/src/stamps/StampsController.h b/src/stamps/StampsController.h index 6b1c401e1..70537142a 100644 --- a/src/stamps/StampsController.h +++ b/src/stamps/StampsController.h @@ -23,6 +23,10 @@ public: StampsController(ControllerCallback * callback); StampsView * GetView() {return stampsView;} Save * GetStamp(); + void RemoveSelected(); + void removeSelectedC(); + void ClearSelection(); + void Selected(std::string stampID, bool selected); void OpenStamp(Save * stamp); void SetStamp(); void NextPage(); diff --git a/src/stamps/StampsModel.cpp b/src/stamps/StampsModel.cpp index b679d136a..cfdc72c4b 100644 --- a/src/stamps/StampsModel.cpp +++ b/src/stamps/StampsModel.cpp @@ -65,6 +65,7 @@ void StampsModel::UpdateStampsList(int pageNumber) stampsList.clear(); currentPage = pageNumber; notifyPageChanged(); + notifyStampsListChanged(); /*notifyStampsListChanged(); for(int i = 0; i < tempStampsList.size(); i++) { @@ -76,11 +77,53 @@ void StampsModel::UpdateStampsList(int pageNumber) for(int i = stampsEnd-20; iNotifySelectedChanged(this); + } +} + StampsModel::~StampsModel() { if(stamp) delete stamp; diff --git a/src/stamps/StampsModel.h b/src/stamps/StampsModel.h index cee158c2f..0356349f8 100644 --- a/src/stamps/StampsModel.h +++ b/src/stamps/StampsModel.h @@ -9,11 +9,13 @@ #define STAMPSMODEL_H_ #include +#include #include #include "search/Save.h" class StampsView; class StampsModel { + vector selected; Save * stamp; std::vector stampIDs; std::vector stampsList; @@ -21,6 +23,7 @@ class StampsModel { int currentPage; void notifyStampsListChanged(); void notifyPageChanged(); + void notifySelectedChanged(); public: StampsModel(); int GetPageCount() { return max(1, (int)(ceil(stampIDs.size()/16))); } @@ -30,6 +33,10 @@ public: void UpdateStampsList(int pageNumber); Save * GetStamp(); void SetStamp(Save * newStamp); + vector GetSelected() { return selected; } + void ClearSelected() { selected.clear(); notifySelectedChanged(); } + void SelectStamp(std::string stampID); + void DeselectStamp(std::string stampID); virtual ~StampsModel(); }; diff --git a/src/stamps/StampsView.cpp b/src/stamps/StampsView.cpp index c0d5981e0..883cabd92 100644 --- a/src/stamps/StampsView.cpp +++ b/src/stamps/StampsView.cpp @@ -9,7 +9,6 @@ #include "client/Client.h" #include "StampsView.h" -#include "interface/SaveButton.h" #include "dialogues/ErrorMessage.h" #include "StampsController.h" #include "StampsModel.h" @@ -50,6 +49,22 @@ StampsView::StampsView(): }; previousButton->SetActionCallback(new PrevPageAction(this)); previousButton->SetAlignment(AlignLeft, AlignBottom); + + class RemoveSelectedAction : public ui::ButtonAction + { + StampsView * v; + public: + RemoveSelectedAction(StampsView * _v) { v = _v; } + void ActionCallback(ui::Button * sender) + { + v->c->RemoveSelected(); + } + }; + + removeSelected = new ui::Button(ui::Point((((XRES+BARSIZE)-100)/2), YRES+MENUSIZE-18), ui::Point(100, 16), "Delete"); + removeSelected->Visible = false; + removeSelected->SetActionCallback(new RemoveSelectedAction(this)); + AddComponent(removeSelected); } void StampsView::OnTick(float dt) @@ -108,6 +123,10 @@ void StampsView::NotifyStampsListChanged(StampsModel * sender) { v->c->OpenStamp(sender->GetSave()); } + virtual void SelectedCallback(ui::SaveButton * sender) + { + v->c->Selected(sender->GetSave()->GetName(), sender->GetSelected()); + } }; for(i = 0; i < saves.size(); i++) { @@ -126,6 +145,7 @@ void StampsView::NotifyStampsListChanged(StampsModel * sender) ), ui::Point(buttonWidth, buttonHeight), saves[i]); + saveButton->SetSelectable(true); saveButton->SetActionCallback(new SaveOpenAction(this)); stampButtons.push_back(saveButton); AddComponent(saveButton); @@ -133,6 +153,27 @@ void StampsView::NotifyStampsListChanged(StampsModel * sender) } } +void StampsView::NotifySelectedChanged(StampsModel * sender) +{ + vector selected = sender->GetSelected(); + for(int j = 0; j < stampButtons.size(); j++) + { + stampButtons[j]->SetSelected(false); + for(int i = 0; i < selected.size(); i++) + { + if(stampButtons[j]->GetSave()->GetName()==selected[i]) + stampButtons[j]->SetSelected(true); + } + } + + if(selected.size()) + { + removeSelected->Visible = true; + } + else + removeSelected->Visible = false; +} + void StampsView::OnMouseWheel(int x, int y, int d) { if(!d) diff --git a/src/stamps/StampsView.h b/src/stamps/StampsView.h index 02e556996..026461196 100644 --- a/src/stamps/StampsView.h +++ b/src/stamps/StampsView.h @@ -13,15 +13,17 @@ #include "interface/Button.h" #include "interface/Textbox.h" #include "interface/Label.h" +#include "interface/SaveButton.h" class StampsController; class StampsModel; class StampsView: public ui::Window { StampsController * c; - std::vector stampButtons; + std::vector stampButtons; ui::Button * previousButton; ui::Button * nextButton; ui::Label * infoLabel; + ui::Button * removeSelected; public: StampsView(); //virtual void OnDraw(); @@ -29,6 +31,7 @@ public: void AttachController(StampsController * c_) { c = c_; }; void NotifyPageChanged(StampsModel * sender); void NotifyStampsListChanged(StampsModel * sender); + void NotifySelectedChanged(StampsModel * sender); virtual void OnMouseWheel(int x, int y, int d); virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt); virtual ~StampsView();