delete last town is just the inverse of create town

This commit is contained in:
2025-06-02 16:52:32 -05:00
parent 5452057b25
commit f4aa9780e0
6 changed files with 36 additions and 26 deletions

View File

@@ -48,6 +48,7 @@ extern cUndoList undo_list;
extern std::string help_text_rsrc;
extern bool editing_town;
extern bool last_shift_continuous;
extern void update_edit_menu();
const char *day_str_1[] = {"Unused","Day creature appears","Day creature disappears",
"Unused","Unused","Unused","Unused","Unused","Unused"};
@@ -1671,7 +1672,8 @@ bool new_town() {
for(short j = 0; j < town->max_dim; j++)
town->terrain(i,j) = preset;
undo_list.add(action_ptr(new aNewTown(scenario.towns.back())));
undo_list.add(action_ptr(new aCreateDeleteTown(true, scenario.towns.back())));
update_edit_menu();
change_made = true;
return true;
@@ -1679,9 +1681,10 @@ bool new_town() {
// before calling this, be sure to do all checks to make sure it's safe.
void delete_last_town() {
cTown* last_town = scenario.towns.back();
undo_list.add(action_ptr(new aCreateDeleteTown(false, scenario.towns.back())));
update_edit_menu();
scenario.towns.pop_back();
delete last_town;
change_made = true;
}

View File

@@ -14,6 +14,7 @@ extern cArea* get_current_area();
extern void start_town_edit();
extern void start_out_edit();
extern void redraw_screen();
extern void set_current_town(int,bool first_restore = false);
cTerrainAction::cTerrainAction(std::string name, short town_num, location where) : cAction(name) {
area.is_town = true;
@@ -72,24 +73,25 @@ bool aEraseSpecial::redo_me() {
return true;
}
aNewTown::aNewTown(cTown* t)
: cAction("Add Town")
aCreateDeleteTown::aCreateDeleteTown(bool create, cTown* t)
: cAction(create ? "Create Town" : "Delete Last Town", !create)
, theTown(t)
{}
bool aNewTown::undo_me() {
bool aCreateDeleteTown::undo_me() {
scenario.towns.resize(scenario.towns.size() - 1);
cur_town = min(cur_town, scenario.towns.size() - 1);
set_current_town(min(cur_town, scenario.towns.size() - 1));
return true;
}
bool aNewTown::redo_me() {
bool aCreateDeleteTown::redo_me() {
scenario.towns.push_back(theTown);
set_current_town(scenario.towns.size() - 1);
return true;
}
aNewTown::~aNewTown() {
if(!isDone()) delete theTown;
aCreateDeleteTown::~aCreateDeleteTown() {
if(isDone() == reversed) delete theTown;
}
bool aDrawTerrain::undo_me() {

View File

@@ -19,7 +19,7 @@ struct ter_change_t {
typedef std::map<location,ter_change_t,loc_compare> stroke_ter_changes_t;
// Action that modified town or outdoor terrain, so we should show the modified area when undoing or redoing
// Action that modified something in town or outdoor terrain, so we should show the modified area when undoing or redoing
class cTerrainAction : public cAction {
public:
cTerrainAction(std::string name, short town_num, location where);
@@ -35,16 +35,7 @@ private:
area_ref_t area;
};
/// Represents the action of adding a new town to the end of the list
class aNewTown : public cAction {
class cTown* theTown;
bool undo_me() override;
bool redo_me() override;
public:
aNewTown(class cTown* t);
~aNewTown();
};
/// Action that erased a special encounter from a spot
class aEraseSpecial : public cTerrainAction {
public:
aEraseSpecial(spec_loc_t special) : cTerrainAction("Erase Special Encounter", special), for_redo(special) {}
@@ -55,6 +46,7 @@ private:
bool editing_town;
};
/// Action which modifies terrain tiles (i.e. paintbrush, pencil, eraser)
class aDrawTerrain : public cTerrainAction {
public:
aDrawTerrain(std::string name, stroke_ter_changes_t stroke_changes) :
@@ -66,4 +58,16 @@ private:
const stroke_ter_changes_t changes;
};
/// Action which adds a new town to the end of the list, or deletes the last one
class aCreateDeleteTown : public cAction {
bool created;
class cTown* theTown;
bool undo_me() override;
bool redo_me() override;
public:
aCreateDeleteTown(bool create, class cTown* t);
~aCreateDeleteTown();
};
#endif