Commit 0fdc01f6 authored by Max-Wilhelm Bruker's avatar Max-Wilhelm Bruker
Browse files

GameScene::event() optimization

parent f7963e71
......@@ -173,6 +173,9 @@ void AbstractCardItem::setName(const QString &_name)
void AbstractCardItem::setHovered(bool _hovered)
{
if (isHovered == _hovered)
return;
isHovered = _hovered;
setZValue(_hovered ? 2000000004 : realZValue);
update();
......
......@@ -145,11 +145,13 @@ bool GameScene::event(QEvent *event)
if (event->type() == QEvent::GraphicsSceneMouseMove) {
QGraphicsSceneMouseEvent *mouseEvent = static_cast<QGraphicsSceneMouseEvent *>(event);
QSet<CardItem *> cardsToUnhover;
QList<QGraphicsItem *> oldItemList = items(mouseEvent->lastScenePos());
for (int i = 0; i < oldItemList.size(); ++i) {
CardItem *card = qgraphicsitem_cast<CardItem *>(oldItemList[i]);
if (card)
card->setHovered(false);
cardsToUnhover.insert(card);
}
QList<QGraphicsItem *> itemList = items(mouseEvent->scenePos());
......@@ -159,24 +161,31 @@ bool GameScene::event(QEvent *event)
for (int i = 0; i < itemList.size(); ++i)
if ((zone = qgraphicsitem_cast<CardZone *>(itemList[i])))
break;
if (zone) {
qreal maxZ = -1;
CardItem *maxZCard = 0;
QList<CardItem *> cardList;
qreal maxZ = -1;
for (int i = 0; i < itemList.size(); ++i) {
CardItem *card = qgraphicsitem_cast<CardItem *>(itemList[i]);
if (!card)
continue;
if (card->getZone() != zone)
continue;
cardList.append(card);
if (card->getRealZValue() > maxZ) {
maxZ = card->getRealZValue();
maxZCard = card;
}
cardsToUnhover.insert(card);
}
if (maxZCard) {
cardsToUnhover.remove(maxZCard);
maxZCard->setHovered(true);
}
for (int i = 0; i < cardList.size(); ++i)
cardList[i]->setHovered(cardList[i] == maxZCard);
}
QSet<CardItem *>::const_iterator i = cardsToUnhover.constBegin();
while (i != cardsToUnhover.constEnd()) {
(*i)->setHovered(false);
++i;
}
}
return QGraphicsScene::event(event);
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment