Ctrl drag now adds/removes to selection (#5336)

* refactor: clean up to use for-each loop

* track cards in rect so far and toggle isSelected on change

* only clear selection if ctrl isn't held

* fix build errors
This commit is contained in:
RickyRister 2024-12-26 12:08:20 -08:00 committed by GitHub
parent ca486e5ed9
commit d5ae4eed26
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 6 deletions

View file

@ -58,11 +58,21 @@ void SelectZone::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
pos.setY(br.height()); pos.setY(br.height());
QRectF selectionRect = QRectF(selectionOrigin, pos).normalized(); QRectF selectionRect = QRectF(selectionOrigin, pos).normalized();
for (int i = 0; i < cards.size(); ++i) { for (auto card : cards) {
if (cards[i]->getAttachedTo()) if (card->getAttachedTo() && card->getAttachedTo()->getZone() != this) {
if (cards[i]->getAttachedTo()->getZone() != this) continue;
continue; }
cards[i]->setSelected(selectionRect.intersects(cards[i]->mapRectToParent(cards[i]->boundingRect())));
bool inRect = selectionRect.intersects(card->mapRectToParent(card->boundingRect()));
if (inRect && !cardsInSelectionRect.contains(card)) {
// selection has just expanded to cover the card
cardsInSelectionRect.insert(card);
card->setSelected(!card->isSelected());
} else if (!inRect && cardsInSelectionRect.contains(card)) {
// selection has just shrunk to no longer cover the card
cardsInSelectionRect.remove(card);
card->setSelected(!card->isSelected());
}
} }
static_cast<GameScene *>(scene())->resizeRubberBand( static_cast<GameScene *>(scene())->resizeRubberBand(
deviceTransform(static_cast<GameScene *>(scene())->getViewportTransform()).map(pos)); deviceTransform(static_cast<GameScene *>(scene())->getViewportTransform()).map(pos));
@ -73,7 +83,9 @@ void SelectZone::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
void SelectZone::mousePressEvent(QGraphicsSceneMouseEvent *event) void SelectZone::mousePressEvent(QGraphicsSceneMouseEvent *event)
{ {
if (event->button() == Qt::LeftButton) { if (event->button() == Qt::LeftButton) {
scene()->clearSelection(); if (!event->modifiers().testFlag(Qt::ControlModifier)) {
scene()->clearSelection();
}
selectionOrigin = event->pos(); selectionOrigin = event->pos();
static_cast<GameScene *>(scene())->startRubberBand(event->scenePos()); static_cast<GameScene *>(scene())->startRubberBand(event->scenePos());
@ -85,6 +97,7 @@ void SelectZone::mousePressEvent(QGraphicsSceneMouseEvent *event)
void SelectZone::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) void SelectZone::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{ {
selectionOrigin = QPoint(); selectionOrigin = QPoint();
cardsInSelectionRect.clear();
static_cast<GameScene *>(scene())->stopRubberBand(); static_cast<GameScene *>(scene())->stopRubberBand();
event->accept(); event->accept();
} }

View file

@ -3,6 +3,8 @@
#include "card_zone.h" #include "card_zone.h"
#include <QSet>
/** /**
* A CardZone where the cards are laid out, with each card directly interactable by clicking. * A CardZone where the cards are laid out, with each card directly interactable by clicking.
*/ */
@ -11,6 +13,7 @@ class SelectZone : public CardZone
Q_OBJECT Q_OBJECT
private: private:
QPointF selectionOrigin; QPointF selectionOrigin;
QSet<CardItem *> cardsInSelectionRect;
protected: protected:
void mouseMoveEvent(QGraphicsSceneMouseEvent *event); void mouseMoveEvent(QGraphicsSceneMouseEvent *event);