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 | |
| 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')
| -rw-r--r-- | src/Config.h | 2 | ||||
| -rw-r--r-- | src/client/Client.cpp | 112 | ||||
| -rw-r--r-- | src/client/Client.h | 12 | ||||
| -rw-r--r-- | src/game/GameController.cpp | 38 | ||||
| -rw-r--r-- | src/game/GameController.h | 6 | ||||
| -rw-r--r-- | src/game/GameModel.cpp | 54 | ||||
| -rw-r--r-- | src/game/GameModel.h | 11 | ||||
| -rw-r--r-- | src/game/GameView.cpp | 145 | ||||
| -rw-r--r-- | src/game/GameView.h | 6 | ||||
| -rw-r--r-- | src/interface/SaveButton.cpp | 41 | ||||
| -rw-r--r-- | src/simulation/Simulation.cpp | 5 | ||||
| -rw-r--r-- | src/simulation/Simulation.h | 1 | ||||
| -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 |
19 files changed, 794 insertions, 61 deletions
diff --git a/src/Config.h b/src/Config.h index 780dd89..d6be81a 100644 --- a/src/Config.h +++ b/src/Config.h @@ -51,6 +51,8 @@ #define LOCAL_SAVE_DIR "Saves" +#define STAMPS_DIR "stamps" + #define APPDATA_SUBDIR "\\HardWIRED" //Number of unique thumbnails to have in cache at one time diff --git a/src/client/Client.cpp b/src/client/Client.cpp index e85d3e6..b472518 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -3,8 +3,16 @@ #include <sstream> #include <string> #include <vector> +#include <iomanip> #include <time.h> +#ifdef WIN32 +#include <direct.h> +#else +#include <sys/stat.h> +#include <unistd.h> +#endif + #include "Config.h" #include "Client.h" #include "MD5.h" @@ -76,6 +84,21 @@ Client::Client(): { http_init(NULL); } + + //Read stamps library + std::ifstream stampsLib; + stampsLib.open(STAMPS_DIR PATH_SEP "stamps.def", ios::binary); + while(true) + { + char data[11]; + memset(data, 0, 11); + if(stampsLib.readsome(data, 10)!=10) + break; + if(!data[0]) + break; + stampIDs.push_back(data); + } + stampsLib.close(); } Client::~Client() @@ -169,6 +192,95 @@ RequestStatus Client::UploadSave(Save * save) return RequestFailure; } +Save * Client::GetStamp(string stampID) +{ + std::ifstream stampFile; + stampFile.open(string(STAMPS_DIR PATH_SEP + stampID + ".stm").c_str(), ios::binary); + if(stampFile.is_open()) + { + stampFile.seekg(0, ios::end); + size_t fileSize = stampFile.tellg(); + stampFile.seekg(0); + + unsigned char * tempData = (unsigned char *)malloc(fileSize); + stampFile.read((char *)tempData, fileSize); + stampFile.close(); + + + Save * tempSave = new Save(0, 0, 0, 0, "", ""); + tempSave->SetData(tempData, fileSize); + return tempSave; + } + else + { + return NULL; + } +} + +void Client::DeleteStamp(string stampID) +{ + return; +} + +string Client::AddStamp(Save * saveData) +{ + unsigned t=(unsigned)time(NULL); + if (lastStampTime!=t) + { + lastStampTime=t; + lastStampName=0; + } + else + lastStampName++; + std::stringstream saveID; + //sprintf(saveID, "%08x%02x", lastStampTime, lastStampName); + saveID + << std::setw(8) << std::setfill('0') << std::hex << lastStampTime + << std::setw(2) << std::setfill('0') << std::hex << lastStampName; + +#ifdef WIN32 + _mkdir(STAMPS_DIR); +#else + mkdir(STAMPS_DIR, 0755); +#endif + + std::ofstream stampStream; + stampStream.open(string(STAMPS_DIR PATH_SEP + saveID.str()+".stm").c_str(), ios::binary); + stampStream.write((const char *)saveData->data, saveData->dataLength); + stampStream.close(); + + stampIDs.push_back(saveID.str()); + + updateStamps(); + + return saveID.str(); +} + +void Client::updateStamps() +{ + +#ifdef WIN32 + _mkdir(STAMPS_DIR); +#else + mkdir(STAMPS_DIR, 0755); +#endif + + std::ofstream stampsStream; + stampsStream.open(string(STAMPS_DIR PATH_SEP "stamps.def").c_str(), ios::binary); + for(int i = 0; i < stampIDs.size(); i++) + { + stampsStream.write(stampIDs[i].c_str(), 10); + } + stampsStream.write("\0", 1); + stampsStream.close(); + return; +} + +vector<string> Client::GetStamps() +{ + return stampIDs; +} + RequestStatus Client::ExecVote(int saveID, int direction) { lastError = ""; diff --git a/src/client/Client.h b/src/client/Client.h index 55d813f..c8f5764 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -29,6 +29,10 @@ class Client: public Singleton<Client> { private: std::string lastError; + vector<string> stampIDs; + int lastStampTime; + int lastStampName; + //Auth session User authUser; @@ -39,6 +43,7 @@ private: int activeThumbRequestTimes[IMGCONNS]; int activeThumbRequestCompleteTimes[IMGCONNS]; std::string activeThumbRequestIDs[IMGCONNS]; + void updateStamps(); public: //Config file handle json::Object configDocument; @@ -49,6 +54,11 @@ public: RequestStatus ExecVote(int saveID, int direction); RequestStatus UploadSave(Save * save); + Save * GetStamp(string stampID); + void DeleteStamp(string stampID); + string AddStamp(Save * saveData); + vector<string> GetStamps(); + unsigned char * GetSaveData(int saveID, int saveDate, int & dataLength); LoginStatus Login(string username, string password, User & user); void ClearThumbnailRequests(); @@ -59,7 +69,7 @@ public: Save * GetSave(int saveID, int saveDate); void SetAuthUser(User user); User GetAuthUser(); - std::vector<string> * RemoveTag(int saveID, string tag); + std::vector<string> * RemoveTag(int saveID, string tag); //TODO RequestStatus std::vector<string> * AddTag(int saveID, string tag); std::string GetLastError() { return lastError; diff --git a/src/game/GameController.cpp b/src/game/GameController.cpp index 7b08716..3408ec5 100644 --- a/src/game/GameController.cpp +++ b/src/game/GameController.cpp @@ -86,6 +86,20 @@ public: } }; +class GameController::StampsCallback: public ControllerCallback +{ + GameController * cc; +public: + StampsCallback(GameController * cc_) { cc = cc_; } + virtual void ControllerExit() + { + if(cc->stamps->GetStamp()) + { + cc->gameModel->SetStamp(cc->stamps->GetStamp()); + } + } +}; + GameController::GameController(): search(NULL), renderOptions(NULL), @@ -140,6 +154,24 @@ GameView * GameController::GetView() return gameView; } +void GameController::PlaceStamp(ui::Point position) +{ + if(gameModel->GetStamp()) + { + gameModel->GetSimulation()->Load(position.X, position.Y, gameModel->GetStamp()->data, gameModel->GetStamp()->dataLength); + gameModel->SetPaused(gameModel->GetPaused()); + } +} + +void GameController::PlaceClipboard(ui::Point position) +{ + if(gameModel->GetClipboard()) + { + gameModel->GetSimulation()->Load(position.X, position.Y, gameModel->GetClipboard()->data, gameModel->GetClipboard()->dataLength); + gameModel->SetPaused(gameModel->GetPaused()); + } +} + void GameController::AdjustBrushSize(int direction) { ui::Point newSize = gameModel->GetBrush()->GetRadius() + ui::Point(direction, direction); @@ -395,6 +427,12 @@ void GameController::OpenTags() } } +void GameController::OpenStamps() +{ + stamps = new StampsController(new StampsCallback(this)); + ui::Engine::Ref().ShowWindow(stamps->GetView()); +} + void GameController::OpenDisplayOptions() { //TODO: Implement diff --git a/src/game/GameController.h b/src/game/GameController.h index cf26f51..cefca8e 100644 --- a/src/game/GameController.h +++ b/src/game/GameController.h @@ -12,6 +12,7 @@ #include "ssave/SSaveController.h" #include "tags/TagsController.h" #include "console/ConsoleController.h" +#include "stamps/StampsController.h" //#include "cat/TPTScriptInterface.h" #include "cat/LuaScriptInterface.h" #include "Menu.h" @@ -34,6 +35,7 @@ private: SSaveController * ssave; ConsoleController * console; TagsController * tagsWindow; + StampsController * stamps; CommandInterface * commandInterface; public: class LoginCallback; @@ -41,6 +43,7 @@ public: class RenderCallback; class SSaveCallback; class TagsCallback; + class StampsCallback; GameController(); ~GameController(); GameView * GetView(); @@ -68,6 +71,9 @@ public: void OpenDisplayOptions(); void OpenRenderOptions(); void OpenSaveWindow(); + void OpenStamps(); + void PlaceStamp(ui::Point position); + void PlaceClipboard(ui::Point position); void ClearSim(); void ReloadSim(); void Vote(int direction); diff --git a/src/game/GameModel.cpp b/src/game/GameModel.cpp index 0106dd7..2a9b69e 100644 --- a/src/game/GameModel.cpp +++ b/src/game/GameModel.cpp @@ -18,7 +18,8 @@ GameModel::GameModel(): currentUser(0, ""), currentSave(NULL), colourSelector(false), - clipboardData(NULL) + clipboard(NULL), + stamp(NULL) { sim = new Simulation(); ren = new Renderer(ui::Engine::Ref().g, sim); @@ -158,6 +159,10 @@ GameModel::~GameModel() } delete sim; delete ren; + if(clipboard) + delete clipboard; + if(stamp) + delete stamp; if(activeTools) delete activeTools; } @@ -414,23 +419,42 @@ void GameModel::ClearSimulation() void GameModel::AddStamp(unsigned char * saveData, int saveSize) { - //Do nothing - - //die alone + Save * tempSave = new Save(0, 0, 0, 0, "", ""); + tempSave->SetData(saveData, saveSize); + Client::Ref().AddStamp(tempSave); + delete tempSave; } void GameModel::SetClipboard(unsigned char * saveData, int saveSize) { - if(clipboardData) - free(clipboardData); - clipboardData = saveData; - clipboardSize = saveSize; + if(clipboard) + delete clipboard; + clipboard = new Save(0, 0, 0, 0, "", ""); + clipboard->SetData(saveData, saveSize); + notifyClipboardChanged(); +} + +Save * GameModel::GetClipboard() +{ + return clipboard; } -unsigned char * GameModel::GetClipboard(int & saveSize) +Save * GameModel::GetStamp() { - saveSize = clipboardSize; - return clipboardData; + return stamp; +} + +void GameModel::SetStamp(Save * newStamp) +{ + if(stamp) + delete stamp; + if(newStamp) + { + stamp = new Save(*newStamp); + } + else + stamp = NULL; + notifyStampChanged(); } void GameModel::notifyColourSelectorColourChanged() @@ -537,6 +561,14 @@ void GameModel::notifyZoomChanged() } } +void GameModel::notifyStampChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyStampChanged(this); + } +} + void GameModel::notifyClipboardChanged() { for(int i = 0; i < observers.size(); i++) diff --git a/src/game/GameModel.h b/src/game/GameModel.h index 14e42ac..7b953c9 100644 --- a/src/game/GameModel.h +++ b/src/game/GameModel.h @@ -31,8 +31,10 @@ public: class GameModel { private: - int clipboardSize; - unsigned char * clipboardData; + //int clipboardSize; + //unsigned char * clipboardData; + Save * stamp; + Save * clipboard; vector<GameView*> observers; vector<Tool*> toolList; vector<Menu*> menuList; @@ -59,6 +61,7 @@ private: void notifyUserChanged(); void notifyZoomChanged(); void notifyClipboardChanged(); + void notifyStampChanged(); void notifyColourSelectorColourChanged(); void notifyColourSelectorVisibilityChanged(); public: @@ -104,9 +107,11 @@ public: ui::Point GetZoomPosition(); void SetZoomWindowPosition(ui::Point position); ui::Point GetZoomWindowPosition(); + void SetStamp(Save * newStamp); void AddStamp(unsigned char * saveData, int saveSize); void SetClipboard(unsigned char * saveData, int saveSize); - unsigned char * GetClipboard(int & saveSize); + Save * GetClipboard(); + Save * GetStamp(); }; #endif // GAMEMODEL_H diff --git a/src/game/GameView.cpp b/src/game/GameView.cpp index 7faeaf1..8c1c14a 100644 --- a/src/game/GameView.cpp +++ b/src/game/GameView.cpp @@ -8,6 +8,7 @@ #include "interface/Colour.h" #include "interface/Keys.h" #include "interface/Slider.h" +#include "search/Thumbnail.h" GameView::GameView(): ui::Window(ui::Point(0, 0), ui::Point(XRES+BARSIZE, YRES+MENUSIZE)), @@ -25,7 +26,9 @@ GameView::GameView(): drawModeReset(false), selectMode(SelectNone), selectPoint1(0, 0), - selectPoint2(0, 0) + selectPoint2(0, 0), + stampThumb(NULL), + clipboardThumb(NULL) { int currentX = 1; //Set up UI @@ -467,6 +470,8 @@ void GameView::OnMouseMove(int x, int y, int dx, int dy) { if(selectMode!=SelectNone) { + if(selectMode==PlaceStamp || selectMode==PlaceClipboard) + selectPoint1 = ui::Point(x, y); if(selectPoint1.X!=-1) selectPoint2 = ui::Point(x, y); return; @@ -514,16 +519,46 @@ void GameView::OnMouseUp(int x, int y, unsigned button) { if(selectMode!=SelectNone) { - int x2 = (selectPoint1.X>selectPoint2.X)?selectPoint1.X:selectPoint2.X; - int y2 = (selectPoint1.Y>selectPoint2.Y)?selectPoint1.Y:selectPoint2.Y; - int x1 = (selectPoint2.X<selectPoint1.X)?selectPoint2.X:selectPoint1.X; - int y1 = (selectPoint2.Y<selectPoint1.Y)?selectPoint2.Y:selectPoint1.Y; - if(button==BUTTON_LEFT && x2-x1>0 && y2-y1>0) + if(button==BUTTON_LEFT) { - if(selectMode==SelectCopy) - c->CopyRegion(ui::Point(x1, y1), ui::Point(x2, y2)); - else if(selectMode==SelectStamp) - c->StampRegion(ui::Point(x1, y1), ui::Point(x2, y2)); + if(selectMode==PlaceStamp || selectMode==PlaceClipboard) + { + Thumbnail * tempThumb = selectMode==PlaceStamp?stampThumb:clipboardThumb; + if(tempThumb) + { + int thumbX = selectPoint2.X - (tempThumb->Size.X/2); + int thumbY = selectPoint2.Y - (tempThumb->Size.Y/2); + + if(thumbX<0) + thumbX = 0; + if(thumbX+(tempThumb->Size.X)>=XRES) + thumbX = XRES-tempThumb->Size.X; + + if(thumbY<0) + thumbY = 0; + if(thumbY+(tempThumb->Size.Y)>=YRES) + thumbY = YRES-tempThumb->Size.Y; + + if(selectMode==PlaceStamp) + c->PlaceStamp(ui::Point(thumbX, thumbY)); + if(selectMode==PlaceClipboard) + c->PlaceClipboard(ui::Point(thumbX, thumbY)); + } + } + else + { + int x2 = (selectPoint1.X>selectPoint2.X)?selectPoint1.X:selectPoint2.X; + int y2 = (selectPoint1.Y>selectPoint2.Y)?selectPoint1.Y:selectPoint2.Y; + int x1 = (selectPoint2.X<selectPoint1.X)?selectPoint2.X:selectPoint1.X; + int y1 = (selectPoint2.Y<selectPoint1.Y)?selectPoint2.Y:selectPoint1.Y; + if(x2-x1>0 && y2-y1>0) + { + if(selectMode==SelectCopy) + c->CopyRegion(ui::Point(x1, y1), ui::Point(x2, y2)); + else if(selectMode==SelectStamp) + c->StampRegion(ui::Point(x1, y1), ui::Point(x2, y2)); + } + } } selectMode = SelectNone; return; @@ -643,6 +678,20 @@ void GameView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool selectPoint1 = ui::Point(-1, -1); } break; + case 'v': + if(ctrl && clipboardThumb) + { + selectMode = PlaceClipboard; + selectPoint2 = ui::Point(-1, -1); + selectPoint1 = selectPoint2; + } + break; + case 'l': + selectMode = PlaceStamp; + selectPoint2 = ui::Point(-1, -1); + selectPoint1 = selectPoint2; + c->OpenStamps(); + break; } } @@ -694,7 +743,27 @@ void GameView::NotifyZoomChanged(GameModel * sender) void GameView::NotifyClipboardChanged(GameModel * sender) { - //Could use this to have a mini preview of the clipboard, meh + if(clipboardThumb) + delete clipboardThumb; + if(sender->GetClipboard()) + { + clipboardThumb = new Thumbnail(0, 0, (pixel*)malloc((256*256)*PIXELSIZE), ui::Point(256, 256)); + } + else + clipboardThumb = NULL; +} + + +void GameView::NotifyStampChanged(GameModel * sender) +{ + if(stampThumb) + delete stampThumb; + if(sender->GetStamp()) + { + stampThumb = new Thumbnail(0, 0, (pixel*)malloc((256*256)*PIXELSIZE), ui::Point(256, 256)); + } + else + stampThumb = NULL; } void GameView::changeColour() @@ -730,29 +799,53 @@ void GameView::OnDraw() if(selectMode!=SelectNone) { - if(selectPoint1.X==-1) + if(selectMode==PlaceStamp || selectMode==PlaceClipboard) { - g->fillrect(0, 0, XRES, YRES, 0, 0, 0, 100); + Thumbnail * tempThumb = selectMode==PlaceStamp?stampThumb:clipboardThumb; + if(tempThumb && selectPoint2.X!=-1) + { + int thumbX = selectPoint2.X - (tempThumb->Size.X/2); + int thumbY = selectPoint2.Y - (tempThumb->Size.Y/2); + + if(thumbX<0) + thumbX = 0; + if(thumbX+(tempThumb->Size.X)>=XRES) + thumbX = XRES-tempThumb->Size.X; + + if(thumbY<0) + thumbY = 0; + if(thumbY+(tempThumb->Size.Y)>=YRES) + thumbY = YRES-tempThumb->Size.Y; + + g->draw_image(tempThumb->Data, thumbX, thumbY, tempThumb->Size.X, tempThumb->Size.Y, 128); + } } else { - int x2 = (selectPoint1.X>selectPoint2.X)?selectPoint1.X:selectPoint2.X; - int y2 = (selectPoint1.Y>selectPoint2.Y)?selectPoint1.Y:selectPoint2.Y; - int x1 = (selectPoint2.X<selectPoint1.X)?selectPoint2.X:selectPoint1.X; - int y1 = (selectPoint2.Y<selectPoint1.Y)?selectPoint2.Y:selectPoint1.Y; + if(selectPoint1.X==-1) + { + g->fillrect(0, 0, XRES, YRES, 0, 0, 0, 100); + } + else + { + int x2 = (selectPoint1.X>selectPoint2.X)?selectPoint1.X:selectPoint2.X; + int y2 = (selectPoint1.Y>selectPoint2.Y)?selectPoint1.Y:selectPoint2.Y; + int x1 = (selectPoint2.X<selectPoint1.X)?selectPoint2.X:selectPoint1.X; + int y1 = (selectPoint2.Y<selectPoint1.Y)?selectPoint2.Y:selectPoint1.Y; - if(x2>XRES-1) - x2 = XRES-1; - if(y2>YRES-1) - y2 = YRES-1; + if(x2>XRES-1) + x2 = XRES-1; + if(y2>YRES-1) + y2 = YRES-1; - g->fillrect(0, 0, XRES, y1, 0, 0, 0, 100); - g->fillrect(0, y2, XRES, YRES-y2, 0, 0, 0, 100); + g->fillrect(0, 0, XRES, y1, 0, 0, 0, 100); + g->fillrect(0, y2, XRES, YRES-y2, 0, 0, 0, 100); - g->fillrect(0, y1-1, x1, (y2-y1)+2, 0, 0, 0, 100); - g->fillrect(x2, y1-1, XRES-x2, (y2-y1)+2, 0, 0, 0, 100); + g->fillrect(0, y1-1, x1, (y2-y1)+2, 0, 0, 0, 100); + g->fillrect(x2, y1-1, XRES-x2, (y2-y1)+2, 0, 0, 0, 100); - g->xor_rect(x1, y1, (x2-x1)+1, (y2-y1)+1); + g->xor_rect(x1, y1, (x2-x1)+1, (y2-y1)+1); + } } } } diff --git a/src/game/GameView.h b/src/game/GameView.h index 0b31182..1be2d96 100644 --- a/src/game/GameView.h +++ b/src/game/GameView.h @@ -21,7 +21,7 @@ enum DrawMode enum SelectMode { - SelectNone, SelectStamp, SelectCopy + SelectNone, SelectStamp, SelectCopy, PlaceClipboard, PlaceStamp }; class GameController; @@ -67,6 +67,9 @@ private: ui::Point selectPoint1; ui::Point selectPoint2; + Thumbnail * clipboardThumb; + Thumbnail * stampThumb; + void changeColour(); public: GameView(); @@ -84,6 +87,7 @@ public: void NotifyColourSelectorVisibilityChanged(GameModel * sender); void NotifyColourSelectorColourChanged(GameModel * sender); void NotifyClipboardChanged(GameModel * sender); + void NotifyStampChanged(GameModel * sender); virtual void OnMouseMove(int x, int y, int dx, int dy); virtual void OnMouseDown(int x, int y, unsigned button); virtual void OnMouseUp(int x, int y, unsigned button); diff --git a/src/interface/SaveButton.cpp b/src/interface/SaveButton.cpp index 9ed2e68..775e38b 100644 --- a/src/interface/SaveButton.cpp +++ b/src/interface/SaveButton.cpp @@ -51,28 +51,31 @@ void SaveButton::Tick(float dt) float scaleFactorY = 1.0f, scaleFactorX = 1.0f; if(!thumbnail) { - tempThumb = Client::Ref().GetThumbnail(save->GetID(), 0); - if(tempThumb) + if(save->GetID()) { - thumbnail = new Thumbnail(*tempThumb); //Store a local copy of the thumbnail - if(thumbnail->Data) + tempThumb = Client::Ref().GetThumbnail(save->GetID(), 0); + if(tempThumb) { - if(thumbnail->Size.Y > (Size.Y-25)) + thumbnail = new Thumbnail(*tempThumb); //Store a local copy of the thumbnail + if(thumbnail->Data) { - scaleFactorY = ((float)(Size.Y-25))/((float)thumbnail->Size.Y); - } - if(thumbnail->Size.X > Size.X-3) - { - scaleFactorX = ((float)Size.X-3)/((float)thumbnail->Size.X); - } - if(scaleFactorY < 1.0f || scaleFactorX < 1.0f) - { - float scaleFactor = scaleFactorY < scaleFactorX ? scaleFactorY : scaleFactorX; - pixel * thumbData = thumbnail->Data; - thumbnail->Data = Graphics::resample_img(thumbData, thumbnail->Size.X, thumbnail->Size.Y, thumbnail->Size.X * scaleFactor, thumbnail->Size.Y * scaleFactor); - thumbnail->Size.X *= scaleFactor; - thumbnail->Size.Y *= scaleFactor; - free(thumbData); + if(thumbnail->Size.Y > (Size.Y-25)) + { + scaleFactorY = ((float)(Size.Y-25))/((float)thumbnail->Size.Y); + } + if(thumbnail->Size.X > Size.X-3) + { + scaleFactorX = ((float)Size.X-3)/((float)thumbnail->Size.X); + } + if(scaleFactorY < 1.0f || scaleFactorX < 1.0f) + { + float scaleFactor = scaleFactorY < scaleFactorX ? scaleFactorY : scaleFactorX; + pixel * thumbData = thumbnail->Data; + thumbnail->Data = Graphics::resample_img(thumbData, thumbnail->Size.X, thumbnail->Size.Y, thumbnail->Size.X * scaleFactor, thumbnail->Size.Y * scaleFactor); + thumbnail->Size.X *= scaleFactor; + thumbnail->Size.Y *= scaleFactor; + free(thumbData); + } } } } diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp index ceaae07..ec17454 100644 --- a/src/simulation/Simulation.cpp +++ b/src/simulation/Simulation.cpp @@ -13,6 +13,11 @@ int Simulation::Load(unsigned char * data, int dataLength) return SaveLoader::LoadSave(data, dataLength, this, true, 0, 0); } +int Simulation::Load(int x, int y, unsigned char * data, int dataLength) +{ + return SaveLoader::LoadSave(data, dataLength, this, false, x, y); +} + unsigned char * Simulation::Save(int & dataLength) { return SaveLoader::BuildSave(dataLength, this, 0, 0, XRES, YRES); diff --git a/src/simulation/Simulation.h b/src/simulation/Simulation.h index b33b349..dd86953 100644 --- a/src/simulation/Simulation.h +++ b/src/simulation/Simulation.h @@ -207,6 +207,7 @@ public: int sandcolour_b; //TODO: Make a single variable int Load(unsigned char * data, int dataLength); + int Load(int x, int y, unsigned char * data, int dataLength); unsigned char * Save(int & dataLength); unsigned char * Save(int x1, int y1, int x2, int y2, int & dataLength); inline int is_blocking(int t, int x, int y); 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_ */ |
