diff --git a/cockatrice/src/carddragitem.cpp b/cockatrice/src/carddragitem.cpp index a2882d0d..3d80ce59 100644 --- a/cockatrice/src/carddragitem.cpp +++ b/cockatrice/src/carddragitem.cpp @@ -53,8 +53,7 @@ void CardDragItem::updatePosition(const QPointF &cursorScenePos) QPointF zonePos = currentZone->scenePos(); QPointF cursorPosInZone = cursorScenePos - zonePos; QPointF cardTopLeft = cursorPosInZone - hotSpot; -// QPointF cardCenter = cardTopLeft + QPointF(CARD_WIDTH / 2, CARD_HEIGHT / 2); - QPointF newPos = zonePos + cursorZone->closestGridPoint(cardTopLeft + QPoint(CARD_WIDTH / 2, CARD_HEIGHT / 2)); + QPointF newPos = zonePos + cursorZone->closestGridPoint(cardTopLeft); // qDebug(QString("cardTopLeft = %1, %2 cardCenter = %3, %4").arg((cardTopLeft).x()).arg((cardTopLeft).y()).arg(cardCenter.x()).arg(cardCenter.y()).toLatin1()); diff --git a/cockatrice/src/cardzone.h b/cockatrice/src/cardzone.h index 9423f485..c1b7ed93 100644 --- a/cockatrice/src/cardzone.h +++ b/cockatrice/src/cardzone.h @@ -44,7 +44,7 @@ public: // getCard() finds a card by id. CardItem *getCard(int cardId, const QString &cardName); // takeCard() finds a card by position and removes it from the zone and from all of its views. - CardItem *takeCard(int position, int cardId, const QString &cardName); + virtual CardItem *takeCard(int position, int cardId, const QString &cardName); void setCardAttr(int cardId, const QString &aname, const QString &avalue); ZoneViewZone *getView() const { return view; } void setView(ZoneViewZone *_view); diff --git a/cockatrice/src/gamescene.cpp b/cockatrice/src/gamescene.cpp index 7c04f583..89809a1d 100644 --- a/cockatrice/src/gamescene.cpp +++ b/cockatrice/src/gamescene.cpp @@ -5,60 +5,57 @@ GameScene::GameScene(ZoneViewLayout *_zvLayout, QObject *parent) : QGraphicsScene(parent), zvLayout(_zvLayout) { - connect(zvLayout, SIGNAL(sizeChanged()), this, SLOT(updateSceneSize())); + connect(zvLayout, SIGNAL(sizeChanged()), this, SLOT(rearrange())); addItem(zvLayout); } -void GameScene::updateSceneSize() -{ - int sceneWidth = 0; - int sceneHeight = 0; - for (int i = 0; i < players.size(); ++i) { - const QRectF br = players[i]->boundingRect(); - if (i) - sceneHeight += playerAreaSpacing; - sceneHeight += br.height(); - if (br.width() > sceneWidth) - sceneWidth = br.width(); - } - sceneWidth += zvLayout->size().width(); - qDebug(QString("updateSceneSize: w=%1 h=%2").arg(sceneWidth).arg(sceneHeight).toLatin1()); - setSceneRect(sceneRect().x(), sceneRect().y(), sceneWidth, sceneHeight); -} - void GameScene::addPlayer(Player *player) { players << player; - updateSceneSize(); addItem(player); - rearrangePlayers(); + rearrange(); + connect(player, SIGNAL(sizeChanged()), this, SLOT(rearrange())); } void GameScene::removePlayer(Player *player) { players.removeAt(players.indexOf(player)); removeItem(player); - updateSceneSize(); - rearrangePlayers(); + rearrange(); } -void GameScene::rearrangePlayers() +void GameScene::rearrange() { + struct PlayerProcessor { + static void processPlayer(Player *p, qreal &w, qreal &h, QPointF &b) + { + const QRectF br = p->boundingRect(); + if (br.width() > w) + w = br.width(); + if (h) + h += playerAreaSpacing; + h += br.height(); + p->setPos(b); + b += QPointF(0, br.height() + playerAreaSpacing); + } + }; + QPointF base; - qreal maxWidth = 0; + qreal sceneWidth = 0; + qreal sceneHeight = 0; Player *localPlayer = 0; - for (int i = 0; i < players.size(); ++i) { - QRectF br = players[i]->boundingRect(); - if (br.width() > maxWidth) - maxWidth = br.width(); - if (!players[i]->getLocal()) { - players[i]->setPos(base); - // Change this for better multiplayer support. - base += QPointF(0, br.height() + playerAreaSpacing); - } else + + for (int i = 0; i < players.size(); ++i) + if (!players[i]->getLocal()) + PlayerProcessor::processPlayer(players[i], sceneWidth, sceneHeight, base); + else localPlayer = players[i]; - } if (localPlayer) - localPlayer->setPos(base); - zvLayout->setPos(QPointF(maxWidth, 0)); + PlayerProcessor::processPlayer(localPlayer, sceneWidth, sceneHeight, base); + + zvLayout->setPos(QPointF(sceneWidth, 0)); + sceneWidth += zvLayout->size().width(); + setSceneRect(sceneRect().x(), sceneRect().y(), sceneWidth, sceneHeight); + + qDebug(QString("rearrange(): w=%1 h=%2").arg(sceneWidth).arg(sceneHeight).toLatin1()); } diff --git a/cockatrice/src/gamescene.h b/cockatrice/src/gamescene.h index 4cf56a34..350170ee 100644 --- a/cockatrice/src/gamescene.h +++ b/cockatrice/src/gamescene.h @@ -14,15 +14,13 @@ private: QList players; ZoneViewLayout *zvLayout; - - void rearrangePlayers(); public: GameScene(ZoneViewLayout *_zvLayout, QObject *parent = 0); public slots: void addPlayer(Player *player); void removePlayer(Player *player); private slots: - void updateSceneSize(); + void rearrange(); }; #endif diff --git a/cockatrice/src/player.cpp b/cockatrice/src/player.cpp index bb801436..91ae972d 100644 --- a/cockatrice/src/player.cpp +++ b/cockatrice/src/player.cpp @@ -37,15 +37,16 @@ Player::Player(const QString &_name, int _id, bool _local, CardDatabase *_db, Cl PileZone *sb = new PileZone(this, "sb", false, true, this); sb->setVisible(false); - CardZone *table = new TableZone(this, this); - CardZone *hand = new HandZone(this, table->boundingRect().height(), this); + table = new TableZone(this, this); + connect(table, SIGNAL(sizeChanged()), this, SLOT(updateBoundingRect())); + hand = new HandZone(this, table->boundingRect().height(), this); base = QPointF(deck->boundingRect().width() + 60, 0); hand->setPos(base); base += QPointF(hand->boundingRect().width(), 0); table->setPos(base); - - bRect = QRectF(0, 0, base.x() + table->boundingRect().width(), base.y() + table->boundingRect().height()); + + updateBoundingRect(); if (local) { aMoveHandToTopLibrary = new QAction(this); @@ -83,7 +84,7 @@ Player::Player(const QString &_name, int _id, bool _local, CardDatabase *_db, Cl handMenu = playerMenu->addMenu(QString()); handMenu->addAction(aMoveHandToTopLibrary); handMenu->addAction(aMoveHandToBottomLibrary); - zones.findZone("hand")->setMenu(handMenu); + hand->setMenu(handMenu); libraryMenu = playerMenu->addMenu(QString()); libraryMenu->addAction(aDrawCard); @@ -93,7 +94,7 @@ Player::Player(const QString &_name, int _id, bool _local, CardDatabase *_db, Cl libraryMenu->addSeparator(); libraryMenu->addAction(aViewLibrary); libraryMenu->addAction(aViewTopCards); - zones.findZone("deck")->setMenu(libraryMenu, aDrawCard); + deck->setMenu(libraryMenu, aDrawCard); } else { handMenu = 0; libraryMenu = 0; @@ -101,16 +102,16 @@ Player::Player(const QString &_name, int _id, bool _local, CardDatabase *_db, Cl graveMenu = playerMenu->addMenu(QString()); graveMenu->addAction(aViewGraveyard); - zones.findZone("grave")->setMenu(graveMenu, aViewGraveyard); + grave->setMenu(graveMenu, aViewGraveyard); rfgMenu = playerMenu->addMenu(QString()); rfgMenu->addAction(aViewRfg); - zones.findZone("rfg")->setMenu(rfgMenu, aViewRfg); + rfg->setMenu(rfgMenu, aViewRfg); if (local) { sbMenu = playerMenu->addMenu(QString()); sbMenu->addAction(aViewSideboard); - zones.findZone("sb")->setMenu(sbMenu, aViewSideboard); + sb->setMenu(sbMenu, aViewSideboard); } else sbMenu = 0; @@ -127,6 +128,12 @@ Player::~Player() clearCounters(); } +void Player::updateBoundingRect() +{ + bRect = QRectF(0, 0, CARD_WIDTH + 60 + hand->boundingRect().width() + table->boundingRect().width(), table->boundingRect().height()); + emit sizeChanged(); +} + void Player::retranslateUi() { aViewGraveyard->setText(tr("&View graveyard")); diff --git a/cockatrice/src/player.h b/cockatrice/src/player.h index 4d39af2a..ffcb45e1 100644 --- a/cockatrice/src/player.h +++ b/cockatrice/src/player.h @@ -13,6 +13,8 @@ class QAction; class ZoneViewZone; class Game; class Counter; +class TableZone; +class HandZone; class Player : public QObject, public QGraphicsItem { Q_OBJECT @@ -29,7 +31,11 @@ signals: void logSetTapped(Player *player, QString cardName, bool tapped); void logSetCounter(Player *player, QString counterName, int value, int oldValue); void logSetDoesntUntap(Player *player, QString cardName, bool doesntUntap); + + void sizeChanged(); private slots: + void updateBoundingRect(); + void actMoveHandToTopLibrary(); void actMoveHandToBottomLibrary(); @@ -53,7 +59,11 @@ private: int id; bool active; bool local; + ZoneList zones; + TableZone *table; + HandZone *hand; + CardDatabase *db; void setCardAttrHelper(CardItem *card, const QString &aname, const QString &avalue, bool allCards); diff --git a/cockatrice/src/tablezone.cpp b/cockatrice/src/tablezone.cpp index 53b21324..f2053af0 100644 --- a/cockatrice/src/tablezone.cpp +++ b/cockatrice/src/tablezone.cpp @@ -16,7 +16,7 @@ TableZone::TableZone(Player *_p, QGraphicsItem *parent) height = 14.0 / 3 * CARD_HEIGHT + 3 * paddingY; else height = 4 * CARD_HEIGHT + 3 * paddingY; - width = 12 * CARD_WIDTH; + width = minWidth * CARD_WIDTH / 2; setCacheMode(DeviceCoordinateCache); setAcceptsHoverEvents(true); @@ -29,6 +29,25 @@ QRectF TableZone::boundingRect() const void TableZone::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/) { +/* +// DEBUG + painter->fillRect(boundingRect(), Qt::black); + for (int i = 0; i < width; i += 2) + for (int j = 0; j < height; j += 2) { +// QPointF p = closestGridPoint(QPointF(i, j)); + QPoint p = mapToGrid(QPointF(i, j)); + QColor c; +// c.setHsv(p.x() / 2, p.y() / 3, 255); + c.setHsv(p.x() * 12, p.y() * 80, 255); + painter->setPen(c); + painter->setBrush(c); + painter->drawRect(i, j, 2, 2); + } + painter->setPen(Qt::black); + painter->drawLine(QPointF(0, 366), QPointF(1000, 366)); +// DEBUG Ende +*/ + if (bgPixmap.isNull()) painter->fillRect(boundingRect(), QColor(0, 0, 100)); else @@ -41,6 +60,11 @@ void TableZone::addCardImpl(CardItem *card, int _x, int _y) qreal x = mapPoint.x(); qreal y = mapPoint.y(); + if (x >= width - marginX - 2 * CARD_WIDTH) { + width += 2 * CARD_WIDTH; + emit sizeChanged(); + } + cards.append(card); // if ((x != -1) && (y != -1)) { if (!player->getLocal()) @@ -48,7 +72,7 @@ void TableZone::addCardImpl(CardItem *card, int _x, int _y) card->setPos(x, y); // } card->setGridPoint(QPoint(_x, _y)); - card->setZValue((y + CARD_HEIGHT) * width + x + 1000); + card->setZValue((y + CARD_HEIGHT) * 10000000 + x + 1000); card->setParentItem(this); card->setVisible(true); card->update(); @@ -84,6 +108,23 @@ void TableZone::toggleTapped() } } +CardItem *TableZone::takeCard(int position, int cardId, const QString &cardName) +{ + CardItem *result = CardZone::takeCard(position, cardId, cardName); + int xMax = 0; + for (int i = 0; i < cards.size(); ++i) + if (cards[i]->getGridPoint().x() > xMax) + xMax = cards[i]->getGridPoint().x(); + if (xMax < minWidth) + xMax = minWidth; + int newWidth = (xMax + 1) * CARD_WIDTH / 2 + 2 * marginX; + if (newWidth < width - 2 * CARD_WIDTH) { + width = newWidth; + emit sizeChanged(); + } + return result; +} + CardItem *TableZone::getCardFromGrid(const QPoint &gridPoint) const { for (int i = 0; i < cards.size(); i++) @@ -115,19 +156,26 @@ QPointF TableZone::mapFromGrid(const QPoint &gridPoint) const QPoint TableZone::mapToGrid(const QPointF &mapPoint) const { - qreal x = mapPoint.x() - 20; - qreal y = mapPoint.y(); + qreal x = mapPoint.x() - marginX; + qreal y = mapPoint.y() + paddingY / 2; if (x < 0) x = 0; - else if (x > width - CARD_WIDTH) - x = width - CARD_WIDTH; + 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()); - if (y >= (CARD_HEIGHT + paddingY) * 3 - paddingY / 2) { + 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( @@ -135,18 +183,14 @@ QPoint TableZone::mapToGrid(const QPointF &mapPoint) const 3 ); else { - qDebug(QString("mapX = %1").arg((int) round((double) x / (1.25 * CARD_WIDTH))).toLatin1()); return QPoint( - x / (1.5 * CARD_WIDTH), + round((double) x / (1.5 * CARD_WIDTH)), 3 ); } } else { qDebug("UEBER grenze"); - return QPoint( - x * 2 / CARD_WIDTH, - y / (CARD_HEIGHT + paddingY) - ); + return result; } } @@ -165,5 +209,5 @@ QPoint TableZone::getFreeGridPoint(int row) const QPointF TableZone::closestGridPoint(const QPointF &point) { - return mapFromGrid(mapToGrid(point)); + return mapFromGrid(mapToGrid(point + QPoint(CARD_WIDTH / 2, CARD_HEIGHT / 2))); } diff --git a/cockatrice/src/tablezone.h b/cockatrice/src/tablezone.h index 358a08e4..8a254ca5 100644 --- a/cockatrice/src/tablezone.h +++ b/cockatrice/src/tablezone.h @@ -3,13 +3,18 @@ #include "cardzone.h" -class TableZone : public CardZone { +class TableZone : public QObject, public CardZone { + Q_OBJECT +signals: + void sizeChanged(); private: int width, height; QPixmap bgPixmap; bool economicGrid; public: static const int paddingY = 20; + static const int marginX = 20; + static const int minWidth = 20; TableZone(Player *_p, QGraphicsItem *parent = 0); QRectF boundingRect() const; @@ -23,6 +28,7 @@ public: QPoint mapToGrid(const QPointF &mapPoint) const; QPoint getFreeGridPoint(int row) const; QPointF closestGridPoint(const QPointF &point); + CardItem *takeCard(int position, int cardId, const QString &cardName); protected: void addCardImpl(CardItem *card, int x, int y); }; diff --git a/cockatrice/src/zoneviewlayout.cpp b/cockatrice/src/zoneviewlayout.cpp index 0f27cea6..1d0649ff 100644 --- a/cockatrice/src/zoneviewlayout.cpp +++ b/cockatrice/src/zoneviewlayout.cpp @@ -6,7 +6,7 @@ ZoneViewLayout::ZoneViewLayout(CardDatabase *_db, QGraphicsItem *parent) : QGraphicsWidget(parent), db(_db) { - + resize(0, 0); } void ZoneViewLayout::reorganize()