From 74c94393125fdced0a38d177a32379e3318b9578 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Tue, 17 Jun 2025 16:27:09 -0500 Subject: [PATCH] undo/redo create graphics sheet --- src/scenedit/scen.core.cpp | 9 +++++++++ src/scenedit/scen.undo.cpp | 40 ++++++++++++++++++++++++++++++++++++++ src/scenedit/scen.undo.hpp | 10 ++++++++++ 3 files changed, 59 insertions(+) diff --git a/src/scenedit/scen.core.cpp b/src/scenedit/scen.core.cpp index 080c8cb9..1b4ca471 100644 --- a/src/scenedit/scen.core.cpp +++ b/src/scenedit/scen.core.cpp @@ -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& snd_names, int curPage, int& max_snd) { diff --git a/src/scenedit/scen.undo.cpp b/src/scenedit/scen.undo.cpp index 2f2daf88..cde4604a 100644 --- a/src/scenedit/scen.undo.cpp +++ b/src/scenedit/scen.undo.cpp @@ -1,5 +1,9 @@ #include "scen.undo.hpp" +#include + +#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; } \ No newline at end of file diff --git a/src/scenedit/scen.undo.hpp b/src/scenedit/scen.undo.hpp index 7d96a3e5..e605daf7 100644 --- a/src/scenedit/scen.undo.hpp +++ b/src/scenedit/scen.undo.hpp @@ -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 \ No newline at end of file