From 5f5e7bef3e9a68efa5b3a8d5952d3d874ec3a15b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Wed, 17 Jul 2019 11:08:22 +0200 Subject: [PATCH] Fixed layer IDs getting re-assigned when resizing the map This happened as well when toggling infinite on/off, since that effectively does a resize as well. Closes #2160 --- src/libtiled/layer.cpp | 21 +++++++++++++++++++-- src/libtiled/layer.h | 1 + src/libtiled/map.cpp | 1 - src/tiled/editablelayer.cpp | 1 + src/tiled/mapdocument.cpp | 4 +--- 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/libtiled/layer.cpp b/src/libtiled/layer.cpp index cb09ed6111..3cd50af22b 100644 --- a/src/libtiled/layer.cpp +++ b/src/libtiled/layer.cpp @@ -52,6 +52,23 @@ Layer::Layer(TypeFlag type, const QString &name, int x, int y) : { } +void Layer::resetIds() +{ + mId = 0; // reset out own ID + + switch (layerType()) { + case ObjectGroupType: + static_cast(this)->resetObjectIds(); + break; + case GroupLayerType: + for (Layer *layer : static_cast(this)->layers()) + layer->resetIds(); + break; + default: + break; + } +} + /** * Returns the effective opacity, which is the opacity multiplied by the * opacity of any parent layers. @@ -165,7 +182,7 @@ bool Layer::canMergeDown() const * A helper function for initializing the members of the given instance to * those of this layer. Used by subclasses when cloning. * - * Layer name, position and size are not cloned, since they are assumed to have + * Layer name, position and size are not copied, since they are assumed to have * already been passed to the constructor. Also, map ownership is not cloned, * since the clone is not added to the map. * @@ -174,7 +191,7 @@ bool Layer::canMergeDown() const */ Layer *Layer::initializeClone(Layer *clone) const { - // mId is not copied, will be assigned when layer is added to a map + clone->mId = mId; clone->mOffset = mOffset; clone->mOpacity = mOpacity; clone->mVisible = mVisible; diff --git a/src/libtiled/layer.h b/src/libtiled/layer.h index a1147621ae..19981b711f 100644 --- a/src/libtiled/layer.h +++ b/src/libtiled/layer.h @@ -80,6 +80,7 @@ class TILEDSHARED_EXPORT Layer : public Object */ int id() const { return mId; } void setId(int id) { mId = id; } + void resetIds(); /** * Returns the type of this layer. diff --git a/src/libtiled/map.cpp b/src/libtiled/map.cpp index 989dcc7ae5..b73e7c161f 100644 --- a/src/libtiled/map.cpp +++ b/src/libtiled/map.cpp @@ -344,7 +344,6 @@ Map *Map::clone() const o->mDrawMarginsDirty = mDrawMarginsDirty; for (const Layer *layer : mLayers) { Layer *clone = layer->clone(); - clone->setId(layer->id()); clone->setMap(o); o->mLayers.append(clone); } diff --git a/src/tiled/editablelayer.cpp b/src/tiled/editablelayer.cpp index 9f278b6e35..5773664f5b 100644 --- a/src/tiled/editablelayer.cpp +++ b/src/tiled/editablelayer.cpp @@ -68,6 +68,7 @@ void EditableLayer::detach() setAsset(nullptr); mDetachedLayer.reset(layer()->clone()); + mDetachedLayer->resetIds(); setObject(mDetachedLayer.get()); EditableManager::instance().mEditableLayers.insert(layer(), this); } diff --git a/src/tiled/mapdocument.cpp b/src/tiled/mapdocument.cpp index ad457cefe1..64a5dfd0a1 100644 --- a/src/tiled/mapdocument.cpp +++ b/src/tiled/mapdocument.cpp @@ -492,11 +492,9 @@ void MapDocument::duplicateLayers(const QList &layers) } Layer *duplicate = layer->clone(); + duplicate->resetIds(); duplicate->setName(tr("Copy of %1").arg(duplicate->name())); - if (duplicate->layerType() == Layer::ObjectGroupType) - static_cast(duplicate)->resetObjectIds(); - auto parentLayer = layer->parentLayer(); int index = previousIndex;