Files
anm2ed/src/anm2/sound.cpp
T

67 lines
1.8 KiB
C++

#include "sound.hpp"
#include "path_.hpp"
#include "working_directory.hpp"
#include "xml_.hpp"
using namespace anm2ed::resource;
using namespace anm2ed::util;
using namespace tinyxml2;
namespace anm2ed::anm2
{
Sound::Sound(const Sound& other) : path(other.path), audio(other.audio) {}
Sound& Sound::operator=(const Sound& other)
{
if (this != &other)
{
path = other.path;
audio = other.audio;
}
return *this;
}
Sound::Sound(const std::filesystem::path& directory, const std::filesystem::path& path)
{
WorkingDirectory workingDirectory(directory);
this->path = !path.empty() ? path::make_relative(path) : this->path;
this->path = path::lower_case_backslash_handle(this->path);
audio = Audio(this->path);
}
Sound::Sound(XMLElement* element, int& id)
{
if (!element) return;
element->QueryIntAttribute("Id", &id);
xml::query_path_attribute(element, "Path", &path);
path = path::lower_case_backslash_handle(path);
audio = Audio(path);
}
XMLElement* Sound::to_element(XMLDocument& document, int id)
{
auto element = document.NewElement("Sound");
element->SetAttribute("Id", id);
auto pathString = path::to_utf8(path);
element->SetAttribute("Path", pathString.c_str());
return element;
}
void Sound::serialize(XMLDocument& document, XMLElement* parent, int id)
{
parent->InsertEndChild(to_element(document, id));
}
std::string Sound::to_string(int id)
{
XMLDocument document{};
document.InsertEndChild(to_element(document, id));
return xml::document_to_string(document);
}
void Sound::reload(const std::filesystem::path& directory) { *this = Sound(directory, this->path); }
bool Sound::is_valid() { return audio.is_valid(); }
void Sound::play() { audio.play(); }
}