Work on asset management

This commit is contained in:
Emil Axelsson
2017-09-18 12:56:55 +02:00
parent aaed6ab6d7
commit 8a30251fcb
5 changed files with 86 additions and 42 deletions

View File

@@ -25,10 +25,6 @@
#ifndef __OPENSPACE_CORE___ASSET___H__
#define __OPENSPACE_CORE___ASSET___H__
#include <openspace/properties/property.h>
#include <openspace/properties/propertyowner.h>
#include <openspace/properties/scalarproperty.h>
#include <ghoul/filesystem/filesystem.h>
#include <string>
#include <vector>
@@ -37,14 +33,9 @@ namespace openspace {
class AssetLoader;
class Asset : public properties::PropertyOwner {
class Asset {
public:
struct Optional : public properties::PropertyOwner {
Optional(Asset* asset, Asset* owner, bool enabled = false);
properties::BoolProperty enabled;
Asset* const asset;
Asset* const owner;
};
using Optional = std::pair<Asset*, bool>;
enum class ReadyState : unsigned int {
Loaded,
@@ -82,9 +73,9 @@ public:
bool hasDependants() const;
bool hasInitializedDependants() const;
std::vector<Asset*> optionals() const;
bool hasOptional(Asset* asset) const;
bool optionalIsEnabled(Asset* asset) const;
std::vector<Asset*> optionalAssets() const;
bool hasOptional(const Asset* asset) const;
bool hasEnabledOptional(const Asset* asset) const;
void setOptionalEnabled(Asset* asset, bool enabled);
void addOptional(Asset* asset, bool enabled);
void removeOptional(Asset* asset);
@@ -92,11 +83,11 @@ public:
void dependantDidInitialize(Asset* dependant);
void dependantWillDeinitialize(Asset* dependant);
void optionalDidInitialize(Asset* optional);
void optionalWillDeinitialize(Asset* optional);
//void optionalDidInitialize(Asset* optional);
//void optionalWillDeinitialize(Asset* optional);
bool shouldSynchronize();
bool shouldInitialize();
//bool shouldSynchronize();
//bool shouldInitialize();
std::string resolveLocalResource(std::string resourceName);
std::string resolveSyncedResource(std::string resourceName);
@@ -124,7 +115,7 @@ private:
std::vector<Asset*> _dependants;
// Optional sub-assets
std::vector<Optional> _optionalAssets;
std::vector<Optional> _optionals;
// Assets that refers to this asset as an optional
std::vector<Asset*> _optionalOwners;

View File

@@ -71,7 +71,6 @@ ImGUIModule::ImGUIModule() : OpenSpaceModule(Name) {
&(OsEng.renderEngine()),
&(OsEng.parallelConnection()),
&(OsEng.console()),
OsEng.assetLoader().rootAsset()
};
return res;
}

View File

@@ -410,7 +410,7 @@ void OpenSpaceEngine::create(int argc, char** argv,
// Set up asset loader
_engine->_assetLoader = std::make_unique<AssetLoader>(
*OsEng.scriptEngine().luaState(), OsEng.resourceSynchronizer(), "${ASSETS}", "${SYNC}");
_engine->_globalPropertyNamespace->addPropertySubOwner(_engine->_assetLoader->rootAsset());
//_engine->_globalPropertyNamespace->addPropertySubOwner(_engine->_assetLoader->rootAsset());
}
void OpenSpaceEngine::destroy() {

View File

@@ -25,6 +25,8 @@
#include <openspace/scene/asset.h>
#include <openspace/scene/assetloader.h>
#include <ghoul/logging/logmanager.h>
#include <algorithm>
namespace {
@@ -136,8 +138,8 @@ std::string Asset::resolveSyncedResource(std::string resourceName) {
}
Asset::Asset(AssetLoader* loader, ghoul::filesystem::Directory directory)
: PropertyOwner({ "RootAsset", "Root asset" })
, _assetDirectory(directory)
//: PropertyOwner({ "RootAsset", "Root asset" })
: _assetDirectory(directory)
, _loader(loader)
, _readyState(Asset::ReadyState::Loaded)
{
@@ -145,8 +147,8 @@ Asset::Asset(AssetLoader* loader, ghoul::filesystem::Directory directory)
}
Asset::Asset(AssetLoader* loader, ghoul::filesystem::Directory baseDirectory, std::string assetPath)
: PropertyOwner({ assetPath, assetPath })
, _readyState(Asset::ReadyState::Loaded)
//: PropertyOwner({ assetPath, assetPath })
: _readyState(Asset::ReadyState::Loaded)
, _loader(loader)
{
if (isRelative(assetPath)) {
@@ -219,7 +221,7 @@ void Asset::addDependency(Asset* dependency) {
_dependencies.push_back(dependency);
dependency->_dependants.push_back(this);
addPropertySubOwner(dependency);
//addPropertySubOwner(dependency);
}
void Asset::removeDependency(Asset* dependency) {
@@ -267,6 +269,41 @@ bool Asset::hasDependants() const {
return foundDep;
}
std::vector<Asset*> Asset::optionalAssets() const {
std::vector<Asset*> assets(_optionals.size());
std::transform(
_optionals.begin(),
_optionals.end(),
assets.begin(),
[](const Optional& o) {
return o.first;
}
);
return assets;
}
bool Asset::hasOptional(const Asset* asset) const {
auto it = std::find_if(
_optionals.begin(),
_optionals.end(),
[&asset](const Optional& o) {
return o.first == asset;
}
);
return it != _optionals.end();
}
bool Asset::hasEnabledOptional(const Asset* asset) const {
auto it = std::find_if(
_optionals.begin(),
_optionals.end(),
[&asset](const Optional& o) {
return o.first == asset && o.second;
}
);
return it != _optionals.end();
}
bool Asset::hasInitializedDependants() const {
bool foundInitializedDep = false;
for (const auto& dependant : _dependants) {
@@ -277,20 +314,37 @@ bool Asset::hasInitializedDependants() const {
return foundInitializedDep;
}
// Dependency toggle
Asset::Optional::Optional(Asset* a, Asset* o, bool enable)
: PropertyOwner({ asset->name(), asset->name() })
, enabled({ "enabled", "Enabled", "Enable optional" }, enable)
, asset(a)
, owner(o)
{
addProperty(enabled);
addPropertySubOwner(a);
enabled.onChange([this]() {
owner->setOptionalEnabled(
asset,
enabled
);
});
void Asset::setOptionalEnabled(Asset* asset, bool enabled) {
auto it = std::find_if(
_optionals.begin(),
_optionals.end(),
[&asset](const Optional& o) {
return o.first == asset;
}
);
if (it != _optionals.end()) {
it->second = enabled;
}
}
void Asset::removeOptional(Asset* asset) {
_optionals.erase(
std::remove_if(
_optionals.begin(),
_optionals.end(),
[&asset](Optional& o) {
return o.first == asset;
}
),
_optionals.end()
);
// TODO: Update and validate
}
void Asset::addOptional(Asset* asset, bool enabled) {
_optionals.push_back(std::make_pair(asset, enabled));
// TODO: Update and validate
}
}

View File

@@ -206,7 +206,7 @@ ghoul::filesystem::Directory AssetLoader::currentDirectory() {
void AssetLoader::loadSingleAsset(const std::string& identifier) {
Asset* imported = importOptional(identifier, true);
std::vector<Asset*> optionals = _rootAsset->optionals();
std::vector<Asset*> optionals = _rootAsset->optionalAssets();
// Remove all other optionals
for (auto& optional : optionals) {