diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2012-08-01 21:29:22 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2012-08-01 21:29:22 (GMT) |
| commit | cbefea85d717bd599fa0559f091b051e904d9e2f (patch) | |
| tree | e11849927aa88b270470c55d8b0fc052d34851e4 | |
| parent | 29ac6380ba649e30dc29771b2833a86f20c9dbfe (diff) | |
| download | powder-cbefea85d717bd599fa0559f091b051e904d9e2f.zip powder-cbefea85d717bd599fa0559f091b051e904d9e2f.tar.gz | |
Local Saving, Server Saving rewrite
26 files changed, 555 insertions, 362 deletions
diff --git a/src/Activity.h b/src/Activity.h new file mode 100644 index 0000000..0263467 --- /dev/null +++ b/src/Activity.h @@ -0,0 +1,42 @@ +#pragma once + +#include "interface/Window.h" + +class Activity +{ +public: + virtual void Exit() {} + virtual void Show() {} + virtual void Hide() {} + virtual ~Activity() {} +}; + +class WindowActivity: public ui::Window, public Activity +{ +public: + WindowActivity(ui::Point position, ui::Point size) : + ui::Window(position, size) + { + Show(); + } + virtual void Exit() + { + Hide(); + SelfDestruct(); + } + virtual void Show() + { + if(ui::Engine::Ref().GetWindow() != this) + { + ui::Engine::Ref().ShowWindow(this); + } + } + virtual void Hide() + { + if(ui::Engine::Ref().GetWindow() == this) + { + ui::Engine::Ref().CloseWindow(); + } + } + virtual ~WindowActivity() {} +};
\ No newline at end of file diff --git a/src/Controller.h b/src/Controller.h index 5d18f36..12748be 100644 --- a/src/Controller.h +++ b/src/Controller.h @@ -16,4 +16,12 @@ public: virtual ~ControllerCallback() {} }; +class Controller +{ +private: + virtual void Exit(); + virtual void Show(); + virtual void Hide(); +}; + #endif /* CONTROLLER_H_ */ diff --git a/src/client/Client.cpp b/src/client/Client.cpp index 7f26a4c..29f6a94 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -198,6 +198,64 @@ std::vector<std::string> Client::DirectorySearch(std::string directory, std::str return searchResults; } +void Client::WriteFile(std::vector<unsigned char> fileData, std::string filename) +{ + try + { + std::ofstream fileStream; + fileStream.open(string(filename).c_str(), ios::binary); + if(fileStream.is_open()) + { + fileStream.write((char*)&fileData[0], fileData.size()); + fileStream.close(); + } + } + catch (std::exception & e) + { + std::cerr << "WriteFile:" << e.what() << std::endl; + throw; + } +} + +bool Client::FileExists(std::string filename) +{ + bool exists = false; + try + { + std::ofstream fileStream; + fileStream.open(string(filename).c_str(), ios::binary); + if(fileStream.is_open()) + { + exists = true; + fileStream.close(); + } + } + catch (std::exception & e) + { + exists = false; + } + return exists; +} + +void Client::WriteFile(std::vector<char> fileData, std::string filename) +{ + try + { + std::ofstream fileStream; + fileStream.open(string(filename).c_str(), ios::binary); + if(fileStream.is_open()) + { + fileStream.write(&fileData[0], fileData.size()); + fileStream.close(); + } + } + catch (std::exception & e) + { + std::cerr << "WriteFile:" << e.what() << std::endl; + throw; + } +} + std::vector<unsigned char> Client::ReadFile(std::string filename) { try @@ -400,7 +458,7 @@ User Client::GetAuthUser() return authUser; } -RequestStatus Client::UploadSave(SaveInfo * save) +RequestStatus Client::UploadSave(SaveInfo & save) { lastError = ""; int gameDataLength; @@ -412,13 +470,14 @@ RequestStatus Client::UploadSave(SaveInfo * save) userIDStream << authUser.ID; if(authUser.ID) { - if(!save->GetGameSave()) + if(!save.GetGameSave()) { lastError = "Empty game save"; return RequestFailure; } + save.SetID(0); - gameData = save->GetGameSave()->Serialise(gameDataLength); + gameData = save.GetGameSave()->Serialise(gameDataLength); if(!gameData) { @@ -427,8 +486,8 @@ RequestStatus Client::UploadSave(SaveInfo * save) } char * postNames[] = { "Name", "Description", "Data:save.bin", "Publish", NULL }; - char * postDatas[] = { (char *)(save->name.c_str()), (char *)(save->Description.c_str()), gameData, (char *)(save->Published?"Public":"Private") }; - int postLengths[] = { save->name.length(), save->Description.length(), gameDataLength, save->Published?6:7 }; + char * postDatas[] = { (char *)(save.GetName().c_str()), (char *)(save.GetDescription().c_str()), gameData, (char *)(save.GetPublished()?"Public":"Private") }; + int postLengths[] = { save.GetName().length(), save.GetDescription().length(), gameDataLength, save.GetPublished()?6:7 }; //std::cout << postNames[0] << " " << postDatas[0] << " " << postLengths[0] << std::endl; data = http_multipart_post("http://" SERVER "/Save.api", postNames, postDatas, postLengths, (char *)(userIDStream.str().c_str()), NULL, (char *)(authUser.SessionID.c_str()), &dataStatus, &dataLength); } @@ -458,7 +517,7 @@ RequestStatus Client::UploadSave(SaveInfo * save) } else { - save->id = tempID; + save.SetID(tempID); } } free(data); diff --git a/src/client/Client.h b/src/client/Client.h index ccf62f3..453342a 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -88,11 +88,15 @@ public: std::vector<unsigned char> ReadFile(std::string filename); + void WriteFile(std::vector<unsigned char> fileData, std::string filename); + void WriteFile(std::vector<char> fileData, std::string filename); + bool FileExists(std::string filename); + void AddListener(ClientListener * listener); void RemoveListener(ClientListener * listener); RequestStatus ExecVote(int saveID, int direction); - RequestStatus UploadSave(SaveInfo * save); + RequestStatus UploadSave(SaveInfo & save); SaveFile * GetStamp(string stampID); void DeleteStamp(string stampID); diff --git a/src/client/GameSave.cpp b/src/client/GameSave.cpp index 96306ac..f615c6f 100644 --- a/src/client/GameSave.cpp +++ b/src/client/GameSave.cpp @@ -278,6 +278,15 @@ void GameSave::setSize(int newWidth, int newHeight) fanVelY[y] = &fanVelYPtr[y*blockWidth]; } +std::vector<char> GameSave::Serialise() +{ + int dataSize; + char * data = Serialise(dataSize); + std::vector<char> dataVect(data, data+dataSize); + delete data; + return dataVect; +} + char * GameSave::Serialise(int & dataSize) { return serialiseOPS(dataSize); diff --git a/src/client/GameSave.h b/src/client/GameSave.h index 5bd9e7a..2c86ea1 100644 --- a/src/client/GameSave.h +++ b/src/client/GameSave.h @@ -64,6 +64,7 @@ public: ~GameSave(); void setSize(int width, int height); char * Serialise(int & dataSize); + std::vector<char> Serialise(); void Transform(matrix2d transform, vector2d translate); void Expand(); diff --git a/src/client/SaveInfo.cpp b/src/client/SaveInfo.cpp index da1033f..1df74c7 100644 --- a/src/client/SaveInfo.cpp +++ b/src/client/SaveInfo.cpp @@ -45,6 +45,21 @@ string SaveInfo::GetName() { return name; } +void SaveInfo::SetDescription(string description) { + Description = description; +} +string SaveInfo::GetDescription() { + return Description; +} + +void SaveInfo::SetPublished(bool published) { + Published = published; +} + +bool SaveInfo::GetPublished() { + return Published; +} + void SaveInfo::SetVote(int vote) { this->vote = vote; diff --git a/src/client/SaveInfo.h b/src/client/SaveInfo.h index e1b013e..a54741a 100644 --- a/src/client/SaveInfo.h +++ b/src/client/SaveInfo.h @@ -44,6 +44,12 @@ public: void SetName(string name); string GetName(); + void SetDescription(string description); + string GetDescription(); + + void SetPublished(bool published); + bool GetPublished(); + void SetUserName(string userName); string GetUserName(); diff --git a/src/elementsearch/ElementSearchActivity.cpp b/src/elementsearch/ElementSearchActivity.cpp index 7a87995..1ee41e1 100644 --- a/src/elementsearch/ElementSearchActivity.cpp +++ b/src/elementsearch/ElementSearchActivity.cpp @@ -28,7 +28,7 @@ public: }; ElementSearchActivity::ElementSearchActivity(GameModel * gameModel, std::vector<Tool*> tools) : - Window(ui::Point(-1, -1), ui::Point(236, 302)), + WindowActivity(ui::Point(-1, -1), ui::Point(236, 302)), gameModel(gameModel), tools(tools), firstResult(NULL) @@ -154,15 +154,6 @@ void ElementSearchActivity::SetActiveTool(int selectionState, Tool * tool) Exit(); } -void ElementSearchActivity::Exit() -{ - if(ui::Engine::Ref().GetWindow() == this) - { - ui::Engine::Ref().CloseWindow(); - } - SelfDestruct(); -} - void ElementSearchActivity::OnDraw() { Graphics * g = ui::Engine::Ref().g; diff --git a/src/elementsearch/ElementSearchActivity.h b/src/elementsearch/ElementSearchActivity.h index 7ed7611..de9700d 100644 --- a/src/elementsearch/ElementSearchActivity.h +++ b/src/elementsearch/ElementSearchActivity.h @@ -10,6 +10,7 @@ #include <vector> #include <string> +#include "Activity.h" #include "interface/Window.h" #include "interface/Textbox.h" #include "game/ToolButton.h" @@ -18,7 +19,7 @@ class Tool; class GameModel; -class ElementSearchActivity: public ui::Window { +class ElementSearchActivity: public WindowActivity { Tool * firstResult; GameModel * gameModel; std::vector<Tool*> tools; @@ -29,7 +30,6 @@ public: class ToolAction; Tool * GetFirstResult() { return firstResult; } ElementSearchActivity(GameModel * gameModel, std::vector<Tool*> tools); - void Exit(); void SetActiveTool(int selectionState, Tool * tool); virtual ~ElementSearchActivity(); virtual void OnDraw(); diff --git a/src/filebrowser/FileBrowserActivity.cpp b/src/filebrowser/FileBrowserActivity.cpp index 4bb68fa..787afd3 100644 --- a/src/filebrowser/FileBrowserActivity.cpp +++ b/src/filebrowser/FileBrowserActivity.cpp @@ -105,12 +105,11 @@ public: }; FileBrowserActivity::FileBrowserActivity(std::string directory, FileSelectedCallback * callback): - ui::Window(ui::Point(-1, -1), ui::Point(450, 300)), + WindowActivity(ui::Point(-1, -1), ui::Point(450, 300)), callback(callback), directory(directory), totalFiles(0) { - ui::Engine::Ref().ShowWindow(this); ui::Label * titleLabel = new ui::Label(ui::Point(4, 5), ui::Point(Size.X-8, 18), "Save Browser"); titleLabel->SetTextColour(style::Colour::WarningTitle); @@ -216,12 +215,6 @@ void FileBrowserActivity::OnMouseDown(int x, int y, unsigned button) Exit(); } -void FileBrowserActivity::Exit() -{ - ui::Engine::Ref().CloseWindow(); - SelfDestruct(); -} - void FileBrowserActivity::NotifyError(Task * task) { diff --git a/src/filebrowser/FileBrowserActivity.h b/src/filebrowser/FileBrowserActivity.h index 3eb99d6..693a16d 100644 --- a/src/filebrowser/FileBrowserActivity.h +++ b/src/filebrowser/FileBrowserActivity.h @@ -2,6 +2,7 @@ #include <vector> #include <string> +#include "Activity.h" #include "interface/Window.h" #include "tasks/TaskListener.h" @@ -23,7 +24,7 @@ namespace ui } class LoadFilesTask; -class FileBrowserActivity: public ui::Window, public TaskListener +class FileBrowserActivity: public TaskListener, public WindowActivity { LoadFilesTask * loadFiles; FileSelectedCallback * callback; @@ -58,6 +59,4 @@ public: virtual void NotifyError(Task * task); virtual void NotifyProgress(Task * task); virtual void NotifyStatus(Task * task); - - void Exit(); };
\ No newline at end of file diff --git a/src/game/GameController.cpp b/src/game/GameController.cpp index eab5dcc..5cf7654 100644 --- a/src/game/GameController.cpp +++ b/src/game/GameController.cpp @@ -17,6 +17,8 @@ #include "update/UpdateActivity.h" #include "Notification.h" #include "filebrowser/FileBrowserActivity.h" +#include "save/LocalSaveActivity.h" +#include "save/ServerSaveActivity.h" using namespace std; @@ -76,22 +78,6 @@ public: } }; -class GameController::SSaveCallback: public ControllerCallback -{ - GameController * cc; -public: - SSaveCallback(GameController * cc_) { cc = cc_; } - virtual void ControllerExit() - { - if(cc->ssave->GetSaveUploaded()) - { - cc->gameModel->SetSave(new SaveInfo(*(cc->ssave->GetSave()))); - - } - //cc->gameModel->SetUser(cc->loginWindow->GetUser()); - } -}; - class GameController::TagsCallback: public ControllerCallback { GameController * cc; @@ -122,7 +108,6 @@ GameController::GameController(): search(NULL), renderOptions(NULL), loginWindow(NULL), - ssave(NULL), console(NULL), tagsWindow(NULL), options(NULL), @@ -537,7 +522,24 @@ void GameController::OpenSearch() void GameController::OpenLocalSaveWindow() { - + Simulation * sim = gameModel->GetSimulation(); + GameSave * gameSave = sim->Save(); + gameSave->paused = gameModel->GetPaused(); + gameSave->gravityMode = sim->gravityMode; + gameSave->airMode = sim->air->airMode; + gameSave->legacyEnable = sim->legacy_enable; + gameSave->waterEEnabled = sim->water_equal_test; + gameSave->gravityEnable = sim->grav->ngrav_enable; + if(!gameSave) + { + new ErrorMessage("Error", "Unable to build save."); + } + else + { + SaveFile tempSave(""); + tempSave.SetGameSave(gameSave); + new LocalSaveActivity(tempSave); + } } void GameController::LoadSaveFile(SaveFile * file) @@ -545,6 +547,12 @@ void GameController::LoadSaveFile(SaveFile * file) gameModel->SetSaveFile(file); } + +void GameController::LoadSave(SaveInfo * save) +{ + gameModel->SetSave(save); +} + void GameController::OpenLocalBrowse() { class LocalSaveOpenCallback: public FileSelectedCallback @@ -579,7 +587,7 @@ void GameController::OpenElementSearch() continue; toolList.insert(toolList.end(), menuToolList.begin(), menuToolList.end()); } - ui::Engine::Ref().ShowWindow(new ElementSearchActivity(gameModel, toolList)); + new ElementSearchActivity(gameModel, toolList); } void GameController::OpenTags() @@ -630,6 +638,17 @@ void GameController::OpenRenderOptions() void GameController::OpenSaveWindow() { + class SaveUploadedCallback: public ServerSaveActivity::SaveUploadedCallback + { + GameController * c; + public: + SaveUploadedCallback(GameController * _c): c(_c) {} + virtual ~SaveUploadedCallback() {}; + virtual void SaveUploaded(SaveInfo save) + { + c->LoadSave(&save); + } + }; if(gameModel->GetUser().ID) { Simulation * sim = gameModel->GetSimulation(); @@ -650,15 +669,14 @@ void GameController::OpenSaveWindow() { SaveInfo tempSave(*gameModel->GetSave()); tempSave.SetGameSave(gameSave); - ssave = new SSaveController(new SSaveCallback(this), tempSave); + new ServerSaveActivity(tempSave, new SaveUploadedCallback(this)); } else { SaveInfo tempSave(0, 0, 0, 0, gameModel->GetUser().Username, ""); tempSave.SetGameSave(gameSave); - ssave = new SSaveController(new SSaveCallback(this), tempSave); + new ServerSaveActivity(tempSave, new SaveUploadedCallback(this)); } - ui::Engine::Ref().ShowWindow(ssave->GetView()); } } else diff --git a/src/game/GameController.h b/src/game/GameController.h index a7e0776..08e8a3b 100644 --- a/src/game/GameController.h +++ b/src/game/GameController.h @@ -1,4 +1,4 @@ -#ifndef GAMECONTROLLER_H + #ifndef GAMECONTROLLER_H #define GAMECONTROLLER_H #include <queue> @@ -9,7 +9,6 @@ #include "search/SearchController.h" #include "render/RenderController.h" #include "login/LoginController.h" -#include "ssave/SSaveController.h" #include "tags/TagsController.h" #include "console/ConsoleController.h" #include "localbrowser/LocalBrowserController.h" @@ -36,7 +35,6 @@ private: SearchController * search; RenderController * renderOptions; LoginController * loginWindow; - SSaveController * ssave; ConsoleController * console; TagsController * tagsWindow; LocalBrowserController * localBrowser; @@ -87,6 +85,7 @@ public: void SetColour(ui::Colour colour); void SetToolStrength(float value); void LoadSaveFile(SaveFile * file); + void LoadSave(SaveInfo * save); void OpenSearch(); void OpenLogin(); void OpenTags(); diff --git a/src/interface/Textbox.cpp b/src/interface/Textbox.cpp index d53ebeb..e4a1261 100644 --- a/src/interface/Textbox.cpp +++ b/src/interface/Textbox.cpp @@ -439,7 +439,7 @@ void Textbox::Draw(const Point& screenPos) if(IsFocused()) { if(border) g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, 255, 255, 255, 255); - g->draw_line(screenPos.X+textPosition.X+cursorPositionX, screenPos.Y-2+textPosition.Y+cursorPositionY, screenPos.X+textPosition.X+cursorPositionX, screenPos.Y+10+textPosition.Y+cursorPositionY, 255, 255, 255, 255); + g->draw_line(screenPos.X+textPosition.X+cursorPositionX, screenPos.Y-2+textPosition.Y+cursorPositionY, screenPos.X+textPosition.X+cursorPositionX, screenPos.Y+9+textPosition.Y+cursorPositionY, 255, 255, 255, 255); } else { diff --git a/src/save/LocalSaveActivity.cpp b/src/save/LocalSaveActivity.cpp new file mode 100644 index 0000000..2c7d5b6 --- /dev/null +++ b/src/save/LocalSaveActivity.cpp @@ -0,0 +1,130 @@ +#include "LocalSaveActivity.h" +#include "interface/Label.h" +#include "interface/Textbox.h" +#include "interface/Button.h" +#include "search/Thumbnail.h" +#include "client/ThumbnailBroker.h" +#include "dialogues/ErrorMessage.h" +#include "dialogues/ConfirmPrompt.h" +#include "client/Client.h" +#include "Style.h" + +class LocalSaveActivity::CancelAction: public ui::ButtonAction +{ + LocalSaveActivity * a; +public: + CancelAction(LocalSaveActivity * a) : a(a) {} + virtual void ActionCallback(ui::Button * sender) + { + a->Exit(); + } +}; + +class LocalSaveActivity::SaveAction: public ui::ButtonAction +{ + LocalSaveActivity * a; +public: + SaveAction(LocalSaveActivity * a) : a(a) {} + virtual void ActionCallback(ui::Button * sender) + { + a->Save(); + } +}; + +LocalSaveActivity::LocalSaveActivity(SaveFile save) : + WindowActivity(ui::Point(-1, -1), ui::Point(220, 200)), + thumbnail(NULL), + save(save) +{ + ui::Label * titleLabel = new ui::Label(ui::Point(4, 5), ui::Point(Size.X-8, 16), "Save to computer:"); + titleLabel->SetTextColour(style::Colour::InformationTitle); + titleLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + titleLabel->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + AddComponent(titleLabel); + + filenameField = new ui::Textbox(ui::Point(8, 25), ui::Point(Size.X-16, 16), save.GetDisplayName(), "[filename]"); + filenameField->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + filenameField->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + AddComponent(filenameField); + + ui::Button * cancelButton = new ui::Button(ui::Point(0, Size.Y-16), ui::Point(Size.X-50, 16), "Cancel"); + cancelButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + cancelButton->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + cancelButton->Appearance.BorderInactive = ui::Colour(200, 200, 200); + cancelButton->SetActionCallback(new CancelAction(this)); + AddComponent(cancelButton); + + ui::Button * okayButton = new ui::Button(ui::Point(Size.X-76, Size.Y-16), ui::Point(76, 16), "Save"); + okayButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + okayButton->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + okayButton->Appearance.TextInactive = style::Colour::InformationTitle; + okayButton->SetActionCallback(new SaveAction(this)); + AddComponent(okayButton); + + if(save.GetGameSave()) + ThumbnailBroker::Ref().RenderThumbnail(save.GetGameSave(), Size.X-16, -1, this); +} + +void LocalSaveActivity::Save() +{ + class FileOverwriteConfirmation: public ConfirmDialogueCallback { + public: + LocalSaveActivity * a; + std::string filename; + FileOverwriteConfirmation(LocalSaveActivity * a, std::string finalFilename) : a(a), filename(finalFilename) {} + virtual void ConfirmCallback(ConfirmPrompt::DialogueResult result) { + if (result == ConfirmPrompt::ResultOkay) + { + a->saveWrite(filename); + a->Exit(); + } + } + virtual ~FileOverwriteConfirmation() { } + }; + + if(filenameField->GetText().length()) + { + std::string finalFilename = std::string(LOCAL_SAVE_DIR) + std::string(PATH_SEP) + filenameField->GetText() + ".cps"; + if(Client::Ref().FileExists(finalFilename)) + { + new ConfirmPrompt("Overwrite file", "Are you sure you wish to overwrite\n"+finalFilename, new FileOverwriteConfirmation(this, finalFilename)); + } + else + { + saveWrite(finalFilename); + Exit(); + } + } + else + { + new ErrorMessage("Error", "You must specify a filename."); + } +} + +void LocalSaveActivity::saveWrite(std::string finalFilename) +{ + Client::Ref().WriteFile(save.GetGameSave()->Serialise(), finalFilename); +} + +void LocalSaveActivity::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); + + if(thumbnail) + { + g->draw_image(thumbnail->Data, Position.X+(Size.X-thumbnail->Size.X)/2, Position.Y+45, thumbnail->Size.X, thumbnail->Size.Y, 255); + g->drawrect(Position.X+(Size.X-thumbnail->Size.X)/2, Position.Y+45, thumbnail->Size.X, thumbnail->Size.Y, 180, 180, 180, 255); + } +} + +void LocalSaveActivity::OnThumbnailReady(Thumbnail * thumbnail) +{ + this->thumbnail = thumbnail; +} + +LocalSaveActivity::~LocalSaveActivity() +{ + +}
\ No newline at end of file diff --git a/src/save/LocalSaveActivity.h b/src/save/LocalSaveActivity.h new file mode 100644 index 0000000..b5d19f3 --- /dev/null +++ b/src/save/LocalSaveActivity.h @@ -0,0 +1,30 @@ +#pragma once + +#include "Activity.h" +#include "client/SaveFile.h" +#include "client/ThumbnailListener.h" + +namespace ui +{ + class Textbox; +} + +class Thumbnail; + +class LocalSaveActivity: public WindowActivity, public ThumbnailListener +{ + SaveFile save; + Thumbnail * thumbnail; + ui::Textbox * filenameField; + class CancelAction; + class SaveAction; + friend class CancelAction; + friend class SaveAction; +public: + LocalSaveActivity(SaveFile save); + void saveWrite(std::string finalFilename); + virtual void Save(); + virtual void OnDraw(); + virtual void OnThumbnailReady(Thumbnail * thumbnail); + virtual ~LocalSaveActivity(); +};
\ No newline at end of file diff --git a/src/ssave/SSaveView.h b/src/save/SSaveView.h index f8e051d..f8e051d 100644 --- a/src/ssave/SSaveView.h +++ b/src/save/SSaveView.h diff --git a/src/save/ServerSaveActivity.cpp b/src/save/ServerSaveActivity.cpp new file mode 100644 index 0000000..b90d0e6 --- /dev/null +++ b/src/save/ServerSaveActivity.cpp @@ -0,0 +1,149 @@ +#include "ServerSaveActivity.h" +#include "interface/Label.h" +#include "interface/Textbox.h" +#include "interface/Button.h" +#include "interface/Checkbox.h" +#include "search/Thumbnail.h" +#include "client/ThumbnailBroker.h" +#include "dialogues/ErrorMessage.h" +#include "dialogues/ConfirmPrompt.h" +#include "client/Client.h" +#include "Style.h" + +class ServerSaveActivity::CancelAction: public ui::ButtonAction +{ + ServerSaveActivity * a; +public: + CancelAction(ServerSaveActivity * a) : a(a) {} + virtual void ActionCallback(ui::Button * sender) + { + a->Exit(); + } +}; + +class ServerSaveActivity::SaveAction: public ui::ButtonAction +{ + ServerSaveActivity * a; +public: + SaveAction(ServerSaveActivity * a) : a(a) {} + virtual void ActionCallback(ui::Button * sender) + { + a->Save(); + } +}; + +ServerSaveActivity::ServerSaveActivity(SaveInfo save, ServerSaveActivity::SaveUploadedCallback * callback) : + WindowActivity(ui::Point(-1, -1), ui::Point(440, 200)), + thumbnail(NULL), + save(save), + callback(callback) +{ + ui::Label * titleLabel = new ui::Label(ui::Point(4, 5), ui::Point((Size.X/2)-8, 16), "Save to server:"); + titleLabel->SetTextColour(style::Colour::InformationTitle); + titleLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + titleLabel->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + AddComponent(titleLabel); + + ui::Label * previewLabel = new ui::Label(ui::Point((Size.X/2)+4, 5), ui::Point((Size.X/2)-8, 16), "Preview:"); + previewLabel->SetTextColour(style::Colour::InformationTitle); + previewLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + previewLabel->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + AddComponent(previewLabel); + + nameField = new ui::Textbox(ui::Point(8, 25), ui::Point((Size.X/2)-16, 16), save.GetName(), "[save name]"); + nameField->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + nameField->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + AddComponent(nameField); + + descriptionField = new ui::Textbox(ui::Point(8, 65), ui::Point((Size.X/2)-16, Size.Y-(65+16+4)), save.GetDescription(), "[save description]"); + descriptionField->SetMultiline(true); + descriptionField->Appearance.VerticalAlign = ui::Appearance::AlignTop; + descriptionField->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + AddComponent(descriptionField); + + publishedCheckbox = new ui::Checkbox(ui::Point(8, 45), ui::Point((Size.X/2)-16, 16), "Publish"); + publishedCheckbox->SetChecked(save.GetPublished()); + AddComponent(publishedCheckbox); + + ui::Button * cancelButton = new ui::Button(ui::Point(0, Size.Y-16), ui::Point((Size.X/2)-50, 16), "Cancel"); + cancelButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + cancelButton->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + cancelButton->Appearance.BorderInactive = ui::Colour(200, 200, 200); + cancelButton->SetActionCallback(new CancelAction(this)); + AddComponent(cancelButton); + + ui::Button * okayButton = new ui::Button(ui::Point((Size.X/2)-76, Size.Y-16), ui::Point(76, 16), "Save"); + okayButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + okayButton->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + okayButton->Appearance.TextInactive = style::Colour::InformationTitle; + okayButton->SetActionCallback(new SaveAction(this)); + AddComponent(okayButton); + + if(save.GetGameSave()) + ThumbnailBroker::Ref().RenderThumbnail(save.GetGameSave(), (Size.X/2)-16, -1, this); +} + +void ServerSaveActivity::Save() +{ + if(nameField->GetText().length()) + { + saveUpload(); + Exit(); + } + else + { + new ErrorMessage("Error", "You must specify a save name."); + } +} + +void ServerSaveActivity::saveUpload() +{ + save.SetName(nameField->GetText()); + save.SetDescription(descriptionField->GetText()); + save.SetPublished(publishedCheckbox->GetChecked()); + save.SetUserName(Client::Ref().GetAuthUser().Username); + save.SetID(0); + + if(Client::Ref().UploadSave(save) != RequestOkay) + { + new ErrorMessage("Error", "Upload failed with error:\n"+Client::Ref().GetLastError()); + } + else if(callback) + { + callback->SaveUploaded(save); + } +} + +void ServerSaveActivity::Exit() +{ + if(callback) + { + delete callback; + callback = NULL; + } + WindowActivity::Exit(); +} + +void ServerSaveActivity::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->draw_line(Position.X+(Size.X/2)-1, Position.Y, Position.X+(Size.X/2)-1, Position.Y+Size.Y-1, 255, 255, 255, 255); + + if(thumbnail) + { + g->draw_image(thumbnail->Data, Position.X+(Size.X/2)+((Size.X/2)-thumbnail->Size.X)/2, Position.Y+25, thumbnail->Size.X, thumbnail->Size.Y, 255); + g->drawrect(Position.X+(Size.X/2)+((Size.X/2)-thumbnail->Size.X)/2, Position.Y+25, thumbnail->Size.X, thumbnail->Size.Y, 180, 180, 180, 255); + } +} + +void ServerSaveActivity::OnThumbnailReady(Thumbnail * thumbnail) +{ + this->thumbnail = thumbnail; +} + +ServerSaveActivity::~ServerSaveActivity() +{ + +}
\ No newline at end of file diff --git a/src/save/ServerSaveActivity.h b/src/save/ServerSaveActivity.h new file mode 100644 index 0000000..2a383f3 --- /dev/null +++ b/src/save/ServerSaveActivity.h @@ -0,0 +1,42 @@ +#pragma once + +#include "Activity.h" +#include "client/SaveInfo.h" +#include "client/ThumbnailListener.h" + +namespace ui +{ + class Textbox; + class Checkbox; +} + +class Thumbnail; +class ServerSaveActivity: public WindowActivity, public ThumbnailListener +{ +public: + class SaveUploadedCallback + { + public: + SaveUploadedCallback() {} + virtual ~SaveUploadedCallback() {} + virtual void SaveUploaded(SaveInfo save) {} + }; + ServerSaveActivity(SaveInfo save, SaveUploadedCallback * callback); + void saveUpload(); + virtual void Save(); + virtual void Exit(); + virtual void OnDraw(); + virtual void OnThumbnailReady(Thumbnail * thumbnail); + virtual ~ServerSaveActivity(); +protected: + SaveUploadedCallback * callback; + SaveInfo save; + Thumbnail * thumbnail; + ui::Textbox * nameField; + ui::Textbox * descriptionField; + ui::Checkbox * publishedCheckbox; + class CancelAction; + class SaveAction; + friend class CancelAction; + friend class SaveAction; +};
\ No newline at end of file diff --git a/src/search/Thumbnail.cpp b/src/search/Thumbnail.cpp index 60c6b2b..e1bc6f8 100644 --- a/src/search/Thumbnail.cpp +++ b/src/search/Thumbnail.cpp @@ -58,6 +58,10 @@ void Thumbnail::Resize(ui::Point newSize) { scaleFactorX = float(newSize.X)/((float)Size.X); } + if(newSize.X == -1) + scaleFactorX = scaleFactorY; + if(newSize.Y == -1) + scaleFactorY = scaleFactorX; if(scaleFactorY < 1.0f || scaleFactorX < 1.0f) { float scaleFactor = scaleFactorY < scaleFactorX ? scaleFactorY : scaleFactorX; diff --git a/src/ssave/SSaveController.cpp b/src/ssave/SSaveController.cpp deleted file mode 100644 index 5f37933..0000000 --- a/src/ssave/SSaveController.cpp +++ /dev/null @@ -1,59 +0,0 @@ -/* - * SSaveController.cpp - * - * Created on: Jan 29, 2012 - * Author: Simon - */ - -#include "SSaveController.h" - -SSaveController::SSaveController(ControllerCallback * callback, SaveInfo save): - HasExited(false) -{ - ssaveView = new SSaveView(); - ssaveView->AttachController(this); - ssaveModel = new SSaveModel(); - ssaveModel->AddObserver(ssaveView); - ssaveModel->SetSave(new SaveInfo(save)); - - this->callback = callback; -} - -void SSaveController::UploadSave(std::string saveName, std::string saveDescription, bool publish) -{ - ssaveModel->UploadSave(saveName, saveDescription, publish); -} - -SaveInfo * SSaveController::GetSave() -{ - return ssaveModel->GetSave(); -} - -bool SSaveController::GetSaveUploaded() -{ - return ssaveModel->GetSaveUploaded(); -} - -void SSaveController::Update() -{ - ssaveModel->Update(); -} - -void SSaveController::Exit() -{ - if(ui::Engine::Ref().GetWindow() == ssaveView) - ui::Engine::Ref().CloseWindow(); - if(callback) - callback->ControllerExit(); - HasExited = true; -} - -SSaveController::~SSaveController() { - if(ui::Engine::Ref().GetWindow() == ssaveView) - ui::Engine::Ref().CloseWindow(); - delete ssaveModel; - delete ssaveView; - if(callback) - delete callback; -} - diff --git a/src/ssave/SSaveController.h b/src/ssave/SSaveController.h deleted file mode 100644 index b8010e0..0000000 --- a/src/ssave/SSaveController.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * SSaveController.h - * - * Created on: Jan 29, 2012 - * Author: Simon - */ - -#ifndef SSAVECONTROLLER_H_ -#define SSAVECONTROLLER_H_ - -#include "SSaveModel.h" -#include "SSaveView.h" -#include "Controller.h" -#include "client/SaveInfo.h" - -class SSaveView; -class SSaveModel; -class SSaveController { - SSaveView * ssaveView; - SSaveModel * ssaveModel; - ControllerCallback * callback; -public: - bool HasExited; - SSaveController(ControllerCallback * callback, SaveInfo save); - SaveInfo * GetSave(); - bool GetSaveUploaded(); - void Exit(); - void Update(); - void UploadSave(std::string saveName, std::string saveDescription, bool publish); - SSaveView * GetView() { return ssaveView; } - virtual ~SSaveController(); -}; - -#endif /* SSAVECONTROLLER_H_ */ diff --git a/src/ssave/SSaveModel.cpp b/src/ssave/SSaveModel.cpp deleted file mode 100644 index 4933144..0000000 --- a/src/ssave/SSaveModel.cpp +++ /dev/null @@ -1,85 +0,0 @@ -/* - * SSaveModel.cpp - * - * Created on: Jan 29, 2012 - * Author: Simon - */ - -#include "SSaveModel.h" -#include "client/Client.h" - -SSaveModel::SSaveModel(): - save(NULL), - saveUploaded(false) -{ - // TODO Auto-generated constructor stub - -} - -void SSaveModel::notifySaveChanged() -{ - for(int i = 0; i < observers.size(); i++) - { - observers[i]->NotifySaveChanged(this); - } -} - -void SSaveModel::notifySaveUploadChanged() -{ - for(int i = 0; i < observers.size(); i++) - { - observers[i]->NotifySaveUploadChanged(this); - } -} - -void SSaveModel::UploadSave(std::string saveName, std::string saveDescription, bool publish) -{ - save->name = saveName; - save->Description = saveDescription; - save->Published = publish; - saveUploaded = false; - notifySaveUploadChanged(); - - if(Client::Ref().UploadSave(save) == RequestOkay) - { - saveUploaded = true; - } - else - { - saveUploaded = false; - } - notifySaveUploadChanged(); -} - -void SSaveModel::SetSave(SaveInfo * save) -{ - this->save = save; - notifySaveChanged(); -} - -SaveInfo * SSaveModel::GetSave() -{ - return this->save; -} - -bool SSaveModel::GetSaveUploaded() -{ - return saveUploaded; -} - -void SSaveModel::AddObserver(SSaveView * observer) -{ - observers.push_back(observer); - observer->NotifySaveChanged(this); - observer->NotifySaveUploadChanged(this); -} - -void SSaveModel::Update() -{ - -} - -SSaveModel::~SSaveModel() { - delete save; -} - diff --git a/src/ssave/SSaveModel.h b/src/ssave/SSaveModel.h deleted file mode 100644 index e83944b..0000000 --- a/src/ssave/SSaveModel.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * SSaveModel.h - * - * Created on: Jan 29, 2012 - * Author: Simon - */ - -#ifndef SSAVEMODEL_H_ -#define SSAVEMODEL_H_ - -#include <vector> - -#include "SSaveView.h" -#include "client/SaveInfo.h" - -using namespace std; - -class SSaveView; -class SSaveModel { - vector<SSaveView*> observers; - SaveInfo * save; - void notifySaveChanged(); - void notifySaveUploadChanged(); - bool saveUploaded; -public: - SSaveModel(); - void AddObserver(SSaveView * observer); - void Update(); - SaveInfo * GetSave(); - void SetSave(SaveInfo * save); - void UploadSave(std::string saveName, std::string saveDescription, bool publish); - bool GetSaveUploaded(); - virtual ~SSaveModel(); -}; - -#endif /* SSAVEMODEL_H_ */ diff --git a/src/ssave/SSaveView.cpp b/src/ssave/SSaveView.cpp deleted file mode 100644 index e37564c..0000000 --- a/src/ssave/SSaveView.cpp +++ /dev/null @@ -1,92 +0,0 @@ -/* - * SSaveView.cpp - * - * Created on: Jan 29, 2012 - * Author: Simon - */ - -#include "SSaveView.h" - -SSaveView::SSaveView(): - ui::Window(ui::Point(-1, -1), ui::Point(200, 200)), - publishCheckbox(NULL), - saveButton(NULL), - closeButton(NULL), - nameField(NULL), - titleLabel(NULL), - descriptionField(NULL) -{ - titleLabel = new ui::Label(ui::Point(2, 1), ui::Point(Size.X-4, 16), "Save to Server"); - titleLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; titleLabel->Appearance.VerticalAlign = ui::Appearance::AlignBottom; - AddComponent(titleLabel); - - nameField = new ui::Textbox(ui::Point(4, 18), ui::Point(Size.X-8, 16), ""); - nameField->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; nameField->Appearance.VerticalAlign = ui::Appearance::AlignBottom; - AddComponent(nameField); - - descriptionField = new ui::Textarea(ui::Point(4, 54), ui::Point(Size.X-8, Size.Y-26-54), ""); - AddComponent(descriptionField); - - publishCheckbox = new ui::Checkbox(ui::Point(4, 36), ui::Point(Size.X-8, 16), "Publish"); - AddComponent(publishCheckbox); - - class CloseAction: public ui::ButtonAction - { - SSaveView * v; - public: - CloseAction(SSaveView * v_) { v = v_; }; - void ActionCallback(ui::Button * sender) - { - v->c->Exit(); - } - }; - closeButton = new ui::Button(ui::Point(0, Size.Y-16), ui::Point(50, 16), "Cancel"); - closeButton->SetActionCallback(new CloseAction(this)); - AddComponent(closeButton); - - class SaveAction: public ui::ButtonAction - { - SSaveView * v; - public: - SaveAction(SSaveView * v_) { v = v_; }; - void ActionCallback(ui::Button * sender) - { - v->c->UploadSave(v->nameField->GetText(), "", v->publishCheckbox->GetChecked()); - } - }; - saveButton = new ui::Button(ui::Point(Size.X-50, Size.Y-16), ui::Point(50, 16), "Save"); - saveButton->SetActionCallback(new SaveAction(this)); - AddComponent(saveButton); -} - -void SSaveView::NotifySaveChanged(SSaveModel * sender) -{ - if(sender->GetSave()) - { - nameField->SetText(sender->GetSave()->GetName()); - publishCheckbox->SetChecked(sender->GetSave()->Published); - } - else - { - nameField->SetText(""); - //publishCheckbox->SetChecked(sender->GetSave()->GetPublished()); - } -} - -void SSaveView::NotifySaveUploadChanged(SSaveModel * sender) -{ - if(sender->GetSaveUploaded()) - c->Exit(); -} - -void SSaveView::OnDraw() -{ - Graphics * g = ui::Engine::Ref().g; - - g->clearrect(Position.X, Position.Y, Size.X, Size.Y); - g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 200, 200, 200, 255); -} - -SSaveView::~SSaveView() { -} - |
