Skip to content

Commit

Permalink
Fixed layer IDs getting re-assigned when resizing the map
Browse files Browse the repository at this point in the history
This happened as well when toggling infinite on/off, since that
effectively does a resize as well.

Closes mapeditor#2160
  • Loading branch information
bjorn authored and gauauu committed Jul 17, 2019
1 parent 8f79ea9 commit 5f5e7be
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
21 changes: 19 additions & 2 deletions src/libtiled/layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ObjectGroup*>(this)->resetObjectIds();
break;
case GroupLayerType:
for (Layer *layer : static_cast<GroupLayer*>(this)->layers())
layer->resetIds();
break;
default:
break;
}
}

/**
* Returns the effective opacity, which is the opacity multiplied by the
* opacity of any parent layers.
Expand Down Expand Up @@ -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.
*
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/libtiled/layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion src/libtiled/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions src/tiled/editablelayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ void EditableLayer::detach()
setAsset(nullptr);

mDetachedLayer.reset(layer()->clone());
mDetachedLayer->resetIds();
setObject(mDetachedLayer.get());
EditableManager::instance().mEditableLayers.insert(layer(), this);
}
Expand Down
4 changes: 1 addition & 3 deletions src/tiled/mapdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,11 +492,9 @@ void MapDocument::duplicateLayers(const QList<Layer *> &layers)
}

Layer *duplicate = layer->clone();
duplicate->resetIds();
duplicate->setName(tr("Copy of %1").arg(duplicate->name()));

if (duplicate->layerType() == Layer::ObjectGroupType)
static_cast<ObjectGroup*>(duplicate)->resetObjectIds();

auto parentLayer = layer->parentLayer();

int index = previousIndex;
Expand Down

0 comments on commit 5f5e7be

Please sign in to comment.