[Refactor] Retype card containers and zone logic onto CardState (#7434)

* [Refactor] Retype card containers and zone logic onto CardState

* [Game] Restore attach bookkeeping via CardItem and repaint on state change

* [Refactor] Give custom zones a hidden graphics owner

Custom PileZoneLogic zones had no CardZone, so moving a card into one
left the CardItem drawn at its old spot. Create a hidden PileZone in
onCustomZoneAdded so cards are reparented and hidden.

---------

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
BruebachL 2026-10-31 18:44:14 +01:00 • committed by GitHub
parent 5d0d466930
commit 65e9f1b370
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
33 changed files with 232 additions and 159 deletions

View file

@ -37,17 +37,26 @@ void CardZone::invalidateItems()
const QList<CardItem *> &CardZone::cardItems() const
{
if (itemsDirty) {
items = getLogic()->getCards();
items.clear();
for (auto *cardState : getLogic()->getCards()) {
if (auto *cardItem = qobject_cast<CardItem *>(cardState->parent())) {
items.append(cardItem);
}
}
itemsDirty = false;
}
return items;
}
void CardZone::onCardAdded(CardItem *addedCard)
void CardZone::onCardAdded(CardState *addedCard)
{
addedCard->setParentItem(this);
addedCard->setVisible(true);
addedCard->update();
auto *addedItem = addedCard == nullptr ? nullptr : qobject_cast<CardItem *>(addedCard->parent());
if (addedItem == nullptr) {
return;
}
addedItem->setParentItem(this);
addedItem->setVisible(true);
addedItem->update();
}
void CardZone::retranslateUi()

View file

@ -16,6 +16,7 @@
class CardDragItem;
class CardItem;
class CardState;
class CardZoneLogic;
class QAction;
class QGraphicsItem;
@ -23,6 +24,8 @@ class QMenu;
class QPoint;
template <typename T> class QList;
class CardItem;
inline Q_LOGGING_CATEGORY(CardZoneLog, "card_zone");
/**
@ -55,7 +58,7 @@ public slots:
* Virtual so subclasses (e.g. SelectZone) can override parenting behavior — the Qt signal
* connection in CardZone's constructor dispatches through the vtable.
*/
virtual void onCardAdded(CardItem *addedCard);
virtual void onCardAdded(CardState *addedCard);
public:
enum

View file

@ -87,12 +87,13 @@ void PileZone::handleDropEvent(const QList<CardDragItem *> &dragItems, CardZoneL
getLogic()->getPlayer()->getPlayerActions()->moveCards(startZone, getLogic(), 0, 0, cards);
}
void PileZone::onCardAdded(CardItem *addedCard)
void PileZone::onCardAdded(CardState *addedCard)
{
CardZone::onCardAdded(addedCard);
if (addedCard) {
addedCard->setPos(0, 0);
addedCard->setVisible(false);
auto *addedItem = addedCard == nullptr ? nullptr : qobject_cast<CardItem *>(addedCard->parent());
if (addedItem) {
addedItem->setPos(0, 0);
addedItem->setVisible(false);
}
}

View file

@ -33,7 +33,7 @@ public:
[[nodiscard]] QPainterPath shape() const override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
void reorganizeCards() override;
void onCardAdded(CardItem *addedCard) override;
void onCardAdded(CardState *addedCard) override;
void
handleDropEvent(const QList<CardDragItem *> &dragItems, CardZoneLogic *startZone, const QPoint &dropPoint) override;

View file

@ -172,12 +172,13 @@ SelectZone::~SelectZone()
}
}
void SelectZone::onCardAdded(CardItem *addedCard)
void SelectZone::onCardAdded(CardState *addedCard)
{
if (cardClipContainer && addedCard) {
addedCard->setParentItem(cardClipContainer);
addedCard->setVisible(true);
addedCard->update();
auto *addedItem = addedCard == nullptr ? nullptr : qobject_cast<CardItem *>(addedCard->parent());
if (cardClipContainer && addedItem) {
addedItem->setParentItem(cardClipContainer);
addedItem->setVisible(true);
addedItem->update();
} else {
CardZone::onCardAdded(addedCard);
}

View file

@ -35,7 +35,7 @@ public:
SelectZone(CardZoneLogic *logic, QGraphicsItem *parent = nullptr);
~SelectZone() override;
void onCardAdded(CardItem *addedCard) override;
void onCardAdded(CardState *addedCard) override;
/**
* @brief Temporarily reparents a card from the clip container to this zone so hover scaling is visible beyond clip

View file

@ -95,7 +95,7 @@ void ZoneViewZone::initializeCards(const QList<const ServerInfo_Card *> &cardLis
auto copy = new CardItem(getLogic()->getPlayer(), this, cardRef, card->id());
copy->setFaceDown(card->face_down());
getLogic()->addCard(copy, false, i);
getLogic()->addCard(copy->getState(), false, i);
}
reorganizeCards();
} else if (!qobject_cast<ZoneViewZoneLogic *>(getLogic())->getOriginalZone()->contentsKnown()) {
@ -107,11 +107,11 @@ void ZoneViewZone::initializeCards(const QList<const ServerInfo_Card *> &cardLis
const CardList &c = qobject_cast<ZoneViewZoneLogic *>(getLogic())->getOriginalZone()->getCards();
int number = numberCards == -1 ? c.size() : (numberCards < c.size() ? numberCards : c.size());
for (int i = 0; i < number; i++) {
CardItem *card = c.at(i);
CardState *card = c.at(i);
auto copy = new CardItem(getLogic()->getPlayer(), this, card->getCardRef(), card->getId());
copy->setFaceDown(card->getFaceDown());
getLogic()->addCard(copy, false, i);
getLogic()->addCard(copy->getState(), false, i);
}
reorganizeCards();
}
@ -127,7 +127,7 @@ void ZoneViewZone::zoneDumpReceived(const Response &r)
auto cardProviderId = QString::fromStdString(cardInfo.provider_id());
auto card = new CardItem(getLogic()->getPlayer(), this, {cardName, cardProviderId}, cardInfo.id(), getLogic());
card->setFaceDown(cardInfo.face_down());
getLogic()->rawInsertCard(card, i);
getLogic()->rawInsertCard(card->getState(), i);
}
qobject_cast<ZoneViewZoneLogic *>(getLogic())->updateCardIds(ZoneViewZoneLogic::INITIALIZE);
@ -145,7 +145,7 @@ void ZoneViewZone::reorganizeCards()
for (auto card : cardItems()) {
if (filterString.check(card->getCard().getCardPtr())) {
card->show();
cardsToDisplay.append(card);
cardsToDisplay.append(card->getState());
} else {
card->hide();
}
@ -213,7 +213,8 @@ ZoneViewZone::GridSize ZoneViewZone::positionCardsForDisplay(CardList &cards, Ca
const auto extractor = CardList::getExtractorFor(pileOption);
for (int i = 0; i < cardCount; i++) {
CardItem *c = cards.at(i);
CardState *c = cards.at(i);
CardItem *cItem = qobject_cast<CardItem *>(c->parent());
QString columnProp = extractor(c);
if (i) { // if not the first card
@ -228,8 +229,8 @@ ZoneViewZone::GridSize ZoneViewZone::positionCardsForDisplay(CardList &cards, Ca
lastColumnProp = columnProp;
qreal x = col * CardDimensions::WIDTH_F;
qreal y = row * CardDimensions::HEIGHT_F / 3;
c->setPos(HORIZONTAL_PADDING + x, VERTICAL_PADDING + y);
c->setRealZValue(i);
cItem->setPos(HORIZONTAL_PADDING + x, VERTICAL_PADDING + y);
cItem->setRealZValue(i);
longestRow = qMax(row, longestRow);
}
@ -254,11 +255,11 @@ ZoneViewZone::GridSize ZoneViewZone::positionCardsForDisplay(CardList &cards, Ca
qCDebug(ViewZoneLog) << "reorganizeCards: rows=" << rows << "cols=" << cols;
for (int i = 0; i < cardCount; i++) {
CardItem *c = cards.at(i);
auto *cItem = qobject_cast<CardItem *>(cards.at(i)->parent());
qreal x = (i / rows) * CardDimensions::WIDTH_F;
qreal y = (i % rows) * CardDimensions::HEIGHT_F / 3;
c->setPos(HORIZONTAL_PADDING + x, VERTICAL_PADDING + y);
c->setRealZValue(i);
cItem->setPos(HORIZONTAL_PADDING + x, VERTICAL_PADDING + y);
cItem->setRealZValue(i);
}
return GridSize{rows, qMax(cols, 1)};