Add AudioResource

This commit is contained in:
Florian Rival
2015-11-18 22:57:45 +01:00
parent 1e4f622b5c
commit 780a022029
11 changed files with 101 additions and 41 deletions

View File

@@ -1029,7 +1029,7 @@ bool DndTextSpriteObjectEditor::OnDropText(wxCoord x, wxCoord y, const wxString&
//Add ressources dragged from the library dialog to the project.
std::vector<gd::String> files;
for (std::size_t i = 2;i<command.size();++i) files.push_back(command[i]);
std::vector<gd::String> names = editor.resourcesEditorPnl->CopyAndAddResources(files, command[1]);
std::vector<gd::String> names = editor.resourcesEditorPnl->CopyAndAddResources(files, command[1], "image");
//And add them as usual to the animation.
for (std::size_t i = 0;i<names.size();++i)

View File

@@ -30,7 +30,7 @@ bool DndTextResourcesEditor::OnDropText(wxCoord x, wxCoord y, const wxString& te
for (std::size_t i = 2;i<command.size();++i)
files.push_back(command[i]);
editor.CopyAndAddResources(files, command[1]);
editor.CopyAndAddResources(files, command[1], "image");
}
else
std::cout << "Drop was triggered but the command was not understood" << std::endl;

View File

@@ -363,14 +363,15 @@ void ResourcesEditor::OnAddImageBtClick( wxCommandEvent& event )
for ( std::size_t i = 0; i < files.GetCount();++i )
filenames.push_back(files[i]);
AddResources(filenames);
AddResources(filenames, "image");
gd::LogStatus( _( "Resources successfully added" ) );
}
}
std::vector<gd::String> ResourcesEditor::CopyAndAddResources(std::vector<gd::String> filenames, const gd::String & destinationDirStr)
std::vector<gd::String> ResourcesEditor::CopyAndAddResources(std::vector<gd::String> filenames,
const gd::String & destinationDirStr, const gd::String & kind)
{
if ( !project.GetProjectFile().empty() ) //If game is not saved, we keep absolute filenames and do not copy resources.
{
@@ -392,10 +393,10 @@ std::vector<gd::String> ResourcesEditor::CopyAndAddResources(std::vector<gd::Str
}
}
return AddResources(filenames);
return AddResources(filenames, kind);
}
std::vector<gd::String> ResourcesEditor::AddResources(const std::vector<gd::String> & filenames)
std::vector<gd::String> ResourcesEditor::AddResources(const std::vector<gd::String> & filenames, const gd::String & kind)
{
std::vector<gd::String> resourceNames;
gd::String alreadyExistingResources;
@@ -423,7 +424,7 @@ std::vector<gd::String> ResourcesEditor::AddResources(const std::vector<gd::Stri
gd::LogStatus( _( "Adding " ) + name );
//Add to all images
if ( project.GetResourcesManager().AddResource(name, file.GetFullPath()) )
if ( project.GetResourcesManager().AddResource(name, file.GetFullPath(), kind) )
{
for ( std::size_t j = 0; j < project.GetUsedPlatforms().size();++j)
project.GetUsedPlatforms()[j]->GetChangesNotifier().OnResourceModified(project, name);
@@ -945,7 +946,7 @@ void ResourcesEditor::Refresh()
void ResourcesEditor::OnDeleteUnusedFiles( wxCommandEvent& event )
{
std::vector<gd::String> unusedImages =
gd::ProjectResourcesAdder::GetAllUselessResources(project);
gd::ProjectResourcesAdder::GetAllUselessImages(project);
//Construct corresponding wxArrayString with unused images
wxArrayString imagesNotUsed;

View File

@@ -44,7 +44,7 @@ public:
* Ask the resources editor to add the resources contained into \a filenames.
* \return A vector containing the name of the added resources.
*/
std::vector<gd::String> AddResources(const std::vector<gd::String> & filenames);
std::vector<gd::String> AddResources(const std::vector<gd::String> & filenames, const gd::String & kind);
/**
* Ask the resources editor to add the resources contained into \a filenames.
@@ -52,7 +52,7 @@ public:
* (which is relative to the project directory) before being added to the project.
* \return A vector containing the name of the added resources.
*/
std::vector<gd::String> CopyAndAddResources(std::vector<gd::String> filenames, const gd::String & destinationDir);
std::vector<gd::String> CopyAndAddResources(std::vector<gd::String> filenames, const gd::String & destinationDir, const gd::String & kind);
/**
* \brief Called by DndTextResourcesEditor when one or more resources from the editor has been dropped

View File

@@ -27,14 +27,14 @@ bool ProjectResourcesAdder::AddAllMissingImages(gd::Project & project)
if (!resourcesManager.HasResource(*it))
{
std::cout << "Adding missing resource \""<<*it<<"\"to the project.";
resourcesManager.AddResource(*it, /*filename=*/*it); //Note that AddResource add a image resource by default.
resourcesManager.AddResource(*it, /*filename=*/*it, "image");
}
}
return true;
}
std::vector<gd::String> ProjectResourcesAdder::GetAllUselessResources(gd::Project & project)
std::vector<gd::String> ProjectResourcesAdder::GetAllUselessImages(gd::Project & project)
{
std::vector<gd::String> unusedResources;
@@ -58,9 +58,9 @@ std::vector<gd::String> ProjectResourcesAdder::GetAllUselessResources(gd::Projec
return unusedResources;
}
void ProjectResourcesAdder::RemoveAllUselessResources(gd::Project & project)
void ProjectResourcesAdder::RemoveAllUselessImages(gd::Project & project)
{
std::vector<gd::String> unusedResources = GetAllUselessResources(project);
std::vector<gd::String> unusedResources = GetAllUselessImages(project);
for(std::size_t i = 0;i < unusedResources.size();++i) {
project.GetResourcesManager().RemoveResource(unusedResources[i]);

View File

@@ -41,7 +41,7 @@ public:
*
* \return A vector containing the name of all unused resources
*/
static std::vector<gd::String> GetAllUselessResources(gd::Project & project);
static std::vector<gd::String> GetAllUselessImages(gd::Project & project);
/**
* \brief Remove all resources that are not used
@@ -52,7 +52,7 @@ public:
*
* \param project The project to be crawled.
*/
static void RemoveAllUselessResources(gd::Project & project);
static void RemoveAllUselessImages(gd::Project & project);
};
}

View File

@@ -86,11 +86,11 @@ const Resource & ResourcesManager::GetResource(const gd::String & name) const
std::shared_ptr<Resource> ResourcesManager::CreateResource(const gd::String & kind)
{
if (kind == "image")
{
return std::shared_ptr<Resource>(new ImageResource);
}
else if (kind == "audio")
return std::shared_ptr<Resource>(new AudioResource);
std::cout << "Bad resource created ( type: " << kind << ")" << std::endl;
std::cout << "Bad resource created (type: " << kind << ")" << std::endl;
return std::shared_ptr<Resource>(new Resource);
}
@@ -133,15 +133,15 @@ bool ResourcesManager::AddResource(const gd::Resource & resource)
return true;
}
bool ResourcesManager::AddResource(const gd::String & name, const gd::String & filename)
bool ResourcesManager::AddResource(const gd::String & name, const gd::String & filename, const gd::String & kind)
{
if ( HasResource(name) ) return false;
std::shared_ptr<ImageResource> image(new ImageResource);
image->SetFile(filename);
image->SetName(name);
std::shared_ptr<Resource> res = CreateResource(kind);
res->SetFile(filename);
res->SetName(name);
resources.push_back(image);
resources.push_back(res);
return true;
}
@@ -618,6 +618,29 @@ void ImageResource::SerializeTo(SerializerElement & element) const
element.SetAttribute("userAdded", IsUserAdded());
element.SetAttribute("file", GetFile()); //Keep the resource path in the current locale (but save it in UTF8 for compatibility on other OSes)
}
#endif
void AudioResource::SetFile(const gd::String & newFile)
{
file = newFile;
//Convert all backslash to slashs.
while (file.find('\\') != gd::String::npos)
file.replace(file.find('\\'), 1, "/");
}
void AudioResource::UnserializeFrom(const SerializerElement & element)
{
SetUserAdded( element.GetBoolAttribute("userAdded") );
SetFile(element.GetStringAttribute("file"));
}
#if defined(GD_IDE_ONLY)
void AudioResource::SerializeTo(SerializerElement & element) const
{
element.SetAttribute("userAdded", IsUserAdded());
element.SetAttribute("file", GetFile()); //Keep the resource path in the current locale (but save it in UTF8 for compatibility on other OSes)
}
ResourceFolder::ResourceFolder(const ResourceFolder & other)

