Skip to content

Commit

Permalink
Highlight selected layers instead of the current one
Browse files Browse the repository at this point in the history
It's a little confusing that the "current" layer is not necessarily a
"selected" one. We may need to make sure that it is always one of the
selected layers, rather than synchronizing it strictly with the current
index in the Layers view.
  • Loading branch information
bjorn committed Dec 18, 2017
1 parent be33335 commit 20716cd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
5 changes: 3 additions & 2 deletions src/libtiled/layer.h
Expand Up @@ -264,8 +264,9 @@ inline QPointF Layer::offset() const


/**
* An iterator for iterating over the layers of a map. When iterating forward,
* group layers are traversed after their children.
* An iterator for iterating over the layers of a map, in the order in which
* they are drawn. When iterating forward, group layers are traversed after
* their children.
*
* Modifying the layer hierarchy while an iterator is active will lead to
* undefined results!
Expand Down
43 changes: 24 additions & 19 deletions src/tiled/mapitem.cpp
Expand Up @@ -77,7 +77,7 @@ MapItem::MapItem(MapDocument *mapDocument, QGraphicsItem *parent)
connect(mapDocument, &MapDocument::layerChanged, this, &MapItem::layerChanged);
connect(mapDocument, &MapDocument::objectGroupChanged, this, &MapItem::objectGroupChanged);
connect(mapDocument, &MapDocument::imageLayerChanged, this, &MapItem::imageLayerChanged);
connect(mapDocument, &MapDocument::currentLayerChanged, this, &MapItem::currentLayerChanged);
connect(mapDocument, &MapDocument::selectedLayersChanged, this, &MapItem::updateCurrentLayerHighlight);
connect(mapDocument, &MapDocument::tilesetTileOffsetChanged, this, &MapItem::adaptToTilesetTileSizeChanges);
connect(mapDocument, &MapDocument::tileImageSourceChanged, this, &MapItem::adaptToTileSizeChanges);
connect(mapDocument, &MapDocument::tilesetReplaced, this, &MapItem::tilesetReplaced);
Expand Down Expand Up @@ -120,11 +120,6 @@ void MapItem::repaintRegion(const QRegion &region, TileLayer *tileLayer)
}
}

void MapItem::currentLayerChanged()
{
updateCurrentLayerHighlight();
}

void MapItem::mapChanged()
{
for (QGraphicsItem *item : mLayerItems) {
Expand All @@ -134,7 +129,6 @@ void MapItem::mapChanged()

for (MapObjectItem *item : mObjectItems)
item->syncWithMapObject();

}

void MapItem::tileLayerChanged(TileLayer *tileLayer)
Expand Down Expand Up @@ -447,9 +441,9 @@ LayerItem *MapItem::createLayerItem(Layer *layer)
void MapItem::updateCurrentLayerHighlight()
{
Preferences *prefs = Preferences::instance();
const auto currentLayer = mMapDocument->currentLayer();
const auto selectedLayers = mMapDocument->selectedLayers();

if (!prefs->highlightCurrentLayer() || !currentLayer) {
if (!prefs->highlightCurrentLayer() || selectedLayers.isEmpty()) {
if (mDarkRectangle->isVisible()) {
mDarkRectangle->setVisible(false);

Expand All @@ -462,9 +456,19 @@ void MapItem::updateCurrentLayerHighlight()
return;
}

// Darken layers below the current layer
const int siblingIndex = currentLayer->siblingIndex();
const auto parentLayer = currentLayer->parentLayer();
Layer *lowestSelectedLayer = nullptr;
LayerIterator iterator(mMapDocument->map());
while (Layer *layer = iterator.next()) {
if (selectedLayers.contains(layer)) {
lowestSelectedLayer = layer;
break;
}
}
Q_ASSERT(lowestSelectedLayer);

// Darken layers below the lowest selected layer
const int siblingIndex = lowestSelectedLayer->siblingIndex();
const auto parentLayer = lowestSelectedLayer->parentLayer();
QGraphicsItem *parentItem = mLayerItems.value(parentLayer);
if (!parentItem)
parentItem = this;
Expand All @@ -474,16 +478,17 @@ void MapItem::updateCurrentLayerHighlight()
mDarkRectangle->setVisible(true);

// Set layers above the current layer to reduced opacity
LayerIterator iterator(mMapDocument->map());
qreal multiplier = 1;
iterator.toFront();
bool foundSelected = false;

while (Layer *layer = iterator.next()) {
GroupLayer *groupLayer = layer->asGroupLayer();
if (!groupLayer)
mLayerItems.value(layer)->setOpacity(layer->opacity() * multiplier);
bool isSelected = selectedLayers.contains(layer);
foundSelected |= isSelected;

if (layer == currentLayer)
multiplier = opacityFactor;
if (!layer->isGroupLayer()) {
qreal multiplier = (foundSelected && !isSelected) ? opacityFactor : 1;
mLayerItems.value(layer)->setOpacity(layer->opacity() * multiplier);
}
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/tiled/mapitem.h
Expand Up @@ -65,8 +65,6 @@ class MapItem : public QGraphicsObject
*/
void repaintRegion(const QRegion &region, TileLayer *tileLayer);

void currentLayerChanged();

void mapChanged();
void tileLayerChanged(TileLayer *tileLayer);

Expand Down

0 comments on commit 20716cd

Please sign in to comment.