undo/redo place/delete boat or horse

This commit is contained in:
2025-06-11 17:09:33 -05:00
parent 0dfddae5f2
commit b8ef14f186
3 changed files with 54 additions and 3 deletions

View File

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

View File

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

View File

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