Skip to content

Commit

Permalink
JSON format: Fixed tile order when loading a tileset using the old fo…
Browse files Browse the repository at this point in the history
…rmat

Since Tiled 1.7, the order of tiles in the tileset file determines the
display order. But this should not be the case for tilesets stored in
the old JSON format (which was replaced in Tiled 1.2), since it used an
object with the tile IDs as string keys for storing tile meta-data and
these keys get sorted alphabetically.

This was resulting in tiles with associated data getting displayed in an
order like "1, 10, 11, 12, 13, ..., 2, 21, ...".
  • Loading branch information
bjorn committed Aug 29, 2023
1 parent 061e33e commit f66c972
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* JSON format: Fixed tile order when loading a tileset using the old format

### Tiled 1.10.2 (4 August 2023)

* Added support for setting custom properties on the project (#2903)
Expand Down
7 changes: 7 additions & 0 deletions src/libtiled/tileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,13 @@ bool Tileset::anyTileOutOfOrder() const
return false;
}

void Tileset::resetTileOrder()
{
mTiles.clear();
for (Tile *tile : std::as_const(mTilesById))
mTiles.append(tile);
}

/**
* Sets the \a image to be used for the given \a tile.
*
Expand Down
1 change: 1 addition & 0 deletions src/libtiled/tileset.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ class TILEDSHARED_EXPORT Tileset : public Object, public QEnableSharedFromThis<T
QList<int> relocateTiles(const QList<Tile *> &tiles, int location);

bool anyTileOutOfOrder() const;
void resetTileOrder();

void setNextTileId(int nextId);
int nextTileId() const;
Expand Down
7 changes: 7 additions & 0 deletions src/libtiled/varianttomapconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,13 @@ SharedTileset VariantToMapConverter::toTileset(const QVariant &variant)
tileset->findOrCreateTile(tileId)->setProperties(properties);
}

if (!tilesVariantMap.isEmpty() || !propertiesVariantMap.isEmpty()) {
// The presence of either of these maps indicates that the tileset
// is in the old 1.0 format. This means the tiles may not have been
// added in the right order.
tileset->resetTileOrder();
}

// Read the tiles saved as a list (1.2 format)
const QVariantList tilesVariantList = tilesVariant.toList();
for (int i = 0; i < tilesVariantList.count(); ++i) {
Expand Down

0 comments on commit f66c972

Please sign in to comment.