From 38d47012268888cf2d22eaaa5b3b4937b844d010 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Fri, 6 Jun 2025 13:20:09 -0500 Subject: [PATCH] undo/redo editing sign text --- src/scenedit/scen.townout.cpp | 22 ++++++++++++---------- src/scenedit/scen.undo.cpp | 16 ++++++++++++++++ src/scenedit/scen.undo.hpp | 13 +++++++++++++ 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/scenedit/scen.townout.cpp b/src/scenedit/scen.townout.cpp index 9958c2d2..93ae6a38 100644 --- a/src/scenedit/scen.townout.cpp +++ b/src/scenedit/scen.townout.cpp @@ -425,16 +425,18 @@ void edit_placed_item(short which_i) { static bool edit_sign_event_filter(cDialog& me, sign_loc_t& which_sign) { if(!me.toast(true)) return true; - which_sign.text = me["text"].getText(); -#if 0 // TODO: Apparently there used to be left/right buttons on this dialog. - if(item_hit == 3) - which_sign--; - else which_sign++; - if(which_sign < 0) - which_sign = (editing_town) ? 14 : 7; - if(which_sign > (editing_town) ? 14 : 7) - which_sign = 0; -#endif + + std::string cur_text = me["text"].getText(); + if(which_sign.text != cur_text){ + undo_list.add(action_ptr(new aEditSignText(which_sign, which_sign.text, cur_text))); + update_edit_menu(); + which_sign.text = cur_text; + } + + // There used to be left/right buttons in the sign editor dialog, but there shouldn't be, + // because left/right is not a meaningful index to let the designer know which sign they're editing. + // I removed the unused code. + return true; } diff --git a/src/scenedit/scen.undo.cpp b/src/scenedit/scen.undo.cpp index 1ae6ee3f..e74aa286 100644 --- a/src/scenedit/scen.undo.cpp +++ b/src/scenedit/scen.undo.cpp @@ -244,3 +244,19 @@ bool aPlaceEraseCreature::redo_me() { } return true; } + +bool aEditSignText::undo_me() { + cArea* cur_area = get_current_area(); + auto& signs = cur_area->sign_locs; + auto iter = std::find(signs.begin(), signs.end(), area.where); + iter->text = old_text; + return true; +} + +bool aEditSignText::redo_me() { + cArea* cur_area = get_current_area(); + auto& signs = cur_area->sign_locs; + auto iter = std::find(signs.begin(), signs.end(), area.where); + iter->text = new_text; + return true; +} \ No newline at end of file diff --git a/src/scenedit/scen.undo.hpp b/src/scenedit/scen.undo.hpp index 5ebfc2bb..0e7719e6 100644 --- a/src/scenedit/scen.undo.hpp +++ b/src/scenedit/scen.undo.hpp @@ -35,6 +35,7 @@ class cTerrainAction : public cAction { public: cTerrainAction(std::string name, short town_num, location where, bool reversed = false); cTerrainAction(std::string name, location out_sec, location where, bool reversed = false); + // Construct cTerrainAction in the current town/outdoor section cTerrainAction(std::string name, location where, bool reversed = false); void undo(); void redo(); @@ -43,6 +44,7 @@ public: private: /// Show where the change happened void showChangeSite(); +protected: area_ref_t area; }; @@ -92,6 +94,17 @@ public: aPlaceEraseCreature(std::string name, bool place, size_t index, cTownperson creature); }; +/// Action which edits sign text +class aEditSignText : public cTerrainAction { + std::string old_text; + std::string new_text; + bool undo_me() override; + bool redo_me() override; +public: + aEditSignText(location loc, std::string old_text, std::string new_text) : + cTerrainAction("Edit Sign Text", loc), old_text(old_text), new_text(new_text) {} +}; + /// Action which adds a new town to the end of the list, or deletes the last one class aCreateDeleteTown : public cAction { bool created;