Allow deleting of stamps

This commit is contained in:
Simon Robertshaw
2012-04-19 15:22:18 +01:00
parent e9fc64eed6
commit c4bace95bf
9 changed files with 251 additions and 45 deletions

View File

@@ -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,8 +234,16 @@ Save * Client::GetStamp(string stampID)
void Client::DeleteStamp(string stampID)
{
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)
{

View File

@@ -19,6 +19,8 @@ SaveButton::SaveButton(Point position, Point size, Save * save):
voteColour(255, 0, 0),
selectable(false),
selected(false)
{
if(save)
{
if(save->votesUp==0)
voteRatio = 0.0f;
@@ -34,7 +36,7 @@ SaveButton::SaveButton(Point position, Point size, Save * save):
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,6 +114,8 @@ void SaveButton::Draw(const Point& screenPos)
scaleFactor = (Size.Y-25)/((float)YRES);
thumbBoxSize = ui::Point(((float)XRES)*scaleFactor, ((float)YRES)*scaleFactor);
}
if(save)
{
if(save->id)
{
if(isMouseInside)
@@ -142,6 +146,7 @@ void SaveButton::Draw(const Point& screenPos)
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)
{

View File

@@ -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();
}

View File

@@ -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)

View File

@@ -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();

View File

@@ -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]);
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;

View File

@@ -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();
};

View File

@@ -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)

View File

@@ -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();