From 3f8eed39c7a036d8aeef0fa5ffb70c86cb3f7c76 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Sat, 14 Jun 2025 09:17:32 -0500 Subject: [PATCH] undo/redo import town --- src/scenedit/scen.main.cpp | 3 ++- src/scenedit/scen.undo.cpp | 24 ++++++++++++++++++++++++ src/scenedit/scen.undo.hpp | 13 +++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/scenedit/scen.main.cpp b/src/scenedit/scen.main.cpp index a6b8c0ba..f578b2ca 100644 --- a/src/scenedit/scen.main.cpp +++ b/src/scenedit/scen.main.cpp @@ -649,7 +649,8 @@ void handle_menu_choice(eMenu item_hit) { case eMenu::TOWN_IMPORT: if(cTown* town = pick_import_town()) { town->reattach(scenario); - delete scenario.towns[cur_town]; + undo_list.add(action_ptr(new aImportTown(cur_town, scenario.towns[cur_town], town))); + update_edit_menu(); scenario.towns[cur_town] = town; ::town = town; change_made = true; diff --git a/src/scenedit/scen.undo.cpp b/src/scenedit/scen.undo.cpp index f54dddc9..63433a54 100644 --- a/src/scenedit/scen.undo.cpp +++ b/src/scenedit/scen.undo.cpp @@ -540,4 +540,28 @@ bool aResizeOutdoors::redo_me() { cur_out.y += mod.top; clamp_current_section(); return true; +} + +// The action is cleared from the tree, so erase objects it owns +aImportTown::~aImportTown() { + // If the import happened, delete the old town + if(isDone()){ + delete old_town; + } + // If it was undone, delete the new town + else{ + delete new_town; + } +} + +bool aImportTown::undo_me() { + scenario.towns[which] = old_town; + set_current_town(which); + return true; +} + +bool aImportTown::redo_me() { + scenario.towns[which] = new_town; + set_current_town(which); + return true; } \ No newline at end of file diff --git a/src/scenedit/scen.undo.hpp b/src/scenedit/scen.undo.hpp index 4e93ed15..d3add663 100644 --- a/src/scenedit/scen.undo.hpp +++ b/src/scenedit/scen.undo.hpp @@ -356,4 +356,17 @@ public: ~aResizeOutdoors(); }; +/// Action which imports a town from another scenario +class aImportTown : public cAction { + size_t which; + cTown* old_town; + cTown* new_town; + bool undo_me() override; + bool redo_me() override; +public: + aImportTown(size_t which, cTown* old_town, cTown* new_town) : + cAction("Import Town"), which(which), old_town(old_town), new_town(new_town) {} + ~aImportTown(); +}; + #endif \ No newline at end of file