Skip to content

Commit

Permalink
Automapping: Fixed adding of new tilesets used by applied changes (#3983
Browse files Browse the repository at this point in the history
)

There was some code that should have done this, but it was checking 
which new tilesets were used before that actual changes had been
applied, which entirely broke it. It was probably broken since Tiled
1.9.
  • Loading branch information
bjorn authored Jun 21, 2024
1 parent fdce601 commit 3b2fd22
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 7 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 15 additions & 7 deletions src/tiled/automapperwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -82,10 +82,13 @@ AutoMapperWrapper::AutoMapperWrapper(MapDocument *mapDocument,
}
}

QSet<SharedTileset> 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()));
}
Expand All @@ -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<AddMapObjects::Entry> &entries : std::as_const(context.newMapObjects)) {
for (const AddMapObjects::Entry &entry : entries) {
Expand All @@ -121,6 +119,7 @@ AutoMapperWrapper::AutoMapperWrapper(MapDocument *mapDocument,
if (!anyObjectForObjectGroup(objectGroup))
continue;

usedTilesets |= layer->usedTilesets();
new AddLayer(mapDocument,
newLayerIndex++,
layer.release(), nullptr, this);
Expand All @@ -133,8 +132,12 @@ AutoMapperWrapper::AutoMapperWrapper(MapDocument *mapDocument,
for (const QVector<AddMapObjects::Entry> &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);
Expand All @@ -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);
}
22 changes: 22 additions & 0 deletions tests/automapping/add-tileset/map-result.tmx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.11.0" orientation="orthogonal" renderorder="right-down" width="5" height="5" tilewidth="16" tileheight="16" infinite="0" nextlayerid="9" nextobjectid="1">
<tileset firstgid="1" source="../spr_test_tileset.tsx"/>
<layer id="1" name="set" width="5" height="5">
<data encoding="csv">
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
</data>
</layer>
<layer id="8" name="auto" width="5" height="5">
<data encoding="csv">
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
</data>
</layer>
</map>
21 changes: 21 additions & 0 deletions tests/automapping/add-tileset/map.tmx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.11.0" orientation="orthogonal" renderorder="right-down" width="5" height="5" tilewidth="16" tileheight="16" infinite="0" nextlayerid="9" nextobjectid="1">
<layer id="1" name="set" width="5" height="5">
<data encoding="csv">
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
</data>
</layer>
<layer id="8" name="auto" width="5" height="5">
<data encoding="csv">
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
</data>
</layer>
</map>
19 changes: 19 additions & 0 deletions tests/automapping/add-tileset/rules.tmx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.11.0" orientation="orthogonal" renderorder="right-down" width="3" height="3" tilewidth="16" tileheight="16" infinite="0" nextlayerid="5" nextobjectid="5">
<tileset firstgid="1" source="../spr_test_tileset.tsx"/>
<tileset firstgid="17" source=":/automap-tiles.tsx"/>
<layer id="1" name="input_set" width="3" height="3">
<data encoding="csv">
0,0,0,
0,20,0,
0,0,0
</data>
</layer>
<layer id="2" name="output_auto" width="3" height="3">
<data encoding="csv">
0,0,0,
0,2,0,
0,0,0
</data>
</layer>
</map>
1 change: 1 addition & 0 deletions tests/automapping/add-tileset/rules.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./rules.tmx

0 comments on commit 3b2fd22

Please sign in to comment.