Ejemplo n.º 1
0
CardItem *CardZone::takeCard(int position, int cardId, bool /*canResize*/)
{
    if (position == -1) {
        // position == -1 means either that the zone is indexed by card id
        // or that it doesn't matter which card you take.
        for (int i = 0; i < cards.size(); ++i)
            if (cards[i]->getId() == cardId) {
                position = i;
                break;
            }
        if (position == -1)
            position = 0;
    }
    if (position >= cards.size())
        return 0;

    CardItem *c = cards.takeAt(position);

    if (view)
        view->removeCard(position);

    c->setId(cardId);

    reorganizeCards();
    emit cardCountChanged();
    return c;
}
Ejemplo n.º 2
0
QVariant PixmapItem::itemChange(GraphicsItemChange change,
                                const QVariant &value)
{
    if (change == ItemPositionChange)
    {
        // value is the new position.
        QPointF newPos = value.toPointF();
        QRectF rect = parentItem()->boundingRect();

        rect.moveLeft(boundingRect().width()*3/4*-1);
        rect.setWidth(rect.width() + boundingRect().width()*2/4 );

        rect.moveTop(boundingRect().height()*3/4*-1);
        rect.setHeight(rect.height() + boundingRect().height()*2/4 );

        CardItem *card = qgraphicsitem_cast<CardItem *>(parentItem());

        if (!rect.contains(newPos))
        {
            // Keep the item inside the scene rect.
            int newX = (int)qMin(rect.right(), qMax(newPos.x(), rect.left()));
            int newY = (int)qMin(rect.bottom(), qMax(newPos.y(), rect.top()));



            if(card->isAlign())
            {
                int gridSize = card->getGridSize();
                newX = (newX/(10*gridSize))*(10*gridSize);
                newY = (newY/(10*gridSize))*(10*gridSize);
            }

            newPos.setX(newX);
            newPos.setY(newY);
            return newPos;
        }
        else
        {
            int newX =  newPos.x();
            int newY = newPos.y();

            if(card->isAlign())
            {
                int gridSize = card->getGridSize();
                newX = newPos.x()/(10*gridSize);
                newX = newX * (10*gridSize);
                newY = newPos.y()/(10*gridSize);
                newY = newY*(10*gridSize);

            }

            newPos.setX(newX);
            newPos.setY(newY);
            return newPos;

        }
    }

    return QGraphicsItem::itemChange(change, value);
}
Ejemplo n.º 3
0
/**
 * An other player plays a card from his hand to the stack
 * @brief Playground::playerPlaysCard
 * @param player
 * @param playedCard
 */
void Playground::playerPlaysCard(PLAYER::Name player, const Card& playedCard)
{
    PlayerItem* p = NULL;
    switch (player) {
    case PLAYER::Name::LEFT: {
        p = players.value(PlayerItem::direction::LEFT);
        break;
    }
    case PLAYER::Name::TOP: {
        p = players.value(PlayerItem::direction::TOP);
        break;
    }
    case PLAYER::Name::RIGHT: {
        p = players.value(PlayerItem::direction::RIGHT);
        break;
    }
    default:
        break;
    }
    p->setActive();

    CardItem* dummyCard = p->findCard(playedCard, true);
    CardItem _playedCard(playedCard);
    history.write("another Player, plays a Card", _playedCard);
    addItem(_playedCard.createImg());
    _playedCard.setPos(dummyCard->getX(), dummyCard->getY());
    p->removeCard(playedCard);
    soundMgr.playCard();
    updateDepotCard(_playedCard, depot);
    p->setUnactive();
    p->rearrangePlayer(this->sceneRect().center().x(), this->sceneRect().center().y());
}
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();
    }
}
Ejemplo n.º 5
0
/**
 * updates the Player card with animation from stack to the playerhand
 * @brief Playground::updatePlayerCard
 * @param fromCard
 * @param toCard
 * @param withAnimation
 */
