From 0d774b5d484b2044bd476dac920cc1edbbae3656 Mon Sep 17 00:00:00 2001 From: Max-Wilhelm Bruker Date: Tue, 8 Sep 2009 17:56:20 +0200 Subject: [PATCH] table grid improvement, unfinished --- cockatrice/cockatrice.pro | 8 +- cockatrice/src/carditem.cpp | 2 +- cockatrice/src/game.cpp | 21 +++-- cockatrice/src/game.h | 8 +- cockatrice/src/gameview.cpp | 10 ++- cockatrice/src/gameview.h | 4 +- cockatrice/src/handzone.cpp | 6 +- cockatrice/src/handzone.h | 3 +- cockatrice/src/player.cpp | 140 ++++++++++++++++++++++++++++++--- cockatrice/src/player.h | 24 ++++-- cockatrice/src/playerarea.cpp | 135 ------------------------------- cockatrice/src/playerarea.h | 32 -------- cockatrice/src/tablezone.cpp | 92 +++++++++------------- cockatrice/src/tablezone.h | 5 +- cockatrice/src/window_main.cpp | 24 ++---- cockatrice/src/window_main.h | 6 +- 16 files changed, 231 insertions(+), 289 deletions(-) delete mode 100644 cockatrice/src/playerarea.cpp delete mode 100644 cockatrice/src/playerarea.h diff --git a/cockatrice/cockatrice.pro b/cockatrice/cockatrice.pro index c3cc92a7..2ab2a333 100644 --- a/cockatrice/cockatrice.pro +++ b/cockatrice/cockatrice.pro @@ -36,7 +36,6 @@ HEADERS += src/counter.h \ src/pilezone.h \ src/carddragitem.h \ src/zoneviewlayout.h \ - src/playerarea.h \ src/carddatabasemodel.h \ src/window_deckeditor.h \ src/decklist.h \ @@ -45,7 +44,8 @@ HEADERS += src/counter.h \ src/abstractgraphicsitem.h \ src/dlg_settings.h \ src/phasestoolbar.h \ - src/chatwidget.h + src/chatwidget.h \ + src/gamescene.h SOURCES += src/counter.cpp \ src/gameselector.cpp \ src/dlg_creategame.cpp \ @@ -75,7 +75,6 @@ SOURCES += src/counter.cpp \ src/pilezone.cpp \ src/carddragitem.cpp \ src/zoneviewlayout.cpp \ - src/playerarea.cpp \ src/carddatabasemodel.cpp \ src/window_deckeditor.cpp \ src/decklist.cpp \ @@ -84,5 +83,6 @@ SOURCES += src/counter.cpp \ src/abstractgraphicsitem.cpp \ src/dlg_settings.cpp \ src/phasestoolbar.cpp \ - src/chatwidget.cpp + src/chatwidget.cpp \ + src/gamescene.cpp TRANSLATIONS += translations/cockatrice_de.ts translations/cockatrice_en.ts diff --git a/cockatrice/src/carditem.cpp b/cockatrice/src/carditem.cpp index f0156008..1f69a8d8 100644 --- a/cockatrice/src/carditem.cpp +++ b/cockatrice/src/carditem.cpp @@ -241,7 +241,7 @@ void CardItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) else { TableZone *table = (TableZone *) zone->getPlayer()->getZones()->findZone("table"); QPoint gridPoint = table->getFreeGridPoint(info->getTableRow()); - table->handleDropEvent(id, zone, table->mapFromGrid(gridPoint).toPoint(), false); + table->handleDropEventByGrid(id, zone, gridPoint, false); } } diff --git a/cockatrice/src/game.cpp b/cockatrice/src/game.cpp index be69f79b..b6150308 100644 --- a/cockatrice/src/game.cpp +++ b/cockatrice/src/game.cpp @@ -1,4 +1,3 @@ -#include #include #include #include @@ -11,14 +10,13 @@ #include "handzone.h" #include "carddatabase.h" #include "dlg_startgame.h" -#include "playerarea.h" #include "counter.h" +#include "gamescene.h" -Game::Game(CardDatabase *_db, Client *_client, QGraphicsScene *_scene, QMenu *_actionsMenu, QMenu *_cardMenu, int playerId, const QString &playerName, QObject *parent) +Game::Game(CardDatabase *_db, Client *_client, GameScene *_scene, QMenu *_actionsMenu, QMenu *_cardMenu, int playerId, const QString &playerName, QObject *parent) : QObject(parent), actionsMenu(_actionsMenu), cardMenu(_cardMenu), db(_db), client(_client), scene(_scene), started(false), currentPhase(-1) { - QRectF sr = scene->sceneRect(); - localPlayer = addPlayer(playerId, playerName, QPointF(0, sr.y() + sr.height() / 2 + 2), true); + localPlayer = addPlayer(playerId, playerName, true); connect(client, SIGNAL(gameEvent(const ServerEventData &)), this, SLOT(gameEvent(const ServerEventData &))); connect(client, SIGNAL(playerListReceived(QList)), this, SLOT(playerListReceived(QList))); @@ -186,9 +184,10 @@ void Game::initSayMenu() } } -Player *Game::addPlayer(int playerId, const QString &playerName, QPointF base, bool local) +Player *Game::addPlayer(int playerId, const QString &playerName, bool local) { - Player *newPlayer = new Player(playerName, playerId, base, local, db, client, scene, this); + Player *newPlayer = new Player(playerName, playerId, local, db, client, this); + scene->addPlayer(newPlayer); connect(newPlayer, SIGNAL(sigShowCardMenu(QPoint)), this, SLOT(showCardMenu(QPoint))); connect(newPlayer, SIGNAL(logMoveCard(Player *, QString, CardZone *, int, CardZone *, int)), this, SIGNAL(logMoveCard(Player *, QString, CardZone *, int, CardZone *, int))); @@ -200,7 +199,7 @@ Player *Game::addPlayer(int playerId, const QString &playerName, QPointF base, b players << newPlayer; emit playerAdded(newPlayer); - + return newPlayer; } @@ -214,7 +213,7 @@ void Game::playerListReceived(QList playerList) int id = temp->getPlayerId(); if (id != localPlayer->getId()) - addPlayer(id, temp->getName(), QPointF(0, 0), false); + addPlayer(id, temp->getName(), false); delete temp; } @@ -248,7 +247,7 @@ void Game::gameEvent(const ServerEventData &msg) emit logSay(p, msg.getEventData()[0]); break; case eventJoin: { - Player *newPlayer = addPlayer(msg.getPlayerId(), msg.getPlayerName(), QPointF(0, 0), false); + Player *newPlayer = addPlayer(msg.getPlayerId(), msg.getPlayerName(), false); emit logJoin(newPlayer); break; } @@ -387,7 +386,7 @@ void Game::actDecLife() void Game::actSetLife() { bool ok; - int life = QInputDialog::getInteger(0, tr("Set life"), tr("New life total:"), localPlayer->area->getCounter("life")->getValue(), 0, 2000000000, 1, &ok); + int life = QInputDialog::getInteger(0, tr("Set life"), tr("New life total:"), localPlayer->getCounter("life")->getValue(), 0, 2000000000, 1, &ok); if (ok) client->setCounter("life", life); } diff --git a/cockatrice/src/game.h b/cockatrice/src/game.h index e2af69a5..210cfb1a 100644 --- a/cockatrice/src/game.h +++ b/cockatrice/src/game.h @@ -5,7 +5,7 @@ #include "playerlist.h" class ServerPlayer; -class QGraphicsScene; +class GameScene; class Player; class Client; class ServerEventData; @@ -29,12 +29,12 @@ private: CardDatabase *db; Client *client; - QGraphicsScene *scene; + GameScene *scene; PlayerList players; Player *localPlayer; bool started; int currentPhase; - Player *addPlayer(int playerId, const QString &playerName, QPointF base, bool local); + Player *addPlayer(int playerId, const QString &playerName, bool local); void initSayMenu(); public slots: void actNextPhase(); @@ -93,7 +93,7 @@ signals: void logSetActivePlayer(Player *player); void setActivePhase(int phase); public: - Game(CardDatabase *_db, Client *_client, QGraphicsScene *_scene, QMenu *_actionsMenu, QMenu *_cardMenu, int playerId, const QString &playerName, QObject *parent = 0); + Game(CardDatabase *_db, Client *_client, GameScene *_scene, QMenu *_actionsMenu, QMenu *_cardMenu, int playerId, const QString &playerName, QObject *parent = 0); ~Game(); Player *getLocalPlayer() const { return localPlayer; } void retranslateUi(); diff --git a/cockatrice/src/gameview.cpp b/cockatrice/src/gameview.cpp index 78b56259..af1c855a 100644 --- a/cockatrice/src/gameview.cpp +++ b/cockatrice/src/gameview.cpp @@ -7,10 +7,18 @@ GameView::GameView(QGraphicsScene *scene, QWidget *parent) setRenderHints(QPainter::TextAntialiasing | QPainter::Antialiasing/* | QPainter::SmoothPixmapTransform*/); setDragMode(RubberBandDrag); // setViewportUpdateMode(FullViewportUpdate); + + connect(scene, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(updateSceneRect(const QRectF &))); } void GameView::resizeEvent(QResizeEvent *event) { QGraphicsView::resizeEvent(event); - fitInView(scene()->sceneRect(), Qt::KeepAspectRatio); + updateSceneRect(scene()->sceneRect()); +} + +void GameView::updateSceneRect(const QRectF &rect) +{ + qDebug(QString("updateSceneRect = %1,%2").arg(rect.width()).arg(rect.height()).toLatin1()); + fitInView(rect, Qt::KeepAspectRatio); } diff --git a/cockatrice/src/gameview.h b/cockatrice/src/gameview.h index e3783caf..5bb06afe 100644 --- a/cockatrice/src/gameview.h +++ b/cockatrice/src/gameview.h @@ -4,10 +4,12 @@ #include class GameView : public QGraphicsView { + Q_OBJECT protected: void resizeEvent(QResizeEvent *event); +public slots: + void updateSceneRect(const QRectF &rect); public: - void scaleToScene(); GameView(QGraphicsScene *scene, QWidget *parent = 0); }; diff --git a/cockatrice/src/handzone.cpp b/cockatrice/src/handzone.cpp index 18885c39..66bb5032 100644 --- a/cockatrice/src/handzone.cpp +++ b/cockatrice/src/handzone.cpp @@ -3,8 +3,8 @@ #include "player.h" #include "client.h" -HandZone::HandZone(Player *_p, QGraphicsItem *parent) - : CardZone(_p, "hand", false, false, _p->getLocal(), parent) +HandZone::HandZone(Player *_p, int _zoneHeight, QGraphicsItem *parent) + : CardZone(_p, "hand", false, false, _p->getLocal(), parent), zoneHeight(_zoneHeight) { QSettings settings; QString bgPath = settings.value("zonebg/hand").toString(); @@ -17,7 +17,7 @@ HandZone::HandZone(Player *_p, QGraphicsItem *parent) QRectF HandZone::boundingRect() const { - return QRectF(0, 0, 100, 578); + return QRectF(0, 0, 100, zoneHeight); } void HandZone::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/) diff --git a/cockatrice/src/handzone.h b/cockatrice/src/handzone.h index 38057ea4..06fe9907 100644 --- a/cockatrice/src/handzone.h +++ b/cockatrice/src/handzone.h @@ -6,8 +6,9 @@ class HandZone : public CardZone { private: QPixmap bgPixmap; + int zoneHeight; public: - HandZone(Player *_p, QGraphicsItem *parent = 0); + HandZone(Player *_p, int _zoneHeight, QGraphicsItem *parent = 0); QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); void reorganizeCards(); diff --git a/cockatrice/src/player.cpp b/cockatrice/src/player.cpp index 942d1d28..bb801436 100644 --- a/cockatrice/src/player.cpp +++ b/cockatrice/src/player.cpp @@ -1,20 +1,51 @@ #include "player.h" #include "client.h" #include "cardzone.h" -#include "playerarea.h" #include "counter.h" #include "zoneviewzone.h" #include "zoneviewwidget.h" #include "game.h" -#include +#include "pilezone.h" +#include "tablezone.h" +#include "handzone.h" +#include +#include #include -Player::Player(const QString &_name, int _id, QPointF _base, bool _local, CardDatabase *_db, Client *_client, QGraphicsScene *_scene, Game *_parent) - : QObject(_parent), defaultNumberTopCards(3), name(_name), id(_id), active(false), base(_base), local(_local), db(_db), client(_client) +Player::Player(const QString &_name, int _id, bool _local, CardDatabase *_db, Client *_client, Game *_parent) + : QObject(_parent), defaultNumberTopCards(3), name(_name), id(_id), active(false), local(_local), db(_db), client(_client) { - area = new PlayerArea(this); - area->setPos(_base); - _scene->addItem(area); + QSettings settings; + QString bgPath = settings.value("zonebg/playerarea").toString(); + if (!bgPath.isEmpty()) + bgPixmap.load(bgPath); + setCacheMode(DeviceCoordinateCache); + + QPointF base = QPointF(55, 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); + + CardZone *table = new TableZone(this, this); + CardZone *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()); if (local) { aMoveHandToTopLibrary = new QAction(this); @@ -93,7 +124,7 @@ Player::~Player() for (int i = 0; i < zones.size(); i++) delete zones.at(i); - delete area; + clearCounters(); } void Player::retranslateUi() @@ -230,7 +261,7 @@ void Player::gameEvent(const ServerEventData &event) zones.at(i)->clearContents(); } - area->clearCounters(); + clearCounters(); CardZone *deck = zones.findZone("deck"); for (; deck_cards; deck_cards--) @@ -359,7 +390,7 @@ void Player::gameEvent(const ServerEventData &event) int colorValue = data[1].toInt(); int value = data[2].toInt(); QColor color(colorValue / 65536, (colorValue % 65536) / 256, colorValue % 256); - area->addCounter(counterName, color, value); + addCounter(counterName, color, value); break; } case eventSetCounter: { @@ -368,7 +399,7 @@ void Player::gameEvent(const ServerEventData &event) } int value = data[1].toInt(); QString counterName = data[0]; - Counter *c = area->getCounter(counterName); + Counter *c = getCounter(counterName); int oldValue = c->getValue(); c->setValue(value); emit logSetCounter(this, c->getName(), value, oldValue); @@ -387,5 +418,90 @@ void Player::showCardMenu(const QPoint &p) void Player::setActive(bool _active) { active = _active; - area->update(); + 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 + 60; + + 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); +} + +Counter *Player::getCounter(const QString &name, bool remove) +{ + for (int i = 0; i < counterList.size(); i++) { + Counter *temp = counterList.at(i); + if (temp->getName() == name) { + if (remove) + counterList.removeAt(i); + return temp; + } + } + return 0; +} + +void Player::addCounter(const QString &name, QColor color, int value) +{ + counterList.append(new Counter(this, name, color, value, this)); + rearrangeCounters(); +} + +void Player::delCounter(const QString &name) +{ + delete getCounter(name, true); + rearrangeCounters(); +} + +void Player::clearCounters() +{ + for (int i = 0; i < counterList.size(); i++) + delete counterList.at(i); + counterList.clear(); +} + +void Player::rearrangeCounters() +{ + const int counterAreaWidth = 55; + qreal y = 50; + for (int i = 0; i < counterList.size(); i++) { + Counter *temp = counterList.at(i); + QRectF br = temp->boundingRect(); + temp->setPos((counterAreaWidth - br.width()) / 2, y); + y += br.height() + 10; + } } diff --git a/cockatrice/src/player.h b/cockatrice/src/player.h index 9a37d1ac..4d39af2a 100644 --- a/cockatrice/src/player.h +++ b/cockatrice/src/player.h @@ -10,11 +10,11 @@ class Client; class CardDatabase; class QMenu; class QAction; -class PlayerArea; class ZoneViewZone; class Game; +class Counter; -class Player : public QObject { +class Player : public QObject, public QGraphicsItem { Q_OBJECT signals: void moveCard(int cardId, QString startZone, QString targetZone, int x, int y); @@ -52,16 +52,30 @@ private: QString name; int id; bool active; - QPointF base; bool local; ZoneList zones; CardDatabase *db; void setCardAttrHelper(CardItem *card, const QString &aname, const QString &avalue, bool allCards); + + QPixmap bgPixmap; + QRectF bRect; + + QList counterList; + void rearrangeCounters(); public: - PlayerArea *area; + enum { Type = typeOther }; + int type() const { return Type; } + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + + Counter *getCounter(const QString &name, bool remove = false); + void addCounter(const QString &name, QColor color, int value); + void delCounter(const QString &name); + void clearCounters(); + Client *client; void addZone(CardZone *z); - Player(const QString &_name, int _id, QPointF _base, bool _local, CardDatabase *_db, Client *_client, QGraphicsScene *_scene, Game *_parent); + Player(const QString &_name, int _id, bool _local, CardDatabase *_db, Client *_client, Game *_parent); ~Player(); void retranslateUi(); QMenu *getPlayerMenu() const { return playerMenu; } diff --git a/cockatrice/src/playerarea.cpp b/cockatrice/src/playerarea.cpp deleted file mode 100644 index df2b43eb..00000000 --- a/cockatrice/src/playerarea.cpp +++ /dev/null @@ -1,135 +0,0 @@ -#include "playerarea.h" -#include "player.h" -#include "tablezone.h" -#include "handzone.h" -#include "pilezone.h" -#include "counter.h" -#include -#include - -PlayerArea::PlayerArea(Player *_player, QGraphicsItem *parent) - : QGraphicsItem(parent), player(_player) -{ - QSettings settings; - QString bgPath = settings.value("zonebg/playerarea").toString(); - if (!bgPath.isEmpty()) - bgPixmap.load(bgPath); - setCacheMode(DeviceCoordinateCache); - - QPointF base = QPointF(55, 50); - - PileZone *deck = new PileZone(_player, "deck", true, false, this); - deck->setPos(base); - - qreal h = deck->boundingRect().height() + 20; - - PileZone *grave = new PileZone(_player, "grave", false, true, this); - grave->setPos(base + QPointF(0, h)); - - PileZone *rfg = new PileZone(_player, "rfg", false, true, this); - rfg->setPos(base + QPointF(0, 2 * h)); - - PileZone *sb = new PileZone(_player, "sb", false, true, this); - sb->setVisible(false); - - base = QPointF(deck->boundingRect().width() + 60, 0); - - CardZone *hand = new HandZone(_player, this); - hand->setPos(base); - base += QPointF(hand->boundingRect().width(), 0); - - CardZone *table = new TableZone(_player, this); - table->setPos(base); - - bRect = QRectF(0, 0, base.x() + table->boundingRect().width(), base.y() + table->boundingRect().height()); -} - -PlayerArea::~PlayerArea() -{ - clearCounters(); -} - -QRectF PlayerArea::boundingRect() const -{ - return bRect; -} - -void PlayerArea::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 = player->getName(); - QFont font("Times"); - font.setPixelSize(20); -// font.setWeight(QFont::Bold); - - int totalWidth = CARD_WIDTH + 60; - - if (player->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); -} - -Counter *PlayerArea::getCounter(const QString &name, bool remove) -{ - for (int i = 0; i < counterList.size(); i++) { - Counter *temp = counterList.at(i); - if (temp->getName() == name) { - if (remove) - counterList.removeAt(i); - return temp; - } - } - return 0; -} - -void PlayerArea::addCounter(const QString &name, QColor color, int value) -{ - counterList.append(new Counter(player, name, color, value, this)); - rearrangeCounters(); -} - -void PlayerArea::delCounter(const QString &name) -{ - delete getCounter(name, true); - rearrangeCounters(); -} - -void PlayerArea::clearCounters() -{ - for (int i = 0; i < counterList.size(); i++) - delete counterList.at(i); - counterList.clear(); -} - -void PlayerArea::rearrangeCounters() -{ - const int counterAreaWidth = 55; - qreal y = 50; - for (int i = 0; i < counterList.size(); i++) { - Counter *temp = counterList.at(i); - QRectF br = temp->boundingRect(); - temp->setPos((counterAreaWidth - br.width()) / 2, y); - y += br.height() + 10; - } -} diff --git a/cockatrice/src/playerarea.h b/cockatrice/src/playerarea.h deleted file mode 100644 index efd98b3b..00000000 --- a/cockatrice/src/playerarea.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef PLAYERAREA_H -#define PLAYERAREA_H - -#include -#include "carditem.h" - -class Player; -class Counter; - -class PlayerArea : public QGraphicsItem { -private: - QPixmap bgPixmap; - QRectF bRect; - Player *player; - QList counterList; - - void rearrangeCounters(); -public: - enum { Type = typeOther }; - int type() const { return Type; } - PlayerArea(Player *_player, QGraphicsItem *parent = 0); - ~PlayerArea(); - QRectF boundingRect() const; - void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); - - Counter *getCounter(const QString &name, bool remove = false); - void addCounter(const QString &name, QColor color, int value); - void delCounter(const QString &name); - void clearCounters(); -}; - -#endif diff --git a/cockatrice/src/tablezone.cpp b/cockatrice/src/tablezone.cpp index 1d71e97d..0eeee2fc 100644 --- a/cockatrice/src/tablezone.cpp +++ b/cockatrice/src/tablezone.cpp @@ -4,56 +4,15 @@ #include "client.h" TableZone::TableZone(Player *_p, QGraphicsItem *parent) - : CardZone(_p, "table", true, false, true, parent), width(864), height(578) + : CardZone(_p, "table", true, false, true, parent), width(864), height(536) { - gridPoints << (QList() << QPoint(8, 12) - << QPoint(9, 13) - << QPoint(10, 14) - << QPoint(12, 12) - << QPoint(13, 13) - << QPoint(14, 14) - << QPoint(4, 12) - << QPoint(5, 13) - << QPoint(6, 14) - << QPoint(16, 12) - << QPoint(17, 13) - << QPoint(18, 14) - << QPoint(0, 12) - << QPoint(1, 13) - << QPoint(2, 14) - << QPoint(20, 12) - << QPoint(21, 13) - << QPoint(22, 14)) - << (QList() << QPoint(10, 8) - << QPoint(13, 8) - << QPoint(7, 8) - << QPoint(16, 8) - << QPoint(4, 8) - << QPoint(19, 8) - << QPoint(1, 8) - << QPoint(22, 8)) - << (QList() << QPoint(10, 4) - << QPoint(13, 4) - << QPoint(7, 4) - << QPoint(16, 4) - << QPoint(4, 4) - << QPoint(19, 4) - << QPoint(1, 4) - << QPoint(22, 4)) - << (QList() << QPoint(10, 0) - << QPoint(13, 0) - << QPoint(7, 0) - << QPoint(16, 0) - << QPoint(4, 0) - << QPoint(19, 0) - << QPoint(1, 0) - << QPoint(22, 0)); QSettings settings; QString bgPath = settings.value("zonebg/table").toString(); if (!bgPath.isEmpty()) bgPixmap.load(bgPath); setCacheMode(DeviceCoordinateCache); + setAcceptsHoverEvents(true); } QRectF TableZone::boundingRect() const @@ -90,7 +49,11 @@ void TableZone::addCardImpl(CardItem *card, int _x, int _y) void TableZone::handleDropEvent(int cardId, CardZone *startZone, const QPoint &dropPoint, bool faceDown) { - QPoint gridPoint = mapToGrid(dropPoint); + 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); } @@ -124,13 +87,21 @@ CardItem *TableZone::getCardFromGrid(const QPoint &gridPoint) const QPointF TableZone::mapFromGrid(const QPoint &gridPoint) const { - return QPointF(gridPoint.x() * CARD_WIDTH / gridPointsPerCardX, - gridPoint.y() * CARD_HEIGHT / gridPointsPerCardY); + if (gridPoint.y() == 3) + return QPointF( + 20 + (CARD_WIDTH * gridPoint.x() + CARD_WIDTH * (gridPoint.x() / 3)) / gridPointsPerCardX, + (CARD_HEIGHT + paddingY) * gridPoint.y() / gridPointsPerCardY + (gridPoint.x() % 3 * CARD_HEIGHT) / 3 + ); + else + return QPointF( + 20 + CARD_WIDTH * gridPoint.x() / gridPointsPerCardX, + (CARD_HEIGHT + paddingY) * gridPoint.y() / gridPointsPerCardY + ); } QPoint TableZone::mapToGrid(const QPointF &mapPoint) const { - qreal x = mapPoint.x(); + qreal x = mapPoint.x() - 20; qreal y = mapPoint.y(); if (x < 0) x = 0; @@ -141,16 +112,27 @@ QPoint TableZone::mapToGrid(const QPointF &mapPoint) const else if (y > height - CARD_HEIGHT) y = height - CARD_HEIGHT; - return QPoint((int) round(((double) x * gridPointsPerCardX) / CARD_WIDTH), (int) round(((double) y * gridPointsPerCardY) / CARD_HEIGHT)); + if (y >= (CARD_HEIGHT + paddingY) * 3 - 1) + return QPoint( + (int) round(((double) x * gridPointsPerCardX) / CARD_WIDTH - x / (2 * CARD_WIDTH)), + 3 + ); + else + return QPoint( + (int) round(((double) x * gridPointsPerCardX) / CARD_WIDTH), + (int) round(((double) y * gridPointsPerCardY) / (CARD_HEIGHT + paddingY)) + ); } QPoint TableZone::getFreeGridPoint(int row) const { - Q_ASSERT(row < gridPoints.size()); - - QList pointList = gridPoints[row]; - for (int i = 0; i < pointList.size(); i++) - if (!getCardFromGrid(pointList[i])) - return pointList[i]; - return QPoint(0, 0); + 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); } diff --git a/cockatrice/src/tablezone.h b/cockatrice/src/tablezone.h index ec06a645..b7b05940 100644 --- a/cockatrice/src/tablezone.h +++ b/cockatrice/src/tablezone.h @@ -6,11 +6,11 @@ class TableZone : public CardZone { private: int width, height; - QList > gridPoints; QPixmap bgPixmap; public: static const int gridPointsPerCardX = 2; - static const int gridPointsPerCardY = 3; + static const int gridPointsPerCardY = 1; + static const int paddingY = 20; TableZone(Player *_p, QGraphicsItem *parent = 0); QRectF boundingRect() const; @@ -18,6 +18,7 @@ public: void reorganizeCards(); void toggleTapped(); void handleDropEvent(int cardId, CardZone *startZone, const QPoint &dropPoint, bool faceDown); + void handleDropEventByGrid(int cardId, CardZone *startZone, const QPoint &gridPoint, bool faceDown); CardItem *getCardFromGrid(const QPoint &gridPoint) const; QPointF mapFromGrid(const QPoint &gridPoint) const; QPoint mapToGrid(const QPointF &mapPoint) const; diff --git a/cockatrice/src/window_main.cpp b/cockatrice/src/window_main.cpp index b1e3bb6e..a055ae98 100644 --- a/cockatrice/src/window_main.cpp +++ b/cockatrice/src/window_main.cpp @@ -29,6 +29,7 @@ #include "messagelogwidget.h" #include "phasestoolbar.h" #include "gameview.h" +#include "gamescene.h" #include "player.h" #include "game.h" #include "carddatabase.h" @@ -154,15 +155,6 @@ void MainWindow::actExit() close(); } -void MainWindow::updateSceneSize() -{ - QRectF sr = scene->sceneRect(); - QSizeF zoneSize = zoneLayout->size(); - qDebug(QString("updateSceneSize: width=%1").arg(1096 + zoneSize.width()).toLatin1()); - scene->setSceneRect(sr.x(), sr.y(), 1096 + zoneSize.width(), sr.height()); - view->fitInView(scene->sceneRect(), Qt::KeepAspectRatio); -} - void MainWindow::actSay() { if (sayEdit->text().isEmpty()) @@ -178,6 +170,7 @@ void MainWindow::playerIdReceived(int id, QString name) connect(game, SIGNAL(hoverCard(QString)), this, SLOT(hoverCard(QString))); connect(game, SIGNAL(playerAdded(Player *)), this, SLOT(playerAdded(Player *))); connect(game, SIGNAL(playerRemoved(Player *)), this, SLOT(playerRemoved(Player *))); + connect(game, SIGNAL(playerRemoved(Player *)), scene, SLOT(removePlayer(Player *))); connect(game, SIGNAL(setActivePhase(int)), phasesToolbar, SLOT(setActivePhase(int))); connect(phasesToolbar, SIGNAL(signalUntapAll()), game, SLOT(actUntapAll())); playerAdded(game->getLocalPlayer()); @@ -281,16 +274,11 @@ MainWindow::MainWindow(QTranslator *_translator, QWidget *parent) db = new CardDatabase(this); - scene = new QGraphicsScene(0, 0, 1096, 1160, this); - view = new GameView(scene); - view->hide(); - -// view->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers))); - zoneLayout = new ZoneViewLayout(db); - zoneLayout->setPos(1096, 0); - scene->addItem(zoneLayout); - connect(zoneLayout, SIGNAL(sizeChanged()), this, SLOT(updateSceneSize())); + scene = new GameScene(zoneLayout, this); + view = new GameView(scene); +// view->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers))); + view->hide(); cardInfo = new CardInfoWidget(db); messageLog = new MessageLogWidget; diff --git a/cockatrice/src/window_main.h b/cockatrice/src/window_main.h index 39f821e5..7c2e2287 100644 --- a/cockatrice/src/window_main.h +++ b/cockatrice/src/window_main.h @@ -23,8 +23,8 @@ #include #include "client.h" -class QGraphicsScene; class GameView; +class GameScene; class Game; class CardDatabase; class Player; @@ -63,8 +63,6 @@ private slots: void actFullScreen(bool checked); void actSettings(); void actExit(); - - void updateSceneSize(); signals: void logConnecting(QString hostname); void logDisconnected(); @@ -86,7 +84,7 @@ private: ChatWidget *chatWidget; Client *client; - QGraphicsScene *scene; + GameScene *scene; GameView *view; Game *game; CardDatabase *db;