undo/redo import town

This commit is contained in:
2025-06-14 09:17:32 -05:00
parent 6fd25f8f4f
commit 3f8eed39c7
3 changed files with 39 additions and 1 deletions

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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