From 07b6044d3677764141fcd75c03152a585282f0f8 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Thu, 12 Jun 2025 10:47:05 -0500 Subject: [PATCH] undo/redo create/delete special item --- src/scenedit/scen.actions.cpp | 27 +++++++++++++++++++++++++-- src/scenedit/scen.undo.cpp | 11 +++++++++++ src/scenedit/scen.undo.hpp | 11 +++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/scenedit/scen.actions.cpp b/src/scenedit/scen.actions.cpp index 43325fc8..bb453ec6 100644 --- a/src/scenedit/scen.actions.cpp +++ b/src/scenedit/scen.actions.cpp @@ -475,21 +475,44 @@ static bool handle_rb_action(location the_point, bool option_hit) { case RB_SPEC_ITEM: size_before = scenario.special_items.size(); if(option_hit) { - if(j == size_before - 1) + // Delete last special item + if(j == size_before - 1){ + undo_list.add(action_ptr(new aCreateDeleteSpecialItem(false, scenario.special_items.back()))); + update_edit_menu(); scenario.special_items.pop_back(); + } else if(j == size_before) break; + // Clear special item (it can't be deleted fully) else { + // TODO undo action scenario.special_items[j] = cSpecItem(); scenario.special_items[j].name = "Unused Special Item"; } } else { + bool is_new = false; + // Create new special item if(j == size_before) { + is_new = true; scenario.special_items.emplace_back(); scenario.special_items.back().name = "New Special Item"; } - if(!edit_spec_item(j) && j == size_before) + + if(edit_spec_item(j)){ + // Create new confirmed + if(is_new){ + undo_list.add(action_ptr(new aCreateDeleteSpecialItem(true, scenario.special_items.back()))); + update_edit_menu(); + } + // Special item edited + else{ + // TODO undo action + } + } + // Create new canceled + else if(is_new){ scenario.special_items.pop_back(); + } } if(size_before > scenario.special_items.size()) pos_before--; diff --git a/src/scenedit/scen.undo.cpp b/src/scenedit/scen.undo.cpp index dea5a3b5..204919fe 100644 --- a/src/scenedit/scen.undo.cpp +++ b/src/scenedit/scen.undo.cpp @@ -329,4 +329,15 @@ bool aEditPlacedCreature::undo_me() { bool aEditPlacedCreature::redo_me() { town->creatures[which] = new_creature; return true; +} + +bool aCreateDeleteSpecialItem::undo_me() { + scenario.special_items.pop_back(); + + return true; +} + +bool aCreateDeleteSpecialItem::redo_me() { + scenario.special_items.push_back(item); + return true; } \ No newline at end of file diff --git a/src/scenedit/scen.undo.hpp b/src/scenedit/scen.undo.hpp index 2442f29a..22375548 100644 --- a/src/scenedit/scen.undo.hpp +++ b/src/scenedit/scen.undo.hpp @@ -234,6 +234,17 @@ public: items(items) {} }; +/// Action which adds new special item to the end of the list, or deletes from the end of the list +class aCreateDeleteSpecialItem : public cAction { + cSpecItem item; + bool undo_me() override; + bool redo_me() override; +public: + aCreateDeleteSpecialItem(bool create, cSpecItem item) : + cAction(create ? "Create Special Item" : "Delete Special Item", !create), + item(item) {} +}; + /// Action which edits or clears a terrain type class aEditClearTerrain : public cAction { ter_num_t which;