void Playground::updatePlayerCard(CardItem& fromCard, CardItem& toCard, bool withAnimation)
{
    fromCard.setPos(stack.getX(), stack.getY());
    addItem(fromCard.createImg());

    qreal x = toCard.getX();
    qreal y = toCard.getY();

    if (withAnimation) {
        QEventLoop pause;
        prepareNewAnimation(pause);
        addPositionAnimation(*fromCard.createImg(), *toCard.createImg());
        startAnimation();
        pause.exec();
    }
    if ((&toCard) != NULL) {
    }
    toCard = CardItem(fromCard);
    toCard.setPos(x, y);
    toCard.createImg()->setZValue(zValue);
    zValue++;
    addItem(toCard.createImg());

    update(sceneRect());
}
Ejemplo n.º 6
0
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();
    }    
}
Ejemplo n.º 7
0
void GameScene::updateHover(const QPointF &scenePos)
{
    QList<QGraphicsItem *> itemList = items(scenePos, Qt::IntersectsItemBoundingRect, Qt::DescendingOrder, getViewTransform());
    
    // Search for the topmost zone and ignore all cards not belonging to that zone.
    CardZone *zone = 0;
    for (int i = 0; i < itemList.size(); ++i)
        if ((zone = qgraphicsitem_cast<CardZone *>(itemList[i])))
            break;
    
    CardItem *maxZCard = 0;
    if (zone) {
        qreal maxZ = -1;
        for (int i = 0; i < itemList.size(); ++i) {
            CardItem *card = qgraphicsitem_cast<CardItem *>(itemList[i]);
            if (!card)
                continue;
            if (card->getAttachedTo()) {
                if (card->getAttachedTo()->getZone() != zone)
                    continue;
            } else if (card->getZone() != zone)
                continue;
            
            if (card->getRealZValue() > maxZ) {
                maxZ = card->getRealZValue();
                maxZCard = card;
            }
        }
    }
    if (hoveredCard && (maxZCard != hoveredCard))
        hoveredCard->setHovered(false);
    if (maxZCard && (maxZCard != hoveredCard))
        maxZCard->setHovered(true);
    hoveredCard = maxZCard;
}
Ejemplo n.º 8
0
void TableZone::reorganizeCards()
{
    QList<ArrowItem *> arrowsToUpdate;
    
    // Calculate card stack widths so mapping functions work properly
    computeCardStackWidths();

    for (int i = 0; i < cards.size(); ++i) {
        QPoint gridPoint = cards[i]->getGridPos();
        if (gridPoint.x() == -1)
            continue;
        
        QPointF mapPoint = mapFromGrid(gridPoint);
        qreal x = mapPoint.x();
        qreal y = mapPoint.y();
        
        int numberAttachedCards = cards[i]->getAttachedCards().size();
        qreal actualX = x + numberAttachedCards * STACKED_CARD_OFFSET_X;
        qreal actualY = y;
        if (numberAttachedCards)
            actualY += 15;
        
        cards[i]->setPos(actualX, actualY);
        cards[i]->setRealZValue((actualY + CARD_HEIGHT) * 100000 + (actualX + 1) * 100);
        
        QListIterator<CardItem *> attachedCardIterator(cards[i]->getAttachedCards());
        int j = 0;
        while (attachedCardIterator.hasNext()) {
            ++j;
            CardItem *attachedCard = attachedCardIterator.next();
            qreal childX = actualX - j * STACKED_CARD_OFFSET_X;
            qreal childY = y + 5;
            attachedCard->setPos(childX, childY);
            attachedCard->setRealZValue((childY + CARD_HEIGHT) * 100000 + (childX + 1) * 100);

            arrowsToUpdate.append(attachedCard->getArrowsFrom());
            arrowsToUpdate.append(attachedCard->getArrowsTo());
        }
        
        arrowsToUpdate.append(cards[i]->getArrowsFrom());
        arrowsToUpdate.append(cards[i]->getArrowsTo());
    }

    QSetIterator<ArrowItem *> arrowIterator(QSet<ArrowItem *>::fromList(arrowsToUpdate));
    while (arrowIterator.hasNext())
        arrowIterator.next()->updatePath();
    
    resizeToContents();
    update();
}
Ejemplo n.º 9
0
void CardItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if (event->buttons().testFlag(Qt::RightButton)) {
        if ((event->screenPos() - event->buttonDownScreenPos(Qt::RightButton)).manhattanLength() < 2 * QApplication::startDragDistance())
            return;
        
        QColor arrowColor = Qt::red;
        if (event->modifiers().testFlag(Qt::ControlModifier))
            arrowColor = Qt::yellow;
        else if (event->modifiers().testFlag(Qt::AltModifier))
            arrowColor = Qt::blue;
        else if (event->modifiers().testFlag(Qt::ShiftModifier))
            arrowColor = Qt::green;
        
        drawArrow(arrowColor);
    } else if (event->buttons().testFlag(Qt::LeftButton)) {
        if ((event->screenPos() - event->buttonDownScreenPos(Qt::LeftButton)).manhattanLength() < 2 * QApplication::startDragDistance())
            return;
        if (zone->getIsView()) {
            const ZoneViewZone *const view = static_cast<const ZoneViewZone *const>(zone);
            if (view->getRevealZone() && !view->getWriteableRevealZone())
                return;
        } else if (!owner->getLocal())
            return;
        
        bool forceFaceDown = event->modifiers().testFlag(Qt::ShiftModifier);
    
        createDragItem(id, event->pos(), event->scenePos(), facedown || forceFaceDown);
        dragItem->grabMouse();
        
        QList<QGraphicsItem *> sel = scene()->selectedItems();
        int j = 0;
        for (int i = 0; i < sel.size(); i++) {
            CardItem *c = (CardItem *) sel.at(i);
            if ((c == this) || (c->getZone() != zone))
                continue;
            ++j;
            QPointF childPos;
            if (zone->getHasCardAttr())
                childPos = c->pos() - pos();
            else
                childPos = QPointF(j * CARD_WIDTH / 2, 0);
            CardDragItem *drag = new CardDragItem(c, c->getId(), childPos, c->getFaceDown() || forceFaceDown, dragItem);
            drag->setPos(dragItem->pos() + childPos);
            scene()->addItem(drag);
        }
    }
    setCursor(Qt::OpenHandCursor);
}
Ejemplo n.º 10
0
CardItem *CardZone::getCard(int cardId, const QString &cardName)
{
    CardItem *c = cards.findCard(cardId, false);
    if (!c) {
        qDebug() << "CardZone::getCard: card id=" << cardId << "not found";
        return 0;
    }
    // If the card's id is -1, this zone is invisible,
    // so we need to give the card an id and a name as it comes out.
    // It can be assumed that in an invisible zone, all cards are equal.
    if ((c->getId() == -1) || (c->getName().isEmpty())) {
        c->setId(cardId);
        c->setName(cardName);
    }
    return c;
}
Ejemplo n.º 11
0
void PileZone::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if ((event->screenPos() - event->buttonDownScreenPos(Qt::LeftButton)).manhattanLength() < QApplication::startDragDistance())
        return;

    if (cards.isEmpty())
        return;

    bool faceDown = event->modifiers().testFlag(Qt::ShiftModifier);
    bool bottomCard = event->modifiers().testFlag(Qt::ControlModifier);
    CardItem *card = bottomCard ? cards.last() : cards.first();
    const int cardid = contentsKnown() ? card->getId() : (bottomCard ? cards.size() - 1 : 0);
    CardDragItem *drag = card->createDragItem(cardid, event->pos(), event->scenePos(), faceDown);
    drag->grabMouse();
    setCursor(Qt::OpenHandCursor);
}
Ejemplo n.º 12
0
void PlayerBoard::discardCards(set<boost::shared_ptr<Card> > cards) {
    PlayerTable* playerTable = ((PlayerTable*)getParent());
    if(playerTable) {
        CCArray itemArray;
        set<boost::shared_ptr<Card> >::iterator it;
        for(it = cards.begin(); it != cards.end(); it++) {
            CardItem* item = CardItem::create(*it);
            item->setPosition(CCPointZero);
            addChild(item);
            itemArray.addObject(item);
        }

        sgs::layer::TableAnimationLayer* animationLayer = playerTable->getTableAnimationLayer();
        animationLayer->playThrowAnimation(&itemArray);
    }
}
Ejemplo n.º 13
0
/**
 * 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);
                }
            }
        }
    }
}
Ejemplo n.º 14
0
bool DiscardPile::_addCardItems(QList<CardItem*> &card_items, Player::Place place)
{
    _m_mutex_pileCards.lock();
    m_visibleCards.append(card_items);
    int numAdded = card_items.size();
    int numRemoved = m_visibleCards.size() - qMax(m_numCardsVisible, numAdded + 1);
    for (int i = 0; i <  numRemoved; i++)
    {
        CardItem* toRemove = m_visibleCards.first();
        toRemove->setZValue(0.0);
        toRemove->setHomeOpacity(0.0);
        connect(toRemove, SIGNAL(movement_animation_finished()), this, SLOT(_destroyCard()));
        toRemove->goBack(true);
        m_visibleCards.removeFirst();
    }
    foreach (CardItem* card_item, m_visibleCards)
    {
        card_item->setHomeOpacity(0.7);
    }
Ejemplo n.º 15
0
void StackZone::reorganizeCards()
{
	if (!cards.isEmpty()) {
		QList<ArrowItem *> arrowsToUpdate;
		
		const int cardCount = cards.size();
		qreal totalWidth = boundingRect().width();
		qreal totalHeight = boundingRect().height();
		qreal cardWidth = cards.at(0)->boundingRect().width();
		qreal cardHeight = cards.at(0)->boundingRect().height();
		qreal xspace = 5;
		qreal x1 = xspace;
		qreal x2 = totalWidth - xspace - cardWidth;
	
		for (int i = 0; i < cardCount; i++) {
			CardItem *c = cards.at(i);
			qreal x = i % 2 ? x2 : x1;
			// If the total height of the cards is smaller than the available height,
			// the cards do not need to overlap and are displayed in the center of the area.
			if (cardHeight * cardCount > totalHeight)
				c->setPos(x, ((qreal) i) * (totalHeight - cardHeight) / (cardCount - 1));
			else
				c->setPos(x, ((qreal) i) * cardHeight + (totalHeight - cardCount * cardHeight) / 2);
			c->setRealZValue(i);
			
			arrowsToUpdate.append(c->getArrowsFrom());
			arrowsToUpdate.append(c->getArrowsTo());
		}
		QSetIterator<ArrowItem *> arrowIterator(QSet<ArrowItem *>::fromList(arrowsToUpdate));
		while (arrowIterator.hasNext())
			arrowIterator.next()->updatePath();
	}
	update();
}
Ejemplo n.º 16
0
void HandZone::reorganizeCards()
{
    if (!cards.isEmpty()) {
        const int cardCount = cards.size();
        if (settingsCache->getHorizontalHand()) {
            bool leftJustified = settingsCache->getLeftJustified();
            qreal cardWidth = cards.at(0)->boundingRect().width();
            const int xPadding = leftJustified ? cardWidth * 1.4 : 5;
            qreal totalWidth =
                leftJustified ? boundingRect().width() - (1 * xPadding) - 5 : boundingRect().width() - 2 * xPadding;

            for (int i = 0; i < cardCount; i++) {
                CardItem *c = cards.at(i);
                // If the total width of the cards is smaller than the available width,
                // the cards do not need to overlap and are displayed in the center of the area.
                if (cardWidth * cardCount > totalWidth)
                    c->setPos(xPadding + ((qreal)i) * (totalWidth - cardWidth) / (cardCount - 1), 5);
                else {
                    qreal xPosition =
                        leftJustified ? xPadding + ((qreal)i) * cardWidth
                                      : xPadding + ((qreal)i) * cardWidth + (totalWidth - cardCount * cardWidth) / 2;
                    c->setPos(xPosition, 5);
                }
                c->setRealZValue(i);
            }
        } else {
            qreal totalWidth = boundingRect().width();
            qreal totalHeight = boundingRect().height();
            qreal cardWidth = cards.at(0)->boundingRect().width();
            qreal cardHeight = cards.at(0)->boundingRect().height();
            qreal xspace = 5;
            qreal x1 = xspace;
            qreal x2 = totalWidth - xspace - cardWidth;

            for (int i = 0; i < cardCount; i++) {
                CardItem *c = cards.at(i);
                qreal x = (i % 2) ? x2 : x1;
                // If the total height of the cards is smaller than the available height,
                // the cards do not need to overlap and are displayed in the center of the area.
                if (cardHeight * cardCount > totalHeight)
                    c->setPos(x, ((qreal)i) * (totalHeight - cardHeight) / (cardCount - 1));
                else
                    c->setPos(x, ((qreal)i) * cardHeight + (totalHeight - cardCount * cardHeight) / 2);
                c->setRealZValue(i);
            }
        }
    }
    update();
}
Ejemplo n.º 17
0
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;
}
Ejemplo n.º 18
0
void Dashboard::selectOnlyCard(bool need_only) {
    if (selected && selected->isSelected())
        selected->clickItem();

    int count = 0;

    QList<CardItem *> items;
    foreach (CardItem *card_item, m_handCards) {
        if (card_item->isEnabled()) {
            items << card_item;
            count++;
            if (need_only && count > 1) {
                unselectAll();
                return;
            }
        }
    }

    QList<int> equip_pos;
    for (int i = 0; i < 4; i++) {
        if (_m_equipCards[i] && _m_equipCards[i]->isMarkable()) {
            equip_pos << i;
            count++;
            if (need_only && count > 1) return;
        }
    }
    if (count == 0) return;
    if (!items.isEmpty()) {
        CardItem *item = items.first();
        item->clickItem();
        selected = item;
        adjustCards();
    } else if (!equip_pos.isEmpty()) {
        int pos = equip_pos.first();
        _m_equipCards[pos]->mark(!_m_equipCards[pos]->isMarked());
        update();
    }
}
Ejemplo n.º 19
0
/**
 * @brief CardUtil::dealCard        发牌
 * @param myList
 * @param leftList
 * @param rightList
 */
