Material: Material editor enhancements

Improves compatibility when using the legacy editor.

Corrects a number of issues that prevented saved files from being displayed when using the legacy editor.

Reduces the verbosity of console messages.
This commit is contained in:
David Carter
2024-02-04 10:55:56 -05:00
committed by Chris Hennes
parent 5d729db905
commit e9b43a0f83
7 changed files with 81 additions and 25 deletions

View File

@@ -242,8 +242,8 @@ void MaterialYamlEntry::addToTree(
QString propertyValue =
QString::fromStdString((itp->second).as<std::string>());
if (type == MaterialValue::Image) {
propertyValue =
propertyValue.remove(QRegularExpression(QString::fromStdString("[\r\n]")));
propertyValue = propertyValue.remove(
QRegularExpression(QString::fromStdString("[\r\n]")));
}
finalModel->setPhysicalValue(QString::fromStdString(propertyName),
propertyValue);
@@ -309,8 +309,8 @@ void MaterialYamlEntry::addToTree(
QString propertyValue =
QString::fromStdString((itp->second).as<std::string>());
if (type == MaterialValue::Image) {
propertyValue =
propertyValue.remove(QRegularExpression(QString::fromStdString("[\r\n]")));
propertyValue = propertyValue.remove(
QRegularExpression(QString::fromStdString("[\r\n]")));
}
finalModel->setAppearanceValue(QString::fromStdString(propertyName),
propertyValue);

View File

@@ -29,6 +29,7 @@
#include <App/Application.h>
#include "Exceptions.h"
#include "MaterialConfigLoader.h"
#include "MaterialLoader.h"
#include "MaterialManager.h"
#include "ModelManager.h"
@@ -123,7 +124,23 @@ std::shared_ptr<Material> MaterialManager::getMaterialByPath(const QString& path
for (auto& library : *_libraryList) {
if (cleanPath.startsWith(library->getDirectory())) {
return library->getMaterialByPath(cleanPath);
try {
return library->getMaterialByPath(cleanPath);
} catch (const MaterialNotFound&) {}
// See if it's a new file saved by the old editor
{
QMutexLocker locker(&_mutex);
if (MaterialConfigLoader::isConfigStyle(path)) {
auto material = MaterialConfigLoader::getMaterialFromPath(library, path);
if (material) {
(*_materialMap)[material->getUUID()] = library->addMaterial(material, path);
}
return material;
}
}
}
}
@@ -244,6 +261,21 @@ MaterialManager::materialsWithModelComplete(const QString& uuid) const
return dict;
}
void MaterialManager::dereference() const
{
// First clear the inheritences
for (auto& it : *_materialMap) {
auto material = it.second;
material->clearDereferenced();
material->clearInherited();
}
// Run the dereference again
for (auto& it : *_materialMap) {
dereference(it.second);
}
}
void MaterialManager::dereference(std::shared_ptr<Material> material) const
{
MaterialLoader::dereference(_materialMap, material);

View File

@@ -85,6 +85,7 @@ public:
void deleteRecursive(const std::shared_ptr<MaterialLibrary>& library, const QString& path) const
{
library->deleteRecursive(path);
dereference();
}
void remove(const QString& uuid) const
{
@@ -106,6 +107,7 @@ public:
std::shared_ptr<std::map<QString, std::shared_ptr<Material>>>
materialsWithModelComplete(const QString& uuid) const;
void dereference(std::shared_ptr<Material> material) const;
void dereference() const;
private:
static std::shared_ptr<std::list<std::shared_ptr<MaterialLibrary>>> _libraryList;

View File

@@ -491,6 +491,19 @@ void Material::clearModels()
_appearance.clear();
}
void Material::clearInherited()
{
_allUuids.clear();
// Rebuild the UUID lists without the inherited UUIDs
for (auto& uuid : _physicalUuids) {
_allUuids << uuid;
}
for (auto& uuid : _appearanceUuids) {
_allUuids << uuid;
}
}
void Material::setName(const QString& name)
{
_name = name;
@@ -599,14 +612,7 @@ void Material::removePhysical(const QString& uuid)
}
// If it's an inherited model, do nothing
bool inherited = true;
for (const auto& it : qAsConst(_physicalUuids)) {
if (it == uuid) {
inherited = false;
break;
}
}
if (inherited) {
if (isInherited(uuid)) {
return;
}
@@ -677,14 +683,7 @@ void Material::removeAppearance(const QString& uuid)
}
// If it's an inherited model, do nothing
bool inherited = true;
for (const auto& it : qAsConst(_appearanceUuids)) {
if (it == uuid) {
inherited = false;
break;
}
}
if (inherited) {
if (isInherited(uuid)) {
return;
}
@@ -988,6 +987,18 @@ bool Material::hasAppearanceProperty(const QString& name) const
return true;
}
bool Material::isInherited(const QString& uuid) const
{
if (_physicalUuids.contains(uuid)) {
return false;
}
if (_appearanceUuids.contains(uuid)) {
return false;
}
return _allUuids.contains(uuid);
}
bool Material::hasModel(const QString& uuid) const
{
return _allUuids.contains(uuid);

View File

@@ -266,6 +266,7 @@ public:
void addAppearance(const QString& uuid);
void removeAppearance(const QString& uuid);
void clearModels();
void clearInherited();
void newUuid();
void setPhysicalValue(const QString& name, const QString& value);
@@ -298,6 +299,7 @@ public:
bool hasModel(const QString& uuid) const;
bool hasPhysicalModel(const QString& uuid) const;
bool hasAppearanceModel(const QString& uuid) const;
bool isInherited(const QString& uuid) const;
bool isModelComplete(const QString& uuid) const
{
return isPhysicalModelComplete(uuid) || isAppearanceModelComplete(uuid);
@@ -324,6 +326,10 @@ public:
{
_dereferenced = true;
}
void clearDereferenced()
{
_dereferenced = false;
}
bool isOldFormat() const
{
return _oldFormat;

View File

@@ -298,6 +298,7 @@ class MaterialEditor:
card_name_list.append([a_name, a_path, self.icons[a_path]])
card_name_list.insert(0, [None, "", ""])
self.widget.ComboMaterial.clear()
for mat in card_name_list:
self.widget.ComboMaterial.addItem(QtGui.QIcon(mat[2]), mat[0], mat[1])
@@ -684,9 +685,16 @@ class MaterialEditor:
from importFCMat import write
write(filename, d)
import Material
# Load the material
manager = Material.MaterialManager()
manager.getMaterialByPath(filename)
self.edited = False
self.updateCardsInCombo()
# Ensure our card is selected
self.widget.ComboMaterial.setCurrentText(path.stem)
def show(self):
return self.widget.show()

View File

@@ -271,14 +271,11 @@ def import_materials(category='Solid', template=False):
fluid = ('1ae66d8c-1ba1-4211-ad12-b9917573b202' in physicalModels)
if not fluid:
path = mat.LibraryRoot + "/" + mat.Directory
print(path)
materials[path] = mat.Properties
cards[path] = mat.Name
icons[path] = mat.LibraryIcon
print(path)
print(mat.Properties)
return (materials, cards, icons)
def add_cards_from_a_dir(materials, cards, icons, mat_dir, icon, template=False):