undo/redo for editing item shortcuts

This commit is contained in:
2025-06-19 17:07:53 -05:00
parent 1db78b4a1b
commit 8d26268e7d
5 changed files with 31 additions and 11 deletions

View File

@@ -649,4 +649,15 @@ std::string cScenario::get_sdf_name(int row, int col) {
if(sdf_names[row].find(col) == sdf_names[row].end())
return "";
return sdf_names[row][col];
}
}
bool cScenario::cItemStorage::operator==(const cScenario::cItemStorage& other) const {
CHECK_EQ(other, ter_type);
CHECK_EQ(other, property);
for(int i = 0; i < 10; ++i){
if(item_num[i] != other.item_num[i]) return false;
if(item_odds[i] != other.item_odds[i]) return false;
}
return true;
}

View File

@@ -87,6 +87,8 @@ public:
short property;
cItemStorage();
cItemStorage& operator = (legacy::item_storage_shortcut_type& old);
bool operator==(const cItemStorage& other) const;
bool operator!=(const cItemStorage& other) { return !(*this == other); }
};
void destroy_terrain();
public:

View File

@@ -2997,7 +2997,12 @@ static bool save_item_placement(cDialog& me, cScenario::cItemStorage& storage, s
if(cre(storage.item_odds[i],
0,100,"All item chances must bve from 0 to 100.","",&me)) return false;
}
scenario.storage_shortcuts[which] = storage;
if(storage != scenario.storage_shortcuts[which]){
// the edit menu will update when the dialog closes
undo_list.add(action_ptr(new aEditItemShortcut(which, scenario.storage_shortcuts[which], storage)));
scenario.storage_shortcuts[which] = storage;
}
return true;
}
@@ -3086,6 +3091,7 @@ void edit_item_placement() {
put_item_placement_in_dlog(shortcut_dlg, storage, cur_shortcut);
shortcut_dlg.run();
update_edit_menu();
}
static bool save_scen_details(cDialog& me, std::string, eKeyMod) {

View File

@@ -920,12 +920,12 @@ bool aEditScenTimers::redo_me() {
return true;
}
bool aEditItemShortcuts::undo_me() {
scenario.storage_shortcuts = old_shortcuts;
bool aEditItemShortcut::undo_me() {
scenario.storage_shortcuts[which] = old_shortcut;
return true;
}
bool aEditItemShortcuts::redo_me() {
scenario.storage_shortcuts = new_shortcuts;
bool aEditItemShortcut::redo_me() {
scenario.storage_shortcuts[which] = new_shortcut;
return true;
}

View File

@@ -572,14 +572,15 @@ public:
cAction("Edit Scenario Event Timers"), old_timers(old_timers), new_timers(new_timers) {}
};
class aEditItemShortcuts : public cAction {
std::array<cScenario::cItemStorage,10> old_shortcuts;
std::array<cScenario::cItemStorage,10> new_shortcuts;
class aEditItemShortcut : public cAction {
size_t which;
cScenario::cItemStorage old_shortcut;
cScenario::cItemStorage new_shortcut;
bool undo_me() override;
bool redo_me() override;
public:
aEditItemShortcuts(std::array<cScenario::cItemStorage,10> old_shortcuts, std::array<cScenario::cItemStorage,10> new_shortcuts) :
cAction("Edit Item Placement Shortcuts"), old_shortcuts(old_shortcuts), new_shortcuts(new_shortcuts) {}
aEditItemShortcut(size_t which, cScenario::cItemStorage old_shortcut, cScenario::cItemStorage new_shortcut) :
cAction("Edit Item Placement Shortcut"), which(which), old_shortcut(old_shortcut), new_shortcut(new_shortcut) {}
};
#endif