Save local saves as current name option (overwrites them automatically). Fix filename not showing when first saving a local save

This commit is contained in:
jacob1 2012-10-19 19:17:15 -04:00 committed by Simon Robertshaw
parent e2622657f0
commit 3a29fc0268
13 changed files with 68 additions and 61 deletions

View File

@ -16,11 +16,6 @@
#include "simulation/Simulation.h" #include "simulation/Simulation.h"
#include "game/GameModel.h" #include "game/GameModel.h"
#ifdef WIN
#include <direct.h>
#else
#include <sys/stat.h>
#endif
#include <time.h> #include <time.h>
#ifndef FFI #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]; 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()); sprintf(filename, LOCAL_LUA_DIR PATH_SEP "%s_%s.lua", fileauthor.c_str(), fileid.c_str());
#ifdef WIN Client::Ref().MakeDirectory(LOCAL_LUA_DIR);
_mkdir(LOCAL_LUA_DIR);
#else
mkdir(LOCAL_LUA_DIR, 0755);
#endif
outputfile = fopen(filename, "r"); outputfile = fopen(filename, "r");
if(outputfile) if(outputfile)

View File

@ -1520,14 +1520,10 @@ int LuaScriptInterface::fileSystem_isDirectory(lua_State * l)
int LuaScriptInterface::fileSystem_makeDirectory(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; int ret = 0;
#ifdef WIN ret = Client::Ref().MakeDirectory(dirname);
ret = _mkdir(filename);
#else
ret = mkdir(filename, 0755);
#endif
lua_pushboolean(l, ret == 0); lua_pushboolean(l, ret == 0);
return 1; return 1;
} }

View File

@ -430,6 +430,15 @@ std::vector<std::string> Client::DirectorySearch(std::string directory, std::str
return searchResults; return searchResults;
} }
int Client::MakeDirectory(const char * dirName)
{
#ifdef WIN
return _mkdir(dirName);
#else
return mkdir(dirName, 0755);
#endif
}
void Client::WriteFile(std::vector<unsigned char> fileData, std::string filename) void Client::WriteFile(std::vector<unsigned char> fileData, std::string filename)
{ {
try try
@ -870,11 +879,7 @@ std::string Client::AddStamp(GameSave * saveData)
<< std::setw(8) << std::setfill('0') << std::hex << lastStampTime << std::setw(8) << std::setfill('0') << std::hex << lastStampTime
<< std::setw(2) << std::setfill('0') << std::hex << lastStampName; << std::setw(2) << std::setfill('0') << std::hex << lastStampName;
#ifdef WIN MakeDirectory(STAMPS_DIR);
_mkdir(STAMPS_DIR);
#else
mkdir(STAMPS_DIR, 0755);
#endif
int gameDataLength; int gameDataLength;
char * gameData = saveData->Serialise(gameDataLength); char * gameData = saveData->Serialise(gameDataLength);
@ -895,12 +900,7 @@ std::string Client::AddStamp(GameSave * saveData)
void Client::updateStamps() void Client::updateStamps()
{ {
MakeDirectory(STAMPS_DIR);
#ifdef WIN
_mkdir(STAMPS_DIR);
#else
mkdir(STAMPS_DIR, 0755);
#endif
std::ofstream stampsStream; std::ofstream stampsStream;
stampsStream.open(std::string(STAMPS_DIR PATH_SEP "stamps.def").c_str(), std::ios::binary); stampsStream.open(std::string(STAMPS_DIR PATH_SEP "stamps.def").c_str(), std::ios::binary);

View File

@ -98,6 +98,7 @@ public:
void Initialise(std::string proxyString); void Initialise(std::string proxyString);
void SetProxy(std::string proxy); void SetProxy(std::string proxy);
int MakeDirectory(const char * dirname);
void WriteFile(std::vector<unsigned char> fileData, std::string filename); void WriteFile(std::vector<unsigned char> fileData, std::string filename);
void WriteFile(std::vector<char> fileData, std::string filename); void WriteFile(std::vector<char> fileData, std::string filename);
bool FileExists(std::string filename); bool FileExists(std::string filename);

View File

@ -56,6 +56,11 @@ std::string SaveFile::GetName()
return filename; return filename;
} }
void SaveFile::SetFileName(std::string fileName)
{
this->filename = fileName;
}
std::string SaveFile::GetDisplayName() std::string SaveFile::GetDisplayName()
{ {
return displayName; return displayName;

View File

@ -25,6 +25,7 @@ public:
std::string GetDisplayName(); std::string GetDisplayName();
void SetDisplayName(std::string displayName); void SetDisplayName(std::string displayName);
std::string GetName(); std::string GetName();
void SetFileName(std::string fileName);
virtual ~SaveFile(); virtual ~SaveFile();
private: private:

View File

@ -909,7 +909,7 @@ void GameController::OpenSearch()
ui::Engine::Ref().ShowWindow(search->GetView()); ui::Engine::Ref().ShowWindow(search->GetView());
} }
void GameController::OpenLocalSaveWindow() void GameController::OpenLocalSaveWindow(bool asCurrent)
{ {
Simulation * sim = gameModel->GetSimulation(); Simulation * sim = gameModel->GetSimulation();
GameSave * gameSave = sim->Save(); GameSave * gameSave = sim->Save();
@ -926,11 +926,13 @@ void GameController::OpenLocalSaveWindow()
else else
{ {
std::string filename = ""; std::string filename = "";
if (gameModel->GetFile()) if (gameModel->GetSaveFile())
filename = gameModel->GetFile()->GetDisplayName(); filename = gameModel->GetSaveFile()->GetDisplayName();
SaveFile tempSave(filename); SaveFile tempSave(filename);
tempSave.SetGameSave(gameSave); tempSave.SetGameSave(gameSave);
if (!asCurrent || !gameModel->GetSaveFile())
{
class LocalSaveCallback: public FileSavedCallback class LocalSaveCallback: public FileSavedCallback
{ {
GameController * c; GameController * c;
@ -945,6 +947,12 @@ void GameController::OpenLocalSaveWindow()
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());
}
}
} }
void GameController::LoadSaveFile(SaveFile * file) void GameController::LoadSaveFile(SaveFile * file)
@ -1209,9 +1217,9 @@ void GameController::ReloadSim()
{ {
gameModel->SetSave(gameModel->GetSave()); 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());
} }
} }

View File

@ -105,7 +105,7 @@ public:
void OpenLogin(); void OpenLogin();
void OpenTags(); void OpenTags();
void OpenSavePreview(int saveID, int saveDate); void OpenSavePreview(int saveID, int saveDate);
void OpenLocalSaveWindow(); void OpenLocalSaveWindow(bool asCurrent);
void OpenLocalBrowse(); void OpenLocalBrowse();
void OpenOptions(); void OpenOptions();
void OpenRenderOptions(); void OpenRenderOptions();

View File

@ -564,7 +564,7 @@ void GameModel::SetSave(SaveInfo * newSave)
UpdateQuickOptions(); UpdateQuickOptions();
} }
SaveFile * GameModel::GetFile() SaveFile * GameModel::GetSaveFile()
{ {
return currentFile; return currentFile;
} }

