diff --git a/src/scenedit/scen.core.cpp b/src/scenedit/scen.core.cpp index 7a1cfdeb..c0b0b3aa 100644 --- a/src/scenedit/scen.core.cpp +++ b/src/scenedit/scen.core.cpp @@ -3791,12 +3791,19 @@ void edit_custom_sheets() { }); pic_dlg["del"].attachClickHandler([&sheets,&cur,&all_pics,&pic_dir](cDialog& me, std::string, eKeyMod) -> bool { int which_pic = all_pics[cur]; + + fs::path fpath = pic_dir/("sheet" + std::to_string(which_pic) + ".png"); + bool moved = false; + sf::Image image_for_undo; + image_for_undo.loadFromFile(fpath.string()); + if(which_pic < spec_scen_g.numSheets) { std::string choice = "del"; if(which_pic < spec_scen_g.numSheets - 1) choice = cChoiceDlog("must-delete-in-order", {"cancel", "del", "move"}, &me).show(); if(choice == "cancel") return true; if(choice == "move") { + moved = true; spec_scen_g.sheets.erase(spec_scen_g.sheets.begin() + which_pic); spec_scen_g.numSheets--; for(; which_pic < spec_scen_g.numSheets; which_pic++) { @@ -3823,8 +3830,8 @@ void edit_custom_sheets() { }else{ all_pics.erase(all_pics.begin() + cur); } - fs::path fpath = pic_dir/("sheet" + std::to_string(which_pic) + ".png"); if(fs::exists(fpath)) fs::remove(fpath); + undo_list.add(action_ptr(new aDeleteGraphicsSheet(which_pic, moved, image_for_undo))); if(all_pics.size() == 1) { me["left"].hide(); me["right"].hide(); diff --git a/src/scenedit/scen.undo.cpp b/src/scenedit/scen.undo.cpp index cde4604a..90659b60 100644 --- a/src/scenedit/scen.undo.cpp +++ b/src/scenedit/scen.undo.cpp @@ -3,6 +3,7 @@ #include #include "gfx/gfxsheets.hpp" +#include "fileio/resmgr/res_image.hpp" #include "scenario/scenario.hpp" #include "scenario/area.hpp" @@ -737,10 +738,11 @@ extern cCustomGraphics spec_scen_g; bool aCreateGraphicsSheet::undo_me() { fs::path sheetPath = get_pic_dir()/("sheet" + std::to_string(index) + ".png"); - fs::remove(sheetPath); + if(fs::exists(sheetPath)) fs::remove(sheetPath); if(index == spec_scen_g.numSheets - 1) { spec_scen_g.sheets.pop_back(); spec_scen_g.numSheets--; + ResMgr::graphics.free("sheet" + std::to_string(index)); } return true; } @@ -757,5 +759,51 @@ bool aCreateGraphicsSheet::redo_me() { img.create(280, 360); img.saveToFile(sheetPath.string().c_str()); } + return true; +} + +bool aDeleteGraphicsSheet::undo_me() { + fs::path sheetPath = get_pic_dir()/("sheet" + std::to_string(index) + ".png"); + // Undo shifting other sheets left + if(move_others && fs::exists(sheetPath)){ + for(int other_index = spec_scen_g.numSheets - 1; other_index >= index; --other_index){ + int old_index = other_index + 1; + fs::path from = get_pic_dir()/("sheet" + std::to_string(other_index) + ".png"); + fs::path to = get_pic_dir()/("sheet" + std::to_string(old_index) + ".png"); + if(!fs::exists(from)) continue; // Just in case + fs::remove(to); + fs::rename(from, to); + } + } + + image.saveToFile(sheetPath.string().c_str()); + if(index <= spec_scen_g.numSheets) spec_scen_g.numSheets++; + return true; +} + +bool aDeleteGraphicsSheet::redo_me() { + fs::path sheetPath = get_pic_dir()/("sheet" + std::to_string(index) + ".png"); + + if(index < spec_scen_g.numSheets){ + if(move_others){ + spec_scen_g.sheets.erase(spec_scen_g.sheets.begin() + index); + spec_scen_g.numSheets--; + for(int which_pic = index; which_pic < spec_scen_g.numSheets; which_pic++) { + fs::path from = get_pic_dir()/("sheet" + std::to_string(which_pic + 1) + ".png"); + fs::path to = get_pic_dir()/("sheet" + std::to_string(which_pic) + ".png"); + if(!fs::exists(from)) continue; // Just in case + fs::remove(to); + fs::rename(from, to); + ResMgr::graphics.free("sheet" + std::to_string(which_pic)); + } + }else{ + spec_scen_g.numSheets = index; + spec_scen_g.sheets.resize(index); + ResMgr::graphics.free("sheet" + std::to_string(index)); + } + } + + if(fs::exists(sheetPath)) fs::remove(sheetPath); + return true; } \ No newline at end of file diff --git a/src/scenedit/scen.undo.hpp b/src/scenedit/scen.undo.hpp index e605daf7..86b4429a 100644 --- a/src/scenedit/scen.undo.hpp +++ b/src/scenedit/scen.undo.hpp @@ -463,4 +463,16 @@ public: cAction("Create Custom Graphics Sheet"), index(index) {} }; +/// Action that deletes a custom graphics sheet +class aDeleteGraphicsSheet : public cAction { + size_t index; + bool move_others; + sf::Image image; + bool undo_me() override; + bool redo_me() override; +public: + aDeleteGraphicsSheet(size_t index, bool move_others, sf::Image image) : + cAction("Delete Custom Graphics Sheet"), index(index), image(image), move_others(move_others) {} +}; + #endif \ No newline at end of file