Skip to content

Commit

Permalink
Resolve class members when exporting and the relevant option is enabled
Browse files Browse the repository at this point in the history
This is be tied into the "Resolve object types and properties" option
for now, both because of string-freeze for Tiled 1.10 and because I will
wait on a use-case for doing this independently of that option before
implementing it as a separate option.

Closes #3411
Closes #3315
  • Loading branch information
bjorn committed Mar 12, 2024
1 parent d2e7a29 commit d29ef08
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
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

0 comments on commit d29ef08

Please sign in to comment.