From 815baa1ffde15c6ddf5c8d7f629847cc257f32a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Mon, 15 Jun 2026 00:50:51 +0200 Subject: [PATCH] Consider removed cards. Took 14 minutes --- cockatrice/src/game/board/card_state.cpp | 11 +++++++++++ cockatrice/src/game/board/card_state.h | 3 +++ cockatrice/src/game/zones/card_zone_logic.cpp | 4 ++++ cockatrice/src/game/zones/card_zone_logic.h | 1 + cockatrice/src/game_graphics/board/card_item.cpp | 7 ------- .../src/game_graphics/player/menu/player_menu.cpp | 4 ++++ cockatrice/src/game_graphics/zones/card_zone.cpp | 15 +++++++++++++++ cockatrice/src/game_graphics/zones/card_zone.h | 1 + 8 files changed, 39 insertions(+), 7 deletions(-) diff --git a/cockatrice/src/game/board/card_state.cpp b/cockatrice/src/game/board/card_state.cpp index 01bc39114..eff651a80 100644 --- a/cockatrice/src/game/board/card_state.cpp +++ b/cockatrice/src/game/board/card_state.cpp @@ -15,6 +15,17 @@ void CardState::prepareDelete() attachedCards.first()->setZone(nullptr); // so that it won't try to call reorganizeCards() attachedCards.first()->setAttachedTo(nullptr); } + + if (getAttachedTo() != nullptr) { + getAttachedTo()->removeAttachedCard(this); + setAttachedTo(nullptr); + } +} + +void CardState::deleteLater() +{ + prepareDelete(); + QObject::deleteLater(); } void CardState::processCardInfo(const ServerInfo_Card &_info) diff --git a/cockatrice/src/game/board/card_state.h b/cockatrice/src/game/board/card_state.h index f83e5bda4..1ab6fd181 100644 --- a/cockatrice/src/game/board/card_state.h +++ b/cockatrice/src/game/board/card_state.h @@ -41,6 +41,9 @@ signals: void zoneChanged(CardState *changedCard, CardZoneLogic *newZone); void visibleChanged(bool visible); +public slots: + void deleteLater(); + public: explicit CardState(PlayerLogic *_owner, const CardRef &cardRef = {}, int _id = -1, CardZoneLogic *_zone = nullptr); diff --git a/cockatrice/src/game/zones/card_zone_logic.cpp b/cockatrice/src/game/zones/card_zone_logic.cpp index ccd1d0d83..6a24f842f 100644 --- a/cockatrice/src/game/zones/card_zone_logic.cpp +++ b/cockatrice/src/game/zones/card_zone_logic.cpp @@ -90,6 +90,8 @@ CardState *CardZoneLogic::takeCard(int position, int cardId, bool toNewZone) c->setId(cardId); + emit cardRemoved(c, c->getGridPos().x(), c->getGridPos().y()); + emit reorganizeCards(); emit cardCountChanged(); return c; @@ -120,6 +122,8 @@ void CardZoneLogic::removeCard(CardState *card) cards.removeOne(card); + emit cardRemoved(card, card->getGridPos().x(), card->getGridPos().y()); + emit reorganizeCards(); emit cardCountChanged(); player->deleteCard(card); diff --git a/cockatrice/src/game/zones/card_zone_logic.h b/cockatrice/src/game/zones/card_zone_logic.h index 5ca57917e..ca378a3e6 100644 --- a/cockatrice/src/game/zones/card_zone_logic.h +++ b/cockatrice/src/game/zones/card_zone_logic.h @@ -29,6 +29,7 @@ class CardZoneLogic : public QObject signals: void cardAdded(CardState *addedCard, int x, int y); + void cardRemoved(CardState *removedCard, int x, int y); void cardCountChanged(); void reorganizeCards(); void updateGraphics(); diff --git a/cockatrice/src/game_graphics/board/card_item.cpp b/cockatrice/src/game_graphics/board/card_item.cpp index cfba63f75..969d9daad 100644 --- a/cockatrice/src/game_graphics/board/card_item.cpp +++ b/cockatrice/src/game_graphics/board/card_item.cpp @@ -41,13 +41,6 @@ void CardItem::prepareDelete() } owner = nullptr; } - - state->prepareDelete(); - - if (state->getAttachedTo() != nullptr) { - state->getAttachedTo()->removeAttachedCard(getState()); - state->setAttachedTo(nullptr); - } } void CardItem::deleteLater() diff --git a/cockatrice/src/game_graphics/player/menu/player_menu.cpp b/cockatrice/src/game_graphics/player/menu/player_menu.cpp index 1b04456b1..8c8891840 100644 --- a/cockatrice/src/game_graphics/player/menu/player_menu.cpp +++ b/cockatrice/src/game_graphics/player/menu/player_menu.cpp @@ -76,6 +76,10 @@ QMenu *PlayerMenu::updateCardMenu(const CardState *card) return nullptr; } + if (!player->getLogic()->getGame()->getActiveCard()) { + return nullptr; + } + // 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() && diff --git a/cockatrice/src/game_graphics/zones/card_zone.cpp b/cockatrice/src/game_graphics/zones/card_zone.cpp index 2d5108418..4dc299944 100644 --- a/cockatrice/src/game_graphics/zones/card_zone.cpp +++ b/cockatrice/src/game_graphics/zones/card_zone.cpp @@ -11,6 +11,7 @@ CardZone::CardZone(CardZoneLogic *_logic, QGraphicsItem *parent) { connect(logic, &CardZoneLogic::retranslateUi, this, &CardZone::retranslateUi); connect(logic, &CardZoneLogic::cardAdded, this, &CardZone::onCardAdded); + connect(logic, &CardZoneLogic::cardRemoved, this, &CardZone::onCardRemoved); 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); @@ -27,6 +28,20 @@ void CardZone::onCardAdded(CardState *toAdd, int /*x*/, int /*y*/) emit cardItemAdded(addedCard); } +void CardZone::onCardRemoved(CardState *toRemove, int /*x*/, int /*y*/) +{ + CardItem *removedCard = getCardItemForId(toRemove->getId()); + if (!removedCard) { + return; + } + if (cards.contains(removedCard)) { + cards.remove(cards.indexOf(removedCard)); + } + removedCard->setVisible(false); + removedCard->setParentItem(nullptr); + removedCard->deleteLater(); +} + void CardZone::retranslateUi() { for (int i = 0; i < cards.size(); ++i) { diff --git a/cockatrice/src/game_graphics/zones/card_zone.h b/cockatrice/src/game_graphics/zones/card_zone.h index 4399d197e..10e6c2c03 100644 --- a/cockatrice/src/game_graphics/zones/card_zone.h +++ b/cockatrice/src/game_graphics/zones/card_zone.h @@ -51,6 +51,7 @@ public slots: * connection in CardZone's constructor dispatches through the vtable. */ virtual void onCardAdded(CardState *addedCard, int x, int y); + void onCardRemoved(CardState *toAdd, int x, int y); signals: void cardItemAdded(CardItem *added);