From b8ef14f186c3f502dd418ec9f9c5ee0f56f45e38 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Wed, 11 Jun 2025 17:09:33 -0500 Subject: [PATCH] undo/redo place/delete boat or horse --- src/scenedit/scen.actions.cpp | 30 +++++++++++++++++++++++++++--- src/scenedit/scen.undo.cpp | 12 ++++++++++++ src/scenedit/scen.undo.hpp | 15 +++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/scenedit/scen.actions.cpp b/src/scenedit/scen.actions.cpp index a192f2ab..d46f1b83 100644 --- a/src/scenedit/scen.actions.cpp +++ b/src/scenedit/scen.actions.cpp @@ -1103,6 +1103,7 @@ static bool handle_terrain_action(location the_point, bool ctrl_hit) { change_made = true; break; case MODE_PLACE_BOAT: case MODE_PLACE_HORSE: { + bool is_new = false; auto& all = overall_mode == MODE_PLACE_BOAT ? scenario.boats : scenario.horses; auto iter = std::find_if(all.begin(), all.end(), [](const cVehicle& what) { if(editing_town && cur_town != what.which_town) return false; @@ -1117,18 +1118,41 @@ static bool handle_terrain_action(location the_point, bool ctrl_hit) { all.emplace_back(); iter = all.end() - 1; } + is_new = true; iter->loc = spot_hit; iter->which_town = editing_town ? cur_town : 200; iter->property = false; iter->exists = false; if(!editing_town) iter->sector = cur_out; } - if(!edit_vehicle(*iter, iter - all.begin(), overall_mode == MODE_PLACE_BOAT)){ + if(edit_vehicle(*iter, iter - all.begin(), overall_mode == MODE_PLACE_BOAT)){ + // Created new + if(is_new){ + undo_list.add(action_ptr(new aPlaceEraseVehicle(true, overall_mode == MODE_PLACE_BOAT, iter - all.begin(), *iter))); + update_edit_menu(); + } + // Edited + else{ + + } + } + else{ + // Edit existing--delete chosen + if(!is_new){ + undo_list.add(action_ptr(new aPlaceEraseVehicle(false, overall_mode == MODE_PLACE_BOAT, iter - all.begin(), *iter))); + update_edit_menu(); + } + // Create new canceled or delete chosen + else{ + // Nothing needs to be recorded + } + // Vehicle can be deleted completely without offseting other vehicle numbers if(iter == (all.end() - 1)){ all.erase(iter); - }else{ + } + // Vehicle can't be fully deleted + else{ *iter = cVehicle(); - iter->is_boat = (overall_mode == MODE_PLACE_BOAT); } } overall_mode = MODE_DRAWING; diff --git a/src/scenedit/scen.undo.cpp b/src/scenedit/scen.undo.cpp index cf1e74a9..90b466da 100644 --- a/src/scenedit/scen.undo.cpp +++ b/src/scenedit/scen.undo.cpp @@ -246,6 +246,18 @@ bool aPlaceEraseCreature::redo_me() { return true; } +bool aPlaceEraseVehicle::undo_me() { + auto& all = is_boat ? scenario.boats : scenario.horses; + all[which] = cVehicle(); + return true; +} + +bool aPlaceEraseVehicle::redo_me() { + auto& all = is_boat ? scenario.boats : scenario.horses; + all[which] = vehicle; + return true; +} + bool aEditSignText::undo_me() { cArea* cur_area = get_current_area(); auto& signs = cur_area->sign_locs; diff --git a/src/scenedit/scen.undo.hpp b/src/scenedit/scen.undo.hpp index a0e69989..444c587c 100644 --- a/src/scenedit/scen.undo.hpp +++ b/src/scenedit/scen.undo.hpp @@ -7,6 +7,7 @@ #include "scenario/scenario.hpp" #include "scenario/item.hpp" #include "scenario/monster.hpp" +#include "scenario/vehicle.hpp" extern cScenario scenario; @@ -94,6 +95,20 @@ public: aPlaceEraseCreature(std::string name, bool place, size_t index, cTownperson creature); }; +/// Action which places or erases vehicles +class aPlaceEraseVehicle : public cTerrainAction { + bool placed; + bool is_boat; + size_t which; + cVehicle vehicle; + bool undo_me() override; + bool redo_me() override; +public: + aPlaceEraseVehicle(bool place, bool is_boat, size_t which, cVehicle vehicle) : + cTerrainAction(std::string { place ? "Place " : "Erase " } + (is_boat ? "Boat" : "Horse"), vehicle.loc, !place), + is_boat(is_boat), which(which), vehicle(vehicle) {} +}; + /// Action which edits sign text class aEditSignText : public cTerrainAction { std::string old_text;