From 897dca2386d035ef17b3bc72c4af9e89128599c6 Mon Sep 17 00:00:00 2001 From: Max-Wilhelm Bruker Date: Wed, 21 Oct 2009 11:02:02 +0200 Subject: [PATCH] arrows: clean all arrows when changing phase; arrow color dependent on modifier keys; remove a single arrow by rightclicking --- cockatrice/src/arrowitem.cpp | 30 ++++++++++++++++++++++++------ cockatrice/src/arrowitem.h | 9 +++++++-- cockatrice/src/carditem.cpp | 11 ++++++++++- cockatrice/src/client.cpp | 5 +++++ cockatrice/src/client.h | 1 + cockatrice/src/game.cpp | 1 - cockatrice/src/player.cpp | 2 +- servatrice/src/servergame.cpp | 10 ++++++++++ 8 files changed, 58 insertions(+), 11 deletions(-) diff --git a/cockatrice/src/arrowitem.cpp b/cockatrice/src/arrowitem.cpp index b6346b4e..fb4a66a0 100644 --- a/cockatrice/src/arrowitem.cpp +++ b/cockatrice/src/arrowitem.cpp @@ -7,8 +7,8 @@ #include #include -ArrowItem::ArrowItem(int _id, CardItem *_startItem, CardItem *_targetItem, const QColor &_color) - : QGraphicsItem(), id(_id), startItem(_startItem), targetItem(_targetItem), color(_color), fullColor(true) +ArrowItem::ArrowItem(Player *_player, int _id, CardItem *_startItem, CardItem *_targetItem, const QColor &_color) + : QGraphicsItem(), player(_player), id(_id), startItem(_startItem), targetItem(_targetItem), color(_color), fullColor(true) { setZValue(2000000005); if (startItem && targetItem) @@ -60,14 +60,32 @@ void ArrowItem::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*opti painter->drawPath(path); } -ArrowDragItem::ArrowDragItem(CardItem *_startItem) - : ArrowItem(-1, _startItem) +void ArrowItem::mousePressEvent(QGraphicsSceneMouseEvent *event) +{ + if (!player->getLocal()) { + event->ignore(); + return; + } + + QList colliding = scene()->items(event->scenePos()); + for (int i = 0; i < colliding.size(); ++i) + if (qgraphicsitem_cast(colliding[i])) { + event->ignore(); + return; + } + + event->accept(); + if (event->button() == Qt::RightButton) + player->client->deleteArrow(id); +} + +ArrowDragItem::ArrowDragItem(CardItem *_startItem, const QColor &_color) + : ArrowItem(static_cast(_startItem->parentItem())->getPlayer(), -1, _startItem, 0, _color) { } void ArrowDragItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { - event->accept(); QPointF endPos = event->scenePos(); QList colliding = scene()->items(endPos); @@ -92,7 +110,7 @@ void ArrowDragItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * /*event*/) if (targetItem && (targetItem != startItem)) { CardZone *startZone = static_cast(startItem->parentItem()); CardZone *targetZone = static_cast(targetItem->parentItem()); - startZone->getPlayer()->client->createArrow( + player->client->createArrow( startZone->getPlayer()->getId(), startZone->getName(), startItem->getId(), diff --git a/cockatrice/src/arrowitem.h b/cockatrice/src/arrowitem.h index 7850a498..f6a250ae 100644 --- a/cockatrice/src/arrowitem.h +++ b/cockatrice/src/arrowitem.h @@ -5,18 +5,23 @@ class CardItem; class QGraphicsSceneMouseEvent; +class QMenu; +class Player; class ArrowItem : public QObject, public QGraphicsItem { Q_OBJECT private: QPainterPath path; + QMenu *menu; protected: + Player *player; int id; CardItem *startItem, *targetItem; QColor color; bool fullColor; + void mousePressEvent(QGraphicsSceneMouseEvent *event); public: - ArrowItem(int id, CardItem *_startItem = 0, CardItem *_targetItem = 0, const QColor &color = Qt::red); + ArrowItem(Player *_player, int _id, CardItem *_startItem, CardItem *_targetItem, const QColor &color); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); QRectF boundingRect() const { return path.boundingRect(); } void updatePath(); @@ -32,7 +37,7 @@ public: class ArrowDragItem : public ArrowItem { Q_OBJECT public: - ArrowDragItem(CardItem *_startItem); + ArrowDragItem(CardItem *_startItem, const QColor &_color); protected: void mouseMoveEvent(QGraphicsSceneMouseEvent *event); void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); diff --git a/cockatrice/src/carditem.cpp b/cockatrice/src/carditem.cpp index 0cc2d6c2..09b405c5 100644 --- a/cockatrice/src/carditem.cpp +++ b/cockatrice/src/carditem.cpp @@ -204,7 +204,16 @@ void CardItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) if (event->buttons().testFlag(Qt::RightButton)) { if ((event->screenPos() - event->buttonDownScreenPos(Qt::RightButton)).manhattanLength() < 2 * QApplication::startDragDistance()) return; - ArrowDragItem *arrow = new ArrowDragItem(this); + + 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; + + ArrowDragItem *arrow = new ArrowDragItem(this, arrowColor); scene()->addItem(arrow); arrow->grabMouse(); } else { diff --git a/cockatrice/src/client.cpp b/cockatrice/src/client.cpp index e333e574..9a4764bf 100644 --- a/cockatrice/src/client.cpp +++ b/cockatrice/src/client.cpp @@ -518,6 +518,11 @@ PendingCommand *Client::createArrow(int startPlayerId, const QString &startZone, return cmd(QString("create_arrow|%1|%2|%3|%4|%5|%6|%7").arg(startPlayerId).arg(startZone).arg(startCardId).arg(targetPlayerId).arg(targetPlayerZone).arg(targetCardId).arg(colorToNumber(color))); } +PendingCommand *Client::deleteArrow(int arrowId) +{ + return cmd(QString("delete_arrow|%1").arg(arrowId)); +} + PendingCommand *Client::setCardAttr(const QString &zone, int cardid, const QString &aname, const QString &avalue) { return cmd(QString("set_card_attr|%1|%2|%3|%4").arg(zone).arg(cardid).arg(aname).arg(avalue)); diff --git a/cockatrice/src/client.h b/cockatrice/src/client.h index 0590196f..b5dbc481 100644 --- a/cockatrice/src/client.h +++ b/cockatrice/src/client.h @@ -397,6 +397,7 @@ public slots: PendingCommand *moveCard(int cardid, const QString &startzone, const QString &targetzone, int x, int y = 0, bool faceDown = false); PendingCommand *createToken(const QString &zone, const QString &name, const QString &powtough, int x, int y); PendingCommand *createArrow(int startPlayerId, const QString &startZone, int startCardId, int targetPlayerId, const QString &targetPlayerZone, int targetCardId, const QColor &color); + PendingCommand *deleteArrow(int arrowId); PendingCommand *setCardAttr(const QString &zone, int cardid, const QString &aname, const QString &avalue); PendingCommand *readyStart(); PendingCommand *incCounter(int counterId, int delta); diff --git a/cockatrice/src/game.cpp b/cockatrice/src/game.cpp index 5f215a0a..727005fa 100644 --- a/cockatrice/src/game.cpp +++ b/cockatrice/src/game.cpp @@ -433,7 +433,6 @@ void Game::cardMenuAction() QList sel = scene->selectedItems(); while (!sel.isEmpty()) { unsigned int i = (unsigned int) (((double) sel.size()) * qrand() / (RAND_MAX + 1.0)); - qDebug(QString("%1 items left, i=%2").arg(sel.size()).arg(i).toLatin1()); CardItem *card = qgraphicsitem_cast(sel.takeAt(i)); // For each item, the handler function is called. (this->*handler)(card); diff --git a/cockatrice/src/player.cpp b/cockatrice/src/player.cpp index 68f0ef53..bf727c5d 100644 --- a/cockatrice/src/player.cpp +++ b/cockatrice/src/player.cpp @@ -725,7 +725,7 @@ void Player::clearCounters() void Player::addArrow(int arrowId, CardItem *startCard, CardItem *targetCard, const QColor &color) { - ArrowItem *arrow = new ArrowItem(arrowId, startCard, targetCard, color); + ArrowItem *arrow = new ArrowItem(this, arrowId, startCard, targetCard, color); arrows.insert(arrowId, arrow); scene()->addItem(arrow); } diff --git a/servatrice/src/servergame.cpp b/servatrice/src/servergame.cpp index 85321f18..f102e200 100644 --- a/servatrice/src/servergame.cpp +++ b/servatrice/src/servergame.cpp @@ -20,6 +20,7 @@ #include "server.h" #include "servergame.h" #include "serversocket.h" +#include "arrow.h" #include ServerGame::ServerGame(ServerSocket *_creator, int _gameId, const QString &_description, const QString &_password, int _maxPlayers, bool _spectatorsAllowed, QObject *parent) @@ -170,6 +171,15 @@ void ServerGame::setActivePlayer(int _activePlayer) void ServerGame::setActivePhase(int _activePhase) { + for (int i = 0; i < players.size(); ++i) { + QMapIterator arrowIterator(players[i]->getArrows()); + while (arrowIterator.hasNext()) { + Arrow *a = arrowIterator.next().value(); + broadcastEvent(QString("delete_arrow|%1").arg(a->getId()), players[i]); + players[i]->deleteArrow(a->getId()); + } + } + activePhase = _activePhase; broadcastEvent(QString("set_active_phase|%1").arg(_activePhase), NULL); }