#include #include "tablezone.h" #include "player.h" #include "client.h" TableZone::TableZone(Player *_p, QGraphicsItem *parent) : CardZone(_p, "table", true, false, true, parent) { QSettings settings; QString bgPath = settings.value("zonebg/table").toString(); if (!bgPath.isEmpty()) bgPixmap.load(bgPath); economicGrid = settings.value("table/economic", 1).toInt(); if (economicGrid) height = 14.0 / 3 * CARD_HEIGHT + 3 * paddingY; else height = 4 * CARD_HEIGHT + 3 * paddingY; width = minWidth + 2 * marginX; setCacheMode(DeviceCoordinateCache); setAcceptsHoverEvents(true); } QRectF TableZone::boundingRect() const { return QRectF(0, 0, width, height); } void TableZone::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/) { if (bgPixmap.isNull()) painter->fillRect(boundingRect(), QColor(0, 0, 100)); else painter->fillRect(boundingRect(), QBrush(bgPixmap)); } void TableZone::addCardImpl(CardItem *card, int _x, int _y) { QPointF mapPoint = mapFromGrid(QPoint(_x, _y)); qreal x = mapPoint.x(); qreal y = mapPoint.y(); cards.append(card); if (!player->getLocal()) y = height - CARD_HEIGHT - y; card->setPos(x, y); card->setGridPoint(QPoint(_x, _y)); resizeToContents(); card->setZValue((y + CARD_HEIGHT) * 10000000 + x + 1000); card->setParentItem(this); card->setVisible(true); card->update(); } void TableZone::handleDropEvent(int cardId, CardZone *startZone, const QPoint &dropPoint, bool faceDown) { handleDropEventByGrid(cardId, startZone, mapToGrid(dropPoint), faceDown); } void TableZone::handleDropEventByGrid(int cardId, CardZone *startZone, const QPoint &gridPoint, bool faceDown) { player->client->moveCard(cardId, startZone->getName(), getName(), gridPoint.x(), gridPoint.y(), faceDown); } void TableZone::reorganizeCards() { } void TableZone::toggleTapped() { QList selectedItems = scene()->selectedItems(); bool tapAll = false; for (int i = 0; i < selectedItems.size(); i++) if (!qgraphicsitem_cast(selectedItems[i])->getTapped()) { tapAll = true; break; } for (int i = 0; i < selectedItems.size(); i++) { CardItem *temp = qgraphicsitem_cast(selectedItems[i]); if (temp->getTapped() != tapAll) setCardAttr(temp->getId(), "tapped", tapAll ? "1" : "0"); } } CardItem *TableZone::takeCard(int position, int cardId, const QString &cardName, bool canResize) { CardItem *result = CardZone::takeCard(position, cardId, cardName); if (canResize) resizeToContents(); return result; } void TableZone::resizeToContents() { qDebug("resizeToContents"); int xMax = 0; for (int i = 0; i < cards.size(); ++i) if (cards[i]->pos().x() > xMax) xMax = cards[i]->pos().x(); qDebug(QString("xMax = %1").arg(xMax).toLatin1()); xMax += 2 * CARD_WIDTH; if (xMax < minWidth) xMax = minWidth; int newWidth = xMax + 2 * marginX; if (newWidth != width) { width = newWidth; emit sizeChanged(); } } CardItem *TableZone::getCardFromGrid(const QPoint &gridPoint) const { for (int i = 0; i < cards.size(); i++) if (cards.at(i)->getGridPoint() == gridPoint) return cards.at(i); return 0; } QPointF TableZone::mapFromGrid(const QPoint &gridPoint) const { qDebug(QString("mapFromGrid: %1, %2").arg(gridPoint.x()).arg(gridPoint.y()).toLatin1()); if (gridPoint.y() == 3) { if (economicGrid) return QPointF( 20 + (CARD_WIDTH * gridPoint.x() + CARD_WIDTH * (gridPoint.x() / 3)) / 2, (CARD_HEIGHT + paddingY) * gridPoint.y() + (gridPoint.x() % 3 * CARD_HEIGHT) / 3 ); else return QPointF( 20 + 3 * CARD_WIDTH * gridPoint.x() / 2, (CARD_HEIGHT + paddingY) * gridPoint.y() ); } else return QPointF( 20 + CARD_WIDTH * gridPoint.x() / 2, (CARD_HEIGHT + paddingY) * gridPoint.y() ); } QPoint TableZone::mapToGrid(const QPointF &mapPoint) const { qreal x = mapPoint.x() - marginX; qreal y = mapPoint.y() + paddingY / 2; if (x < 0) x = 0; else if (x > width - CARD_WIDTH - marginX) x = width - CARD_WIDTH - marginX; if (y < 0) y = 0; else if (y > height - CARD_HEIGHT) y = height - CARD_HEIGHT; qDebug(QString("mapToGrid: %1, %2").arg(x).arg(y).toLatin1()); QPoint result = QPoint( // (int) round((double) x * 2 / CARD_WIDTH), // (int) round((double) y / (CARD_HEIGHT + paddingY)) x * 2 / CARD_WIDTH, y / (CARD_HEIGHT + paddingY) ); if (result.y() == 3) { qDebug("UNTER grenze"); if (economicGrid) return QPoint( x * 2 / CARD_WIDTH - floor(x / (2 * CARD_WIDTH)), 3 ); else { return QPoint( x / (1.5 * CARD_WIDTH), 3 ); } } else { qDebug("UEBER grenze"); return result; } } QPoint TableZone::getFreeGridPoint(int row) const { int x = 0; int y = 3 - row; if (y == 3) while (getCardFromGrid(QPoint(x, y))) ++x; else while (((x >= 2) && getCardFromGrid(QPoint(x - 2, y))) || ((x >= 1) && getCardFromGrid(QPoint(x - 1, y))) || getCardFromGrid(QPoint(x, y)) || getCardFromGrid(QPoint(x + 1, y)) || getCardFromGrid(QPoint(x + 2, y))) ++x; return QPoint(x, y); } QPointF TableZone::closestGridPoint(const QPointF &point) { return mapFromGrid(mapToGrid(point + QPoint(CARD_WIDTH / 2, CARD_HEIGHT / 2))); }