Skip to content

Commit

Permalink
Fixed up restoring of previously selected layer
Browse files Browse the repository at this point in the history
Stored number is now interpreted as "global layer index".
  • Loading branch information
bjorn committed Jan 31, 2017
1 parent 782893a commit a02c667
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
35 changes: 34 additions & 1 deletion src/libtiled/layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include "objectgroup.h"
#include "tilelayer.h"

using namespace Tiled;
namespace Tiled {

Layer::Layer(TypeFlag type, const QString &name, int x, int y) :
Object(LayerType),
Expand Down Expand Up @@ -212,3 +212,36 @@ Layer *LayerIterator::previous()

return layer;
}

/**
* Returns the global layer index for the given \a layer. Obtained by iterating
* the layer's map while incrementing the index until layer is found.
*/
int globalIndex(Layer *layer)
{
if (!layer)
return -1;

LayerIterator counter(layer->map());
int index = 0;
while (counter.next() && counter.currentLayer() != layer)
++index;

return index;
}

/**
* Returns the layer at the given global \a index.
*
* \sa globalIndex()
*/
Layer *layerAtGlobalIndex(const Map *map, int index)
{
LayerIterator counter(map);
while (counter.next() && index > 0)
--index;

return counter.currentLayer();
}

} // namespace Tiled
3 changes: 3 additions & 0 deletions src/libtiled/layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@ inline bool LayerIterator::hasParent() const
}


TILEDSHARED_EXPORT int globalIndex(Layer *layer);
TILEDSHARED_EXPORT Layer *layerAtGlobalIndex(const Map *map, int index);

} // namespace Tiled

#endif // LAYER_H
10 changes: 4 additions & 6 deletions src/tiled/mapeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,9 @@ void MapEditor::addDocument(Document *document)
view->horizontalScrollBar()->setSliderPosition(hor);
view->verticalScrollBar()->setSliderPosition(ver);

// todo: find a way to restore selected layer
int layer = mapState.value(QLatin1String("selectedLayer")).toInt();
if (layer > 0 && layer < mapDocument->map()->layerCount())
mapDocument->setCurrentLayer(mapDocument->map()->layerAt(layer));
int layerIndex = mapState.value(QLatin1String("selectedLayer")).toInt();
if (Layer *layer = layerAtGlobalIndex(mapDocument->map(), layerIndex))
mapDocument->setCurrentLayer(layer);
}
}

Expand All @@ -337,8 +336,7 @@ void MapEditor::removeDocument(Document *document)
mapState.insert(QLatin1String("scale"), mapView->zoomable()->scale());
mapState.insert(QLatin1String("scrollX"), mapView->horizontalScrollBar()->sliderPosition());
mapState.insert(QLatin1String("scrollY"), mapView->verticalScrollBar()->sliderPosition());
// todo: find a good way to store a layer reference now that it is a hierarchy
mapState.insert(QLatin1String("selectedLayer"), mapDocument->layerIndex(mapDocument->currentLayer()));
mapState.insert(QLatin1String("selectedLayer"), globalIndex(mapDocument->currentLayer()));
mMapStates.insert(mapDocument->fileName(), mapState);

Preferences *prefs = Preferences::instance();
Expand Down

0 comments on commit a02c667

Please sign in to comment.