void CardUtil::dealCard(QList<CardItem *> &myList, QList<CardItem *> &leftList, QList<CardItem *> &rightList, QList<CardItem *> &bottomList)
{
    QList<CardItem *> itemList = makeAllCard();
    for(int i=0;i<17;i++){
        qsrand(QTime::currentTime().msec());
        int index = qrand()%(itemList.size());
        CardItem *item = itemList.takeAt(index);
        item->isFront = true;
		item->setSelected(true);
        myList.append(item);

        qsrand(QTime::currentTime().msec());
        index = qrand()%(itemList.size());
        item = itemList.takeAt(index);
        leftList.append(item);

        qsrand(QTime::currentTime().msec());
        index = qrand()%(itemList.size());
        item = itemList.takeAt(index);
        rightList.append(item);
    }
    bottomList = itemList;
}
Ejemplo n.º 20
0
void TableZone::toggleTapped()
{
    QList<QGraphicsItem *> selectedItems = scene()->selectedItems();
    bool tapAll = false;
    for (int i = 0; i < selectedItems.size(); i++)
        if (!qgraphicsitem_cast<CardItem *>(selectedItems[i])->getTapped()) {
            tapAll = true;
            break;
        }
    QList< const ::google::protobuf::Message * > cmdList;
    for (int i = 0; i < selectedItems.size(); i++) {
        CardItem *temp = qgraphicsitem_cast<CardItem *>(selectedItems[i]);
        if (temp->getTapped() != tapAll) {
            Command_SetCardAttr *cmd = new Command_SetCardAttr;
            cmd->set_zone(name.toStdString());
            cmd->set_card_id(temp->getId());
            cmd->set_attribute(AttrTapped);
            cmd->set_attr_value(tapAll ? "1" : "0");
            cmdList.append(cmd);
        }
    }
    player->sendGameCommand(player->prepareGameCommand(cmdList));
}
Ejemplo n.º 21
0
void CardItem::drawArrow(const QColor &arrowColor)
{
    if (static_cast<TabGame *>(owner->parent())->getSpectator())
        return;
    
    Player *arrowOwner = static_cast<TabGame *>(owner->parent())->getActiveLocalPlayer();
    ArrowDragItem *arrow = new ArrowDragItem(arrowOwner, this, arrowColor);
    scene()->addItem(arrow);
    arrow->grabMouse();
    
    QListIterator<QGraphicsItem *> itemIterator(scene()->selectedItems());
    while (itemIterator.hasNext()) {
        CardItem *c = qgraphicsitem_cast<CardItem *>(itemIterator.next());
        if (!c || (c == this))
            continue;
        if (c->getZone() != zone)
            continue;
        
        ArrowDragItem *childArrow = new ArrowDragItem(arrowOwner, c, arrowColor);
        scene()->addItem(childArrow);
        arrow->addChildArrow(childArrow);
    }
}
Ejemplo n.º 22
0
CardItem *CardList::findCard(const int id, const bool remove, int *position)
{
	if (!contentsKnown) {
		if (empty())
			return 0;
		CardItem *temp = at(0);
		if (remove)
			removeAt(0);
		if (position)
			*position = id;
		return temp;
	} else
		for (int i = 0; i < size(); i++) {
			CardItem *temp = at(i);
			if (temp->getId() == id) {
				if (remove)
					removeAt(i);
				if (position)
					*position = i;
				return temp;
			}
		}
	return 0;
}
Ejemplo n.º 23
0
/**
 * 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());
}
Ejemplo n.º 24
0
void ArrowDragItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    if (!startItem)
        return;

    if (targetItem && (targetItem != startItem)) {
        CardZone *startZone = static_cast<CardItem *>(startItem)->getZone();
        // For now, we can safely assume that the start item is always a card.
        // The target item can be a player as well.
        CardItem *startCard = qgraphicsitem_cast<CardItem *>(startItem);
        CardItem *targetCard = qgraphicsitem_cast<CardItem *>(targetItem);

        Command_CreateArrow cmd;
        cmd.mutable_arrow_color()->CopyFrom(convertQColorToColor(color));
        cmd.set_start_player_id(startZone->getPlayer()->getId());
        cmd.set_start_zone(startZone->getName().toStdString());
        cmd.set_start_card_id(startCard->getId());

        if (targetCard) {
            CardZone *targetZone = targetCard->getZone();
            cmd.set_target_player_id(targetZone->getPlayer()->getId());
            cmd.set_target_zone(targetZone->getName().toStdString());
            cmd.set_target_card_id(targetCard->getId());
        } else {
            PlayerTarget *targetPlayer = qgraphicsitem_cast<PlayerTarget *>(targetItem);
            cmd.set_target_player_id(targetPlayer->getOwner()->getId());
        }
        if (startZone->getName().compare("hand") == 0)  {
            startCard->playCard(false);
            CardInfo *ci = startCard->getInfo();
            if (((!settingsCache->getPlayToStack() && ci->getTableRow() == 3) ||
                ((settingsCache->getPlayToStack() && ci->getTableRow() != 0) &&
                startCard->getZone()->getName().toStdString() != "stack")))
                cmd.set_start_zone("stack");
            else
                cmd.set_start_zone(settingsCache->getPlayToStack() ? "stack" :"table");
        }
        player->sendGameCommand(cmd);
    }
    delArrow();

    for (int i = 0; i < childArrows.size(); ++i)
        childArrows[i]->mouseReleaseEvent(event);
}
Ejemplo n.º 25
0
void CardContainer::fillCards(const QList<int> &card_ids){
    if(card_ids.isEmpty()) return;
    else if(!items.isEmpty()){
        items_stack.push(items);
        items.clear();
    }
    QList<CardItem*> card_items = _createCards(card_ids);

    static const QPointF pos1(30, 40);
    static const QPointF pos2(30, 184);
    static const int card_width = 93;
    static const int skip = 102;
    static const qreal whole_width = skip * 4 + card_width;
    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 / 2 + 1;
            qreal real_skip = whole_width / half;
            
            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);
    }    
}
Ejemplo n.º 26
0
void ArrowAttachItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * /*event*/)
{
    if (!startItem)
        return;

    if (targetItem && (targetItem != startItem)) {
        CardItem *startCard = qgraphicsitem_cast<CardItem *>(startItem);
        CardZone *startZone = startCard->getZone();
        CardItem *targetCard = qgraphicsitem_cast<CardItem *>(targetItem);
        CardZone *targetZone = targetCard->getZone();

        Command_AttachCard cmd;
        cmd.set_start_zone(startZone->getName().toStdString());
        cmd.set_card_id(startCard->getId());
        cmd.set_target_player_id(targetZone->getPlayer()->getId());
        cmd.set_target_zone(targetZone->getName().toStdString());
        cmd.set_target_card_id(targetCard->getId());

        player->sendGameCommand(cmd);
    }

    delArrow();
}
Ejemplo n.º 27
0
QVariant CardModel::data(const QModelIndex &index, int role) const
{
	QVariant res;
	if (!index.isValid())
	{
		return res;
	}

	CardItem* item = (CardItem*)items.value(index.row());
	if (role == Qt::DisplayRole)
	{
		if (item != NULL)
		{
			res = item->getName() +
				  " (" + QString::number(item->getDiscont()) + ")";
		}
	}
	else if (role == Qt::EditRole
			|| role == SortRole
			|| role == SearchRole)
	{
		if (item != NULL)
		{
			res = item->getName();
		}
	}
	else if (role == KeyRole)
	{
		res = item->getId();
	}
	else if (role == DiscontRole)
	{
		res = item->getDiscont();
	}

	return res;
}
Ejemplo n.º 28
0
/**
 * @brief CardUtil::makeAllCard 生成所有54张牌。
 * @return
 */
