arrows: clean all arrows when changing phase; arrow color dependent on modifier keys; remove a single arrow by rightclicking

This commit is contained in:
Max-Wilhelm Bruker 2009-10-21 11:02:02 +02:00
parent c57e138a78
commit 897dca2386
8 changed files with 58 additions and 11 deletions

View file

@ -7,8 +7,8 @@
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsScene>
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<QGraphicsItem *> colliding = scene()->items(event->scenePos());
for (int i = 0; i < colliding.size(); ++i)
if (qgraphicsitem_cast<CardItem *>(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<CardZone *>(_startItem->parentItem())->getPlayer(), -1, _startItem, 0, _color)
{
}
void ArrowDragItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
event->accept();
QPointF endPos = event->scenePos();
QList<QGraphicsItem *> colliding = scene()->items(endPos);
@ -92,7 +110,7 @@ void ArrowDragItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * /*event*/)
if (targetItem && (targetItem != startItem)) {
CardZone *startZone = static_cast<CardZone *>(startItem->parentItem());
CardZone *targetZone = static_cast<CardZone *>(targetItem->parentItem());
startZone->getPlayer()->client->createArrow(
player->client->createArrow(
startZone->getPlayer()->getId(),
startZone->getName(),
startItem->getId(),

View file

@ -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);

View file

@ -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 {

View file

@ -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));

View file

@ -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);

View file

@ -433,7 +433,6 @@ void Game::cardMenuAction()
QList<QGraphicsItem *> 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<CardItem *>(sel.takeAt(i));
// For each item, the handler function is called.
(this->*handler)(card);

View file

@ -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);
}

View file

@ -20,6 +20,7 @@
#include "server.h"
#include "servergame.h"
#include "serversocket.h"
#include "arrow.h"
#include <QSqlQuery>
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<int, Arrow *> 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);
}