From 89fe7d2d6988fa4b759a4806d0a90e93c7fa8650 Mon Sep 17 00:00:00 2001 From: Max-Wilhelm Bruker Date: Wed, 24 Feb 2010 14:43:18 +0100 Subject: [PATCH] more beautiful arrows; fixed server bug regarding facedown cards --- cockatrice/src/arrowitem.cpp | 38 +++++++++++++++++++++++++++--------- cockatrice/src/carditem.cpp | 10 +++++++++- cockatrice/src/carditem.h | 2 ++ common/server_game.cpp | 3 ++- 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/cockatrice/src/arrowitem.cpp b/cockatrice/src/arrowitem.cpp index f8b70eb7..2bb1e3f2 100644 --- a/cockatrice/src/arrowitem.cpp +++ b/cockatrice/src/arrowitem.cpp @@ -27,23 +27,39 @@ void ArrowItem::updatePath(const QPointF &endPoint) const double arrowWidth = 15.0; const double headWidth = 40.0; const double headLength = headWidth / sqrt(2); + const double phi = 15; QPointF startPoint = startItem->mapToScene(QPointF(startItem->boundingRect().width() / 2, startItem->boundingRect().height() / 2)); QLineF line(startPoint, endPoint); qreal lineLength = line.length(); prepareGeometryChange(); - if (lineLength < headLength) + if (lineLength < 30) path = QPainterPath(); else { - path = QPainterPath(QPointF(0, -arrowWidth / 2)); - path.lineTo(0, arrowWidth / 2); - path.lineTo(lineLength - headLength, arrowWidth / 2); - path.lineTo(lineLength - headLength, headWidth / 2); - path.lineTo(lineLength, 0); - path.lineTo(lineLength - headLength, -headWidth / 2); - path.lineTo(lineLength - headLength, -arrowWidth / 2); - path.lineTo(0, -arrowWidth / 2); + QPointF c(lineLength / 2, tan(phi * M_PI / 180) * lineLength); + + QPainterPath centerLine; + centerLine.moveTo(0, 0); + centerLine.quadTo(c, QPointF(lineLength, 0)); + + double percentage = 1 - headLength / lineLength; + QPointF arrowBodyEndPoint = centerLine.pointAtPercent(percentage); + QLineF testLine(arrowBodyEndPoint, centerLine.pointAtPercent(percentage + 0.001)); + qreal alpha = testLine.angle() - 90; + QPointF endPoint1 = arrowBodyEndPoint + arrowWidth / 2 * QPointF(cos(alpha * M_PI / 180), -sin(alpha * M_PI / 180)); + QPointF endPoint2 = arrowBodyEndPoint + arrowWidth / 2 * QPointF(-cos(alpha * M_PI / 180), sin(alpha * M_PI / 180)); + QPointF point1 = endPoint1 + (headWidth - arrowWidth) / 2 * QPointF(cos(alpha * M_PI / 180), -sin(alpha * M_PI / 180)); + QPointF point2 = endPoint2 + (headWidth - arrowWidth) / 2 * QPointF(-cos(alpha * M_PI / 180), sin(alpha * M_PI / 180)); + + path = QPainterPath(-arrowWidth / 2 * QPointF(cos((phi - 90) * M_PI / 180), sin((phi - 90) * M_PI / 180))); + path.quadTo(c, endPoint1); + path.lineTo(point1); + path.lineTo(QPointF(lineLength, 0)); + path.lineTo(point2); + path.lineTo(endPoint2); + path.quadTo(c, arrowWidth / 2 * QPointF(cos((phi - 90) * M_PI / 180), sin((phi - 90) * M_PI / 180))); + path.lineTo(-arrowWidth / 2 * QPointF(cos((phi - 90) * M_PI / 180), sin((phi - 90) * M_PI / 180))); } setPos(startPoint); @@ -94,12 +110,15 @@ void ArrowDragItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) for (int i = colliding.size() - 1; i >= 0; i--) if ((cursorItem = qgraphicsitem_cast(colliding.at(i)))) break; + if ((cursorItem != targetItem) && targetItem) + targetItem->setBeingPointedAt(false); if (!cursorItem) { fullColor = false; targetItem = 0; updatePath(endPos); } else { fullColor = true; + cursorItem->setBeingPointedAt(true); targetItem = cursorItem; updatePath(); } @@ -109,6 +128,7 @@ void ArrowDragItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) void ArrowDragItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * /*event*/) { if (targetItem && (targetItem != startItem)) { + targetItem->setBeingPointedAt(false); CardZone *startZone = static_cast(startItem->parentItem()); CardZone *targetZone = static_cast(targetItem->parentItem()); player->sendGameCommand(new Command_CreateArrow( diff --git a/cockatrice/src/carditem.cpp b/cockatrice/src/carditem.cpp index c9825b97..b16058bf 100644 --- a/cockatrice/src/carditem.cpp +++ b/cockatrice/src/carditem.cpp @@ -11,7 +11,7 @@ #include "protocol_datastructures.h" CardItem::CardItem(Player *_owner, const QString &_name, int _cardid, QGraphicsItem *parent) - : AbstractCardItem(_name, parent), owner(_owner), id(_cardid), attacking(false), facedown(false), counters(0), doesntUntap(false), dragItem(NULL) + : AbstractCardItem(_name, parent), owner(_owner), id(_cardid), attacking(false), facedown(false), counters(0), doesntUntap(false), beingPointedAt(false), dragItem(NULL) { owner->addCard(this); } @@ -27,6 +27,8 @@ void CardItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, AbstractCardItem::paint(painter, option, widget); if (counters) paintNumberEllipse(counters, painter); + if (beingPointedAt) + painter->fillRect(boundingRect(), QBrush(QColor(255, 0, 0, 100))); painter->restore(); } @@ -61,6 +63,12 @@ void CardItem::setDoesntUntap(bool _doesntUntap) doesntUntap = _doesntUntap; } +void CardItem::setBeingPointedAt(bool _beingPointedAt) +{ + beingPointedAt = _beingPointedAt; + update(); +} + void CardItem::resetState() { attacking = false; diff --git a/cockatrice/src/carditem.h b/cockatrice/src/carditem.h index 0c53df09..49edd28e 100644 --- a/cockatrice/src/carditem.h +++ b/cockatrice/src/carditem.h @@ -22,6 +22,7 @@ private: QString annotation; bool doesntUntap; QPoint gridPoint; + bool beingPointedAt; CardDragItem *dragItem; public: enum { Type = typeCard }; @@ -44,6 +45,7 @@ public: void setAnnotation(const QString &_annotation); bool getDoesntUntap() const { return doesntUntap; } void setDoesntUntap(bool _doesntUntap); + void setBeingPointedAt(bool _beingPointedAt); void resetState(); void processCardInfo(ServerInfo_Card *info); diff --git a/common/server_game.cpp b/common/server_game.cpp index 851d9226..6b7a1f01 100644 --- a/common/server_game.cpp +++ b/common/server_game.cpp @@ -268,7 +268,8 @@ QList Server_Game::getGameState(Server_Player *playerWhosAs QListIterator cardIterator(zone->cards); while (cardIterator.hasNext()) { Server_Card *card = cardIterator.next(); - cardList.append(new ServerInfo_Card(card->getId(), card->getName(), card->getX(), card->getY(), card->getCounters(), card->getTapped(), card->getAttacking(), card->getAnnotation())); + QString displayedName = card->getFaceDown() ? QString() : card->getName(); + cardList.append(new ServerInfo_Card(card->getId(), displayedName, card->getX(), card->getY(), card->getCounters(), card->getTapped(), card->getAttacking(), card->getAnnotation())); } } zoneList.append(new ServerInfo_Zone(zone->getName(), zone->getType(), zone->hasCoords(), zone->cards.size(), cardList));