summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2012-04-19 14:22:18 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2012-04-19 14:22:18 (GMT)
commitc4bace95bf97546ecb2fff4343fe82d0540d34ab (patch)
treea35378f9a3062041751606239d82fd45e92b7062 /src
parente9fc64eed6e5cbf5e750063e1ad2e30887840dbf (diff)
downloadpowder-c4bace95bf97546ecb2fff4343fe82d0540d34ab.zip
powder-c4bace95bf97546ecb2fff4343fe82d0540d34ab.tar.gz
Allow deleting of stamps
Diffstat (limited to 'src')
-rw-r--r--src/client/Client.cpp13
-rw-r--r--src/interface/SaveButton.cpp81
-rw-r--r--src/search/SearchController.cpp30
-rw-r--r--src/stamps/StampsController.cpp66
-rw-r--r--src/stamps/StampsController.h4
-rw-r--r--src/stamps/StampsModel.cpp45
-rw-r--r--src/stamps/StampsModel.h7
-rw-r--r--src/stamps/StampsView.cpp43
-rw-r--r--src/stamps/StampsView.h5
9 files changed, 250 insertions, 44 deletions
diff --git a/src/client/Client.cpp b/src/client/Client.cpp
index 9e4732b..9a11084 100644
--- a/src/client/Client.cpp
+++ b/src/client/Client.cpp
@@ -5,6 +5,7 @@
#include <vector>
#include <iomanip>
#include <time.h>
+#include <stdio.h>
#ifdef WIN32
#include <direct.h>
@@ -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 eacf0ef..6e08e95 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);
+ 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
- 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(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);
+ }
- 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);
+ {
+ //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->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);
+ {
+ 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 b0936ef..ec5f805 100644
--- a/src/search/SearchController.cpp
+++ b/src/search/SearchController.cpp
@@ -220,6 +220,7 @@ void SearchController::removeSelectedC()
std::vector<int> 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<int> 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<int> saves;
+ public:
+ FavouriteSavesTask(std::vector<int> 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<int> selected = searchModel->GetSelected();
+ new TaskWindow("Favouring saves", new FavouriteSavesTask(selected));
ClearSelection();
}
diff --git a/src/stamps/StampsController.cpp b/src/stamps/StampsController.cpp
index 07c1efd..fde93a5 100644
--- a/src/stamps/StampsController.cpp
+++ b/src/stamps/StampsController.cpp
@@ -4,9 +4,15 @@
* Created on: Mar 29, 2012
* Author: Simon
*/
+#include <sstream>
+#include <unistd.h>
+#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<std::string> stamps;
+ public:
+ RemoveSavesTask(std::vector<std::string> 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<std::string> 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 6b1c401..7053714 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 b679d13..cfdc72c 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; i<stampIDs.size() && i<stampsEnd; i++)
{
Save * tempSave = Client::Ref().GetStamp(stampIDs[i]);
- stampsList.push_back(tempSave);
+ if(tempSave)
+ {
+ stampsList.push_back(tempSave);
+ }
}
notifyStampsListChanged();
}
+void StampsModel::SelectStamp(std::string stampID)
+{
+ for(int i = 0; i < selected.size(); i++)
+ {
+ if(selected[i]==stampID)
+ {
+ return;
+ }
+ }
+ selected.push_back(stampID);
+ notifySelectedChanged();
+}
+
+void StampsModel::DeselectStamp(std::string stampID)
+{
+ bool changed = false;
+restart:
+ for(int i = 0; i < selected.size(); i++)
+ {
+ if(selected[i]==stampID)
+ {
+ selected.erase(selected.begin()+i);
+ changed = true;
+ goto restart; //Just ensure all cases are removed.
+ }
+ }
+ if(changed)
+ notifySelectedChanged();
+}
+
+void StampsModel::notifySelectedChanged()
+{
+ for(int i = 0; i < observers.size(); i++)
+ {
+ StampsView* cObserver = observers[i];
+ cObserver->NotifySelectedChanged(this);
+ }
+}
+
StampsModel::~StampsModel() {
if(stamp)
delete stamp;
diff --git a/src/stamps/StampsModel.h b/src/stamps/StampsModel.h
index cee158c..0356349 100644
--- a/src/stamps/StampsModel.h
+++ b/src/stamps/StampsModel.h
@@ -9,11 +9,13 @@
#define STAMPSMODEL_H_
#include <vector>
+#include <string>
#include <math.h>
#include "search/Save.h"
class StampsView;
class StampsModel {
+ vector<std::string> selected;
Save * stamp;
std::vector<std::string> stampIDs;
std::vector<Save*> 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<std::string> 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 c0d5981..883cabd 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<std::string> 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 02e5569..0264611 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<ui::Component*> stampButtons;
+ std::vector<ui::SaveButton*> 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();