/** * MouseEvent for clicking on a humanCard or on the Stack * @brief Playground::mousePressEvent * @param event */ void Playground::mousePressEvent(QGraphicsSceneMouseEvent* event) { if (event->button() == Qt::LeftButton) { QGraphicsItem* item = itemAt(event->buttonDownScenePos(event->button()), QTransform()); if (item != NULL) { //Clicked on Stack if (item == stack.createImg()) { players.value(PlayerItem::direction::HUMAN)->unsetPlayableCards(); players.value(PlayerItem::direction::HUMAN)->setUnactive(); emit drawCard(); } //Clicked on Human Card PlayerItem* human = players.value(PlayerItem::direction::HUMAN); for (int j = 0; j < human->getCards()->size(); ++j) { CardItem* c = human->getCards()->at(j); if (c->createImg() == item && c->getPlayable()) { history.write("You play a Card", c->getCard().getSuit(), c->getCard().getValue()); Card::cardSuit chosenColor = Card::NONE; if (c->getCard().getValue() == wishSuitCard) { chosenColor = chooseColor(); } soundMgr.playCard(); updateDepotCard(*c, depot); soundMgr.drawCard(); human->removeCard(c->getCard()); human->unsetPlayableCards(); human->setUnactive(); emit playCard(depot.getCard(), chosenColor); } } } } }
void CardContainer::fillCards(const QList<int> &card_ids, const QList<int> &disabled_ids) { QList<CardItem *> card_items; if (card_ids.isEmpty() && items.isEmpty()) return; else if (card_ids.isEmpty() && !items.isEmpty()) { card_items = items; items.clear(); } else if (!items.isEmpty()) { retained_stack.push(retained()); items_stack.push(items); foreach (CardItem *item, items) item->hide(); items.clear(); } close_button->hide(); if (card_items.isEmpty()) card_items = _createCards(card_ids); int card_width = G_COMMON_LAYOUT.m_cardNormalWidth; QPointF pos1(30 + card_width / 2, 40 + G_COMMON_LAYOUT.m_cardNormalHeight / 2); QPointF pos2(30 + card_width / 2, 184 + G_COMMON_LAYOUT.m_cardNormalHeight / 2); int skip = 102; qreal whole_width = skip * 4; items.append(card_items); int n = items.length(); for (int i = 0; i < n; i++) { QPointF pos; if (n <= 10) { if (i < 5) { pos = pos1; pos.setX(pos.x() + i * skip); } else { pos = pos2; pos.setX(pos.x() + (i - 5) * skip); } } else { int half = (n + 1) / 2; qreal real_skip = whole_width / (half - 1); if (i < half) { pos = pos1; pos.setX(pos.x() + i * real_skip); } else { pos = pos2; pos.setX(pos.x() + (i - half) * real_skip); } } CardItem *item = items[i]; item->setPos(pos); item->setHomePos(pos); item->setOpacity(1.0); item->setHomeOpacity(1.0); item->setFlag(QGraphicsItem::ItemIsFocusable); if (disabled_ids.contains(item->getCard()->getEffectiveId())) item->setEnabled(false); item->show(); } }
void CardContainer::fillCards(const QList<int> &card_ids, const QList<int> &disabled_ids) { QList<CardItem *> card_items; if (card_ids.isEmpty() && items.isEmpty()) return; else if (card_ids.isEmpty() && !items.isEmpty()) { card_items = items; items.clear(); } else if (!items.isEmpty()) { retained_stack.push(retained()); items_stack.push(items); foreach (CardItem *item, items) item->hide(); items.clear(); } close_button->hide(); if (card_items.isEmpty()) card_items = _createCards(card_ids); items.append(card_items); _repaint(); int n = items.length(); const int blank = 3; int card_width = G_COMMON_LAYOUT.m_cardNormalWidth; int card_height = G_COMMON_LAYOUT.m_cardNormalHeight; bool one_row = true; int width = (card_width + blank) * items.length() - blank + 50; if (width * 1.5 > RoomSceneInstance->sceneRect().width()) { width = (card_width + blank) * ((items.length() + 1) / 2) - blank + 50; one_row = false; } int first_row = one_row ? items.length() : (items.length() + 1) / 2; for (int i = 0; i < n; i++) { QPointF pos; if (i < first_row) { pos.setX(25 + (card_width + blank) * i); pos.setY(45); } else { if (n % 2 == 1) pos.setX(25 + card_width / 2 + blank / 2 + (card_width + blank) * (i - first_row)); else pos.setX(25 + (card_width + blank) * (i - first_row)); pos.setY(45 + card_height + blank); } CardItem *item = items[i]; item->resetTransform(); item->setPos(pos); item->setHomePos(pos); item->setOpacity(1.0); item->setHomeOpacity(1.0); item->setFlag(QGraphicsItem::ItemIsFocusable); if (disabled_ids.contains(item->getCard()->getEffectiveId())) item->setEnabled(false); item->show(); } }
QString GameHistory::cardToString(CardItem item){ QString hand; switch (item.getCard().getValue()) { case Card::cardValue::SEVEN: hand += "7 "; break; case Card::cardValue::EIGHT: hand += "8 "; break; case Card::cardValue::NINE: hand += "9 "; break; case Card::cardValue::TEN: hand += "10 "; break; case Card::cardValue::JACK: hand += "Jack "; break; case Card::cardValue::QUEEN: hand += "Queen "; break; case Card::cardValue::KING: hand += "King "; break; case Card::cardValue::ACE: hand += "Ace "; break; } switch (item.getCard().getSuit()) { case Card::cardSuit::HEARTS: hand += "Hearts"; break; case Card::cardSuit::SPADES: hand += "Spades"; break; case Card::cardSuit::DIAMONDS: hand += "Diamonds"; break; case Card::cardSuit::CLUBS: hand += "Clubs"; break; default: break; } return hand; }
/** * updates the Depot Card * @brief Playground::updateDepotCard * @param fromCard * @param toCard * @param withAnimation with Animation or without */ void Playground::updateDepotCard(CardItem& fromCard, CardItem& toCard, bool withAnimation) { qreal x = toCard.getX(); qreal y = toCard.getY(); if (withAnimation) { QEventLoop pause; prepareNewAnimation(pause); addPositionAnimation(*fromCard.createImg(), *toCard.createImg()); startAnimation(); pause.exec(QEventLoop::AllEvents); } removeItem(toCard.createImg()); toCard = CardItem(fromCard.getCard()); toCard.setPos(x, y); toCard.createImg()->setZValue(zValue); zValue++; addItem(toCard.createImg()); update(sceneRect()); }
void CardContainer::fillCards(const QList<int> &card_ids, const QList<int> &disabled_ids) { if (card_ids == ids) return; QList<CardItem *> card_items; if (card_ids.isEmpty() && items.isEmpty()) return; else if (card_ids.isEmpty() && !items.isEmpty()) { card_items = items; items.clear(); } else if (!items.isEmpty()) { retained_stack.push(retained()); items_stack.push(items); foreach(CardItem *item, items) item->hide(); items.clear(); } scene_width = RoomSceneInstance->sceneRect().width(); confirm_button->hide(); if (card_items.isEmpty()) card_items = _createCards(card_ids); items.append(card_items); itemCount = items.length(); prepareGeometryChange(); int card_width = G_COMMON_LAYOUT.m_cardNormalWidth; int card_height = G_COMMON_LAYOUT.m_cardNormalHeight; bool one_row = true; int width = (card_width + cardInterval) * itemCount - cardInterval + 50; if (width * 1.5 > scene_width) { width = (card_width + cardInterval) * ((itemCount + 1) / 2) - cardInterval + 50; one_row = false; } int first_row = one_row ? itemCount : (itemCount + 1) / 2; for (int i = 0; i < itemCount; i++) { QPointF pos; if (i < first_row) { pos.setX(25 + (card_width + cardInterval) * i); pos.setY(45); } else { if (itemCount % 2 == 1) pos.setX(25 + card_width / 2 + cardInterval / 2 + (card_width + cardInterval) * (i - first_row)); else pos.setX(25 + (card_width + cardInterval) * (i - first_row)); pos.setY(45 + card_height + cardInterval); } CardItem *item = items[i]; item->resetTransform(); item->setPos(pos); item->setHomePos(pos); item->setOpacity(1.0); item->setHomeOpacity(1.0); item->setFlag(QGraphicsItem::ItemIsFocusable); if (disabled_ids.contains(item->getCard()->getEffectiveId())) item->setEnabled(false); item->setOuterGlowEffectEnabled(true); item->show(); ids << item->getId(); } confirm_button->setPos(boundingRect().center().x() - confirm_button->boundingRect().width() / 2, boundingRect().height() - 60); }