#include "player.h" #include "client.h" #include "cardzone.h" #include "counter.h" #include "arrowitem.h" #include "zoneviewzone.h" #include "zoneviewwidget.h" #include "pilezone.h" #include "tablezone.h" #include "handzone.h" #include "cardlist.h" #include "tab_game.h" #include "protocol_items.h" #include #include #include Player::Player(const QString &_name, int _id, bool _local, Client *_client, TabGame *_parent) : QObject(_parent), defaultNumberTopCards(3), name(_name), id(_id), active(false), local(_local), client(_client) { QSettings settings; QString bgPath = settings.value("zonebg/playerarea").toString(); if (!bgPath.isEmpty()) bgPixmap.load(bgPath); setCacheMode(DeviceCoordinateCache); QPointF base = QPointF(counterAreaWidth, 50); PileZone *deck = new PileZone(this, "deck", true, false, this); deck->setPos(base); qreal h = deck->boundingRect().height() + 20; PileZone *grave = new PileZone(this, "grave", false, true, this); grave->setPos(base + QPointF(0, h)); PileZone *rfg = new PileZone(this, "rfg", false, true, this); rfg->setPos(base + QPointF(0, 2 * h)); PileZone *sb = new PileZone(this, "sb", false, true, this); sb->setVisible(false); table = new TableZone(this, this); connect(table, SIGNAL(sizeChanged()), this, SLOT(updateBoundingRect())); hand = new HandZone(this, (int) table->boundingRect().height(), this); base = QPointF(deck->boundingRect().width() + counterAreaWidth + 5, 0); hand->setPos(base); base += QPointF(hand->boundingRect().width(), 0); table->setPos(base); updateBoundingRect(); if (local) { aMoveHandToTopLibrary = new QAction(this); aMoveHandToTopLibrary->setData(QList() << "deck" << 0); aMoveHandToBottomLibrary = new QAction(this); aMoveHandToBottomLibrary->setData(QList() << "deck" << -1); aMoveHandToGrave = new QAction(this); aMoveHandToGrave->setData(QList() << "grave" << 0); aMoveHandToRfg = new QAction(this); aMoveHandToRfg->setData(QList() << "rfg" << 0); connect(aMoveHandToTopLibrary, SIGNAL(triggered()), hand, SLOT(moveAllToZone())); connect(aMoveHandToBottomLibrary, SIGNAL(triggered()), hand, SLOT(moveAllToZone())); connect(aMoveHandToGrave, SIGNAL(triggered()), hand, SLOT(moveAllToZone())); connect(aMoveHandToRfg, SIGNAL(triggered()), hand, SLOT(moveAllToZone())); aMoveGraveToTopLibrary = new QAction(this); aMoveGraveToTopLibrary->setData(QList() << "deck" << 0); aMoveGraveToBottomLibrary = new QAction(this); aMoveGraveToBottomLibrary->setData(QList() << "deck" << -1); aMoveGraveToHand = new QAction(this); aMoveGraveToHand->setData(QList() << "hand" << 0); aMoveGraveToRfg = new QAction(this); aMoveGraveToRfg->setData(QList() << "rfg" << 0); connect(aMoveGraveToTopLibrary, SIGNAL(triggered()), grave, SLOT(moveAllToZone())); connect(aMoveGraveToBottomLibrary, SIGNAL(triggered()), grave, SLOT(moveAllToZone())); connect(aMoveGraveToHand, SIGNAL(triggered()), grave, SLOT(moveAllToZone())); connect(aMoveGraveToRfg, SIGNAL(triggered()), grave, SLOT(moveAllToZone())); aMoveRfgToTopLibrary = new QAction(this); aMoveRfgToTopLibrary->setData(QList() << "deck" << 0); aMoveRfgToBottomLibrary = new QAction(this); aMoveRfgToBottomLibrary->setData(QList() << "deck" << -1); aMoveRfgToHand = new QAction(this); aMoveRfgToHand->setData(QList() << "hand" << 0); aMoveRfgToGrave = new QAction(this); aMoveRfgToGrave->setData(QList() << "grave" << 0); connect(aMoveRfgToTopLibrary, SIGNAL(triggered()), rfg, SLOT(moveAllToZone())); connect(aMoveRfgToBottomLibrary, SIGNAL(triggered()), rfg, SLOT(moveAllToZone())); connect(aMoveRfgToHand, SIGNAL(triggered()), rfg, SLOT(moveAllToZone())); connect(aMoveRfgToGrave, SIGNAL(triggered()), rfg, SLOT(moveAllToZone())); aViewLibrary = new QAction(this); connect(aViewLibrary, SIGNAL(triggered()), this, SLOT(actViewLibrary())); aViewTopCards = new QAction(this); connect(aViewTopCards, SIGNAL(triggered()), this, SLOT(actViewTopCards())); } aViewGraveyard = new QAction(this); connect(aViewGraveyard, SIGNAL(triggered()), this, SLOT(actViewGraveyard())); aViewRfg = new QAction(this); connect(aViewRfg, SIGNAL(triggered()), this, SLOT(actViewRfg())); if (local) { aViewSideboard = new QAction(this); connect(aViewSideboard, SIGNAL(triggered()), this, SLOT(actViewSideboard())); aDrawCard = new QAction(this); connect(aDrawCard, SIGNAL(triggered()), this, SLOT(actDrawCard())); aDrawCards = new QAction(this); connect(aDrawCards, SIGNAL(triggered()), this, SLOT(actDrawCards())); aShuffle = new QAction(this); connect(aShuffle, SIGNAL(triggered()), this, SLOT(actShuffle())); aMulligan = new QAction(this); connect(aMulligan, SIGNAL(triggered()), this, SLOT(actMulligan())); } playerMenu = new QMenu(QString()); if (local) { handMenu = playerMenu->addMenu(QString()); handMenu->addAction(aMulligan); handMenu->addAction(aMoveHandToTopLibrary); handMenu->addAction(aMoveHandToBottomLibrary); handMenu->addAction(aMoveHandToGrave); handMenu->addAction(aMoveHandToRfg); hand->setMenu(handMenu); libraryMenu = playerMenu->addMenu(QString()); libraryMenu->addAction(aDrawCard); libraryMenu->addAction(aDrawCards); libraryMenu->addSeparator(); libraryMenu->addAction(aShuffle); libraryMenu->addSeparator(); libraryMenu->addAction(aViewLibrary); libraryMenu->addAction(aViewTopCards); deck->setMenu(libraryMenu, aDrawCard); } else { handMenu = 0; libraryMenu = 0; } graveMenu = playerMenu->addMenu(QString()); graveMenu->addAction(aViewGraveyard); grave->setMenu(graveMenu, aViewGraveyard); rfgMenu = playerMenu->addMenu(QString()); rfgMenu->addAction(aViewRfg); rfg->setMenu(rfgMenu, aViewRfg); if (local) { graveMenu->addSeparator(); graveMenu->addAction(aMoveGraveToTopLibrary); graveMenu->addAction(aMoveGraveToBottomLibrary); graveMenu->addAction(aMoveGraveToHand); graveMenu->addAction(aMoveGraveToRfg); rfgMenu->addSeparator(); rfgMenu->addAction(aMoveRfgToTopLibrary); rfgMenu->addAction(aMoveRfgToBottomLibrary); rfgMenu->addAction(aMoveRfgToHand); rfgMenu->addAction(aMoveRfgToGrave); sbMenu = playerMenu->addMenu(QString()); sbMenu->addAction(aViewSideboard); sb->setMenu(sbMenu, aViewSideboard); aUntapAll = new QAction(this); connect(aUntapAll, SIGNAL(triggered()), this, SLOT(actUntapAll())); aRollDie = new QAction(this); connect(aRollDie, SIGNAL(triggered()), this, SLOT(actRollDie())); aCreateToken = new QAction(this); connect(aCreateToken, SIGNAL(triggered()), this, SLOT(actCreateToken())); playerMenu->addSeparator(); countersMenu = playerMenu->addMenu(QString()); playerMenu->addSeparator(); playerMenu->addAction(aUntapAll); playerMenu->addSeparator(); playerMenu->addAction(aRollDie); playerMenu->addSeparator(); playerMenu->addAction(aCreateToken); playerMenu->addSeparator(); sayMenu = playerMenu->addMenu(QString()); initSayMenu(); } else { countersMenu = 0; sbMenu = 0; } retranslateUi(); } Player::~Player() { qDebug("Player destructor"); QMapIterator i(zones); while (i.hasNext()) delete i.next().value(); clearCounters(); clearArrows(); delete playerMenu; } void Player::updateBoundingRect() { prepareGeometryChange(); bRect = QRectF(0, 0, CARD_WIDTH + 5 + counterAreaWidth + hand->boundingRect().width() + table->boundingRect().width(), table->boundingRect().height()); emit sizeChanged(); } void Player::retranslateUi() { aViewGraveyard->setText(tr("&View graveyard")); aViewRfg->setText(tr("&View exile")); playerMenu->setTitle(tr("Player \"%1\"").arg(name)); graveMenu->setTitle(tr("&Graveyard")); rfgMenu->setTitle(tr("&Exile")); if (local) { aMoveHandToTopLibrary->setText(tr("Move to &top of library")); aMoveHandToBottomLibrary->setText(tr("Move to &bottom of library")); aMoveHandToGrave->setText(tr("Move to g&raveyard")); aMoveHandToRfg->setText(tr("Move to &exile")); aMoveGraveToTopLibrary->setText(tr("Move to &top of library")); aMoveGraveToBottomLibrary->setText(tr("Move to &bottom of library")); aMoveGraveToHand->setText(tr("Move to &hand")); aMoveGraveToRfg->setText(tr("Move to &exile")); aMoveRfgToTopLibrary->setText(tr("Move to &top of library")); aMoveRfgToBottomLibrary->setText(tr("Move to &bottom of library")); aMoveRfgToHand->setText(tr("Move to &hand")); aMoveRfgToGrave->setText(tr("Move to g&raveyard")); aViewLibrary->setText(tr("&View library")); aViewLibrary->setShortcut(tr("F3")); aViewTopCards->setText(tr("View &top cards of library...")); aViewGraveyard->setShortcut(tr("F4")); aViewSideboard->setText(tr("&View sideboard")); aDrawCard->setText(tr("&Draw card")); aDrawCard->setShortcut(tr("Ctrl+D")); aDrawCards->setText(tr("D&raw cards...")); aDrawCards->setShortcut(tr("Ctrl+E")); aMulligan->setText(tr("Take &mulligan")); aMulligan->setShortcut(tr("Ctrl+M")); aShuffle->setText(tr("&Shuffle")); aShuffle->setShortcut(tr("Ctrl+S")); handMenu->setTitle(tr("&Hand")); sbMenu->setTitle(tr("&Sideboard")); libraryMenu->setTitle(tr("&Library")); countersMenu->setTitle(tr("&Counters")); aUntapAll->setText(tr("&Untap all permanents")); aUntapAll->setShortcut(tr("Ctrl+U")); aRollDie->setText(tr("R&oll die...")); aRollDie->setShortcut(tr("Ctrl+I")); aCreateToken->setText(tr("&Create token...")); aCreateToken->setShortcut(tr("Ctrl+T")); sayMenu->setTitle(tr("S&ay")); QMapIterator counterIterator(counters); while (counterIterator.hasNext()) counterIterator.next().value()->retranslateUi(); } } void Player::initSayMenu() { sayMenu->clear(); QSettings settings; settings.beginGroup("messages"); int count = settings.value("count", 0).toInt(); for (int i = 0; i < count; i++) { QAction *newAction = new QAction(settings.value(QString("msg%1").arg(i)).toString(), this); QString shortcut; switch (i) { case 0: shortcut = tr("F5"); break; case 1: shortcut = tr("F6"); break; case 2: shortcut = tr("F7"); break; case 3: shortcut = tr("F8"); break; case 4: shortcut = tr("F9"); break; case 5: shortcut = tr("F10"); break; } newAction->setShortcut(shortcut); connect(newAction, SIGNAL(triggered()), this, SLOT(actSayMessage())); sayMenu->addAction(newAction); } } void Player::prepareForGame() { if (local) { sendGameCommand(new Command_CreateCounter(-1, "life", Qt::white, 25, 20)); sendGameCommand(new Command_CreateCounter(-1, "w", QColor(255, 255, 150), 20, 0)); sendGameCommand(new Command_CreateCounter(-1, "u", QColor(150, 150, 255), 20, 0)); sendGameCommand(new Command_CreateCounter(-1, "b", QColor(150, 150, 150), 20, 0)); sendGameCommand(new Command_CreateCounter(-1, "r", QColor(250, 150, 150), 20, 0)); sendGameCommand(new Command_CreateCounter(-1, "g", QColor(150, 255, 150), 20, 0)); sendGameCommand(new Command_CreateCounter(-1, "x", QColor(255, 255, 255), 20, 0)); sendGameCommand(new Command_CreateCounter(-1, "storm", QColor(255, 255, 255), 20, 0)); mulliganCards = 7; } } void Player::actViewLibrary() { emit toggleZoneView(this, "deck", -1); } void Player::actViewTopCards() { bool ok; int number = QInputDialog::getInteger(0, tr("View top cards of library"), tr("Number of cards:"), defaultNumberTopCards, 1, 2000000000, 1, &ok); if (ok) { defaultNumberTopCards = number; emit toggleZoneView(this, "deck", number); } } void Player::actViewGraveyard() { emit toggleZoneView(this, "grave", -1); } void Player::actViewRfg() { emit toggleZoneView(this, "rfg", -1); } void Player::actViewSideboard() { emit toggleZoneView(this, "sb", -1); } void Player::actShuffle() { sendGameCommand(new Command_Shuffle); } void Player::actDrawCard() { sendGameCommand(new Command_DrawCards(-1, 1)); } void Player::actMulligan() { if (mulliganCards <= 0) return; const CardList &handCards = hand->getCards(); for (int i = 0; i < handCards.size(); i++) sendGameCommand(new Command_MoveCard(-1, "hand", handCards.at(i)->getId(), "deck", 0, -1, false)); sendGameCommand(new Command_Shuffle); sendGameCommand(new Command_DrawCards(-1, mulliganCards--)); } void Player::actDrawCards() { int number = QInputDialog::getInteger(0, tr("Draw cards"), tr("Number:")); if (number) sendGameCommand(new Command_DrawCards(-1, number)); } void Player::actUntapAll() { // client->setCardAttr("table", -1, "tapped", "false"); } void Player::actRollDie() { bool ok; int sides = QInputDialog::getInteger(0, tr("Roll die"), tr("Number of sides:"), 20, 2, 1000, 1, &ok); if (ok) sendGameCommand(new Command_RollDie(-1, sides)); } void Player::actCreateToken() { QString cardname = QInputDialog::getText(0, tr("Create token"), tr("Name:")); if (!cardname.isEmpty()) sendGameCommand(new Command_CreateToken(-1, "table", cardname, QString(), 0, 0)); } void Player::actSayMessage() { QAction *a = qobject_cast(sender()); sendGameCommand(new Command_Say(-1, a->text())); } void Player::setCardAttrHelper(CardItem *card, const QString &aname, const QString &avalue, bool allCards) { if (aname == "tapped") { bool tapped = avalue == "1"; if (!(!tapped && card->getDoesntUntap() && allCards)) { if (!allCards) emit logSetTapped(this, card->getName(), tapped); card->setTapped(tapped); } } else if (aname == "attacking") card->setAttacking(avalue == "1"); else if (aname == "facedown") card->setFaceDown(avalue == "1"); else if (aname == "counters") { int value = avalue.toInt(); emit logSetCardCounters(this, card->getName(), value, card->getCounters()); card->setCounters(value); } else if (aname == "annotation") card->setAnnotation(avalue); else if (aname == "doesnt_untap") { bool value = (avalue == "1"); emit logSetDoesntUntap(this, card->getName(), value); card->setDoesntUntap(value); } } void Player::eventDeckSelect(Event_DeckSelect *event) { emit logDeckSelect(this, event->getDeckId()); } void Player::eventSay(Event_Say *event) { emit logSay(this, event->getMessage()); } void Player::eventReadyStart(Event_ReadyStart * /*event*/) { emit logReadyStart(this); } void Player::eventShuffle(Event_Shuffle * /*event*/) { emit logShuffle(this); } void Player::eventRollDie(Event_RollDie *event) { emit logRollDie(this, event->getSides(), event->getValue()); } void Player::eventCreateArrow(Event_CreateArrow *event) { ArrowItem *arrow = addArrow(event->getArrow()); if (!arrow) return; emit logCreateArrow(this, arrow->getStartItem()->getOwner(), arrow->getStartItem()->getName(), arrow->getTargetItem()->getOwner(), arrow->getTargetItem()->getName()); } void Player::eventDeleteArrow(Event_DeleteArrow *event) { delArrow(event->getArrowId()); } void Player::eventCreateToken(Event_CreateToken *event) { CardZone *zone = zones.value(event->getZone(), 0); if (!zone) return; CardItem *card = new CardItem(this, event->getCardName(), event->getCardId()); emit logCreateToken(this, card->getName()); zone->addCard(card, true, event->getX(), event->getY()); } void Player::eventSetCardAttr(Event_SetCardAttr *event) { CardZone *zone = zones.value(event->getZone(), 0); if (!zone) return; if (event->getCardId() == -1) { const CardList &cards = zone->getCards(); for (int i = 0; i < cards.size(); i++) setCardAttrHelper(cards.at(i), event->getAttrName(), event->getAttrValue(), true); if (event->getAttrName() == "tapped") emit logSetTapped(this, QString("-1"), event->getAttrValue() == "1"); } else { CardItem *card = zone->getCard(event->getCardId(), QString()); setCardAttrHelper(card, event->getAttrName(), event->getAttrValue(), false); } } void Player::eventCreateCounter(Event_CreateCounter *event) { addCounter(event->getCounter()); } void Player::eventSetCounter(Event_SetCounter *event) { Counter *c = counters.value(event->getCounterId(), 0); if (!c) return; int oldValue = c->getValue(); c->setValue(event->getValue()); emit logSetCounter(this, c->getName(), event->getValue(), oldValue); } void Player::eventDelCounter(Event_DelCounter *event) { delCounter(event->getCounterId()); } void Player::eventDumpZone(Event_DumpZone *event) { Player *zoneOwner = static_cast(parent())->getPlayers().value(event->getZoneOwnerId(), 0); if (!zoneOwner) return; CardZone *zone = zoneOwner->getZones().value(event->getZone(), 0); if (!zone) return; emit logDumpZone(this, zone, event->getNumberCards()); } void Player::eventStopDumpZone(Event_StopDumpZone *event) { Player *zoneOwner = static_cast(parent())->getPlayers().value(event->getZoneOwnerId(), 0); if (!zoneOwner) return; CardZone *zone = zoneOwner->getZones().value(event->getZone(), 0); if (!zone) return; emit logStopDumpZone(this, zone); } void Player::eventMoveCard(Event_MoveCard *event) { CardZone *startZone = zones.value(event->getStartZone(), 0); CardZone *targetZone = zones.value(event->getTargetZone(), 0); if (!startZone || !targetZone) return; int position = event->getPosition(); int x = event->getX(); int y = event->getY(); int logPosition = position; int logX = x; if (position == -1) position = 0; if (x == -1) x = 0; CardItem *card = startZone->takeCard(position, event->getCardId(), event->getCardName(), startZone != targetZone); if (!card) return; card->deleteDragItem(); card->setFaceDown(event->getFaceDown()); // The log event has to be sent before the card is added to the target zone // because the addCard function can modify the card object. emit logMoveCard(this, card->getName(), startZone, logPosition, targetZone, logX); targetZone->addCard(card, true, x, y); // Look at all arrows from and to the card. // If the card was moved to another zone, delete the arrows, otherwise update them. QMapIterator playerIterator(static_cast(parent())->getPlayers()); while (playerIterator.hasNext()) { Player *p = playerIterator.next().value(); QList arrowsToDelete; QMapIterator arrowIterator(p->getArrows()); while (arrowIterator.hasNext()) { ArrowItem *arrow = arrowIterator.next().value(); if ((arrow->getStartItem() == card) || (arrow->getTargetItem() == card)) { if (startZone == targetZone) arrow->updatePath(); else arrowsToDelete.append(arrow->getId()); } } for (int i = 0; i < arrowsToDelete.size(); ++i) p->delArrow(arrowsToDelete[i]); } } void Player::eventDrawCards(Event_DrawCards *event) { CardZone *deck = zones.value("deck"); CardZone *hand = zones.value("hand"); const QList &cardList = event->getCardList(); if (!cardList.isEmpty()) for (int i = 0; i < cardList.size(); ++i) hand->addCard(deck->takeCard(0, cardList[i]->getId(), cardList[i]->getName()), false, -1); else for (int i = 0; i < event->getNumberCards(); ++i) hand->addCard(deck->takeCard(0, -1, QString()), false, -1); hand->reorganizeCards(); deck->reorganizeCards(); emit logDrawCards(this, event->getNumberCards()); } void Player::processGameEvent(GameEvent *event) { switch (event->getItemId()) { case ItemId_Event_DeckSelect: eventDeckSelect(qobject_cast(event)); break; case ItemId_Event_Say: eventSay(qobject_cast(event)); break; case ItemId_Event_ReadyStart: eventReadyStart(qobject_cast(event)); break; case ItemId_Event_Shuffle: eventShuffle(qobject_cast(event)); break; case ItemId_Event_RollDie: eventRollDie(qobject_cast(event)); break; case ItemId_Event_CreateArrow: eventCreateArrow(qobject_cast(event)); break; case ItemId_Event_DeleteArrow: eventDeleteArrow(qobject_cast(event)); break; case ItemId_Event_CreateToken: eventCreateToken(qobject_cast(event)); break; case ItemId_Event_SetCardAttr: eventSetCardAttr(qobject_cast(event)); break; case ItemId_Event_CreateCounter: eventCreateCounter(qobject_cast(event)); break; case ItemId_Event_SetCounter: eventSetCounter(qobject_cast(event)); break; case ItemId_Event_DelCounter: eventDelCounter(qobject_cast(event)); break; case ItemId_Event_DumpZone: eventDumpZone(qobject_cast(event)); break; case ItemId_Event_StopDumpZone: eventStopDumpZone(qobject_cast(event)); break; case ItemId_Event_MoveCard: eventMoveCard(qobject_cast(event)); break; case ItemId_Event_DrawCards: eventDrawCards(qobject_cast(event)); break; default: { qDebug() << "unhandled game event"; } } } void Player::showCardMenu(const QPoint &p) { emit sigShowCardMenu(p); } void Player::setActive(bool _active) { active = _active; update(); } QRectF Player::boundingRect() const { return bRect; } void Player::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/) { if (bgPixmap.isNull()) painter->fillRect(boundingRect(), QColor(200, 200, 200)); else painter->fillRect(boundingRect(), QBrush(bgPixmap)); QString nameStr = getName(); QFont font("Times"); font.setPixelSize(20); // font.setWeight(QFont::Bold); int totalWidth = CARD_WIDTH + counterAreaWidth + 5; if (getActive()) { QFontMetrics fm(font); double w = fm.width(nameStr) * 1.7; double h = fm.height() * 1.7; if (w < h) w = h; painter->setPen(Qt::transparent); QRadialGradient grad(QPointF(0.5, 0.5), 0.5); grad.setCoordinateMode(QGradient::ObjectBoundingMode); grad.setColorAt(0, QColor(150, 200, 150, 255)); grad.setColorAt(0.7, QColor(150, 200, 150, 255)); grad.setColorAt(1, QColor(150, 150, 150, 0)); painter->setBrush(QBrush(grad)); painter->drawEllipse(QRectF(((double) (totalWidth - w)) / 2, 0, w, h)); } painter->setFont(font); painter->setPen(QPen(Qt::black)); painter->drawText(QRectF(0, 0, totalWidth, 40), Qt::AlignCenter, nameStr); } void Player::processPlayerInfo(ServerInfo_Player *info) { for (int i = 0; i < info->getZoneList().size(); ++i) { ServerInfo_Zone *zoneInfo = info->getZoneList()[i]; CardZone *zone = zones.value(zoneInfo->getName(), 0); if (!zone) continue; zone->clearContents(); const QList &cardList = zoneInfo->getCardList(); if (cardList.isEmpty()) { for (int j = 0; j < zoneInfo->getCardCount(); ++j) zone->addCard(new CardItem(this), false, -1); } else { for (int j = 0; j < cardList.size(); ++j) { CardItem *card = new CardItem(this); card->processCardInfo(cardList[i]); zone->addCard(card, false, cardList[i]->getX(), cardList[i]->getY()); } } zone->reorganizeCards(); } clearCounters(); for (int i = 0; i < info->getCounterList().size(); ++i) { addCounter(info->getCounterList().at(i)); } clearArrows(); for (int i = 0; i < info->getArrowList().size(); ++i) addArrow(info->getArrowList().at(i)); } void Player::addCard(CardItem *c) { emit newCardAdded(c); } void Player::addZone(CardZone *z) { zones.insert(z->getName(), z); } Counter *Player::addCounter(ServerInfo_Counter *counter) { return addCounter(counter->getId(), counter->getName(), counter->getColor(), counter->getRadius(), counter->getCount()); } Counter *Player::addCounter(int counterId, const QString &name, QColor color, int radius, int value) { Counter *c = new Counter(this, counterId, name, color, radius, value, this); counters.insert(counterId, c); if (countersMenu) countersMenu->addMenu(c->getMenu()); rearrangeCounters(); return c; } void Player::delCounter(int counterId) { Counter *c = counters.value(counterId, 0); if (!c) return; counters.remove(counterId); delete c; rearrangeCounters(); } void Player::clearCounters() { QMapIterator counterIterator(counters); while (counterIterator.hasNext()) delete counterIterator.next().value(); counters.clear(); } ArrowItem *Player::addArrow(ServerInfo_Arrow *arrow) { const QMap &playerList = static_cast(parent())->getPlayers(); Player *startPlayer = playerList.value(arrow->getStartPlayerId(), 0); Player *targetPlayer = playerList.value(arrow->getTargetPlayerId(), 0); if (!startPlayer || !targetPlayer) return 0; CardZone *startZone = startPlayer->getZones().value(arrow->getStartZone(), 0); CardZone *targetZone = targetPlayer->getZones().value(arrow->getTargetZone(), 0); if (!startZone || !targetZone) return 0; CardItem *startCard = startZone->getCard(arrow->getStartCardId(), QString()); CardItem *targetCard = targetZone->getCard(arrow->getTargetCardId(), QString()); if (!startCard || !targetCard) return 0; return addArrow(arrow->getId(), startCard, targetCard, arrow->getColor()); } ArrowItem *Player::addArrow(int arrowId, CardItem *startCard, CardItem *targetCard, const QColor &color) { ArrowItem *arrow = new ArrowItem(this, arrowId, startCard, targetCard, color); arrows.insert(arrowId, arrow); scene()->addItem(arrow); return arrow; } void Player::delArrow(int arrowId) { ArrowItem *a = arrows.value(arrowId, 0); if (!a) return; arrows.remove(arrowId); delete a; } void Player::clearArrows() { QMapIterator arrowIterator(arrows); while (arrowIterator.hasNext()) delete arrowIterator.next().value(); arrows.clear(); } void Player::rearrangeCounters() { qreal marginTop = 50; qreal marginBottom = 15; // Determine total height of bounding rectangles qreal totalHeight = 0; QMapIterator counterIterator(counters); while (counterIterator.hasNext()) { counterIterator.next(); totalHeight += counterIterator.value()->boundingRect().height(); } // Determine free space between objects qreal padding = (boundingRect().height() - marginTop - marginBottom - totalHeight) / (counters.size() - 1); qreal y = boundingRect().y() + marginTop; if (counters.size() == 1) { padding = 0; y += (boundingRect().height() - marginTop - marginBottom) / 2; } // Place objects for (counterIterator.toFront(); counterIterator.hasNext(); ) { Counter *c = counterIterator.next().value(); QRectF br = c->boundingRect(); c->setPos((counterAreaWidth - br.width()) / 2, y); y += br.height() + padding; } } void Player::sendGameCommand(GameCommand *command) { static_cast(parent())->sendGameCommand(command); }