diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2012-04-02 16:01:28 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2012-04-02 16:01:28 (GMT) |
| commit | efddc12e5d2aadc5eee1927245ad38b9dee89aed (patch) | |
| tree | cf7ad38119f0734609944158e33bbb944925c777 /src/stamps | |
| parent | 289556ac7078963b6af361f5812dd62e6712359f (diff) | |
| download | powder-efddc12e5d2aadc5eee1927245ad38b9dee89aed.zip powder-efddc12e5d2aadc5eee1927245ad38b9dee89aed.tar.gz | |
Stamps browser, placement + clipboard sampling and placement - No clipboard or stamp thumbnail generation, needs thumbnail generator from SaveLoader
Diffstat (limited to 'src/stamps')
| -rw-r--r-- | src/stamps/StampsController.cpp | 69 | ||||
| -rw-r--r-- | src/stamps/StampsController.h | 35 | ||||
| -rw-r--r-- | src/stamps/StampsModel.cpp | 84 | ||||
| -rw-r--r-- | src/stamps/StampsModel.h | 36 | ||||
| -rw-r--r-- | src/stamps/StampsModelException.h | 23 | ||||
| -rw-r--r-- | src/stamps/StampsView.cpp | 140 | ||||
| -rw-r--r-- | src/stamps/StampsView.h | 35 |
7 files changed, 422 insertions, 0 deletions
diff --git a/src/stamps/StampsController.cpp b/src/stamps/StampsController.cpp new file mode 100644 index 0000000..07c1efd --- /dev/null +++ b/src/stamps/StampsController.cpp @@ -0,0 +1,69 @@ +/* + * StampsController.cpp + * + * Created on: Mar 29, 2012 + * Author: Simon + */ + +#include "StampsController.h" +#include "interface/Engine.h" + +#include "StampsModel.h" +#include "StampsView.h" + +StampsController::StampsController(ControllerCallback * callback): + HasDone(false) +{ + stampsModel = new StampsModel(); + stampsView = new StampsView(); + stampsView->AttachController(this); + stampsModel->AddObserver(stampsView); + + this->callback = callback; + + stampsModel->UpdateStampsList(1); +} + +void StampsController::OpenStamp(Save * stamp) +{ + stampsModel->SetStamp(stamp); +} + +Save * StampsController::GetStamp() +{ + return stampsModel->GetStamp(); +} + +void StampsController::NextPage() +{ + if(stampsModel->GetPageNum()>1) + stampsModel->UpdateStampsList(stampsModel->GetPageNum()-1); +} + +void StampsController::PrevPage() +{ + if(stampsModel->GetPageNum() <= stampsModel->GetPageCount()) + stampsModel->UpdateStampsList(stampsModel->GetPageNum()+1); +} + +void StampsController::Update() +{ + if(stampsModel->GetStamp()) + { + Exit(); + } +} + +void StampsController::Exit() +{ + if(ui::Engine::Ref().GetWindow() == stampsView) + ui::Engine::Ref().CloseWindow(); + if(callback) + callback->ControllerExit(); + HasDone = true; +} + +StampsController::~StampsController() { + // TODO Auto-generated destructor stub +} + diff --git a/src/stamps/StampsController.h b/src/stamps/StampsController.h new file mode 100644 index 0000000..6b1c401 --- /dev/null +++ b/src/stamps/StampsController.h @@ -0,0 +1,35 @@ +/* + * StampsController.h + * + * Created on: Mar 29, 2012 + * Author: Simon + */ + +#ifndef STAMPSCONTROLLER_H_ +#define STAMPSCONTROLLER_H_ + +#include "Controller.h" +#include "StampsView.h" +#include "search/Save.h" + +class StampsView; +class StampsModel; +class StampsController { + ControllerCallback * callback; + StampsView * stampsView; + StampsModel * stampsModel; +public: + bool HasDone; + StampsController(ControllerCallback * callback); + StampsView * GetView() {return stampsView;} + Save * GetStamp(); + void OpenStamp(Save * stamp); + void SetStamp(); + void NextPage(); + void PrevPage(); + void Update(); + void Exit(); + virtual ~StampsController(); +}; + +#endif /* STAMPSCONTROLLER_H_ */ diff --git a/src/stamps/StampsModel.cpp b/src/stamps/StampsModel.cpp new file mode 100644 index 0000000..f76d4f1 --- /dev/null +++ b/src/stamps/StampsModel.cpp @@ -0,0 +1,84 @@ +/* + * StampsModel.cpp + * + * Created on: Mar 29, 2012 + * Author: Simon + */ + +#include "StampsModel.h" +#include "StampsView.h" +#include "client/Client.h" +#include "StampsModelException.h" + +StampsModel::StampsModel(): + stamp(NULL) +{ + // TODO Auto-generated constructor stub + stampIDs = Client::Ref().GetStamps(); +} + + +std::vector<Save *> StampsModel::GetStampsList() +{ + return stampsList; +} + +void StampsModel::AddObserver(StampsView * observer) +{ + observers.push_back(observer); + observer->NotifyStampsListChanged(this); +} + +void StampsModel::notifyStampsListChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyStampsListChanged(this); + } +} + +void StampsModel::notifyPageChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyPageChanged(this); + } +} + +Save * StampsModel::GetStamp() +{ + return stamp; +} + +void StampsModel::SetStamp(Save * newStamp) +{ + if(stamp) + delete stamp; + stamp = new Save(*newStamp); +} + +void StampsModel::UpdateStampsList(int pageNumber) +{ + std::vector<Save*> tempStampsList = stampsList; + stampsList.clear(); + /*notifyStampsListChanged(); + for(int i = 0; i < tempStampsList.size(); i++) + { + delete tempStampsList[i]; + }*/ + + int stampsEnd = pageNumber*20; + + for(int i = stampsEnd-20; i<stampIDs.size() && i<stampsEnd; i++) + { + Save * tempSave = Client::Ref().GetStamp(stampIDs[i]); + stampsList.push_back(tempSave); + } + notifyStampsListChanged(); +} + +StampsModel::~StampsModel() { + if(stamp) + delete stamp; +} + diff --git a/src/stamps/StampsModel.h b/src/stamps/StampsModel.h new file mode 100644 index 0000000..cee158c --- /dev/null +++ b/src/stamps/StampsModel.h @@ -0,0 +1,36 @@ +/* + * StampsModel.h + * + * Created on: Mar 29, 2012 + * Author: Simon + */ + +#ifndef STAMPSMODEL_H_ +#define STAMPSMODEL_H_ + +#include <vector> +#include <math.h> +#include "search/Save.h" + +class StampsView; +class StampsModel { + Save * stamp; + std::vector<std::string> stampIDs; + std::vector<Save*> stampsList; + std::vector<StampsView*> observers; + int currentPage; + void notifyStampsListChanged(); + void notifyPageChanged(); +public: + StampsModel(); + int GetPageCount() { return max(1, (int)(ceil(stampIDs.size()/16))); } + int GetPageNum() { return currentPage; } + void AddObserver(StampsView * observer); + std::vector<Save *> GetStampsList(); + void UpdateStampsList(int pageNumber); + Save * GetStamp(); + void SetStamp(Save * newStamp); + virtual ~StampsModel(); +}; + +#endif /* STAMPSMODEL_H_ */ diff --git a/src/stamps/StampsModelException.h b/src/stamps/StampsModelException.h new file mode 100644 index 0000000..7310aa0 --- /dev/null +++ b/src/stamps/StampsModelException.h @@ -0,0 +1,23 @@ +/* + * StampsModelException.h + * + * Created on: Mar 29, 2012 + * Author: Simon + */ + +#ifndef STAMPSMODELEXCEPTION_H_ +#define STAMPSMODELEXCEPTION_H_ + +#include <string> +#include <exception> +using namespace std; + +class StampsModelException { + string message; +public: + StampsModelException(string message_): message(message_) {}; + const char * what() const throw() { return message.c_str(); }; + ~StampsModelException() throw() {}; +}; + +#endif /* STAMPSMODELEXCEPTION_H_ */ diff --git a/src/stamps/StampsView.cpp b/src/stamps/StampsView.cpp new file mode 100644 index 0000000..ddb3fb4 --- /dev/null +++ b/src/stamps/StampsView.cpp @@ -0,0 +1,140 @@ +/* + * StampsView.cpp + * + * Created on: Mar 29, 2012 + * Author: Simon + */ + +#include <sstream> +#include "client/Client.h" +#include "StampsView.h" + +#include "interface/SaveButton.h" +#include "dialogues/ErrorMessage.h" +#include "StampsController.h" +#include "StampsModel.h" +#include "StampsModelException.h" + +StampsView::StampsView(): + ui::Window(ui::Point(0, 0), ui::Point(XRES+BARSIZE, YRES+MENUSIZE)) +{ + nextButton = new ui::Button(ui::Point(XRES+BARSIZE-52, YRES+MENUSIZE-18), ui::Point(50, 16), "Next \x95"); + previousButton = new ui::Button(ui::Point(1, YRES+MENUSIZE-18), ui::Point(50, 16), "\x96 Prev"); + infoLabel = new ui::Label(ui::Point(51, YRES+MENUSIZE-18), ui::Point(XRES+BARSIZE-102, 16), "Loading..."); + AddComponent(infoLabel); + AddComponent(nextButton); + AddComponent(previousButton); + + class NextPageAction : public ui::ButtonAction + { + StampsView * v; + public: + NextPageAction(StampsView * _v) { v = _v; } + void ActionCallback(ui::Button * sender) + { + v->c->NextPage(); + } + }; + nextButton->SetActionCallback(new NextPageAction(this)); + nextButton->SetAlignment(AlignRight, AlignBottom); + + class PrevPageAction : public ui::ButtonAction + { + StampsView * v; + public: + PrevPageAction(StampsView * _v) { v = _v; } + void ActionCallback(ui::Button * sender) + { + v->c->PrevPage(); + } + }; + previousButton->SetActionCallback(new PrevPageAction(this)); + previousButton->SetAlignment(AlignLeft, AlignBottom); +} + +void StampsView::OnTick(float dt) +{ + c->Update(); +} + +void StampsView::NotifyPageChanged(StampsModel * sender) +{ + std::stringstream pageInfo; + pageInfo << "Page " << sender->GetPageNum() << " of " << sender->GetPageCount(); + infoLabel->SetText(pageInfo.str()); + if(sender->GetPageNum() == 1) + { + previousButton->Visible = false; + } + else + { + previousButton->Visible = true; + } + if(sender->GetPageNum() == sender->GetPageCount()) + { + nextButton->Visible = false; + } + else + { + nextButton->Visible = true; + } +} + +void StampsView::NotifyStampsListChanged(StampsModel * sender) +{ + int i = 0; + int buttonWidth, buttonHeight, saveX = 0, saveY = 0, savesX = 5, savesY = 4, buttonPadding = 2; + int buttonAreaWidth, buttonAreaHeight, buttonXOffset, buttonYOffset; + + vector<Save*> saves = sender->GetStampsList(); + Client::Ref().ClearThumbnailRequests(); + for(i = 0; i < stampButtons.size(); i++) + { + RemoveComponent(stampButtons[i]); + delete stampButtons[i]; + } + + buttonXOffset = 0; + buttonYOffset = 50; + buttonAreaWidth = Size.X; + buttonAreaHeight = Size.Y - buttonYOffset - 18; + buttonWidth = (buttonAreaWidth/savesX) - buttonPadding*2; + buttonHeight = (buttonAreaHeight/savesY) - buttonPadding*2; + class SaveOpenAction: public ui::SaveButtonAction + { + StampsView * v; + public: + SaveOpenAction(StampsView * _v) { v = _v; } + virtual void ActionCallback(ui::SaveButton * sender) + { + v->c->OpenStamp(sender->GetSave()); + } + }; + for(i = 0; i < saves.size(); i++) + { + if(saveX == savesX) + { + if(saveY == savesY-1) + break; + saveX = 0; + saveY++; + } + ui::SaveButton * saveButton; + saveButton = new ui::SaveButton( + ui::Point( + buttonXOffset + buttonPadding + saveX*(buttonWidth+buttonPadding*2), + buttonYOffset + buttonPadding + saveY*(buttonHeight+buttonPadding*2) + ), + ui::Point(buttonWidth, buttonHeight), + saves[i]); + saveButton->SetActionCallback(new SaveOpenAction(this)); + stampButtons.push_back(saveButton); + AddComponent(saveButton); + saveX++; + } +} + +StampsView::~StampsView() { + // TODO Auto-generated destructor stub +} + diff --git a/src/stamps/StampsView.h b/src/stamps/StampsView.h new file mode 100644 index 0000000..a906cdc --- /dev/null +++ b/src/stamps/StampsView.h @@ -0,0 +1,35 @@ +/* + * StampsView.h + * + * Created on: Mar 29, 2012 + * Author: Simon + */ + +#ifndef STAMPSVIEW_H_ +#define STAMPSVIEW_H_ + +#include <vector> +#include "interface/Window.h" +#include "interface/Button.h" +#include "interface/Textbox.h" +#include "interface/Label.h" + +class StampsController; +class StampsModel; +class StampsView: public ui::Window { + StampsController * c; + std::vector<ui::Component*> stampButtons; + ui::Button * previousButton; + ui::Button * nextButton; + ui::Label * infoLabel; +public: + StampsView(); + //virtual void OnDraw(); + virtual void OnTick(float dt); + void AttachController(StampsController * c_) { c = c_; }; + void NotifyPageChanged(StampsModel * sender); + void NotifyStampsListChanged(StampsModel * sender); + virtual ~StampsView(); +}; + +#endif /* STAMPSVIEW_H_ */ |
