From e49342667f121636b892de7afe35b011880008fb Mon Sep 17 00:00:00 2001 From: Ketan Gupta Date: Mon, 28 Aug 2017 01:30:37 +0530 Subject: [PATCH] Fixed Automapping on infinite maps (#1692) This change enables Automapping rules to be used on infinite maps. The rule maps themselves still can't be infinite maps. --- src/libtiled/tilelayer.cpp | 14 +++++--------- src/tiled/automapper.cpp | 28 +++++++++++----------------- src/tiled/automapperwrapper.cpp | 4 ++-- src/tiled/automappingmanager.cpp | 19 ++++++++++++++++--- 4 files changed, 34 insertions(+), 31 deletions(-) diff --git a/src/libtiled/tilelayer.cpp b/src/libtiled/tilelayer.cpp index 51515691f3..5d343ca419 100644 --- a/src/libtiled/tilelayer.cpp +++ b/src/libtiled/tilelayer.cpp @@ -197,20 +197,17 @@ void Tiled::TileLayer::setCell(int x, int y, const Cell &cell) TileLayer *TileLayer::copy(const QRegion ®ion) const { - const QRect bounds = region.boundingRect(); const QRect areaBounds = region.boundingRect(); - const int offsetX = qMax(0, areaBounds.x() - bounds.x()); - const int offsetY = qMax(0, areaBounds.y() - bounds.y()); TileLayer *copied = new TileLayer(QString(), 0, 0, - bounds.width(), bounds.height()); + areaBounds.width(), areaBounds.height()); for (const QRect &rect : region.rects()) for (int x = rect.left(); x <= rect.right(); ++x) for (int y = rect.top(); y <= rect.bottom(); ++y) - copied->setCell(x - areaBounds.x() + offsetX, - y - areaBounds.y() + offsetY, + copied->setCell(x - areaBounds.x(), + y - areaBounds.y(), cellAt(x, y)); return copied; @@ -235,7 +232,6 @@ void TileLayer::merge(const QPoint &pos, const TileLayer *layer) void TileLayer::setCells(int x, int y, TileLayer *layer, const QRegion &mask) { - // Determine the overlapping area QRegion area = QRect(x, y, layer->width(), layer->height()); if (!mask.isEmpty()) @@ -641,8 +637,8 @@ QRegion TileLayer::computeDiffRegion(const TileLayer *other) const const int dx = other->x() - mX; const int dy = other->y() - mY; - QRect r = QRect(0, 0, width(), height()); - r &= QRect(dx, dy, other->width(), other->height()); + + const QRect r = bounds().united(other->bounds()).translated(-position()); for (int y = r.top(); y <= r.bottom(); ++y) { for (int x = r.left(); x <= r.right(); ++x) { diff --git a/src/tiled/automapper.cpp b/src/tiled/automapper.cpp index 0bc1028f77..50bdcf28a4 100644 --- a/src/tiled/automapper.cpp +++ b/src/tiled/automapper.cpp @@ -690,19 +690,6 @@ static bool compareLayerTo(const TileLayer *setLayer, bool matchListYes = false; bool matchListNo = false; - - if (!setLayer->contains(x + offset.x(), y + offset.y())) { - foreach (const TileLayer *comparedTileLayer, listYes) { - if (!comparedTileLayer->contains(x, y)) - return false; - - const Cell &c2 = comparedTileLayer->cellAt(x, y); - if (!c2.isEmpty()) - return false; - } - continue; - } - const Cell &c1 = setLayer->cellAt(x + offset.x(), y + offset.y()); @@ -809,11 +796,18 @@ void AutoMapper::copyTileRegion(const TileLayer *srcLayer, int srcX, int srcY, int width, int height, TileLayer *dstLayer, int dstX, int dstY) { - const int startX = qMax(dstX, 0); - const int startY = qMax(dstY, 0); + int startX = dstX; + int startY = dstY; + + int endX = dstX + width; + int endY = dstY + height; - const int endX = qMin(dstX + width, dstLayer->width()); - const int endY = qMin(dstY + height, dstLayer->height()); + if (!mMapWork->infinite()) { + startX = qMax(0, startX); + startY = qMax(0, startY); + endX = qMin(dstLayer->width(), endX); + endY = qMin(dstLayer->height(), endY); + } const int offsetX = srcX - dstX; const int offsetY = srcY - dstY; diff --git a/src/tiled/automapperwrapper.cpp b/src/tiled/automapperwrapper.cpp index fd7d9fb629..befea28c4d 100644 --- a/src/tiled/automapperwrapper.cpp +++ b/src/tiled/automapperwrapper.cpp @@ -63,7 +63,7 @@ AutoMapperWrapper::AutoMapperWrapper(MapDocument *mapDocument, TileLayer *before = mLayersBefore.at(beforeIndex); TileLayer *after = static_cast(map->layerAt(layerIndex)); - if (before->drawMargins() != after->drawMargins()) + if (before->drawMargins() != after->drawMargins() || before->bounds() != after->bounds()) emit mMapDocument->tileLayerDrawMarginsChanged(after); // reduce memory usage by saving only diffs @@ -115,7 +115,7 @@ void AutoMapperWrapper::redo() void AutoMapperWrapper::patchLayer(int layerIndex, TileLayer *layer) { Map *map = mMapDocument->map(); - QRect b = layer->bounds(); + QRect b = layer->rect(); Q_ASSERT(map->layerAt(layerIndex)->asTileLayer()); TileLayer *t = static_cast(map->layerAt(layerIndex)); diff --git a/src/tiled/automappingmanager.cpp b/src/tiled/automappingmanager.cpp index 4e557e6612..fa3708c45f 100644 --- a/src/tiled/automappingmanager.cpp +++ b/src/tiled/automappingmanager.cpp @@ -52,10 +52,23 @@ void AutomappingManager::autoMap() return; Map *map = mMapDocument->map(); - int w = map->width(); - int h = map->height(); - autoMapInternal(QRect(0, 0, w, h), nullptr); + QRect bounds; + + if (map->infinite()) { + LayerIterator iterator(map); + + while (Layer *layer = iterator.next()) { + if (TileLayer *tileLayer = dynamic_cast(layer)) + bounds = bounds.united(tileLayer->bounds()); + } + } else { + int w = map->width(); + int h = map->height(); + bounds = QRect(0, 0, w, h); + } + + autoMapInternal(bounds, nullptr); } void AutomappingManager::autoMap(const QRegion &where, Layer *touchedLayer)