From 3a29fc0268dbda68e80f7de586c6692ba7a11565 Mon Sep 17 00:00:00 2001 From: jacob1 Date: Fri, 19 Oct 2012 19:17:15 -0400 Subject: [PATCH] Save local saves as current name option (overwrites them automatically). Fix filename not showing when first saving a local save --- src/cat/LegacyLuaAPI.cpp | 11 +--------- src/cat/LuaScriptInterface.cpp | 8 ++----- src/client/Client.cpp | 22 ++++++++++---------- src/client/Client.h | 1 + src/client/SaveFile.cpp | 5 +++++ src/client/SaveFile.h | 1 + src/game/GameController.cpp | 38 ++++++++++++++++++++-------------- src/game/GameController.h | 2 +- src/game/GameModel.cpp | 2 +- src/game/GameModel.h | 2 +- src/game/GameView.cpp | 23 ++++++++++++++------ src/game/GameView.h | 1 + src/save/LocalSaveActivity.cpp | 13 +++--------- 13 files changed, 68 insertions(+), 61 deletions(-) diff --git a/src/cat/LegacyLuaAPI.cpp b/src/cat/LegacyLuaAPI.cpp index dfebf01a2..d6e6ce6a7 100644 --- a/src/cat/LegacyLuaAPI.cpp +++ b/src/cat/LegacyLuaAPI.cpp @@ -16,11 +16,6 @@ #include "simulation/Simulation.h" #include "game/GameModel.h" -#ifdef WIN -#include -#else -#include -#endif #include #ifndef FFI @@ -1747,11 +1742,7 @@ int luatpt_getscript(lua_State* l) filename = new char[fileauthor.length()+fileid.length()+strlen(PATH_SEP)+strlen(LOCAL_LUA_DIR)+6]; sprintf(filename, LOCAL_LUA_DIR PATH_SEP "%s_%s.lua", fileauthor.c_str(), fileid.c_str()); -#ifdef WIN - _mkdir(LOCAL_LUA_DIR); -#else - mkdir(LOCAL_LUA_DIR, 0755); -#endif + Client::Ref().MakeDirectory(LOCAL_LUA_DIR); outputfile = fopen(filename, "r"); if(outputfile) diff --git a/src/cat/LuaScriptInterface.cpp b/src/cat/LuaScriptInterface.cpp index b9124c5ec..56da7e03b 100644 --- a/src/cat/LuaScriptInterface.cpp +++ b/src/cat/LuaScriptInterface.cpp @@ -1520,14 +1520,10 @@ int LuaScriptInterface::fileSystem_isDirectory(lua_State * l) int LuaScriptInterface::fileSystem_makeDirectory(lua_State * l) { - const char * filename = lua_tostring(l, 1); + const char * dirname = lua_tostring(l, 1); int ret = 0; -#ifdef WIN - ret = _mkdir(filename); -#else - ret = mkdir(filename, 0755); -#endif + ret = Client::Ref().MakeDirectory(dirname); lua_pushboolean(l, ret == 0); return 1; } diff --git a/src/client/Client.cpp b/src/client/Client.cpp index b35c29219..763814a35 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -430,6 +430,15 @@ std::vector Client::DirectorySearch(std::string directory, std::str return searchResults; } +int Client::MakeDirectory(const char * dirName) +{ +#ifdef WIN + return _mkdir(dirName); +#else + return mkdir(dirName, 0755); +#endif +} + void Client::WriteFile(std::vector fileData, std::string filename) { try @@ -870,11 +879,7 @@ std::string Client::AddStamp(GameSave * saveData) << std::setw(8) << std::setfill('0') << std::hex << lastStampTime << std::setw(2) << std::setfill('0') << std::hex << lastStampName; -#ifdef WIN - _mkdir(STAMPS_DIR); -#else - mkdir(STAMPS_DIR, 0755); -#endif + MakeDirectory(STAMPS_DIR); int gameDataLength; char * gameData = saveData->Serialise(gameDataLength); @@ -895,12 +900,7 @@ std::string Client::AddStamp(GameSave * saveData) void Client::updateStamps() { - -#ifdef WIN - _mkdir(STAMPS_DIR); -#else - mkdir(STAMPS_DIR, 0755); -#endif + MakeDirectory(STAMPS_DIR); std::ofstream stampsStream; stampsStream.open(std::string(STAMPS_DIR PATH_SEP "stamps.def").c_str(), std::ios::binary); diff --git a/src/client/Client.h b/src/client/Client.h index 4f657886b..642fd63d7 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -98,6 +98,7 @@ public: void Initialise(std::string proxyString); void SetProxy(std::string proxy); + int MakeDirectory(const char * dirname); void WriteFile(std::vector fileData, std::string filename); void WriteFile(std::vector fileData, std::string filename); bool FileExists(std::string filename); diff --git a/src/client/SaveFile.cpp b/src/client/SaveFile.cpp index 368d89d03..fe7ad4f6d 100644 --- a/src/client/SaveFile.cpp +++ b/src/client/SaveFile.cpp @@ -56,6 +56,11 @@ std::string SaveFile::GetName() return filename; } +void SaveFile::SetFileName(std::string fileName) +{ + this->filename = fileName; +} + std::string SaveFile::GetDisplayName() { return displayName; diff --git a/src/client/SaveFile.h b/src/client/SaveFile.h index 9a9310f98..b63d18144 100644 --- a/src/client/SaveFile.h +++ b/src/client/SaveFile.h @@ -25,6 +25,7 @@ public: std::string GetDisplayName(); void SetDisplayName(std::string displayName); std::string GetName(); + void SetFileName(std::string fileName); virtual ~SaveFile(); private: diff --git a/src/game/GameController.cpp b/src/game/GameController.cpp index 96f966cb7..dfe3a9449 100644 --- a/src/game/GameController.cpp +++ b/src/game/GameController.cpp @@ -909,7 +909,7 @@ void GameController::OpenSearch() ui::Engine::Ref().ShowWindow(search->GetView()); } -void GameController::OpenLocalSaveWindow() +void GameController::OpenLocalSaveWindow(bool asCurrent) { Simulation * sim = gameModel->GetSimulation(); GameSave * gameSave = sim->Save(); @@ -926,24 +926,32 @@ void GameController::OpenLocalSaveWindow() else { std::string filename = ""; - if (gameModel->GetFile()) - filename = gameModel->GetFile()->GetDisplayName(); + if (gameModel->GetSaveFile()) + filename = gameModel->GetSaveFile()->GetDisplayName(); SaveFile tempSave(filename); tempSave.SetGameSave(gameSave); - class LocalSaveCallback: public FileSavedCallback + if (!asCurrent || !gameModel->GetSaveFile()) { - GameController * c; - public: - LocalSaveCallback(GameController * _c): c(_c) {} - virtual ~LocalSaveCallback() {}; - virtual void FileSaved(SaveFile* file) + class LocalSaveCallback: public FileSavedCallback { - c->gameModel->SetSaveFile(file); - } - }; + GameController * c; + public: + LocalSaveCallback(GameController * _c): c(_c) {} + virtual ~LocalSaveCallback() {}; + virtual void FileSaved(SaveFile* file) + { + c->gameModel->SetSaveFile(file); + } + }; - new LocalSaveActivity(tempSave, new LocalSaveCallback(this)); + new LocalSaveActivity(tempSave, new LocalSaveCallback(this)); + } + else if (gameModel->GetSaveFile()) + { + Client::Ref().MakeDirectory(LOCAL_SAVE_DIR); + Client::Ref().WriteFile(gameSave->Serialise(), gameModel->GetSaveFile()->GetName()); + } } } @@ -1209,9 +1217,9 @@ void GameController::ReloadSim() { gameModel->SetSave(gameModel->GetSave()); } - else if(gameModel->GetFile() && gameModel->GetFile()->GetGameSave()) + else if(gameModel->GetSaveFile() && gameModel->GetSaveFile()->GetGameSave()) { - gameModel->SetSaveFile(gameModel->GetFile()); + gameModel->SetSaveFile(gameModel->GetSaveFile()); } } diff --git a/src/game/GameController.h b/src/game/GameController.h index 14dbccbb3..2ac8bea1c 100644 --- a/src/game/GameController.h +++ b/src/game/GameController.h @@ -105,7 +105,7 @@ public: void OpenLogin(); void OpenTags(); void OpenSavePreview(int saveID, int saveDate); - void OpenLocalSaveWindow(); + void OpenLocalSaveWindow(bool asCurrent); void OpenLocalBrowse(); void OpenOptions(); void OpenRenderOptions(); diff --git a/src/game/GameModel.cpp b/src/game/GameModel.cpp index f905a1ccd..0a3c57701 100644 --- a/src/game/GameModel.cpp +++ b/src/game/GameModel.cpp @@ -564,7 +564,7 @@ void GameModel::SetSave(SaveInfo * newSave) UpdateQuickOptions(); } -SaveFile * GameModel::GetFile() +SaveFile * GameModel::GetSaveFile() { return currentFile; } diff --git a/src/game/GameModel.h b/src/game/GameModel.h index b4c0bd3d6..7288c3266 100644 --- a/src/game/GameModel.h +++ b/src/game/GameModel.h @@ -144,7 +144,7 @@ public: void SetVote(int direction); SaveInfo * GetSave(); - SaveFile * GetFile(); + SaveFile * GetSaveFile(); Brush * GetBrush(); void SetSave(SaveInfo * newSave); void SetSaveFile(SaveFile * newSave); diff --git a/src/game/GameView.cpp b/src/game/GameView.cpp index 9a2f77260..73b383301 100644 --- a/src/game/GameView.cpp +++ b/src/game/GameView.cpp @@ -181,7 +181,8 @@ GameView::GameView(): recording(false), screenshotIndex(0), recordingIndex(0), - toolTipPresence(0) + toolTipPresence(0), + currentSaveType(0) { int currentX = 1; @@ -238,14 +239,14 @@ GameView::GameView(): void ActionCallbackRight(ui::Button * sender) { if(v->CtrlBehaviour()) - v->c->OpenLocalSaveWindow(); + v->c->OpenLocalSaveWindow(false); else v->c->OpenSaveWindow(); } void ActionCallbackLeft(ui::Button * sender) { if(v->CtrlBehaviour()) - v->c->OpenLocalSaveWindow(); + v->c->OpenLocalSaveWindow(true); else v->c->SaveAsCurrent(); } @@ -846,11 +847,15 @@ void GameView::NotifySaveChanged(GameModel * sender) { tagSimulationButton->SetText("[no tags set]"); } + currentSaveType = 1; } - else if (sender->GetFile()) + else if (sender->GetSaveFile()) { - ((SplitButton*)saveSimulationButton)->SetShowSplit(false); - saveSimulationButton->SetText(sender->GetFile()->GetDisplayName()); + if (ctrlBehaviour) + ((SplitButton*)saveSimulationButton)->SetShowSplit(true); + else + ((SplitButton*)saveSimulationButton)->SetShowSplit(false); + saveSimulationButton->SetText(sender->GetSaveFile()->GetDisplayName()); reloadButton->Enabled = true; upVoteButton->Enabled = false; upVoteButton->Appearance.BackgroundInactive = (ui::Colour(0, 0, 0)); @@ -858,6 +863,7 @@ void GameView::NotifySaveChanged(GameModel * sender) upVoteButton->Appearance.BackgroundInactive = (ui::Colour(0, 0, 0)); tagSimulationButton->Enabled = false; tagSimulationButton->SetText("[no tags set]"); + currentSaveType = 2; } else { @@ -870,6 +876,7 @@ void GameView::NotifySaveChanged(GameModel * sender) upVoteButton->Appearance.BackgroundInactive = (ui::Colour(0, 0, 0)); tagSimulationButton->Enabled = false; tagSimulationButton->SetText("[no tags set]"); + currentSaveType = 0; } } @@ -1685,6 +1692,8 @@ void GameView::enableCtrlBehaviour() saveSimulationButton->Appearance.TextInactive = saveSimulationButton->Appearance.TextHover = ui::Colour(0, 0, 0); searchButton->Appearance.BackgroundInactive = searchButton->Appearance.BackgroundHover = ui::Colour(255, 255, 255); searchButton->Appearance.TextInactive = searchButton->Appearance.TextHover = ui::Colour(0, 0, 0); + if (currentSaveType == 2) + ((SplitButton*)saveSimulationButton)->SetShowSplit(true); if(isMouseDown) { if(!shiftBehaviour) @@ -1708,6 +1717,8 @@ void GameView::disableCtrlBehaviour() searchButton->Appearance.BackgroundInactive = ui::Colour(0, 0, 0); searchButton->Appearance.BackgroundHover = ui::Colour(20, 20, 20); searchButton->Appearance.TextInactive = searchButton->Appearance.TextHover = ui::Colour(255, 255, 255); + if (currentSaveType == 2) + ((SplitButton*)saveSimulationButton)->SetShowSplit(false); if(!shiftBehaviour) c->SetToolStrength(1.0f); else diff --git a/src/game/GameView.h b/src/game/GameView.h index f36aef39d..e17279f1a 100644 --- a/src/game/GameView.h +++ b/src/game/GameView.h @@ -55,6 +55,7 @@ private: std::string buttonTip; std::string introTextMessage; int toolIndex; + int currentSaveType; int infoTipPresence; std::string toolTip; diff --git a/src/save/LocalSaveActivity.cpp b/src/save/LocalSaveActivity.cpp index d2c4a2f5b..b57a9933e 100644 --- a/src/save/LocalSaveActivity.cpp +++ b/src/save/LocalSaveActivity.cpp @@ -1,8 +1,3 @@ -#ifdef WIN -#include -#else -#include -#endif #include "LocalSaveActivity.h" #include "interface/Label.h" #include "interface/Textbox.h" @@ -94,6 +89,8 @@ void LocalSaveActivity::Save() if(filenameField->GetText().length()) { std::string finalFilename = std::string(LOCAL_SAVE_DIR) + std::string(PATH_SEP) + filenameField->GetText() + ".cps"; + save.SetDisplayName(filenameField->GetText()); + save.SetFileName(finalFilename); if(Client::Ref().FileExists(finalFilename)) { new ConfirmPrompt("Overwrite file", "Are you sure you wish to overwrite\n"+finalFilename, new FileOverwriteConfirmation(this, finalFilename)); @@ -112,11 +109,7 @@ void LocalSaveActivity::Save() void LocalSaveActivity::saveWrite(std::string finalFilename) { -#ifdef WIN - _mkdir(LOCAL_SAVE_DIR); -#else - mkdir(LOCAL_SAVE_DIR, 0755); -#endif + Client::Ref().MakeDirectory(LOCAL_SAVE_DIR); Client::Ref().WriteFile(save.GetGameSave()->Serialise(), finalFilename); callback->FileSaved(&save); }