From 68d384c03a81264786e85394ef1c3a56beebf0df Mon Sep 17 00:00:00 2001 From: Simon Adamov <kode@mailbox.org> Date: Sat, 14 Dec 2024 06:45:32 +0100 Subject: [PATCH] remove unnecessary code; simplify opponent hand display --- src/client/gui/cardwidget.cpp | 16 ++++++-- src/client/gui/cardwidget.h | 2 + src/client/gui/gamewidget.cpp | 77 +++++++---------------------------- 3 files changed, 29 insertions(+), 66 deletions(-) diff --git a/src/client/gui/cardwidget.cpp b/src/client/gui/cardwidget.cpp index 38860e2..e340369 100644 --- a/src/client/gui/cardwidget.cpp +++ b/src/client/gui/cardwidget.cpp @@ -46,14 +46,16 @@ void CardWidget::paintEvent(QPaintEvent *event) QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); - // Draw card background if (m_faceDown) { - painter.setBrush(QColor(0, 0, 128)); // Dark blue for back + painter.setBrush(QColor(0, 0, 128)); // Dark blue background painter.drawRect(rect()); - // Optionally, add a pattern or text indicating the card is face down + painter.setPen(Qt::white); - painter.drawText(rect(), Qt::AlignCenter, "?"); + painter.setFont(QFont("Arial", 40, QFont::Bold)); + + // Draw large card count in center + painter.drawText(rect(), Qt::AlignCenter, QString::number(m_cardCount)); } else { @@ -150,4 +152,10 @@ void CardWidget::leaveEvent(QEvent *event) shadow->setOffset(3, 3); shadow->setColor(QColor(0, 0, 0, 80)); } +} + +void CardWidget::setCardCount(int count) +{ + m_cardCount = count; + update(); } \ No newline at end of file diff --git a/src/client/gui/cardwidget.h b/src/client/gui/cardwidget.h index 3095793..c2f670f 100644 --- a/src/client/gui/cardwidget.h +++ b/src/client/gui/cardwidget.h @@ -15,6 +15,7 @@ public: int getValue() const; Suit getSuit() const; void setFaceDown(bool faceDown); + void setCardCount(int count); signals: void clicked(); @@ -29,6 +30,7 @@ private: int m_value; Suit m_suit; bool m_faceDown; + int m_cardCount; }; #endif // CARDWIDGET_H \ No newline at end of file diff --git a/src/client/gui/gamewidget.cpp b/src/client/gui/gamewidget.cpp index afacf27..2ac842d 100644 --- a/src/client/gui/gamewidget.cpp +++ b/src/client/gui/gamewidget.cpp @@ -129,7 +129,7 @@ void GameWidget::createPlayerHand() { auto cardWidget = std::make_unique<CardWidget>(card->get_value(), card->get_suit()); - // Use Qt5 connection syntax and check if connection succeeds + // Connect the card widget's clicked signal to the onPlayCard slot bool connected = connect(cardWidget.get(), &CardWidget::clicked, this, &GameWidget::onPlayCard, Qt::AutoConnection); @@ -314,32 +314,6 @@ void GameWidget::updateBetsScoresWonTricks() } } -void GameWidget::clearOtherPlayersHandWidgets() -{ - for (auto &cardWidget : otherPlayersHandWidgets) - { - if (cardWidget) - { - // Assuming cards are added to a specific layout, remove them accordingly - // For example: - // someLayout->removeWidget(cardWidget.get()); - cardWidget->deleteLater(); - } - } - otherPlayersHandWidgets.clear(); -} - -void GameWidget::clearAllHands() -{ - clearPlayerHandWidgets(); - clearOtherPlayersHandWidgets(); -} - -void GameWidget::dealCards() -{ - // Implement the logic to deal cards to players -} - void GameWidget::onYourTurn() { enableInteraction(true); @@ -479,57 +453,36 @@ void GameWidget::updateCenterPile() centerPileCardsLayout->addWidget(cardWidget); } } + void GameWidget::createOpponentHands() { clearOpponentHandWidgets(); - const int CARD_OVERLAP = 20; // Pixels between each card const auto &players = GameController::instance()->getGameState()->get_players(); + int index = 0; for (const auto &player : players) { if (player.get() != GameController::instance()->getLocalPlayer()) { - // Create face-down cards for opponent int handSize = player->get_hand().size(); - std::vector<std::unique_ptr<CardWidget>> opponentHand; - - // Create a horizontal spacer widget to properly align cards - QWidget *handContainer = new QWidget(); - QHBoxLayout *handLayout = new QHBoxLayout(handContainer); - handLayout->setSpacing(0); // Remove default spacing - handLayout->setContentsMargins(0, 0, 0, 0); - - // Calculate total width needed - int totalWidth = CARD_OVERLAP * (handSize - 1) + 100; // 100 is card width - handContainer->setFixedWidth(totalWidth); - - for (int i = 0; i < handSize; ++i) - { - auto cardWidget = std::make_unique<CardWidget>(0, Suit::Jester); - cardWidget->setFaceDown(true); - cardWidget->setFixedWidth(100); - - // Set negative margin to create overlap effect - handLayout->addWidget(cardWidget.get()); - cardWidget->setContentsMargins(i == 0 ? 0 : -CARD_OVERLAP, 0, 0, 0); - - opponentHand.push_back(std::move(cardWidget)); - } + auto cardWidget = std::make_unique<CardWidget>(0, Suit::Jester, nullptr, true); + cardWidget->setCardCount(handSize); // Set the number to display - // Add the container to appropriate layout - if (leftOpponentHandWidgets.empty()) + // Add the cardWidget to the appropriate layout + if (index == 0) { - qDebug() << "Adding cards to left opponent layout."; - leftOpponentCardsLayout->addWidget(handContainer); - leftOpponentHandWidgets = std::move(opponentHand); + leftOpponentCardsLayout->addWidget(cardWidget.get()); + leftOpponentHandWidgets.clear(); + leftOpponentHandWidgets.push_back(std::move(cardWidget)); } - else if (rightOpponentHandWidgets.empty()) + else if (index == 1) { - qDebug() << "Adding cards to right opponent layout."; - rightOpponentCardsLayout->addWidget(handContainer); - rightOpponentHandWidgets = std::move(opponentHand); + rightOpponentCardsLayout->addWidget(cardWidget.get()); + rightOpponentHandWidgets.clear(); + rightOpponentHandWidgets.push_back(std::move(cardWidget)); } + index++; } } } -- GitLab