undo/redo create graphics sheet

This commit is contained in:
2025-06-17 16:27:09 -05:00
parent a80c9e9367
commit 74c9439312
3 changed files with 59 additions and 0 deletions

View File

@@ -3675,6 +3675,9 @@ void edit_custom_sheets() {
spec_scen_g.sheets[0]->copyToImage().saveToFile((pic_dir/"sheet0.png").string().c_str());
all_pics.insert(all_pics.begin(), 0);
ResMgr::graphics.pushPath(pic_dir);
// We'll update the edit menu after this dialog closes
undo_list.add(action_ptr(new aCreateGraphicsSheet(0)));
}
set_cursor(watch_curs);
@@ -3780,6 +3783,10 @@ void edit_custom_sheets() {
me["left"].show();
me["right"].show();
set_dlg_custom_sheet(me, all_pics[cur]);
// We'll update the edit menu after this dialog closes
undo_list.add(action_ptr(new aCreateGraphicsSheet(newSheet)));
return true;
});
pic_dlg["del"].attachClickHandler([&sheets,&cur,&all_pics,&pic_dir](cDialog& me, std::string, eKeyMod) -> bool {
@@ -3863,6 +3870,8 @@ void edit_custom_sheets() {
if(overall_mode <= MODE_MAIN_SCREEN)
shut_down_menus(editing_town ? 2 : 1);
else shut_down_menus(3);
update_edit_menu();
}
static bool edit_custom_sound_action(cDialog& me, std::string action, std::vector<std::string>& snd_names, int curPage, int& max_snd) {

View File

@@ -1,5 +1,9 @@
#include "scen.undo.hpp"
#include <boost/filesystem.hpp>
#include "gfx/gfxsheets.hpp"
#include "scenario/scenario.hpp"
#include "scenario/area.hpp"
#include "scen.actions.hpp"
@@ -718,4 +722,40 @@ bool aToggleOutFields::redo_me() {
current_terrain->special_spot[space.x][space.y] = on;
}
return true;
}
fs::path get_pic_dir() {
extern fs::path tempDir;
extern std::string scenario_temp_dir_name;
fs::path pic_dir = tempDir/scenario_temp_dir_name/"graphics";
if(!scenario.scen_file.has_extension()) // It's an unpacked scenario
pic_dir = scenario.scen_file/"graphics";
return pic_dir;
}
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(index == spec_scen_g.numSheets - 1) {
spec_scen_g.sheets.pop_back();
spec_scen_g.numSheets--;
}
return true;
}
bool aCreateGraphicsSheet::redo_me() {
fs::path sheetPath = get_pic_dir()/("sheet" + std::to_string(index) + ".png");
if(index == spec_scen_g.numSheets) {
spec_scen_g.sheets.emplace_back();
spec_scen_g.init_sheet(index);
spec_scen_g.sheets[index]->copyToImage().saveToFile(sheetPath.string().c_str());
spec_scen_g.numSheets++;
}else{
sf::Image img;
img.create(280, 360);
img.saveToFile(sheetPath.string().c_str());
}
return true;
}

View File

@@ -453,4 +453,14 @@ public:
is_road(is_road), on(on), stroke(stroke) {}
};
/// Action that creates a custom graphics sheet
class aCreateGraphicsSheet : public cAction {
size_t index;
bool undo_me() override;
bool redo_me() override;
public:
aCreateGraphicsSheet(size_t index) :
cAction("Create Custom Graphics Sheet"), index(index) {}
};
#endif