[Refactor] Move card item creation out of game logic

This commit is contained in:
Lukas Brübach 2026-10-07 10:08:54 +02:00 • committed by GitHub
parent e0d2f158e9
commit 16caae5e5a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 121 additions and 82 deletions

View file

@ -89,14 +89,18 @@ void CardItem::prepareDelete()
owner = nullptr;
}
const QList<CardItem *> &attachedCards = state->getAttachedCards();
while (!attachedCards.isEmpty()) {
attachedCards.first()->setZone(nullptr); // so that it won't try to call reorganizeCards()
attachedCards.first()->setAttachedTo(nullptr);
const QList<CardState *> &attachedStates = state->getAttachedCards();
while (!attachedStates.isEmpty()) {
auto *attachedItem = qobject_cast<CardItem *>(attachedStates.first()->parent());
if (attachedItem == nullptr) {
break;
}
attachedItem->setZone(nullptr); // so that it won't try to call reorganizeCards()
attachedItem->setAttachedTo(nullptr);
}
if (state->getAttachedTo() != nullptr) {
state->getAttachedTo()->removeAttachedCard(this);
state->getAttachedTo()->removeAttachedCard(state);
state->setAttachedTo(nullptr);
}
}
@ -226,7 +230,7 @@ void CardItem::setPT(const QString &_pt)
void CardItem::setAttachedTo(CardItem *_attachedTo)
{
if (state->getAttachedTo() != nullptr) {
state->getAttachedTo()->removeAttachedCard(this);
state->getAttachedTo()->removeAttachedCard(state);
}
state->setGridPoint(QPoint(-1, state->getGridPoint().y()));
@ -238,7 +242,7 @@ void CardItem::setAttachedTo(CardItem *_attachedTo)
deleteLater();
} else {
emit state->getAttachedTo()->getZone()->cardAdded(state);
state->getAttachedTo()->addAttachedCard(this);
state->getAttachedTo()->addAttachedCard(state);
if (state->getZone() != state->getAttachedTo()->getZone()) {
state->getAttachedTo()->getZone()->reorganizeCards();
}

View file

@ -120,15 +120,15 @@ public:
return state->getAttachedTo();
}
void setAttachedTo(CardItem *_attachedTo);
void addAttachedCard(CardItem *card)
void addAttachedCard(CardState *card)
{
state->addAttachedCard(card);
}
void removeAttachedCard(CardItem *card)
void removeAttachedCard(CardState *card)
{
state->removeAttachedCard(card);
}
[[nodiscard]] const QList<CardItem *> &getAttachedCards() const
[[nodiscard]] const QList<CardState *> &getAttachedCards() const
{
return state->getAttachedCards();
}

View file

