diff --git a/NEWS.md b/NEWS.md index 533ac6a51a..7a7767e430 100644 --- a/NEWS.md +++ b/NEWS.md @@ -35,6 +35,7 @@ * AutoMapping: Ignore empty outputs per-rule (#3523) * Automapping: Added per-input-layer properties for ignoring flip flags (#3803) * AutoMapping: Always apply output sets with empty index +* AutoMapping: Fixed adding of new tilesets used by applied changes * Windows: Fixed the support for WebP images (updated to Qt 6.6.1, #3661) * Fixed issues related to map and tileset reloading * Fixed possible crash after assigning to tiled.activeAsset diff --git a/src/tiled/automapperwrapper.cpp b/src/tiled/automapperwrapper.cpp index 8516399f6f..3fc4ae1735 100644 --- a/src/tiled/automapperwrapper.cpp +++ b/src/tiled/automapperwrapper.cpp @@ -26,9 +26,9 @@ #include "addremovetileset.h" #include "automapper.h" #include "changeproperties.h" -#include "containerhelpers.h" #include "map.h" #include "mapdocument.h" +#include "mapobject.h" #include "objectreferenceshelper.h" #include "tile.h" #include "tilelayer.h" @@ -82,10 +82,13 @@ AutoMapperWrapper::AutoMapperWrapper(MapDocument *mapDocument, } } + QSet usedTilesets; // keep track of tilesets used by pending changes + // Apply the changes to existing tile layers for (auto& [original, outputLayer] : context.originalToOutputLayerMapping) { const QRegion diffRegion = original->computeDiffRegion(*outputLayer); if (!diffRegion.isEmpty()) { + usedTilesets |= outputLayer->usedTilesets(); paint(original, 0, 0, outputLayer.get(), diffRegion.translated(original->position())); } @@ -96,11 +99,6 @@ AutoMapperWrapper::AutoMapperWrapper(MapDocument *mapDocument, new ChangeProperties(mapDocument, QString(), original, *propertiesIt, this); } - // Make sure to add any newly used tilesets to the map - for (const SharedTileset &tileset : std::as_const(context.newTilesets)) - if (context.targetMap->isTilesetUsed(tileset.data())) - new AddTileset(mapDocument, tileset, this); - auto anyObjectForObjectGroup = [&] (ObjectGroup *objectGroup) { for (const QVector &entries : std::as_const(context.newMapObjects)) { for (const AddMapObjects::Entry &entry : entries) { @@ -121,6 +119,7 @@ AutoMapperWrapper::AutoMapperWrapper(MapDocument *mapDocument, if (!anyObjectForObjectGroup(objectGroup)) continue; + usedTilesets |= layer->usedTilesets(); new AddLayer(mapDocument, newLayerIndex++, layer.release(), nullptr, this); @@ -133,8 +132,12 @@ AutoMapperWrapper::AutoMapperWrapper(MapDocument *mapDocument, for (const QVector &entries : std::as_const(context.newMapObjects)) { // Each group of copied objects needs to be rewired separately ObjectReferencesHelper objectRefs(mapDocument->map()); - for (auto &entry : std::as_const(entries)) + for (auto &entry : std::as_const(entries)) { objectRefs.reassignId(entry.mapObject); + + if (Tile *tile = entry.mapObject->cell().tile()) + usedTilesets.insert(tile->tileset()->sharedFromThis()); + } objectRefs.rewire(); allEntries.append(entries); @@ -146,4 +149,9 @@ AutoMapperWrapper::AutoMapperWrapper(MapDocument *mapDocument, // Remove any objects that have been scheduled for removal if (!context.mapObjectsToRemove.isEmpty()) new RemoveMapObjects(mapDocument, context.mapObjectsToRemove.values(), this); + + // Make sure to add any newly used tilesets to the map + for (const SharedTileset &tileset : std::as_const(context.newTilesets)) + if (usedTilesets.contains(tileset)) + new AddTileset(mapDocument, tileset, this); } diff --git a/tests/automapping/add-tileset/map-result.tmx b/tests/automapping/add-tileset/map-result.tmx new file mode 100644 index 0000000000..173ca3674a --- /dev/null +++ b/tests/automapping/add-tileset/map-result.tmx @@ -0,0 +1,22 @@ + + + + + +0,0,0,0,0, +0,0,0,0,0, +0,0,0,0,0, +0,0,0,0,0, +0,0,0,0,0 + + + + +2,2,2,2,2, +2,2,2,2,2, +2,2,2,2,2, +2,2,2,2,2, +2,2,2,2,2 + + + diff --git a/tests/automapping/add-tileset/map.tmx b/tests/automapping/add-tileset/map.tmx new file mode 100644 index 0000000000..95d3362b45 --- /dev/null +++ b/tests/automapping/add-tileset/map.tmx @@ -0,0 +1,21 @@ + + + + +0,0,0,0,0, +0,0,0,0,0, +0,0,0,0,0, +0,0,0,0,0, +0,0,0,0,0 + + + + +0,0,0,0,0, +0,0,0,0,0, +0,0,0,0,0, +0,0,0,0,0, +0,0,0,0,0 + + + diff --git a/tests/automapping/add-tileset/rules.tmx b/tests/automapping/add-tileset/rules.tmx new file mode 100644 index 0000000000..74cc3dc017 --- /dev/null +++ b/tests/automapping/add-tileset/rules.tmx @@ -0,0 +1,19 @@ + + + + + + +0,0,0, +0,20,0, +0,0,0 + + + + +0,0,0, +0,2,0, +0,0,0 + + + diff --git a/tests/automapping/add-tileset/rules.txt b/tests/automapping/add-tileset/rules.txt new file mode 100644 index 0000000000..262b188fa9 --- /dev/null +++ b/tests/automapping/add-tileset/rules.txt @@ -0,0 +1 @@ +./rules.tmx