View File

@ -144,7 +144,7 @@ public:
void SetVote(int direction); void SetVote(int direction);
SaveInfo * GetSave(); SaveInfo * GetSave();
SaveFile * GetFile(); SaveFile * GetSaveFile();
Brush * GetBrush(); Brush * GetBrush();
void SetSave(SaveInfo * newSave); void SetSave(SaveInfo * newSave);
void SetSaveFile(SaveFile * newSave); void SetSaveFile(SaveFile * newSave);

View File

@ -181,7 +181,8 @@ GameView::GameView():
recording(false), recording(false),
screenshotIndex(0), screenshotIndex(0),
recordingIndex(0), recordingIndex(0),
toolTipPresence(0) toolTipPresence(0),
currentSaveType(0)
{ {
int currentX = 1; int currentX = 1;
@ -238,14 +239,14 @@ GameView::GameView():
void ActionCallbackRight(ui::Button * sender) void ActionCallbackRight(ui::Button * sender)
{ {
if(v->CtrlBehaviour()) if(v->CtrlBehaviour())
v->c->OpenLocalSaveWindow(); v->c->OpenLocalSaveWindow(false);
else else
v->c->OpenSaveWindow(); v->c->OpenSaveWindow();
} }
void ActionCallbackLeft(ui::Button * sender) void ActionCallbackLeft(ui::Button * sender)
{ {
if(v->CtrlBehaviour()) if(v->CtrlBehaviour())
v->c->OpenLocalSaveWindow(); v->c->OpenLocalSaveWindow(true);
else else
v->c->SaveAsCurrent(); v->c->SaveAsCurrent();
} }
@ -846,11 +847,15 @@ void GameView::NotifySaveChanged(GameModel * sender)
{ {
tagSimulationButton->SetText("[no tags set]"); tagSimulationButton->SetText("[no tags set]");
} }
currentSaveType = 1;
} }
else if (sender->GetFile()) else if (sender->GetSaveFile())
{ {
if (ctrlBehaviour)
((SplitButton*)saveSimulationButton)->SetShowSplit(true);
else
((SplitButton*)saveSimulationButton)->SetShowSplit(false); ((SplitButton*)saveSimulationButton)->SetShowSplit(false);
saveSimulationButton->SetText(sender->GetFile()->GetDisplayName()); saveSimulationButton->SetText(sender->GetSaveFile()->GetDisplayName());
reloadButton->Enabled = true; reloadButton->Enabled = true;
upVoteButton->Enabled = false; upVoteButton->Enabled = false;
upVoteButton->Appearance.BackgroundInactive = (ui::Colour(0, 0, 0)); 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)); upVoteButton->Appearance.BackgroundInactive = (ui::Colour(0, 0, 0));
tagSimulationButton->Enabled = false; tagSimulationButton->Enabled = false;
tagSimulationButton->SetText("[no tags set]"); tagSimulationButton->SetText("[no tags set]");
currentSaveType = 2;
} }
else else
{ {
@ -870,6 +876,7 @@ void GameView::NotifySaveChanged(GameModel * sender)
upVoteButton->Appearance.BackgroundInactive = (ui::Colour(0, 0, 0)); upVoteButton->Appearance.BackgroundInactive = (ui::Colour(0, 0, 0));
tagSimulationButton->Enabled = false; tagSimulationButton->Enabled = false;
tagSimulationButton->SetText("[no tags set]"); 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); saveSimulationButton->Appearance.TextInactive = saveSimulationButton->Appearance.TextHover = ui::Colour(0, 0, 0);
searchButton->Appearance.BackgroundInactive = searchButton->Appearance.BackgroundHover = ui::Colour(255, 255, 255); searchButton->Appearance.BackgroundInactive = searchButton->Appearance.BackgroundHover = ui::Colour(255, 255, 255);
searchButton->Appearance.TextInactive = searchButton->Appearance.TextHover = ui::Colour(0, 0, 0); searchButton->Appearance.TextInactive = searchButton->Appearance.TextHover = ui::Colour(0, 0, 0);
if (currentSaveType == 2)
((SplitButton*)saveSimulationButton)->SetShowSplit(true);
if(isMouseDown) if(isMouseDown)
{ {
if(!shiftBehaviour) if(!shiftBehaviour)
@ -1708,6 +1717,8 @@ void GameView::disableCtrlBehaviour()
searchButton->Appearance.BackgroundInactive = ui::Colour(0, 0, 0); searchButton->Appearance.BackgroundInactive = ui::Colour(0, 0, 0);
searchButton->Appearance.BackgroundHover = ui::Colour(20, 20, 20); searchButton->Appearance.BackgroundHover = ui::Colour(20, 20, 20);
searchButton->Appearance.TextInactive = searchButton->Appearance.TextHover = ui::Colour(255, 255, 255); searchButton->Appearance.TextInactive = searchButton->Appearance.TextHover = ui::Colour(255, 255, 255);
if (currentSaveType == 2)
((SplitButton*)saveSimulationButton)->SetShowSplit(false);
if(!shiftBehaviour) if(!shiftBehaviour)
c->SetToolStrength(1.0f); c->SetToolStrength(1.0f);
else else

View File

@ -55,6 +55,7 @@ private:
std::string buttonTip; std::string buttonTip;
std::string introTextMessage; std::string introTextMessage;
int toolIndex; int toolIndex;
int currentSaveType;
int infoTipPresence; int infoTipPresence;
std::string toolTip; std::string toolTip;

View File

@ -1,8 +1,3 @@
#ifdef WIN
#include <direct.h>
#else
#include <sys/stat.h>
#endif
#include "LocalSaveActivity.h" #include "LocalSaveActivity.h"
#include "interface/Label.h" #include "interface/Label.h"
#include "interface/Textbox.h" #include "interface/Textbox.h"
@ -94,6 +89,8 @@ void LocalSaveActivity::Save()
if(filenameField->GetText().length()) if(filenameField->GetText().length())
{ {
std::string finalFilename = std::string(LOCAL_SAVE_DIR) + std::string(PATH_SEP) + filenameField->GetText() + ".cps"; 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)) if(Client::Ref().FileExists(finalFilename))
{ {
new ConfirmPrompt("Overwrite file", "Are you sure you wish to overwrite\n"+finalFilename, new FileOverwriteConfirmation(this, 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) void LocalSaveActivity::saveWrite(std::string finalFilename)
{ {
#ifdef WIN Client::Ref().MakeDirectory(LOCAL_SAVE_DIR);
_mkdir(LOCAL_SAVE_DIR);
#else
mkdir(LOCAL_SAVE_DIR, 0755);
#endif
Client::Ref().WriteFile(save.GetGameSave()->Serialise(), finalFilename); Client::Ref().WriteFile(save.GetGameSave()->Serialise(), finalFilename);
callback->FileSaved(&save); callback->FileSaved(&save);
} }