diff --git a/src/client/Client.cpp b/src/client/Client.cpp index 4b7ef950f..b748005db 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -432,6 +432,58 @@ LoginStatus Client::Login(string username, string password, User & user) return LoginError; } +RequestStatus Client::DeleteSave(int saveID) +{ + lastError = ""; + std::vector * tags = NULL; + std::stringstream urlStream; + char * data = NULL; + int dataStatus, dataLength; + urlStream << "http://" << SERVER << "/Browse/Delete.json?ID=" << saveID; + if(authUser.ID) + { + std::stringstream userIDStream; + userIDStream << authUser.ID; + data = http_auth_get((char *)urlStream.str().c_str(), (char *)(userIDStream.str().c_str()), NULL, (char *)(authUser.SessionID.c_str()), &dataStatus, &dataLength); + } + else + { + lastError = "Not authenticated"; + return RequestFailure; + } + if(dataStatus == 200 && data) + { + try + { + std::istringstream dataStream(data); + json::Object objDocument; + json::Reader::Read(objDocument, dataStream); + + int status = ((json::Number)objDocument["Status"]).Value(); + + if(status!=1) + goto failure; + } + catch (json::Exception &e) + { + lastError = "Could not read response"; + goto failure; + } + } + else + { + lastError = http_ret_text(dataStatus); + goto failure; + } + if(data) + free(data); + return RequestOkay; +failure: + if(data) + free(data); + return RequestFailure; +} + Save * Client::GetSave(int saveID, int saveDate) { lastError = ""; diff --git a/src/client/Client.h b/src/client/Client.h index c8f57646a..d0ea14b1a 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -67,6 +67,7 @@ public: Thumbnail * GetPreview(int saveID, int saveDate); Thumbnail * GetThumbnail(int saveID, int saveDate); Save * GetSave(int saveID, int saveDate); + RequestStatus DeleteSave(int saveID); void SetAuthUser(User user); User GetAuthUser(); std::vector * RemoveTag(int saveID, string tag); //TODO RequestStatus diff --git a/src/dialogues/ConfirmPrompt.cpp b/src/dialogues/ConfirmPrompt.cpp new file mode 100644 index 000000000..d97e8c8d2 --- /dev/null +++ b/src/dialogues/ConfirmPrompt.cpp @@ -0,0 +1,67 @@ +/* + * ConfirmPrompt.cpp + * + * Created on: Apr 6, 2012 + * Author: Simon + */ + +#include "ConfirmPrompt.h" +#include "interface/Label.h" +#include "interface/Button.h" + +ConfirmPrompt::ConfirmPrompt(std::string title, std::string message, ConfirmDialogueCallback * callback_): + ui::Window(ui::Point(-1, -1), ui::Point(200, 75)), + callback(callback_) +{ + ui::Label * titleLabel = new ui::Label(ui::Point(2, 1), ui::Point(Size.X-4, 16), title); + titleLabel->SetTextColour(ui::Colour(220, 220, 50)); + titleLabel->SetAlignment(AlignLeft, AlignBottom); + AddComponent(titleLabel); + + ui::Label * messageLabel = new ui::Label(ui::Point(4, 18), ui::Point(Size.X-8, 60), message); + messageLabel->SetAlignment(AlignLeft, AlignTop); + AddComponent(messageLabel); + + class CloseAction: public ui::ButtonAction + { + public: + ConfirmPrompt * prompt; + DialogueResult result; + CloseAction(ConfirmPrompt * prompt_, DialogueResult result_) { prompt = prompt_; result = result_; } + void ActionCallback(ui::Button * sender) + { + ui::Engine::Ref().CloseWindow(); + prompt->callback->ConfirmCallback(result); + //delete prompt; TODO: Fix component disposal + } + }; + + + ui::Button * cancelButton = new ui::Button(ui::Point(0, Size.Y-16), ui::Point(Size.X-50, 16), "Cancel"); + cancelButton->SetAlignment(AlignLeft, AlignBottom); + cancelButton->SetBorderColour(ui::Colour(200, 200, 200)); + cancelButton->SetActionCallback(new CloseAction(this, ResultCancel)); + AddComponent(cancelButton); + + ui::Button * okayButton = new ui::Button(ui::Point(Size.X-50, Size.Y-16), ui::Point(50, 16), "Continue"); + okayButton->SetAlignment(AlignLeft, AlignBottom); + okayButton->SetTextColour(ui::Colour(220, 220, 50)); + okayButton->SetActionCallback(new CloseAction(this, ResultOkay)); + AddComponent(okayButton); + + ui::Engine::Ref().ShowWindow(this); +} + +void ConfirmPrompt::OnDraw() +{ + Graphics * g = ui::Engine::Ref().g; + + g->clearrect(Position.X-2, Position.Y-2, Size.X+4, Size.Y+4); + g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 200, 200, 200, 255); +} + +ConfirmPrompt::~ConfirmPrompt() { + if(callback) + delete callback; +} + diff --git a/src/dialogues/ConfirmPrompt.h b/src/dialogues/ConfirmPrompt.h new file mode 100644 index 000000000..f86a38636 --- /dev/null +++ b/src/dialogues/ConfirmPrompt.h @@ -0,0 +1,30 @@ +/* + * ConfirmPrompt.h + * + * Created on: Apr 6, 2012 + * Author: Simon + */ + +#ifndef CONFIRMPROMPT_H_ +#define CONFIRMPROMPT_H_ + +#include "interface/Window.h" + +class ConfirmDialogueCallback; +class ConfirmPrompt: public ui::Window { +public: + enum DialogueResult { ResultCancel, ResultOkay }; + ConfirmPrompt(std::string title, std::string message, ConfirmDialogueCallback * callback_); + virtual void OnDraw(); + virtual ~ConfirmPrompt(); + ConfirmDialogueCallback * callback; +}; + +class ConfirmDialogueCallback +{ + public: + virtual void ConfirmCallback(ConfirmPrompt::DialogueResult result) {} + virtual ~ConfirmDialogueCallback() {} +}; + +#endif /* CONFIRMPROMPT_H_ */ diff --git a/src/dialogues/ErrorMessage.cpp b/src/dialogues/ErrorMessage.cpp index bce0c3377..76039939e 100644 --- a/src/dialogues/ErrorMessage.cpp +++ b/src/dialogues/ErrorMessage.cpp @@ -23,16 +23,20 @@ ErrorMessage::ErrorMessage(std::string title, std::string message): class DismissAction: public ui::ButtonAction { + ErrorMessage * message; + public: + DismissAction(ErrorMessage * message_) { message = message_; } void ActionCallback(ui::Button * sender) { ui::Engine::Ref().CloseWindow(); + //delete message; TODO: Fix component disposal } }; ui::Button * okayButton = new ui::Button(ui::Point(0, Size.Y-16), ui::Point(Size.X, 16), "Dismiss"); okayButton->SetAlignment(AlignRight, AlignBottom); okayButton->SetBorderColour(ui::Colour(200, 200, 200)); - okayButton->SetActionCallback(new DismissAction()); + okayButton->SetActionCallback(new DismissAction(this)); AddComponent(okayButton); ui::Engine::Ref().ShowWindow(this); } diff --git a/src/interface/Component.cpp b/src/interface/Component.cpp index 0efc2b1a0..5db7acceb 100644 --- a/src/interface/Component.cpp +++ b/src/interface/Component.cpp @@ -148,5 +148,4 @@ void Component::OnMouseWheelInside(int localx, int localy, int d) Component::~Component() { - } diff --git a/src/interface/SaveButton.cpp b/src/interface/SaveButton.cpp index d044ef612..338ef962c 100644 --- a/src/interface/SaveButton.cpp +++ b/src/interface/SaveButton.cpp @@ -17,7 +17,9 @@ SaveButton::SaveButton(Point position, Point size, Save * save): isMouseInside(false), isButtonDown(false), actionCallback(NULL), - voteColour(255, 0, 0) + voteColour(255, 0, 0), + selectable(false), + selected(false) { if(save->votesUp==0) voteRatio = 0.0f; @@ -93,6 +95,11 @@ void SaveButton::Draw(const Point& screenPos) float scaleFactor; ui::Point thumbBoxSize(0, 0); + if(selected && selectable) + { + g->fillrect(screenPos.X, screenPos.Y, Size.X, Size.Y, 100, 170, 255, 100); + } + if(thumbnail) { thumbBoxSize = ui::Point(thumbnail->Size.X, thumbnail->Size.Y); @@ -136,6 +143,14 @@ 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) + { + g->clearrect(screenPos.X+(Size.X-20), screenPos.Y+6, 14, 14); + g->drawrect(screenPos.X+(Size.X-20), screenPos.Y+6, 14, 14, 255, 255, 255, 255); + if(selected) + g->fillrect(screenPos.X+(Size.X-18), screenPos.Y+8, 10, 10, 255, 255, 255, 255); + } } void SaveButton::OnMouseUnclick(int x, int y, unsigned int button) @@ -145,6 +160,13 @@ void SaveButton::OnMouseUnclick(int x, int y, unsigned int button) return; //left click only! } + if(x>=Size.X-20 && y>=6 && y<=20 && x<=Size.X-6 && selectable) + { + selected = !selected; + DoSelection(); + return; + } + if(isButtonDown) { DoAction(); @@ -155,7 +177,13 @@ void SaveButton::OnMouseUnclick(int x, int y, unsigned int button) void SaveButton::OnMouseClick(int x, int y, unsigned int button) { + if(button !=1 && selectable) + { + selected = !selected; + DoSelection(); + } if(button != 1) return; //left click only! + isButtonDown = true; } @@ -175,6 +203,12 @@ void SaveButton::DoAction() actionCallback->ActionCallback(this); } +void SaveButton::DoSelection() +{ + if(selectable) + actionCallback->SelectedCallback(this); +} + void SaveButton::SetActionCallback(SaveButtonAction * action) { actionCallback = action; diff --git a/src/interface/SaveButton.h b/src/interface/SaveButton.h index d4b257015..de1b6ed1f 100644 --- a/src/interface/SaveButton.h +++ b/src/interface/SaveButton.h @@ -16,6 +16,7 @@ class SaveButtonAction { public: virtual void ActionCallback(ui::SaveButton * sender) {} + virtual void SelectedCallback(ui::SaveButton * sender) {} virtual ~SaveButtonAction() {} }; @@ -36,12 +37,18 @@ public: virtual void Draw(const Point& screenPos); virtual void Tick(float dt); + void SetSelected(bool selected_) { selected = selected_; } + bool GetSelected() { return selected; } + void SetSelectable(bool selectable_) { selectable = selectable_; } + bool GetSelectable() { return selectable; } + Save * GetSave() { return save; } inline bool GetState() { return state; } virtual void DoAction(); + virtual void DoSelection(); void SetActionCallback(SaveButtonAction * action); protected: - bool isButtonDown, state, isMouseInside; + bool isButtonDown, state, isMouseInside, selected, selectable; float voteRatio; Colour voteColour; SaveButtonAction * actionCallback; diff --git a/src/search/SearchController.cpp b/src/search/SearchController.cpp index 1449f9ad4..90cd41b72 100644 --- a/src/search/SearchController.cpp +++ b/src/search/SearchController.cpp @@ -1,10 +1,15 @@ #include +#include +#include #include "SearchController.h" #include "SearchModel.h" #include "SearchView.h" #include "interface/Panel.h" +#include "dialogues/ConfirmPrompt.h" #include "preview/PreviewController.h" #include "client/Client.h" +#include "tasks/Task.h" +#include "tasks/TaskWindow.h" class SearchController::OpenCallback: public ControllerCallback { @@ -125,8 +130,91 @@ void SearchController::ShowOwn(bool show) searchModel->SetShowOwn(false); } +void SearchController::Selected(int saveID, bool selected) +{ + if(!Client::Ref().GetAuthUser().ID) + return; + + if(selected) + searchModel->SelectSave(saveID); + else + searchModel->DeselectSave(saveID); +} + void SearchController::OpenSave(int saveID) { activePreview = new PreviewController(saveID, new OpenCallback(this)); ui::Engine::Ref().ShowWindow(activePreview->GetView()); } + +void SearchController::ClearSelection() +{ + searchModel->ClearSelected(); +} + +void SearchController::RemoveSelected() +{ + class RemoveSelectedConfirmation: public ConfirmDialogueCallback { + public: + SearchController * c; + RemoveSelectedConfirmation(SearchController * 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 " << searchModel->GetSelected().size() << " save"; + if(searchModel->GetSelected().size()>1) + desc << "s"; + new ConfirmPrompt("Delete saves", desc.str(), new RemoveSelectedConfirmation(this)); +} + +void SearchController::removeSelectedC() +{ + class RemoveSavesTask : public Task + { + std::vector saves; + public: + RemoveSavesTask(std::vector saves_) { saves = saves_; } + virtual void doWork() + { + for(int i = 0; i < saves.size(); i++) + { + std::stringstream saveID; + saveID << "Deleting save [" << saves[i] << "] ..."; + notifyStatus(saveID.str()); + if(Client::Ref().DeleteSave(saves[i])!=RequestOkay) + { + std::stringstream saveIDF; + saveIDF << "\boFailed to delete [" << 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("Removing saves", new RemoveSavesTask(selected)); + ClearSelection(); +} + +void SearchController::UnpublishSelected() +{ + +} + +void SearchController::unpublishSelectedC() +{ + ClearSelection(); +} + +void SearchController::FavouriteSelected() +{ + ClearSelection(); +} diff --git a/src/search/SearchController.h b/src/search/SearchController.h index cf348309b..3168419c9 100644 --- a/src/search/SearchController.h +++ b/src/search/SearchController.h @@ -21,6 +21,8 @@ private: double nextQueryTime; std::string nextQuery; bool nextQueryDone; + void removeSelectedC(); + void unpublishSelectedC(); public: class OpenCallback; bool HasExited; @@ -33,8 +35,13 @@ public: void PrevPage(); void ChangeSort(); void ShowOwn(bool show); + void Selected(int saveID, bool selected); void OpenSave(int saveID); void Update(); + void ClearSelection(); + void RemoveSelected(); + void UnpublishSelected(); + void FavouriteSelected(); Save * GetLoadedSave(); }; diff --git a/src/search/SearchModel.cpp b/src/search/SearchModel.cpp index 4a410c386..e1d984995 100644 --- a/src/search/SearchModel.cpp +++ b/src/search/SearchModel.cpp @@ -37,6 +37,8 @@ void SearchModel::UpdateSaveList(int pageNumber, std::string query) currentPage = pageNumber; notifySaveListChanged(); notifyPageChanged(); + selected.clear(); + notifySelectedChanged(); //Threading if(!updateSaveListWorking) @@ -94,6 +96,36 @@ void SearchModel::AddObserver(SearchView * observer) observer->NotifyShowOwnChanged(this); } +void SearchModel::SelectSave(int saveID) +{ + for(int i = 0; i < selected.size(); i++) + { + if(selected[i]==saveID) + { + return; + } + } + selected.push_back(saveID); + notifySelectedChanged(); +} + +void SearchModel::DeselectSave(int saveID) +{ + bool changed = false; +restart: + for(int i = 0; i < selected.size(); i++) + { + if(selected[i]==saveID) + { + selected.erase(selected.begin()+i); + changed = true; + goto restart; //Just ensure all cases are removed. + } + } + if(changed) + notifySelectedChanged(); +} + void SearchModel::notifySaveListChanged() { for(int i = 0; i < observers.size(); i++) @@ -130,6 +162,15 @@ void SearchModel::notifyShowOwnChanged() } } +void SearchModel::notifySelectedChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + SearchView* cObserver = observers[i]; + cObserver->NotifySelectedChanged(this); + } +} + SearchModel::~SearchModel() { if(loadedSave) diff --git a/src/search/SearchModel.h b/src/search/SearchModel.h index 2587c0340..8f2ae5860 100644 --- a/src/search/SearchModel.h +++ b/src/search/SearchModel.h @@ -18,12 +18,14 @@ private: string currentSort; string lastQuery; string lastError; + vector selected; vector observers; vector saveList; int currentPage; int resultCount; bool showOwn; void notifySaveListChanged(); + void notifySelectedChanged(); void notifyPageChanged(); void notifySortChanged(); void notifyShowOwnChanged(); @@ -38,6 +40,7 @@ private: public: SearchModel(); virtual ~SearchModel(); + void AddObserver(SearchView * observer); void UpdateSaveList(int pageNumber, std::string query); vector GetSaveList(); @@ -52,6 +55,10 @@ public: void SetLoadedSave(Save * save); Save * GetLoadedSave(); bool GetSavesLoaded() { return saveListLoaded; } + vector GetSelected() { return selected; } + void ClearSelected() { selected.clear(); notifySelectedChanged(); } + void SelectSave(int saveID); + void DeselectSave(int saveID); void Update(); }; diff --git a/src/search/SearchView.cpp b/src/search/SearchView.cpp index 4b9b6b2f5..4064658c1 100644 --- a/src/search/SearchView.cpp +++ b/src/search/SearchView.cpp @@ -99,6 +99,70 @@ SearchView::SearchView(): ui::Label * searchPrompt = new ui::Label(ui::Point(10, 10), ui::Point(50, 16), "Search:"); searchPrompt->SetAlignment(AlignLeft, AlignBottom); AddComponent(searchPrompt); + + class RemoveSelectedAction : public ui::ButtonAction + { + SearchView * v; + public: + RemoveSelectedAction(SearchView * _v) { v = _v; } + void ActionCallback(ui::Button * sender) + { + v->c->RemoveSelected(); + } + }; + + class UnpublishSelectedAction : public ui::ButtonAction + { + SearchView * v; + public: + UnpublishSelectedAction(SearchView * _v) { v = _v; } + void ActionCallback(ui::Button * sender) + { + v->c->UnpublishSelected(); + } + }; + + class FavouriteSelectedAction : public ui::ButtonAction + { + SearchView * v; + public: + FavouriteSelectedAction(SearchView * _v) { v = _v; } + void ActionCallback(ui::Button * sender) + { + v->c->FavouriteSelected(); + } + }; + + class ClearSelectionAction : public ui::ButtonAction + { + SearchView * v; + public: + ClearSelectionAction(SearchView * _v) { v = _v; } + void ActionCallback(ui::Button * sender) + { + v->c->ClearSelection(); + } + }; + + removeSelected = new ui::Button(ui::Point((((XRES+BARSIZE)-415)/2), YRES+MENUSIZE-18), ui::Point(100, 16), "Delete"); + removeSelected->Visible = false; + removeSelected->SetActionCallback(new RemoveSelectedAction(this)); + AddComponent(removeSelected); + + unpublishSelected = new ui::Button(ui::Point((((XRES+BARSIZE)-415)/2)+105, YRES+MENUSIZE-18), ui::Point(100, 16), "Unpublish"); + unpublishSelected->Visible = false; + unpublishSelected->SetActionCallback(new UnpublishSelectedAction(this)); + AddComponent(unpublishSelected); + + favouriteSelected = new ui::Button(ui::Point((((XRES+BARSIZE)-415)/2)+210, YRES+MENUSIZE-18), ui::Point(100, 16), "Favourite"); + favouriteSelected->Visible = false; + favouriteSelected->SetActionCallback(new FavouriteSelectedAction(this)); + AddComponent(favouriteSelected); + + clearSelection = new ui::Button(ui::Point((((XRES+BARSIZE)-415)/2)+315, YRES+MENUSIZE-18), ui::Point(100, 16), "Clear selection"); + clearSelection->Visible = false; + clearSelection->SetActionCallback(new ClearSelectionAction(this)); + AddComponent(clearSelection); } void SearchView::doSearch() @@ -211,6 +275,10 @@ void SearchView::NotifySaveListChanged(SearchModel * sender) { v->c->OpenSave(sender->GetSave()->GetID()); } + virtual void SelectedCallback(ui::SaveButton * sender) + { + v->c->Selected(sender->GetSave()->GetID(), sender->GetSelected()); + } }; for(i = 0; i < saves.size(); i++) { @@ -230,6 +298,8 @@ void SearchView::NotifySaveListChanged(SearchModel * sender) ui::Point(buttonWidth, buttonHeight), saves[i]); saveButton->SetActionCallback(new SaveOpenAction(this)); + if(Client::Ref().GetAuthUser().ID) + saveButton->SetSelectable(true); saveButtons.push_back(saveButton); AddComponent(saveButton); saveX++; @@ -237,6 +307,35 @@ void SearchView::NotifySaveListChanged(SearchModel * sender) } } +void SearchView::NotifySelectedChanged(SearchModel * sender) +{ + vector selected = sender->GetSelected(); + for(int j = 0; j < saveButtons.size(); j++) + { + saveButtons[j]->SetSelected(false); + for(int i = 0; i < selected.size(); i++) + { + if(saveButtons[j]->GetSave()->GetID()==selected[i]) + saveButtons[j]->SetSelected(true); + } + } + + if(selected.size()) + { + removeSelected->Visible = true; + unpublishSelected->Visible = true; + favouriteSelected->Visible = true; + clearSelection->Visible = true; + } + else + { + removeSelected->Visible = false; + unpublishSelected->Visible = false; + favouriteSelected->Visible = false; + clearSelection->Visible = false; + } +} + void SearchView::OnTick(float dt) { c->Update(); diff --git a/src/search/SearchView.h b/src/search/SearchView.h index af43a8c0a..ed4b06e44 100644 --- a/src/search/SearchView.h +++ b/src/search/SearchView.h @@ -27,9 +27,15 @@ private: ui::Button * sortButton; ui::Button * ownButton; ui::Spinner * loadingSpinner; + + ui::Button * removeSelected; + ui::Button * unpublishSelected; + ui::Button * favouriteSelected; + ui::Button * clearSelection; void doSearch(); public: void NotifySaveListChanged(SearchModel * sender); + void NotifySelectedChanged(SearchModel * sender); void NotifyPageChanged(SearchModel * sender); void NotifySortChanged(SearchModel * sender); void NotifyShowOwnChanged(SearchModel * sender); diff --git a/src/tasks/Task.cpp b/src/tasks/Task.cpp new file mode 100644 index 000000000..c20a0ea9d --- /dev/null +++ b/src/tasks/Task.cpp @@ -0,0 +1,84 @@ +/* + * Task.cpp + * + * Created on: Apr 6, 2012 + * Author: Simon + */ + + +#include +#include "Task.h" +#include "TaskListener.h" + +void Task::SetTaskListener(TaskListener * listener) +{ + this->listener = listener; +} + +void Task::Start() +{ + pthread_create(&doWorkThread, 0, &Task::doWork_helper, this); +} + +int Task::GetProgress() +{ + return progress; +} + +std::string Task::GetStatus() +{ + return status; +} + +bool Task::GetDone() +{ + return done; +} + +Task::~Task() +{ + +} + +void Task::doWork() +{ + notifyStatus("Fake progress"); + for(int i = 0; i < 100; i++) + { + notifyProgress(i); + usleep((100)*1000); + } +} + +void * Task::doWork_helper(void * ref) +{ + ((Task*)ref)->doWork(); + ((Task*)ref)->notifyDone(); + return NULL; +} + +void Task::notifyProgress(int progress) +{ + if(this->progress!=progress) { + this->progress = progress; + if(listener) + listener->NotifyProgress(this); + } +} + +void Task::notifyStatus(std::string status) +{ + if(this->status!=status) { + this->status = status; + if(listener) + listener->NotifyStatus(this); + } +} + +void Task::notifyDone() +{ + if(listener) + { + done = true; listener->NotifyDone(this); + } +} diff --git a/src/tasks/Task.h b/src/tasks/Task.h new file mode 100644 index 000000000..90463cbf1 --- /dev/null +++ b/src/tasks/Task.h @@ -0,0 +1,38 @@ +/* + * Task.h + * + * Created on: Apr 6, 2012 + * Author: Simon + */ + +#ifndef TASK_H_ +#define TASK_H_ + +#include +#include +#include "TaskListener.h" + +class TaskListener; +class Task { +public: + void SetTaskListener(TaskListener * listener); + void Start(); + int GetProgress(); + bool GetDone(); + std::string GetStatus(); + Task() {} + virtual ~Task(); +protected: + int progress; + bool done; + std::string status; + TaskListener * listener; + pthread_t doWorkThread; + virtual void doWork(); + static void * doWork_helper(void * ref); + void notifyProgress(int progress); + void notifyStatus(std::string status); + void notifyDone(); +}; + +#endif /* TASK_H_ */ diff --git a/src/tasks/TaskListener.h b/src/tasks/TaskListener.h new file mode 100644 index 000000000..fc5d5e7ae --- /dev/null +++ b/src/tasks/TaskListener.h @@ -0,0 +1,20 @@ +/* + * TaskListener.h + * + * Created on: Apr 6, 2012 + * Author: Simon + */ + +#ifndef TASKLISTENER_H_ +#define TASKLISTENER_H_ + +class Task; +class TaskListener { +public: + virtual void NotifyDone(Task * task) {} + virtual void NotifyProgress(Task * task) {} + virtual void NotifyStatus(Task * task) {} + virtual ~TaskListener() {} +}; + +#endif /* TASK_H_ */ diff --git a/src/tasks/TaskWindow.cpp b/src/tasks/TaskWindow.cpp new file mode 100644 index 000000000..ac22c79ed --- /dev/null +++ b/src/tasks/TaskWindow.cpp @@ -0,0 +1,66 @@ +/* + * TaskWindow.cpp + * + * Created on: Apr 6, 2012 + * Author: Simon + */ + +#include "interface/Label.h" +#include "TaskWindow.h" +#include "Task.h" + +TaskWindow::TaskWindow(std::string title_, Task * task_): + task(task_), + title(title_), + ui::Window(ui::Point(-1, -1), ui::Point(300, 200)), + progress(0), + done(false) +{ + + ui::Label * tempLabel = new ui::Label(ui::Point(3, 3), ui::Point(Size.X-6, 16), title); + AddComponent(tempLabel); + + statusLabel = new ui::Label(ui::Point(3, 19), ui::Point(Size.X-6, 16), ""); + AddComponent(statusLabel); + + ui::Engine::Ref().ShowWindow(this); + + task->SetTaskListener(this); + task->Start(); +} + +void TaskWindow::NotifyStatus(Task * task) +{ + statusLabel->SetText(task->GetStatus()); +} + +void TaskWindow::NotifyDone(Task * task) +{ + if(ui::Engine::Ref().GetWindow()==this) + { + ui::Engine::Ref().CloseWindow(); + delete this; + } +} + +void TaskWindow::NotifyProgress(Task * task) +{ + progress = task->GetProgress(); +} + +void TaskWindow::OnDraw() +{ + Graphics * g = ui::Engine::Ref().g; + g->clearrect(Position.X-2, Position.Y-2, Size.X+3, Size.Y+3); + g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 255, 255, 255, 255); + + g->drawrect(Position.X + 20, Position.Y + 36, Size.X-40, 24, 255, 255, 255, 255); + + float size = float(Size.X-40)*(float(progress)/100.0f); // TIL... + g->fillrect(Position.X + 20, Position.Y + 36, size, 24, 255, 255, 255, 255); +} + +TaskWindow::~TaskWindow() { + delete task; +} + diff --git a/src/tasks/TaskWindow.h b/src/tasks/TaskWindow.h new file mode 100644 index 000000000..cacb59cda --- /dev/null +++ b/src/tasks/TaskWindow.h @@ -0,0 +1,32 @@ +/* + * TaskWindow.h + * + * Created on: Apr 6, 2012 + * Author: Simon + */ + +#ifndef TASKWINDOW_H_ +#define TASKWINDOW_H_ + +#include +#include "interface/Label.h" +#include "interface/Window.h" +#include "tasks/TaskListener.h" + +class Task; +class TaskWindow: public ui::Window, public TaskListener { + Task * task; + std::string title; + int progress; + bool done; + ui::Label * statusLabel; +public: + TaskWindow(std::string title_, Task * task_); + virtual void NotifyStatus(Task * task); + virtual void NotifyDone(Task * task); + virtual void NotifyProgress(Task * task); + virtual void OnDraw(); + virtual ~TaskWindow(); +}; + +#endif /* TASKWINDOW_H_ */