Skip to content

Commit

Permalink
Merge pull request #14697 from iota97/undo-latest-save
Browse files Browse the repository at this point in the history
Add undo last save as well
  • Loading branch information
hrydgard authored Aug 7, 2021
2 parents ff340b8 + b643d60 commit d055cf3
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,8 @@ static ConfigSetting generalSettings[] = {
ConfigSetting("StateSlot", &g_Config.iCurrentStateSlot, 0, true, true),
ConfigSetting("EnableStateUndo", &g_Config.bEnableStateUndo, &DefaultEnableStateUndo, true, true),
ConfigSetting("StateLoadUndoGame", &g_Config.sStateLoadUndoGame, "NA", true, false),
ConfigSetting("StateUndoLastSaveGame", &g_Config.sStateUndoLastSaveGame, "NA", true, false),
ConfigSetting("StateUndoLastSaveSlot", &g_Config.iStateUndoLastSaveSlot, -5, true, false), // Start with an "invalid" value
ConfigSetting("RewindFlipFrequency", &g_Config.iRewindFlipFrequency, 0, true, true),

ConfigSetting("ShowOnScreenMessage", &g_Config.bShowOnScreenMessages, true, true, false),
Expand Down
2 changes: 2 additions & 0 deletions Core/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ struct Config {
bool bUISound;
bool bEnableStateUndo;
std::string sStateLoadUndoGame;
std::string sStateUndoLastSaveGame;
int iStateUndoLastSaveSlot;
int iAutoLoadSaveState; // 0 = off, 1 = oldest, 2 = newest, >2 = slot number + 3
bool bEnableCheats;
bool bReloadCheats;
Expand Down
18 changes: 18 additions & 0 deletions Core/SaveState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,8 @@ namespace SaveState
if (g_Config.bEnableStateUndo) {
DeleteIfExists(fnUndo);
RenameIfExists(fn, fnUndo);
g_Config.sStateUndoLastSaveGame = GenerateFullDiscId(gameFilename);
g_Config.iStateUndoLastSaveSlot = slot;
} else {
DeleteIfExists(fn);
}
Expand Down Expand Up @@ -571,6 +573,14 @@ namespace SaveState
return false;
}


bool UndoLastSave(const Path &gameFilename) {
if (g_Config.sStateUndoLastSaveGame != GenerateFullDiscId(gameFilename))
return false;

return UndoSaveSlot(gameFilename, g_Config.iStateUndoLastSaveSlot);
}

bool HasSaveInSlot(const Path &gameFilename, int slot)
{
Path fn = GenerateSaveSlotFilename(gameFilename, slot, STATE_EXTENSION);
Expand All @@ -583,6 +593,14 @@ namespace SaveState
return File::Exists(fn);
}

bool HasUndoLastSave(const Path &gameFilename)
{
if (g_Config.sStateUndoLastSaveGame != GenerateFullDiscId(gameFilename))
return false;

return HasUndoSaveInSlot(gameFilename, g_Config.iStateUndoLastSaveSlot);
}

bool HasScreenshotInSlot(const Path &gameFilename, int slot)
{
Path fn = GenerateSaveSlotFilename(gameFilename, slot, SCREENSHOT_EXTENSION);
Expand Down
2 changes: 2 additions & 0 deletions Core/SaveState.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ namespace SaveState
void SaveSlot(const Path &gameFilename, int slot, Callback callback, void *cbUserData = 0);
void LoadSlot(const Path &gameFilename, int slot, Callback callback, void *cbUserData = 0);
bool UndoSaveSlot(const Path &gameFilename, int slot);
bool UndoLastSave(const Path &gameFilename);
bool UndoLoad(const Path &gameFilename, Callback callback, void *cbUserData = 0);
// Checks whether there's an existing save in the specified slot.
bool HasSaveInSlot(const Path &gameFilename, int slot);
bool HasUndoSaveInSlot(const Path &gameFilename, int slot);
bool HasUndoLastSave(const Path &gameFilename);
bool HasUndoLoad(const Path &gameFilename);
bool HasScreenshotInSlot(const Path &gameFilename, int slot);

Expand Down
13 changes: 12 additions & 1 deletion UI/PauseScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,13 @@ void GamePauseScreen::CreateViews() {

LinearLayout *buttonRow = leftColumnItems->Add(new LinearLayout(ORIENT_HORIZONTAL));
if (g_Config.bEnableStateUndo) {
UI::Choice *loadUndoButton = buttonRow->Add(new Choice(pa->T("Undo last state load")));
UI::Choice *loadUndoButton = buttonRow->Add(new Choice(pa->T("Undo last load")));
loadUndoButton->SetEnabled(SaveState::HasUndoLoad(gamePath_));
loadUndoButton->OnClick.Handle(this, &GamePauseScreen::OnLoadUndo);

UI::Choice *saveUndoButton = buttonRow->Add(new Choice(pa->T("Undo last save")));
saveUndoButton->SetEnabled(SaveState::HasUndoLastSave(gamePath_));
saveUndoButton->OnClick.Handle(this, &GamePauseScreen::OnLastSaveUndo);
}

if (g_Config.iRewindFlipFrequency > 0) {
Expand Down Expand Up @@ -506,6 +510,13 @@ UI::EventReturn GamePauseScreen::OnLoadUndo(UI::EventParams &e) {
return UI::EVENT_DONE;
}

UI::EventReturn GamePauseScreen::OnLastSaveUndo(UI::EventParams &e) {
SaveState::UndoLastSave(gamePath_);

RecreateViews();
return UI::EVENT_DONE;
}

UI::EventReturn GamePauseScreen::OnCwCheat(UI::EventParams &e) {
screenManager()->push(new CwCheatScreen(gamePath_));
return UI::EVENT_DONE;
Expand Down
1 change: 1 addition & 0 deletions UI/PauseScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class GamePauseScreen : public UIDialogScreenWithGameBackground {

UI::EventReturn OnRewind(UI::EventParams &e);
UI::EventReturn OnLoadUndo(UI::EventParams &e);
UI::EventReturn OnLastSaveUndo(UI::EventParams &e);

UI::EventReturn OnScreenshotClicked(UI::EventParams &e);
UI::EventReturn OnCwCheat(UI::EventParams &e);
Expand Down

0 comments on commit d055cf3

Please sign in to comment.