Material: Material editor enhancements
Continues the work of the material subsystem improvements. This merge covers the continued development of the material editor. The primary improvements are in the handling of 2D and 3D array properties. These properties are now fully editable, and can be saved and restored. The cards now separate the author and license. These were previously saved as a single item. Future support will be provided for standard open source licenses. Saving operations validate the cards to ensure UUIDs of materials are considered. Warnings are given when a save could potentially impact the models, such as saving over a material instead of creating a new instance. The editor is still not complete. There are a number of functional elements, such as drag/drop operations, folder creation, and deletion operations that need to be added to the main tree. State needs to be saved and restored to improve the user experience. The appearance preview also needs significant work. This will be handled in a future PR.
This commit is contained in:
@@ -23,6 +23,7 @@
|
||||
#ifndef _PreComp_
|
||||
#endif
|
||||
|
||||
#include <QDirIterator>
|
||||
#include <QMutexLocker>
|
||||
|
||||
#include <App/Application.h>
|
||||
@@ -37,8 +38,10 @@ using namespace Materials;
|
||||
|
||||
/* TRANSLATOR Material::Materials */
|
||||
|
||||
std::shared_ptr<std::list<MaterialLibrary*>> MaterialManager::_libraryList = nullptr;
|
||||
std::shared_ptr<std::map<QString, Material*>> MaterialManager::_materialMap = nullptr;
|
||||
std::shared_ptr<std::list<std::shared_ptr<MaterialLibrary>>> MaterialManager::_libraryList =
|
||||
nullptr;
|
||||
std::shared_ptr<std::map<QString, std::shared_ptr<Material>>> MaterialManager::_materialMap =
|
||||
nullptr;
|
||||
QMutex MaterialManager::_mutex;
|
||||
|
||||
TYPESYSTEM_SOURCE(Materials::MaterialManager, Base::BaseClass)
|
||||
@@ -60,10 +63,10 @@ void MaterialManager::initLibraries()
|
||||
|
||||
delete manager;
|
||||
|
||||
_materialMap = std::make_shared<std::map<QString, Material*>>();
|
||||
_materialMap = std::make_shared<std::map<QString, std::shared_ptr<Material>>>();
|
||||
|
||||
if (_libraryList == nullptr) {
|
||||
_libraryList = std::make_shared<std::list<MaterialLibrary*>>();
|
||||
_libraryList = std::make_shared<std::list<std::shared_ptr<MaterialLibrary>>>();
|
||||
}
|
||||
|
||||
// Load the libraries
|
||||
@@ -71,23 +74,15 @@ void MaterialManager::initLibraries()
|
||||
}
|
||||
}
|
||||
|
||||
void MaterialManager::saveMaterial(MaterialLibrary* library,
|
||||
Material& material,
|
||||
void MaterialManager::saveMaterial(std::shared_ptr<MaterialLibrary> library,
|
||||
std::shared_ptr<Material> material,
|
||||
const QString& path,
|
||||
bool saveAsCopy)
|
||||
bool overwrite,
|
||||
bool saveAsCopy,
|
||||
bool saveInherited)
|
||||
{
|
||||
Material* newMaterial = library->saveMaterial(material, path, saveAsCopy);
|
||||
|
||||
try {
|
||||
Material* old = _materialMap->at(newMaterial->getUUID());
|
||||
if (old) {
|
||||
delete old;
|
||||
}
|
||||
}
|
||||
catch (const std::out_of_range&) {
|
||||
}
|
||||
|
||||
(*_materialMap)[material.getUUID()] = newMaterial;
|
||||
auto newMaterial = library->saveMaterial(material, path, overwrite, saveAsCopy, saveInherited);
|
||||
(*_materialMap)[newMaterial->getUUID()] = newMaterial;
|
||||
}
|
||||
|
||||
bool MaterialManager::isMaterial(const fs::path& p)
|
||||
@@ -102,31 +97,43 @@ bool MaterialManager::isMaterial(const fs::path& p)
|
||||
return false;
|
||||
}
|
||||
|
||||
const Material& MaterialManager::getMaterial(const QString& uuid) const
|
||||
bool MaterialManager::isMaterial(const QFileInfo& file)
|
||||
{
|
||||
if (!file.isFile()) {
|
||||
return false;
|
||||
}
|
||||
// check file extension
|
||||
if (file.suffix() == QString::fromStdString("FCMat")) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::shared_ptr<Material> MaterialManager::getMaterial(const QString& uuid) const
|
||||
{
|
||||
try {
|
||||
return *(_materialMap->at(uuid));
|
||||
return _materialMap->at(uuid);
|
||||
}
|
||||
catch (std::out_of_range&) {
|
||||
catch (std::out_of_range& e) {
|
||||
throw MaterialNotFound();
|
||||
}
|
||||
}
|
||||
|
||||
const Material& MaterialManager::getMaterialByPath(const QString& path) const
|
||||
std::shared_ptr<Material> MaterialManager::getMaterialByPath(const QString& path) const
|
||||
{
|
||||
QString cleanPath = QDir::cleanPath(path);
|
||||
|
||||
for (auto library : *_libraryList) {
|
||||
Base::Console().Log("MaterialManager::getMaterialByPath() Checking library '%s'->'%s'\n",
|
||||
library->getName().toStdString().c_str(),
|
||||
library->getDirectory().toStdString().c_str());
|
||||
// Base::Console().Log("MaterialManager::getMaterialByPath() Checking library '%s'->'%s'\n",
|
||||
// library->getName().toStdString().c_str(),
|
||||
// library->getDirectory().toStdString().c_str());
|
||||
|
||||
|
||||
if (cleanPath.startsWith(library->getDirectory())) {
|
||||
Base::Console().Log("MaterialManager::getMaterialByPath() Library '%s'\n",
|
||||
library->getDirectory().toStdString().c_str());
|
||||
Base::Console().Log("MaterialManager::getMaterialByPath() Path '%s'\n",
|
||||
cleanPath.toStdString().c_str());
|
||||
// Base::Console().Log("MaterialManager::getMaterialByPath() Library '%s'\n",
|
||||
// library->getDirectory().toStdString().c_str());
|
||||
// Base::Console().Log("MaterialManager::getMaterialByPath() Path '%s'\n",
|
||||
// cleanPath.toStdString().c_str());
|
||||
return library->getMaterialByPath(cleanPath);
|
||||
}
|
||||
}
|
||||
@@ -136,13 +143,51 @@ const Material& MaterialManager::getMaterialByPath(const QString& path) const
|
||||
throw MaterialNotFound();
|
||||
}
|
||||
|
||||
const Material& MaterialManager::getMaterialByPath(const QString& path, const QString& lib) const
|
||||
std::shared_ptr<Material> MaterialManager::getMaterialByPath(const QString& path,
|
||||
const QString& lib) const
|
||||
{
|
||||
auto library = getLibrary(lib); // May throw LibraryNotFound
|
||||
return library->getMaterialByPath(path); // May throw MaterialNotFound
|
||||
}
|
||||
|
||||
MaterialLibrary* MaterialManager::getLibrary(const QString& name) const
|
||||
bool MaterialManager::exists(const QString& uuid) const
|
||||
{
|
||||
try {
|
||||
auto material = getMaterial(uuid);
|
||||
if (material.get() != nullptr) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch (const MaterialNotFound&) {
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
std::shared_ptr<Material> MaterialManager::getParent(std::shared_ptr<Material> material)
|
||||
{
|
||||
if (material->getParentUUID().isEmpty()) {
|
||||
throw MaterialNotFound();
|
||||
}
|
||||
|
||||
return getMaterial(material->getParentUUID());
|
||||
}
|
||||
|
||||
bool MaterialManager::exists(std::shared_ptr<MaterialLibrary> library, const QString& uuid) const
|
||||
{
|
||||
try {
|
||||
auto material = getMaterial(uuid);
|
||||
if (material.get() != nullptr) {
|
||||
return (*material->getLibrary() == *library);
|
||||
}
|
||||
}
|
||||
catch (const MaterialNotFound&) {
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
std::shared_ptr<MaterialLibrary> MaterialManager::getLibrary(const QString& name) const
|
||||
{
|
||||
for (auto library : *_libraryList) {
|
||||
if (library->getName() == name) {
|
||||
@@ -153,13 +198,13 @@ MaterialLibrary* MaterialManager::getLibrary(const QString& name) const
|
||||
throw LibraryNotFound();
|
||||
}
|
||||
|
||||
std::shared_ptr<std::list<MaterialLibrary*>> MaterialManager::getMaterialLibraries()
|
||||
std::shared_ptr<std::list<std::shared_ptr<MaterialLibrary>>> MaterialManager::getMaterialLibraries()
|
||||
{
|
||||
if (_libraryList == nullptr) {
|
||||
if (_materialMap == nullptr) {
|
||||
_materialMap = std::make_shared<std::map<QString, Material*>>();
|
||||
_materialMap = std::make_shared<std::map<QString, std::shared_ptr<Material>>>();
|
||||
}
|
||||
_libraryList = std::make_shared<std::list<MaterialLibrary*>>();
|
||||
_libraryList = std::make_shared<std::list<std::shared_ptr<MaterialLibrary>>>();
|
||||
|
||||
// Load the libraries
|
||||
MaterialLoader loader(_materialMap, _libraryList);
|
||||
@@ -167,87 +212,21 @@ std::shared_ptr<std::list<MaterialLibrary*>> MaterialManager::getMaterialLibrari
|
||||
return _libraryList;
|
||||
}
|
||||
|
||||
std::shared_ptr<std::map<QString, MaterialTreeNode*>>
|
||||
MaterialManager::getMaterialTree(const MaterialLibrary& library) const
|
||||
{
|
||||
std::shared_ptr<std::map<QString, MaterialTreeNode*>> materialTree =
|
||||
std::make_shared<std::map<QString, MaterialTreeNode*>>();
|
||||
|
||||
for (auto it = _materialMap->begin(); it != _materialMap->end(); it++) {
|
||||
auto filename = it->first;
|
||||
auto material = it->second;
|
||||
|
||||
if (material->getLibrary() == library) {
|
||||
fs::path path = material->getDirectory().toStdString();
|
||||
|
||||
// Start at the root
|
||||
auto node = materialTree;
|
||||
for (auto itp = path.begin(); itp != path.end(); itp++) {
|
||||
if (QString::fromStdString(itp->string())
|
||||
.endsWith(QString::fromStdString(".FCMat"))) {
|
||||
MaterialTreeNode* child = new MaterialTreeNode();
|
||||
child->setData(material);
|
||||
(*node)[QString::fromStdString(itp->string())] = child;
|
||||
}
|
||||
else {
|
||||
// Add the folder only if it's not already there
|
||||
QString folderName = QString::fromStdString(itp->string());
|
||||
std::shared_ptr<std::map<QString, MaterialTreeNode*>> mapPtr;
|
||||
if (node->count(folderName) == 0) {
|
||||
mapPtr = std::make_shared<std::map<QString, MaterialTreeNode*>>();
|
||||
MaterialTreeNode* child = new MaterialTreeNode();
|
||||
child->setFolder(mapPtr);
|
||||
(*node)[folderName] = child;
|
||||
node = mapPtr;
|
||||
}
|
||||
else {
|
||||
node = (*node)[folderName]->getFolder();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto folderList = getMaterialFolders(library);
|
||||
for (auto folder : *folderList) {
|
||||
fs::path path = folder.toStdString();
|
||||
|
||||
// Start at the root
|
||||
auto node = materialTree;
|
||||
for (auto itp = path.begin(); itp != path.end(); itp++) {
|
||||
// Add the folder only if it's not already there
|
||||
QString folderName = QString::fromStdString(itp->string());
|
||||
if (node->count(folderName) == 0) {
|
||||
std::shared_ptr<std::map<QString, MaterialTreeNode*>> mapPtr =
|
||||
std::make_shared<std::map<QString, MaterialTreeNode*>>();
|
||||
MaterialTreeNode* child = new MaterialTreeNode();
|
||||
child->setFolder(mapPtr);
|
||||
(*node)[folderName] = child;
|
||||
node = mapPtr;
|
||||
}
|
||||
else {
|
||||
node = (*node)[folderName]->getFolder();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return materialTree;
|
||||
}
|
||||
|
||||
std::shared_ptr<std::list<QString>>
|
||||
MaterialManager::getMaterialFolders(const MaterialLibrary& library) const
|
||||
MaterialManager::getMaterialFolders(std::shared_ptr<MaterialLibrary> library) const
|
||||
{
|
||||
return MaterialLoader::getMaterialFolders(library);
|
||||
return MaterialLoader::getMaterialFolders(*library);
|
||||
}
|
||||
|
||||
std::shared_ptr<std::map<QString, Material*>> MaterialManager::materialsWithModel(QString uuid)
|
||||
std::shared_ptr<std::map<QString, std::shared_ptr<Material>>>
|
||||
MaterialManager::materialsWithModel(QString uuid)
|
||||
{
|
||||
std::shared_ptr<std::map<QString, Material*>> dict =
|
||||
std::make_shared<std::map<QString, Material*>>();
|
||||
std::shared_ptr<std::map<QString, std::shared_ptr<Material>>> dict =
|
||||
std::make_shared<std::map<QString, std::shared_ptr<Material>>>();
|
||||
|
||||
for (auto it = _materialMap->begin(); it != _materialMap->end(); it++) {
|
||||
QString key = it->first;
|
||||
Material* material = it->second;
|
||||
auto material = it->second;
|
||||
|
||||
if (material->hasModel(uuid)) {
|
||||
(*dict)[key] = material;
|
||||
@@ -257,15 +236,15 @@ std::shared_ptr<std::map<QString, Material*>> MaterialManager::materialsWithMode
|
||||
return dict;
|
||||
}
|
||||
|
||||
std::shared_ptr<std::map<QString, Material*>>
|
||||
std::shared_ptr<std::map<QString, std::shared_ptr<Material>>>
|
||||
MaterialManager::materialsWithModelComplete(QString uuid)
|
||||
{
|
||||
std::shared_ptr<std::map<QString, Material*>> dict =
|
||||
std::make_shared<std::map<QString, Material*>>();
|
||||
std::shared_ptr<std::map<QString, std::shared_ptr<Material>>> dict =
|
||||
std::make_shared<std::map<QString, std::shared_ptr<Material>>>();
|
||||
|
||||
for (auto it = _materialMap->begin(); it != _materialMap->end(); it++) {
|
||||
QString key = it->first;
|
||||
Material* material = it->second;
|
||||
auto material = it->second;
|
||||
|
||||
if (material->isModelComplete(uuid)) {
|
||||
(*dict)[key] = material;
|
||||
@@ -274,3 +253,8 @@ MaterialManager::materialsWithModelComplete(QString uuid)
|
||||
|
||||
return dict;
|
||||
}
|
||||
|
||||
void MaterialManager::dereference(std::shared_ptr<Material> material)
|
||||
{
|
||||
MaterialLoader::dereference(_materialMap, material);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user