autosave loading

This commit is contained in:
2025-03-02 10:27:58 -06:00
committed by Celtic Minstrel
parent 2396637d23
commit 53de92c932
2 changed files with 41 additions and 7 deletions

View File

@@ -1730,7 +1730,7 @@ class cFilePicker {
fs::path save_folder; fs::path save_folder;
bool picking_auto; bool picking_auto;
bool saving; bool saving;
cDialog me{*ResMgr::dialogs.get("pick-save")}; cDialog me;
cStack& get_stack() { return dynamic_cast<cStack&>(me["list"]); } cStack& get_stack() { return dynamic_cast<cStack&>(me["list"]); }
std::string template_info_str; std::string template_info_str;
@@ -1830,8 +1830,23 @@ class cFilePicker {
me["save" + suffix].attachClickHandler(std::bind(&cFilePicker::doSave, this, file)); me["save" + suffix].attachClickHandler(std::bind(&cFilePicker::doSave, this, file));
}else{ }else{
me["load" + suffix].attachClickHandler(std::bind(&cFilePicker::doLoad, this, file)); me["load" + suffix].attachClickHandler(std::bind(&cFilePicker::doLoad, this, file));
// TODO check if a newer autosave exists
me["auto" + suffix + "-more-recent"].hide(); std::vector<std::pair<fs::path, std::time_t>> auto_mtimes;
fs::path auto_folder = file;
if(!picking_auto){
auto_folder.replace_extension(".auto");
if(fs::is_directory(auto_folder)) auto_mtimes = sorted_file_mtimes(auto_folder);
}
if(auto_mtimes.empty()){
me["auto" + suffix].hide();
me["auto" + suffix + "-more-recent"].hide();
}else{
// If an autosave is newer than the main save, show an indicator
if(std::difftime(mtime, auto_mtimes.front().second) > 0)
me["auto" + suffix + "-more-recent"].show();
me["auto" + suffix].attachClickHandler(std::bind(&cFilePicker::showAuto, this, auto_folder));
}
} }
} }
@@ -1866,6 +1881,13 @@ class cFilePicker {
++pages_populated; ++pages_populated;
} }
bool showAuto(fs::path auto_folder) {
fs::path autosave = run_autosave_picker(auto_folder, &me);
if(!autosave.empty())
doLoad(autosave);
return true;
}
bool doLoad(fs::path selected_file) { bool doLoad(fs::path selected_file) {
me.setResult(selected_file); me.setResult(selected_file);
me.toast(false); me.toast(false);
@@ -1927,7 +1949,8 @@ class cFilePicker {
} }
public: public:
cFilePicker(fs::path save_folder, bool saving, bool picking_auto = false) : cFilePicker(fs::path save_folder, bool saving, cDialog* parent = nullptr, bool picking_auto = false) :
me(*ResMgr::dialogs.get("pick-save"), parent),
save_folder(save_folder), save_folder(save_folder),
picking_auto(picking_auto), picking_auto(picking_auto),
saving(saving), saving(saving),
@@ -1938,11 +1961,22 @@ public:
if(saving){ if(saving){
me["title-load"].hide(); me["title-load"].hide();
me["title-auto"].hide();
me["file1"].setText(""); // Keep the frame me["file1"].setText(""); // Keep the frame
for(int i = 0; i < SLOTS_PER_PAGE; ++i){ for(int i = 0; i < SLOTS_PER_PAGE; ++i){
me["load" + std::to_string(i+1)].hide(); me["load" + std::to_string(i+1)].hide();
} }
}else{ }else{
if(picking_auto){
me["title-load"].hide();
std::string title = me["title-auto"].getText();
fs::path party_name = save_folder.filename();
party_name.replace_extension();
boost::replace_first(title, "{Folder}", party_name.string());
me["title-auto"].setText(title);
}else{
me["title-auto"].hide();
}
me["title-save"].hide(); me["title-save"].hide();
me["file1-field"].hide(); me["file1-field"].hide();
me["file1-extension-label"].hide(); me["file1-extension-label"].hide();
@@ -1979,8 +2013,8 @@ static fs::path run_file_picker(fs::path save_folder, bool saving) {
return cFilePicker(save_folder, saving).run(); return cFilePicker(save_folder, saving).run();
} }
fs::path run_autosave_picker(fs::path save_file) { fs::path run_autosave_picker(fs::path auto_folder, cDialog* parent) {
return ""; return cFilePicker(auto_folder, false, parent, true).run();
} }
fs::path run_file_picker(bool saving) { fs::path run_file_picker(bool saving) {

View File

@@ -27,6 +27,6 @@ void tip_of_day();
struct scen_header_type pick_a_scen(); struct scen_header_type pick_a_scen();
fs::path run_file_picker(bool saving); fs::path run_file_picker(bool saving);
// Pick from the autosaves made while playing in a given save file // Pick from the autosaves made while playing in a given save file
fs::path run_autosave_picker(fs::path save_file); fs::path run_autosave_picker(fs::path save_file, cDialog* parent = nullptr);
#endif #endif