QList<CardItem *> CardUtil::makeAllCard()
{
    QList<CardItem *> itemList;
    for(int i=0;i<13;i++){
        CardItem *itema = new CardItem();
        itema->fkPic = QPixmap(":images/image/" + QString::number(i+1) + "a.png");
        itema->CardNum = i+1;
        itema->cardType = "a";
        itema->setSelected(true);
        itemList.append(itema);

        CardItem *itemb = new CardItem();
        itemb->fkPic = QPixmap(":images/image/" + QString::number(i+1) + "b.png");
        itemb->CardNum = i+1;
        itemb->cardType = "b";
        itemb->setSelected(true);
        itemList.append(itemb);

        CardItem *itemc = new CardItem();
        itemc->fkPic = QPixmap(":images/image/" + QString::number(i+1) + "c.png");
        itemc->CardNum = i+1;
        itemc->cardType = "c";
        itemc->setSelected(true);
        itemList.append(itemc);

        CardItem *itemd = new CardItem();
        itemd->fkPic = QPixmap(":images/image/" + QString::number(i+1) + "d.png");
        itemd->CardNum = i+1;
        itemd->cardType = "d";
        itemd->setSelected(true);
        itemList.append(itemd);
    }
    CardItem *itema = new CardItem();
    itema->fkPic = QPixmap(":images/image/14a.png");
    itema->CardNum = 14;
    itema->cardType = "a";
    itema->setSelected(true);
    itemList.append(itema);

    CardItem *itemb = new CardItem();
    itemb->fkPic = QPixmap(":images/image/15a.png");
    itemb->CardNum = 15;
    itemb->cardType = "a";
    itemb->setSelected(true);
    itemList.append(itemb);
    return itemList;
}
Ejemplo n.º 29
0
void TableZone::reorganizeCards()
{
    QList<ArrowItem *> arrowsToUpdate;
    
    // Calculate table grid distortion so that the mapping functions work properly
    QMap<int, int> gridPointStackCount;
    for (int i = 0; i < cards.size(); ++i) {
        const QPoint &gridPoint = cards[i]->getGridPos();
        if (gridPoint.x() == -1)
            continue;
        
        const int key = gridPoint.x() / 3 + gridPoint.y() * 1000;
        gridPointStackCount.insert(key, gridPointStackCount.value(key, 0) + 1);
    }
    gridPointWidth.clear();
    for (int i = 0; i < cards.size(); ++i) {
        const QPoint &gridPoint = cards[i]->getGridPos();
        if (gridPoint.x() == -1)
            continue;
        
        const int key = gridPoint.x() / 3 + gridPoint.y() * 1000;
        const int stackCount = gridPointStackCount.value(key, 0);
        if (stackCount == 1)
            gridPointWidth.insert(key, CARD_WIDTH * (1 + cards[i]->getAttachedCards().size() / 3.0));
        else
            gridPointWidth.insert(key, CARD_WIDTH * (1 + (stackCount - 1) / 3.0));
    }
    
    for (int i = 0; i < cards.size(); ++i) {
        QPoint gridPoint = cards[i]->getGridPos();
        if (gridPoint.x() == -1)
            continue;
        
        QPointF mapPoint = mapFromGrid(gridPoint);
        qreal x = mapPoint.x();
        qreal y = mapPoint.y();
        
        int numberAttachedCards = cards[i]->getAttachedCards().size();
        qreal actualX = x + numberAttachedCards * CARD_WIDTH / 3.0;
        qreal actualY = y;
        if (numberAttachedCards)
            actualY += 15;
        
        cards[i]->setPos(actualX, actualY);
        cards[i]->setRealZValue((actualY + CARD_HEIGHT) * 100000 + (actualX + 1) * 100);
        
        QListIterator<CardItem *> attachedCardIterator(cards[i]->getAttachedCards());
        int j = 0;
        while (attachedCardIterator.hasNext()) {
            ++j;
            CardItem *attachedCard = attachedCardIterator.next();
            qreal childX = actualX - j * CARD_WIDTH / 3.0;
            qreal childY = y + 5;
            attachedCard->setPos(childX, childY);
            attachedCard->setRealZValue((childY + CARD_HEIGHT) * 100000 + (childX + 1) * 100);

            arrowsToUpdate.append(attachedCard->getArrowsFrom());
            arrowsToUpdate.append(attachedCard->getArrowsTo());
        }
        
        arrowsToUpdate.append(cards[i]->getArrowsFrom());
        arrowsToUpdate.append(cards[i]->getArrowsTo());
    }

    QSetIterator<ArrowItem *> arrowIterator(QSet<ArrowItem *>::fromList(arrowsToUpdate));
    while (arrowIterator.hasNext())
        arrowIterator.next()->updatePath();
    
    resizeToContents();
    update();
}
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);
}