View File

@@ -220,6 +220,34 @@ private:
gd::String file;
};
/**
* \brief Describe an audio file used by a project.
*
* \see Resource
* \ingroup ResourcesManagement
*/
class GD_CORE_API AudioResource : public Resource
{
public:
AudioResource() : Resource() { SetKind("audio"); };
virtual ~AudioResource() {};
virtual AudioResource* Clone() const { return new AudioResource(*this);}
virtual const gd::String & GetFile() const {return file;};
virtual void SetFile(const gd::String & newFile);
#if defined(GD_IDE_ONLY)
virtual bool UseFile() { return true; }
void SerializeTo(SerializerElement & element) const;
#endif
void UnserializeFrom(const SerializerElement & element);
private:
gd::String file;
};
/**
* \brief Inventory all resources used by a project
*
@@ -273,7 +301,8 @@ public:
/**
* \brief Add a resource created from a file.
*/
bool AddResource(const gd::String & name, const gd::String & filename);
bool AddResource(const gd::String & name, const gd::String & filename,
const gd::String & kind);
/**
* \brief Remove a resource

View File

@@ -48,14 +48,16 @@ TEST_CASE( "Resources", "[common][resources]" ) {
}
SECTION("ArbitraryResourceWorker") {
gd::Project project;
project.GetResourcesManager().AddResource("res1", "path/to/file1.png");
project.GetResourcesManager().AddResource("res2", "path/to/file2.png");
project.GetResourcesManager().AddResource("res3", "path/to/file3.png");
project.GetResourcesManager().AddResource("res1", "path/to/file1.png", "image");
project.GetResourcesManager().AddResource("res2", "path/to/file2.png", "image");
project.GetResourcesManager().AddResource("res3", "path/to/file3.png", "image");
project.GetResourcesManager().AddResource("res4", "path/to/file4.png", "audio");
ArbitraryResourceWorkerTest worker;
project.ExposeResources(worker);
REQUIRE(worker.files.size() == 3);
REQUIRE(worker.files.size() == 4);
REQUIRE(std::find(worker.files.begin(), worker.files.end(), "path/to/file2.png") != worker.files.end());
REQUIRE(std::find(worker.files.begin(), worker.files.end(), "path/to/file4.png") != worker.files.end());
SECTION("Object using a resource") {
gd::SpriteObject obj("myObject");
@@ -71,21 +73,22 @@ TEST_CASE( "Resources", "[common][resources]" ) {
worker.files.clear();
worker.images.clear();
project.ExposeResources(worker);
REQUIRE(worker.files.size() == 3);
REQUIRE(worker.files.size() == 4);
REQUIRE(worker.images.size() == 1);
REQUIRE(worker.images[0] == "res1");
SECTION("ProjectResourcesAdder") {
std::vector<gd::String> uselessResources =
gd::ProjectResourcesAdder::GetAllUselessResources(project);
gd::ProjectResourcesAdder::GetAllUselessImages(project);
REQUIRE(uselessResources.size() == 2);
gd::ProjectResourcesAdder::RemoveAllUselessResources(project);
gd::ProjectResourcesAdder::RemoveAllUselessImages(project);
std::vector<gd::String> remainingResources =
project.GetResourcesManager().GetAllResourcesList();
REQUIRE(remainingResources.size() == 1);
REQUIRE(remainingResources.size() == 2);
REQUIRE(remainingResources[0] == "res1");
REQUIRE(remainingResources[1] == "res4");
}
}
}

View File

@@ -51,14 +51,16 @@ TEST_CASE( "ResourcesMergingHelper", "[common]" ) {
resourcesMerger.SetBaseDirectory("/game/base/folder/");
gd::Project project;
project.GetResourcesManager().AddResource("Image1", "/image1.png");
project.GetResourcesManager().AddResource("Image2", "image2.png");
project.GetResourcesManager().AddResource("Image3", "subfolder/image3.png");
project.GetResourcesManager().AddResource("Image1", "/image1.png", "image");
project.GetResourcesManager().AddResource("Image2", "image2.png", "image");
project.GetResourcesManager().AddResource("Audio1", "audio1.png", "audio");
project.GetResourcesManager().AddResource("Image3", "subfolder/image3.png", "image");
project.ExposeResources(resourcesMerger);
auto resourcesFilenames = resourcesMerger.GetAllResourcesOldAndNewFilename();
REQUIRE(resourcesFilenames["MakeAbsolute(/image1.png)"] == "FileNameFrom(MakeAbsolute(/image1.png))");
REQUIRE(resourcesFilenames["MakeAbsolute(audio1.png)"] == "FileNameFrom(MakeAbsolute(audio1.png))");
REQUIRE(resourcesFilenames["MakeAbsolute(image2.png)"] == "FileNameFrom(MakeAbsolute(image2.png))");
REQUIRE(resourcesFilenames["MakeAbsolute(subfolder/image3.png)"] == "FileNameFrom(MakeAbsolute(subfolder/image3.png))");
}
@@ -69,15 +71,17 @@ TEST_CASE( "ResourcesMergingHelper", "[common]" ) {
resourcesMerger.PreserveDirectoriesStructure(true);
gd::Project project;
project.GetResourcesManager().AddResource("Image1", "/image1.png");
project.GetResourcesManager().AddResource("Image2", "image2.png");
project.GetResourcesManager().AddResource("Image3", "subfolder/image3.png");
project.GetResourcesManager().AddResource("Image1", "/image1.png", "image");
project.GetResourcesManager().AddResource("Image2", "image2.png", "image");
project.GetResourcesManager().AddResource("Audio1", "audio1.png", "audio");
project.GetResourcesManager().AddResource("Image3", "subfolder/image3.png", "image");
project.ExposeResources(resourcesMerger);
auto resourcesFilenames = resourcesMerger.GetAllResourcesOldAndNewFilename();
REQUIRE(resourcesFilenames["MakeAbsolute(/image1.png)"] == "MakeRelative(MakeAbsolute(/image1.png))");
REQUIRE(resourcesFilenames["MakeAbsolute(image2.png)"] == "MakeRelative(MakeAbsolute(image2.png))");
REQUIRE(resourcesFilenames["MakeAbsolute(audio1.png)"] == "MakeRelative(MakeAbsolute(audio1.png))");
REQUIRE(resourcesFilenames["MakeAbsolute(subfolder/image3.png)"] == "MakeRelative(MakeAbsolute(subfolder/image3.png))");
}
}

View File

@@ -121,7 +121,7 @@ void TileSet::LoadResources(RuntimeGame &game)
void TileSet::LoadResources(gd::Project &game)
{
if(game.GetResourcesManager().HasResource(textureName))
try
{
gd::ImageResource & image = dynamic_cast<gd::ImageResource&>(game.GetResourcesManager().GetResource(textureName));
//Load the resource into a wxBitmap (IDE only) and also get its SFMLTextureWrapper
@@ -144,7 +144,7 @@ void TileSet::LoadResources(gd::Project &game)
//Readjust the m_collidable std::vector according to the number of tiles
m_collidable.resize(GetTilesCount(), true);
}
else
catch(...)
{
m_tilesetTexture = std::shared_ptr<SFMLTextureWrapper>();
}