Skip to content

Commit

Permalink
Fixed copying and capturing stamps on staggered maps
Browse files Browse the repository at this point in the history
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 0f7804c)
  • Loading branch information
bjorn committed Aug 10, 2021
1 parent 0a23524 commit ef6a542
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
15 changes: 10 additions & 5 deletions src/libtiled/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,20 @@ void Map::normalizeTileLayerPositionsAndMapSize()
while (auto tileLayer = static_cast<TileLayer*>(it.next()))
contentRect |= tileLayer->region().boundingRect();

if (!contentRect.isEmpty()) {
QPoint offset = contentRect.topLeft();
if (!contentRect.topLeft().isNull()) {
it.toFront();
while (auto tileLayer = static_cast<TileLayer*>(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<Map::StaggerIndex>((staggerIndex() + staggerOffSet) % 2));
}

setWidth(contentRect.width());
setHeight(contentRect.height());
}

/**
Expand Down
14 changes: 5 additions & 9 deletions src/tiled/capturestamphelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map::StaggerIndex>((staggerIndex + staggerIndexOffSet) % 2));
stamp->setStaggerIndex(static_cast<Map::StaggerIndex>((staggerIndex + staggerOffSet) % 2));

// Add tileset references to map
stamp->addTilesets(stamp->usedTilesets());
Expand Down

0 comments on commit ef6a542

Please sign in to comment.