undo/redo create/delete custom sound

This commit is contained in:
2025-06-18 11:50:56 -05:00
parent 7ed3f36351
commit 5d4ff5134d
3 changed files with 41 additions and 0 deletions

View File

@@ -3972,8 +3972,12 @@ static bool edit_custom_sound_action(cDialog& me, std::string action, std::vecto
} else if(action == "del") {
if(which_snd - 100 < snd_names.size())
snd_names[which_snd - 100].clear();
sf::SoundBuffer sound_for_undo;
sound_for_undo.loadFromFile(sndfile.string());
fs::remove(sndfile);
me["name" + std::to_string(which_snd % 10)].setText("");
undo_list.add(action_ptr(new aCreateDeleteSound(false, which_snd, sound_for_undo)));
} else if(action == "open") {
fs::path fpath = nav_get_rsrc({"wav"});
if(fpath.empty()) return true;
@@ -3982,6 +3986,14 @@ static bool edit_custom_sound_action(cDialog& me, std::string action, std::vecto
beep();
return true;
}
// Replace custom sound
if(fs::exists(sndfile)){
}
// First import
else{
undo_list.add(action_ptr(new aCreateDeleteSound(true, which_snd, snd)));
}
fs::copy_file(fpath, sndfile, fs::copy_options::overwrite_existing);
ResMgr::sounds.free(sound_to_fname(which_snd));
if(which_snd > max_snd)
@@ -4058,6 +4070,8 @@ void edit_custom_sounds() {
get_sound_names_from_dlg(snd_dlg, snd_names, curPage);
snd_names.swap(scenario.snd_names);
}
update_edit_menu();
}
fs::path run_file_picker(bool saving){

View File

@@ -4,6 +4,8 @@
#include "gfx/gfxsheets.hpp"
#include "fileio/resmgr/res_image.hpp"
#include "fileio/resmgr/res_sound.hpp"
#include "sounds.hpp"
#include "scenario/scenario.hpp"
#include "scenario/area.hpp"
@@ -835,3 +837,15 @@ bool aReplaceGraphicsSheet::redo_me() {
return true;
}
extern fs::path get_snd_path(size_t index);
bool aCreateDeleteSound::undo_me() {
fs::remove(get_snd_path(index).string());
return true;
}
bool aCreateDeleteSound::redo_me() {
ResMgr::sounds.free(sound_to_fname(index));
return sound.saveToFile(get_snd_path(index).string());
}

View File

@@ -1,6 +1,8 @@
#ifndef BoE_scen_undo_h
#define BoE_scen_undo_h
#include <SFML/Audio/SoundBuffer.hpp>
#include "location.hpp"
#include "tools/undo.hpp"
#include "scenario/town.hpp"
@@ -487,4 +489,15 @@ public:
cAction(name), index(index), old_image(old_image), new_image(new_image) {}
};
class aCreateDeleteSound : public cAction {
size_t index;
sf::SoundBuffer sound;
bool undo_me() override;
bool redo_me() override;
public:
aCreateDeleteSound(bool create, size_t index, sf::SoundBuffer sound) :
cAction(create ? "Import Custom Sound" : "Delete Custom Sound", !create),
index(index), sound(sound) {}
};
#endif