Skip to content

Commit

Permalink
Avoid potential issues with initializing object IDs
Browse files Browse the repository at this point in the history
When child ObjectGroup layers would be added to a GroupLayer instance
before it is added to the map, the objects in the ObjectGroup would not
get their IDs initialized.

Delegate the setting of map ownership to the Map class, so that the IDs
will initialize also in such a scenario.
  • Loading branch information
bjorn committed Feb 7, 2017
1 parent 38712a4 commit a6ffb2c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/libtiled/grouplayer.cpp
Expand Up @@ -49,11 +49,11 @@ void GroupLayer::insertLayer(int index, Layer *layer)
void GroupLayer::adoptLayer(Layer *layer)
{
layer->setParentLayer(this);
layer->setMap(map());

if (map())
if (ObjectGroup *group = layer->asObjectGroup())
map()->initializeObjectIds(*group);
map()->adoptLayer(layer);
else
layer->setMap(nullptr);
}

Layer *GroupLayer::takeLayerAt(int index)
Expand Down Expand Up @@ -115,10 +115,15 @@ Layer *GroupLayer::clone() const

void GroupLayer::setMap(Map *map)
{
// todo: What about initializing object IDs?
Layer::setMap(map);
for (Layer *layer : mLayers)
layer->setMap(map);

if (map) {
for (Layer *layer : mLayers)
map->adoptLayer(layer);
} else {
for (Layer *layer : mLayers)
layer->setMap(nullptr);
}
}

GroupLayer *GroupLayer::initializeClone(GroupLayer *clone) const
Expand Down
2 changes: 2 additions & 0 deletions src/libtiled/map.h
Expand Up @@ -369,6 +369,8 @@ class TILEDSHARED_EXPORT Map : public Object
void initializeObjectIds(ObjectGroup &objectGroup);

private:
friend class GroupLayer; // so it cal call adoptLayer

void adoptLayer(Layer *layer);

void recomputeDrawMargins() const;
Expand Down

0 comments on commit a6ffb2c

Please sign in to comment.