@ -205,7 +205,7 @@ void GameScene::onCardSelectionChanged(AbstractCardItem *abstractCard, bool sele
auto *owner = card->getOwner();
if (selected) {
owner->requestCardMenuUpdate(card);
owner->requestCardMenuUpdate(card->getState());
return;
}
@ -231,7 +231,7 @@ void GameScene::onCardRightClicked(AbstractCardItem *abstractCard, QPoint screen
setActiveCard(card);
if (auto *menu = view->getPlayerMenu()->updateCardMenu(card)) {
if (auto *menu = view->getPlayerMenu()->updateCardMenu(card->getState())) {
menu->popup(screenPos);
}
}
@ -650,11 +650,15 @@ void GameScene::requestArrowDeletion(int playerId, int arrowId)
}
}
void GameScene::onCardZoneChanged(CardItem *card, bool sameZone)
void GameScene::onCardZoneChanged(CardState *card, bool sameZone)
{
auto *item = qobject_cast<CardItem *>(card->parent());
if (!item) {
return;
}
QList<ArrowItem *> toDelete;
for (auto *arrow : arrowRegistry.all()) {
if (arrow->getStartItem() == card || arrow->getTargetItem() == card) {
if (arrow->getStartItem() == item || arrow->getTargetItem() == item) {
if (sameZone) {
arrow->updatePath();
} else {

View file

@ -283,7 +283,7 @@ public slots:
/// Queues up arrow deletion but doesn't directly modify the scene
void requestArrowDeletion(int playerId, int arrowId);
void onCardZoneChanged(CardItem *card, bool sameZone);
void onCardZoneChanged(CardState *card, bool sameZone);
/**
* @brief Refreshes the view of a card that the server just moved.
*

View file

@ -87,22 +87,24 @@ void PlayerMenu::setMenusForGraphicItems()
}
}
QMenu *PlayerMenu::updateCardMenu(CardItem *card)
QMenu *PlayerMenu::updateCardMenu(CardState *card)
{
if (!card) {
emit cardMenuUpdated(nullptr);
return nullptr;
}
auto *cardItem = qobject_cast<CardItem *>(card->parent());
// If is spectator (as spectators don't need card menus), return
// only update the menu if the card is actually selected
if ((player->getLogic()->getGame()->getPlayerManager()->isSpectator() &&
!player->getLogic()->getGame()->getPlayerManager()->isJudge()) ||
player->getGameScene()->getActiveCard() != card) {
cardItem == nullptr || player->getGameScene()->getActiveCard() != cardItem) {
return nullptr;
}
CardMenu *menu = new CardMenu(player, card, shortcutsActive);
CardMenu *menu = new CardMenu(player, cardItem, shortcutsActive);
connect(menu, &CardMenu::cardInfoRequested, this, &PlayerMenu::cardInfoRequested);
emit cardMenuUpdated(menu);

View file

@ -14,7 +14,7 @@
#include <qtmetamacros.h>
#include <utility>
class CardItem;
class CardState;
class CardMenu;
class PlayerGraphicsItem;
class AbstractPlayerComponent;
@ -43,7 +43,7 @@ signals:
public slots:
void setMenusForGraphicItems();
QMenu *updateCardMenu(CardItem *card);
QMenu *updateCardMenu(CardState *card);
private slots:
void refreshShortcuts();

View file

@ -24,6 +24,7 @@ CardZone::CardZone(CardZoneLogic *_logic, QGraphicsItem *parent)
connect(logic, &CardZoneLogic::cardCountChanged, this, &CardZone::invalidateItems);
connect(logic, &CardZoneLogic::reorganizeCards, this, &CardZone::invalidateItems);
connect(logic, &CardZoneLogic::cardAdded, this, &CardZone::onCardAdded);
connect(logic, &CardZoneLogic::requestCreateCard, this, &CardZone::onCreateCardRequested);
connect(logic, &CardZoneLogic::setGraphicsVisibility, this, [this](bool v) { this->setVisible(v); });
connect(logic, &CardZoneLogic::updateGraphics, this, [this]() { update(); });
connect(logic, &CardZoneLogic::reorganizeCards, this, &CardZone::reorganizeCards);
@ -59,6 +60,14 @@ void CardZone::onCardAdded(CardState *addedCard)
addedItem->update();
}
void CardZone::onCreateCardRequested(const ServerInfo_Card &cardInfo, bool reorganize)
{
CardRef cardRef = {QString::fromStdString(cardInfo.name()), QString::fromStdString(cardInfo.provider_id())};
auto *card = new CardItem(getLogic()->getPlayer(), nullptr, cardRef, cardInfo.id());
card->processCardInfo(cardInfo);
getLogic()->addCard(card->getState(), reorganize, cardInfo.x(), cardInfo.y());
}
void CardZone::retranslateUi()
{
for (auto *card : cardItems()) {

View file

@ -58,6 +58,8 @@ public slots:
* connection in CardZone's constructor dispatches through the vtable.
*/
virtual void onCardAdded(CardState *addedCard);
/** @brief Builds the card item for @p cardInfo and hands it to this zone's logic. */
void onCreateCardRequested(const ServerInfo_Card &cardInfo, bool reorganize);
public:
enum

View file

@ -248,11 +248,14 @@ void TableZone::reorganizeCards()
cardItems()[i]->setPos(actualX, actualY);
cardItems()[i]->setRealZValue(ZValues::tableCardZValue(actualX, actualY));
QListIterator<CardItem *> attachedCardIterator(cardItems()[i]->getAttachedCards());
QListIterator<CardState *> attachedCardIterator(cardItems()[i]->getAttachedCards());
int j = 0;
while (attachedCardIterator.hasNext()) {
++j;
CardItem *attachedCard = attachedCardIterator.next();
CardItem *attachedCard = qobject_cast<CardItem *>(attachedCardIterator.next()->parent());
if (attachedCard == nullptr) {
continue;
}
qreal childX = actualX - j * STACKED_CARD_OFFSET_X;
qreal childY = y + 5;
attachedCard->setPos(childX, childY);