Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve class members when exporting and the relevant option is enabled #3905

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* tmxviewer: Added support for viewing JSON maps (#3866)
* AutoMapping: Ignore empty outputs per-rule (#3523)
* Windows: Fixed the support for WebP images (updated to Qt 6.6.1, #3661)
* Fixed the option to resolve properties on export to also resolve class members (#3411, #3315)
* Fixed terrain tool behavior and terrain overlays after changing terrain set type (#3204, #3260)
* Fixed mouse handling issue when zooming while painting (#3863)
* Fixed possible crash after a scripted tool disappears while active
Expand Down
49 changes: 48 additions & 1 deletion src/tiled/exporthelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,50 @@ const Map *ExportHelper::prepareExportMap(const Map *map, std::unique_ptr<Map> &
return exportMap.get();
}

static bool resolveClassPropertyMembers(QVariant &value)
{
if (value.userType() != propertyValueId())
return false;

auto propertyValue = value.value<PropertyValue>();
const PropertyType *propertyType = propertyValue.type();
if (!propertyType || !propertyType->isClass())
return false;

auto classType = static_cast<const ClassPropertyType*>(propertyType);
QVariantMap classValue = propertyValue.value.toMap();
bool changed = false;

// iterate over the members of the class type, making sure each
// member is present in classValue, recursively resolving its members
auto it = classType->members.begin();
const auto it_end = classType->members.end();
for (; it != it_end; ++it) {
const auto &memberName = it.key();
auto &value = classValue[memberName];

if (!value.isValid()) {
value = it.value();
changed = true;
}

changed |= resolveClassPropertyMembers(value);
}

if (changed) {
propertyValue.value = classValue;
value = QVariant::fromValue(propertyValue);
}

return changed;
}

static void resolveClassPropertyMembers(QVariantMap &properties)
{
for (auto &value : properties)
resolveClassPropertyMembers(value);
}

void ExportHelper::resolveProperties(Object *object) const
{
switch (object->typeId()) {
Expand Down Expand Up @@ -175,6 +219,7 @@ void ExportHelper::resolveProperties(Object *object) const
// Override with own properties
mergeProperties(properties, mapObject->properties());

resolveClassPropertyMembers(properties);
mapObject->setProperties(properties);
return;
}
Expand Down Expand Up @@ -211,7 +256,9 @@ void ExportHelper::resolveProperties(Object *object) const
break;
}

object->setProperties(object->resolvedProperties());
auto properties = object->resolvedProperties();
resolveClassPropertyMembers(properties);
object->setProperties(properties);
}

} // namespace Tiled
Loading