From ef6a54268e018a7571160c966dfae2356e282d53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Tue, 10 Aug 2021 15:09:10 +0200 Subject: [PATCH] Fixed copying and capturing stamps on staggered maps When copying a selected area or creating a tile stamp from the selection, the captured stamp would only have the tiles in the correct position when the selection started at even positions. When capturing an area at an odd position, the stagger index needs to be adjusted. This was already done for right-click capturing in the CaptureStampHelper, and now it also happens in Map::normalizeTileLayerPositionsAndMapSize. Fixes #2874 (cherry picked from commit 0f7804cc29bf04a16078e16295ad687f3775bff0) --- src/libtiled/map.cpp | 15 ++++++++++----- src/tiled/capturestamphelper.cpp | 14 +++++--------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/libtiled/map.cpp b/src/libtiled/map.cpp index f683f322fa..11a2d1544f 100644 --- a/src/libtiled/map.cpp +++ b/src/libtiled/map.cpp @@ -416,15 +416,20 @@ void Map::normalizeTileLayerPositionsAndMapSize() while (auto tileLayer = static_cast(it.next())) contentRect |= tileLayer->region().boundingRect(); - if (!contentRect.isEmpty()) { - QPoint offset = contentRect.topLeft(); + if (!contentRect.topLeft().isNull()) { it.toFront(); while (auto tileLayer = static_cast(it.next())) - tileLayer->setPosition(tileLayer->position() - offset); + tileLayer->setPosition(tileLayer->position() - contentRect.topLeft()); - setWidth(contentRect.width()); - setHeight(contentRect.height()); + // Adjust the stagger index when layers are moved by odd amounts + const int staggerOffSet = (staggerAxis() == Map::StaggerX ? contentRect.x() + : contentRect.y()) % 2; + + setStaggerIndex(static_cast((staggerIndex() + staggerOffSet) % 2)); } + + setWidth(contentRect.width()); + setHeight(contentRect.height()); } /** diff --git a/src/tiled/capturestamphelper.cpp b/src/tiled/capturestamphelper.cpp index d9be5a7a42..e50aaae0a1 100644 --- a/src/tiled/capturestamphelper.cpp +++ b/src/tiled/capturestamphelper.cpp @@ -72,16 +72,12 @@ TileStamp CaptureStampHelper::endCapture(const MapDocument &mapDocument, QPoint } if (stamp->layerCount() > 0) { - auto staggerIndex = stamp->staggerIndex(); + // Adjust the stagger index when the capture starts at an odd offset + const auto staggerIndex = stamp->staggerIndex(); + const int staggerOffSet = (stamp->staggerAxis() == Map::StaggerX ? captured.x() + : captured.y()) % 2; - // Gets if the relative stagger should be the same as the base layer - int staggerIndexOffSet; - if (stamp->staggerAxis() == Map::StaggerX) - staggerIndexOffSet = captured.x() % 2; - else - staggerIndexOffSet = captured.y() % 2; - - stamp->setStaggerIndex(static_cast((staggerIndex + staggerIndexOffSet) % 2)); + stamp->setStaggerIndex(static_cast((staggerIndex + staggerOffSet) % 2)); // Add tileset references to map stamp->addTilesets(stamp->usedTilesets());