Wrapping redundant save logic into function in the material editor document class
Signed-off-by: Guthrie Adams <guthadam@amazon.com>
This commit is contained in:
@@ -168,30 +168,20 @@ namespace MaterialEditor
|
||||
return false;
|
||||
}
|
||||
|
||||
// create source data from properties
|
||||
// populate sourceData with modified or overridden properties and save object
|
||||
AZ::RPI::MaterialSourceData sourceData;
|
||||
sourceData.m_materialTypeVersion = m_materialAsset->GetMaterialTypeAsset()->GetVersion();
|
||||
sourceData.m_materialType = AtomToolsFramework::GetExteralReferencePath(m_absolutePath, m_materialSourceData.m_materialType);
|
||||
sourceData.m_parentMaterial = AtomToolsFramework::GetExteralReferencePath(m_absolutePath, m_materialSourceData.m_parentMaterial);
|
||||
|
||||
// populate sourceData with modified or overwritten properties
|
||||
const bool savedProperties = SavePropertiesToSourceData(m_absolutePath, sourceData, [](const AtomToolsFramework::DynamicProperty& property)
|
||||
{
|
||||
auto propertyFilter = [](const AtomToolsFramework::DynamicProperty& property) {
|
||||
return !AtomToolsFramework::ArePropertyValuesEqual(property.GetValue(), property.GetConfig().m_parentValue);
|
||||
});
|
||||
};
|
||||
|
||||
if (!savedProperties)
|
||||
if (!SaveSourceData(sourceData, propertyFilter))
|
||||
{
|
||||
return SaveFailed();
|
||||
}
|
||||
|
||||
// write sourceData to .material file
|
||||
if (!AZ::RPI::JsonUtils::SaveObjectToFile(m_absolutePath, sourceData))
|
||||
{
|
||||
AZ_Error("MaterialDocument", false, "Document could not be saved: '%s'.", m_absolutePath.c_str());
|
||||
return SaveFailed();
|
||||
}
|
||||
|
||||
// after saving, reset to a clean state
|
||||
for (auto& propertyPair : m_properties)
|
||||
{
|
||||
@@ -211,30 +201,20 @@ namespace MaterialEditor
|
||||
return false;
|
||||
}
|
||||
|
||||
// create source data from properties
|
||||
// populate sourceData with modified or overridden properties and save object
|
||||
AZ::RPI::MaterialSourceData sourceData;
|
||||
sourceData.m_materialTypeVersion = m_materialAsset->GetMaterialTypeAsset()->GetVersion();
|
||||
sourceData.m_materialType = AtomToolsFramework::GetExteralReferencePath(m_savePathNormalized, m_materialSourceData.m_materialType);
|
||||
sourceData.m_parentMaterial = AtomToolsFramework::GetExteralReferencePath(m_savePathNormalized, m_materialSourceData.m_parentMaterial);
|
||||
|
||||
// populate sourceData with modified or overwritten properties
|
||||
const bool savedProperties = SavePropertiesToSourceData(m_savePathNormalized, sourceData, [](const AtomToolsFramework::DynamicProperty& property)
|
||||
{
|
||||
auto propertyFilter = [](const AtomToolsFramework::DynamicProperty& property) {
|
||||
return !AtomToolsFramework::ArePropertyValuesEqual(property.GetValue(), property.GetConfig().m_parentValue);
|
||||
});
|
||||
};
|
||||
|
||||
if (!savedProperties)
|
||||
if (!SaveSourceData(sourceData, propertyFilter))
|
||||
{
|
||||
return SaveFailed();
|
||||
}
|
||||
|
||||
// write sourceData to .material file
|
||||
if (!AZ::RPI::JsonUtils::SaveObjectToFile(m_savePathNormalized, sourceData))
|
||||
{
|
||||
AZ_Error("MaterialDocument", false, "Document could not be saved: '%s'.", m_savePathNormalized.c_str());
|
||||
return SaveFailed();
|
||||
}
|
||||
|
||||
// If the document is saved to a new file we need to reopen the new document to update assets, paths, property deltas.
|
||||
if (!Open(m_savePathNormalized))
|
||||
{
|
||||
@@ -251,7 +231,7 @@ namespace MaterialEditor
|
||||
return false;
|
||||
}
|
||||
|
||||
// create source data from properties
|
||||
// populate sourceData with modified or overridden properties and save object
|
||||
AZ::RPI::MaterialSourceData sourceData;
|
||||
sourceData.m_materialTypeVersion = m_materialAsset->GetMaterialTypeAsset()->GetVersion();
|
||||
sourceData.m_materialType = AtomToolsFramework::GetExteralReferencePath(m_savePathNormalized, m_materialSourceData.m_materialType);
|
||||
@@ -262,24 +242,15 @@ namespace MaterialEditor
|
||||
sourceData.m_parentMaterial = AtomToolsFramework::GetExteralReferencePath(m_savePathNormalized, m_absolutePath);
|
||||
}
|
||||
|
||||
// populate sourceData with modified properties
|
||||
const bool savedProperties = SavePropertiesToSourceData(m_savePathNormalized, sourceData, [](const AtomToolsFramework::DynamicProperty& property)
|
||||
{
|
||||
auto propertyFilter = [](const AtomToolsFramework::DynamicProperty& property) {
|
||||
return !AtomToolsFramework::ArePropertyValuesEqual(property.GetValue(), property.GetConfig().m_originalValue);
|
||||
});
|
||||
};
|
||||
|
||||
if (!savedProperties)
|
||||
if (!SaveSourceData(sourceData, propertyFilter))
|
||||
{
|
||||
return SaveFailed();
|
||||
}
|
||||
|
||||
// write sourceData to .material file
|
||||
if (!AZ::RPI::JsonUtils::SaveObjectToFile(m_savePathNormalized, sourceData))
|
||||
{
|
||||
AZ_Error("MaterialDocument", false, "Document could not be saved: '%s'.", m_savePathNormalized.c_str());
|
||||
return SaveFailed();
|
||||
}
|
||||
|
||||
// If the document is saved to a new file we need to reopen the new document to update assets, paths, property deltas.
|
||||
if (!Open(m_savePathNormalized))
|
||||
{
|
||||
@@ -362,13 +333,12 @@ namespace MaterialEditor
|
||||
}
|
||||
}
|
||||
|
||||
bool MaterialDocument::SavePropertiesToSourceData(
|
||||
const AZStd::string& exportPath, AZ::RPI::MaterialSourceData& sourceData, PropertyFilterFunction propertyFilter) const
|
||||
bool MaterialDocument::SaveSourceData(AZ::RPI::MaterialSourceData& sourceData, PropertyFilterFunction propertyFilter) const
|
||||
{
|
||||
bool result = true;
|
||||
bool addPropertiesResult = true;
|
||||
|
||||
// populate sourceData with properties that meet the filter
|
||||
m_materialTypeSourceData.EnumerateProperties([&](const AZStd::string& propertyIdContext, const auto& propertyDefinition) {
|
||||
m_materialTypeSourceData.EnumerateProperties([&, this](const AZStd::string& propertyIdContext, const auto& propertyDefinition) {
|
||||
|
||||
AZ::Name propertyId{propertyIdContext + propertyDefinition->GetName()};
|
||||
|
||||
@@ -381,7 +351,7 @@ namespace MaterialEditor
|
||||
if (!AtomToolsFramework::ConvertToExportFormat(exportPath, propertyId, *propertyDefinition, propertyValue))
|
||||
{
|
||||
AZ_Error("MaterialDocument", false, "Document property could not be converted: '%s' in '%s'.", propertyId.GetCStr(), m_absolutePath.c_str());
|
||||
result = false;
|
||||
addPropertiesResult = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -393,7 +363,12 @@ namespace MaterialEditor
|
||||
return true;
|
||||
});
|
||||
|
||||
return result;
|
||||
if (!addPropertiesResult || !AZ::RPI::JsonUtils::SaveObjectToFile(m_savePathNormalized, sourceData))
|
||||
{
|
||||
AZ_Error("MaterialDocument", false, "Document could not be saved: '%s'.", m_savePathNormalized.c_str());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MaterialDocument::Open(AZStd::string_view loadPath)
|
||||
|
||||
@@ -74,8 +74,7 @@ namespace MaterialEditor
|
||||
// AZ::TickBus overrides...
|
||||
void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
|
||||
|
||||
bool SavePropertiesToSourceData(
|
||||
const AZStd::string& exportPath, AZ::RPI::MaterialSourceData& sourceData, PropertyFilterFunction propertyFilter) const;
|
||||
bool SaveSourceData(AZ::RPI::MaterialSourceData& sourceData, PropertyFilterFunction propertyFilter) const;
|
||||
|
||||
// AtomToolsFramework::AtomToolsDocument overrides...
|
||||
void Clear() override;
|
||||
|
||||
Reference in New Issue
Block a user