reworked in-game avatar display

This commit is contained in:
Max-Wilhelm Bruker 2011-01-04 23:16:02 +01:00
parent 72223b3229
commit 02f2fb9764
15 changed files with 253 additions and 94 deletions

View file

@ -7,7 +7,8 @@ OBJECTS_DIR = build
RESOURCES = cockatrice.qrc RESOURCES = cockatrice.qrc
QT += network svg QT += network svg
HEADERS += src/counter.h \ HEADERS += src/abstractcounter.h \
src/counter_general.h \
src/dlg_creategame.h \ src/dlg_creategame.h \
src/dlg_connect.h \ src/dlg_connect.h \
src/dlg_create_token.h \ src/dlg_create_token.h \
@ -86,7 +87,8 @@ HEADERS += src/counter.h \
../common/server_protocolhandler.h \ ../common/server_protocolhandler.h \
../common/server_arrowtarget.h ../common/server_arrowtarget.h
SOURCES += src/counter.cpp \ SOURCES += src/abstractcounter.cpp \
src/counter_general.cpp \
src/dlg_creategame.cpp \ src/dlg_creategame.cpp \
src/dlg_connect.cpp \ src/dlg_connect.cpp \
src/dlg_create_token.cpp \ src/dlg_create_token.cpp \

View file

@ -11,7 +11,7 @@ class QTimer;
const int CARD_WIDTH = 72; const int CARD_WIDTH = 72;
const int CARD_HEIGHT = 102; const int CARD_HEIGHT = 102;
class AbstractCardItem : public QObject, public ArrowTarget { class AbstractCardItem : public ArrowTarget {
Q_OBJECT Q_OBJECT
protected: protected:
CardInfo *info; CardInfo *info;

View file

@ -1,16 +1,17 @@
#include "counter.h" #include "abstractcounter.h"
#include "player.h" #include "player.h"
#include "protocol_items.h" #include "protocol_items.h"
#include <QPainter> #include <QPainter>
#include <QMenu> #include <QMenu>
#include <QAction> #include <QAction>
#include <QGraphicsSceneMouseEvent> #include <QGraphicsSceneMouseEvent>
#include <QGraphicsSceneHoverEvent>
Counter::Counter(Player *_player, int _id, const QString &_name, QColor _color, int _radius, int _value, QGraphicsItem *parent) AbstractCounter::AbstractCounter(Player *_player, int _id, const QString &_name, bool _shownInCounterArea, int _value, QGraphicsItem *parent)
: QGraphicsItem(parent), player(_player), id(_id), name(_name), color(_color), radius(_radius), value(_value), aDec(0), aInc(0), dialogSemaphore(false), deleteAfterDialog(false) : QGraphicsItem(parent), player(_player), id(_id), name(_name), value(_value), hovered(false), aDec(0), aInc(0), dialogSemaphore(false), deleteAfterDialog(false), shownInCounterArea(_shownInCounterArea)
{ {
if (radius > Player::counterAreaWidth / 2) setAcceptsHoverEvents(true);
radius = Player::counterAreaWidth / 2;
if (player->getLocal()) { if (player->getLocal()) {
menu = new QMenu(name); menu = new QMenu(name);
aSet = new QAction(this); aSet = new QAction(this);
@ -36,12 +37,12 @@ Counter::Counter(Player *_player, int _id, const QString &_name, QColor _color,
retranslateUi(); retranslateUi();
} }
Counter::~Counter() AbstractCounter::~AbstractCounter()
{ {
delete menu; delete menu;
} }
void Counter::delCounter() void AbstractCounter::delCounter()
{ {
if (dialogSemaphore) if (dialogSemaphore)
deleteAfterDialog = true; deleteAfterDialog = true;
@ -49,14 +50,14 @@ void Counter::delCounter()
deleteLater(); deleteLater();
} }
void Counter::retranslateUi() void AbstractCounter::retranslateUi()
{ {
if (menu) { if (menu) {
aSet->setText(tr("&Set counter...")); aSet->setText(tr("&Set counter..."));
} }
} }
void Counter::setShortcutsActive() void AbstractCounter::setShortcutsActive()
{ {
if (name == "life") { if (name == "life") {
aSet->setShortcut(tr("Ctrl+L")); aSet->setShortcut(tr("Ctrl+L"));
@ -65,7 +66,7 @@ void Counter::setShortcutsActive()
} }
} }
void Counter::setShortcutsInactive() void AbstractCounter::setShortcutsInactive()
{ {
if (name == "life") { if (name == "life") {
aSet->setShortcut(QKeySequence()); aSet->setShortcut(QKeySequence());
@ -74,31 +75,13 @@ void Counter::setShortcutsInactive()
} }
} }
QRectF Counter::boundingRect() const void AbstractCounter::setValue(int _value)
{
return QRectF(0, 0, radius * 2, radius * 2);
}
void Counter::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
{
painter->setBrush(QBrush(color));
painter->drawEllipse(boundingRect());
if (value) {
QFont f("Serif");
f.setPixelSize(radius * 0.8);
f.setWeight(QFont::Bold);
painter->setFont(f);
painter->drawText(boundingRect(), Qt::AlignCenter, QString::number(value));
}
}
void Counter::setValue(int _value)
{ {
value = _value; value = _value;
update(); update();
} }
void Counter::mousePressEvent(QGraphicsSceneMouseEvent *event) void AbstractCounter::mousePressEvent(QGraphicsSceneMouseEvent *event)
{ {
if (event->button() == Qt::LeftButton) { if (event->button() == Qt::LeftButton) {
player->sendGameCommand(new Command_IncCounter(-1, id, 1)); player->sendGameCommand(new Command_IncCounter(-1, id, 1));
@ -114,13 +97,25 @@ void Counter::mousePressEvent(QGraphicsSceneMouseEvent *event)
event->ignore(); event->ignore();
} }
void Counter::incrementCounter() void AbstractCounter::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
hovered = true;
update();
}
void AbstractCounter::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
hovered = false;
update();
}
void AbstractCounter::incrementCounter()
{ {
int delta = static_cast<QAction *>(sender())->data().toInt(); int delta = static_cast<QAction *>(sender())->data().toInt();
player->sendGameCommand(new Command_IncCounter(-1, id, delta)); player->sendGameCommand(new Command_IncCounter(-1, id, delta));
} }
void Counter::setCounter() void AbstractCounter::setCounter()
{ {
bool ok; bool ok;
dialogSemaphore = true; dialogSemaphore = true;

View file

@ -7,35 +7,36 @@ class Player;
class QMenu; class QMenu;
class QAction; class QAction;
class Counter : public QObject, public QGraphicsItem { class AbstractCounter : public QObject, public QGraphicsItem {
Q_OBJECT Q_OBJECT
private: protected:
Player *player; Player *player;
int id; int id;
QString name; QString name;
QColor color;
int radius;
int value; int value;
bool hovered;
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
private:
QAction *aSet, *aDec, *aInc; QAction *aSet, *aDec, *aInc;
QMenu *menu; QMenu *menu;
bool dialogSemaphore, deleteAfterDialog; bool dialogSemaphore, deleteAfterDialog;
bool shownInCounterArea;
private slots: private slots:
void incrementCounter(); void incrementCounter();
void setCounter(); void setCounter();
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
public: public:
Counter(Player *_player, int _id, const QString &_name, QColor _color, int _radius, int _value, QGraphicsItem *parent = 0); AbstractCounter(Player *_player, int _id, const QString &_name, bool _shownInCounterArea, int _value, QGraphicsItem *parent = 0);
~Counter(); ~AbstractCounter();
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
QMenu *getMenu() const { return menu; } QMenu *getMenu() const { return menu; }
void retranslateUi(); void retranslateUi();
int getId() const { return id; } int getId() const { return id; }
QString getName() const { return name; } QString getName() const { return name; }
bool getShownInCounterArea() const { return shownInCounterArea; }
int getValue() const { return value; } int getValue() const { return value; }
void setValue(int _value); void setValue(int _value);
void delCounter(); void delCounter();

View file

@ -12,11 +12,12 @@ enum GraphicsItemType {
typeOther = QGraphicsItem::UserType + 6 typeOther = QGraphicsItem::UserType + 6
}; };
class AbstractGraphicsItem : public QGraphicsItem { class AbstractGraphicsItem : public QObject, public QGraphicsItem {
Q_OBJECT
protected: protected:
void paintNumberEllipse(int number, int radius, const QColor &color, int position, int count, QPainter *painter); void paintNumberEllipse(int number, int radius, const QColor &color, int position, int count, QPainter *painter);
public: public:
AbstractGraphicsItem(QGraphicsItem *parent = 0) : QGraphicsItem(parent) { } AbstractGraphicsItem(QGraphicsItem *parent = 0) : QObject(), QGraphicsItem(parent) { }
}; };
#endif #endif

View file

@ -13,7 +13,7 @@
ArrowItem::ArrowItem(Player *_player, int _id, ArrowTarget *_startItem, ArrowTarget *_targetItem, const QColor &_color) ArrowItem::ArrowItem(Player *_player, int _id, ArrowTarget *_startItem, ArrowTarget *_targetItem, const QColor &_color)
: QGraphicsItem(), player(_player), id(_id), startItem(_startItem), targetItem(_targetItem), color(_color), fullColor(true) : QGraphicsItem(), player(_player), id(_id), startItem(_startItem), targetItem(_targetItem), color(_color), fullColor(true)
{ {
qDebug() << "ArrowItem constructor: startItem=" << startItem; qDebug() << "ArrowItem constructor: startItem=" << static_cast<QGraphicsItem *>(startItem);
setZValue(2000000005); setZValue(2000000005);
if (startItem) if (startItem)

View file

@ -14,7 +14,7 @@ class QAction;
class QPainter; class QPainter;
class CardDragItem; class CardDragItem;
class CardZone : public QObject, public AbstractGraphicsItem { class CardZone : public AbstractGraphicsItem {
Q_OBJECT Q_OBJECT
protected: protected:
Player *player; Player *player;

View file

@ -0,0 +1,26 @@
#include "counter_general.h"
#include <QPainter>
GeneralCounter::GeneralCounter(Player *_player, int _id, const QString &_name, const QColor &_color, int _radius, int _value, QGraphicsItem *parent)
: AbstractCounter(_player, _id, _name, true, _value, parent), color(_color), radius(_radius)
{
}
QRectF GeneralCounter::boundingRect() const
{
return QRectF(0, 0, radius * 2, radius * 2);
}
void GeneralCounter::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
{
painter->setBrush(QBrush(color));
painter->drawEllipse(boundingRect());
if (value) {
QFont f("Serif");
f.setPixelSize(radius * 0.8);
f.setWeight(QFont::Bold);
painter->setFont(f);
painter->drawText(boundingRect(), Qt::AlignCenter, QString::number(value));
}
}

View file

@ -0,0 +1,17 @@
#ifndef COUNTER_GENERAL_H
#define COUNTER_GENERAL_H
#include "abstractcounter.h"
class GeneralCounter : public AbstractCounter {
Q_OBJECT
private:
QColor color;
int radius;
public:
GeneralCounter(Player *_player, int _id, const QString &_name, const QColor &_color, int _radius, int _value, QGraphicsItem *parent = 0);
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
};
#endif

View file

@ -7,7 +7,7 @@
class QPainter; class QPainter;
class QPixmap; class QPixmap;
class HandCounter : public QObject, public AbstractGraphicsItem { class HandCounter : public AbstractGraphicsItem {
Q_OBJECT Q_OBJECT
private: private:
int number; int number;

View file

@ -66,6 +66,9 @@ void HandZone::reorganizeCards()
const int xPadding = 5; const int xPadding = 5;
qreal totalWidth = boundingRect().width() - 2 * xPadding; qreal totalWidth = boundingRect().width() - 2 * xPadding;
qreal cardWidth = cards.at(0)->boundingRect().width(); qreal cardWidth = cards.at(0)->boundingRect().width();
if (cardWidth * cardCount < totalWidth)
cardWidth += 5;
for (int i = 0; i < cardCount; i++) { for (int i = 0; i < cardCount; i++) {
CardItem *c = cards.at(i); CardItem *c = cards.at(i);

View file

@ -1,7 +1,7 @@
#include "player.h" #include "player.h"
#include "cardzone.h" #include "cardzone.h"
#include "playertarget.h" #include "playertarget.h"
#include "counter.h" #include "counter_general.h"
#include "arrowitem.h" #include "arrowitem.h"
#include "zoneviewzone.h" #include "zoneviewzone.h"
#include "zoneviewwidget.h" #include "zoneviewwidget.h"
@ -32,26 +32,28 @@ Player::Player(ServerInfo_User *info, int _id, bool _local, TabGame *_parent)
updateBgPixmap(); updateBgPixmap();
playerTarget = new PlayerTarget(this); playerTarget = new PlayerTarget(this);
playerTarget->setPos(QPointF(counterAreaWidth + (CARD_HEIGHT + 5 - playerTarget->boundingRect().width()) / 2.0, 5)); qreal avatarMargin = (counterAreaWidth + CARD_HEIGHT + 15 - playerTarget->boundingRect().width()) / 2.0;
playerTarget->setPos(QPointF(avatarMargin, avatarMargin));
PileZone *deck = new PileZone(this, "deck", true, false, this); PileZone *deck = new PileZone(this, "deck", true, false, this);
QPointF base = QPointF(counterAreaWidth + (CARD_HEIGHT - CARD_WIDTH + 5) / 2.0, 5 + playerTarget->boundingRect().height() + 5 - (CARD_HEIGHT - CARD_WIDTH) / 2.0); QPointF base = QPointF(counterAreaWidth + (CARD_HEIGHT - CARD_WIDTH + 15) / 2.0, 10 + playerTarget->boundingRect().height() + 5 - (CARD_HEIGHT - CARD_WIDTH) / 2.0);
deck->setPos(base); deck->setPos(base);
qreal h = deck->boundingRect().width() + 5; qreal h = deck->boundingRect().width() + 5;
HandCounter *handCounter = new HandCounter(this);
handCounter->setPos(base + QPointF(0, h + 10));
qreal h2 = handCounter->boundingRect().height();
PileZone *grave = new PileZone(this, "grave", false, true, this); PileZone *grave = new PileZone(this, "grave", false, true, this);
grave->setPos(base + QPointF(0, h)); grave->setPos(base + QPointF(0, h + h2 + 10));
PileZone *rfg = new PileZone(this, "rfg", false, true, this); PileZone *rfg = new PileZone(this, "rfg", false, true, this);
rfg->setPos(base + QPointF(0, 2 * h)); rfg->setPos(base + QPointF(0, 2 * h + h2 + 10));
PileZone *sb = new PileZone(this, "sb", false, false, this); PileZone *sb = new PileZone(this, "sb", false, false, this);
sb->setVisible(false); sb->setVisible(false);
HandCounter *handCounter = new HandCounter(this);
handCounter->setPos(base + QPointF(0, 3 * h + 7));
table = new TableZone(this, this); table = new TableZone(this, this);
connect(table, SIGNAL(sizeChanged()), this, SLOT(updateBoundingRect())); connect(table, SIGNAL(sizeChanged()), this, SLOT(updateBoundingRect()));
@ -312,7 +314,7 @@ void Player::playerListActionTriggered()
void Player::rearrangeZones() void Player::rearrangeZones()
{ {
QPointF base = QPointF(CARD_HEIGHT + counterAreaWidth + 5, 0); QPointF base = QPointF(CARD_HEIGHT + counterAreaWidth + 15, 0);
if (settingsCache->getHorizontalHand()) { if (settingsCache->getHorizontalHand()) {
if (mirrored) { if (mirrored) {
@ -370,7 +372,7 @@ void Player::updateBgPixmap()
void Player::updateBoundingRect() void Player::updateBoundingRect()
{ {
prepareGeometryChange(); prepareGeometryChange();
qreal width = CARD_HEIGHT + 5 + counterAreaWidth + stack->boundingRect().width(); qreal width = CARD_HEIGHT + 15 + counterAreaWidth + stack->boundingRect().width();
if (settingsCache->getHorizontalHand()) { if (settingsCache->getHorizontalHand()) {
qreal handHeight = hand->isVisible() ? hand->boundingRect().height() : 0; qreal handHeight = hand->isVisible() ? hand->boundingRect().height() : 0;
bRect = QRectF(0, 0, width + table->boundingRect().width(), table->boundingRect().height() + handHeight); bRect = QRectF(0, 0, width + table->boundingRect().width(), table->boundingRect().height() + handHeight);
@ -426,7 +428,7 @@ void Player::retranslateUi()
aCreateAnotherToken->setText(tr("C&reate another token")); aCreateAnotherToken->setText(tr("C&reate another token"));
sayMenu->setTitle(tr("S&ay")); sayMenu->setTitle(tr("S&ay"));
QMapIterator<int, Counter *> counterIterator(counters); QMapIterator<int, AbstractCounter *> counterIterator(counters);
while (counterIterator.hasNext()) while (counterIterator.hasNext())
counterIterator.next().value()->retranslateUi(); counterIterator.next().value()->retranslateUi();
@ -457,7 +459,7 @@ void Player::setShortcutsActive()
aCreateToken->setShortcut(tr("Ctrl+T")); aCreateToken->setShortcut(tr("Ctrl+T"));
aCreateAnotherToken->setShortcut(tr("Ctrl+G")); aCreateAnotherToken->setShortcut(tr("Ctrl+G"));
QMapIterator<int, Counter *> counterIterator(counters); QMapIterator<int, AbstractCounter *> counterIterator(counters);
while (counterIterator.hasNext()) while (counterIterator.hasNext())
counterIterator.next().value()->setShortcutsActive(); counterIterator.next().value()->setShortcutsActive();
} }
@ -478,7 +480,7 @@ void Player::setShortcutsInactive()
aCreateToken->setShortcut(QKeySequence()); aCreateToken->setShortcut(QKeySequence());
aCreateAnotherToken->setShortcut(QKeySequence()); aCreateAnotherToken->setShortcut(QKeySequence());
QMapIterator<int, Counter *> counterIterator(counters); QMapIterator<int, AbstractCounter *> counterIterator(counters);
while (counterIterator.hasNext()) while (counterIterator.hasNext())
counterIterator.next().value()->setShortcutsInactive(); counterIterator.next().value()->setShortcutsInactive();
} }
@ -752,7 +754,7 @@ void Player::eventCreateCounters(Event_CreateCounters *event)
void Player::eventSetCounter(Event_SetCounter *event) void Player::eventSetCounter(Event_SetCounter *event)
{ {
Counter *c = counters.value(event->getCounterId(), 0); AbstractCounter *c = counters.value(event->getCounterId(), 0);
if (!c) if (!c)
return; return;
int oldValue = c->getValue(); int oldValue = c->getValue();
@ -1011,7 +1013,7 @@ QRectF Player::boundingRect() const
void Player::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/) void Player::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
{ {
int totalWidth = CARD_HEIGHT + counterAreaWidth + 5; int totalWidth = CARD_HEIGHT + counterAreaWidth + 15;
if (bgPixmap.isNull()) if (bgPixmap.isNull())
painter->fillRect(QRectF(0, 0, totalWidth, boundingRect().height()), QColor(200, 200, 200)); painter->fillRect(QRectF(0, 0, totalWidth, boundingRect().height()), QColor(200, 200, 200));
else else
@ -1111,14 +1113,22 @@ void Player::addZone(CardZone *z)
zones.insert(z->getName(), z); zones.insert(z->getName(), z);
} }
Counter *Player::addCounter(ServerInfo_Counter *counter) AbstractCounter *Player::addCounter(ServerInfo_Counter *counter)
{ {
return addCounter(counter->getId(), counter->getName(), counter->getColor().getQColor(), counter->getRadius(), counter->getCount()); return addCounter(counter->getId(), counter->getName(), counter->getColor().getQColor(), counter->getRadius(), counter->getCount());
} }
Counter *Player::addCounter(int counterId, const QString &name, QColor color, int radius, int value) AbstractCounter *Player::addCounter(int counterId, const QString &name, QColor color, int radius, int value)
{ {
Counter *c = new Counter(this, counterId, name, color, radius, value, this); qDebug() << "addCounter:" << getName() << counterId << name;
if (counters.contains(counterId))
return 0;
AbstractCounter *c;
if (name == "life")
c = playerTarget->addCounter(counterId, name, value);
else
c = new GeneralCounter(this, counterId, name, color, radius, value, this);
counters.insert(counterId, c); counters.insert(counterId, c);
if (countersMenu) if (countersMenu)
countersMenu->addMenu(c->getMenu()); countersMenu->addMenu(c->getMenu());
@ -1130,9 +1140,11 @@ Counter *Player::addCounter(int counterId, const QString &name, QColor color, in
void Player::delCounter(int counterId) void Player::delCounter(int counterId)
{ {
Counter *c = counters.value(counterId, 0); AbstractCounter *c = counters.value(counterId, 0);
if (!c) if (!c)
return; return;
if (c->getName() == "life")
playerTarget->delCounter();
counters.remove(counterId); counters.remove(counterId);
c->delCounter(); c->delCounter();
rearrangeCounters(); rearrangeCounters();
@ -1140,10 +1152,11 @@ void Player::delCounter(int counterId)
void Player::clearCounters() void Player::clearCounters()
{ {
QMapIterator<int, Counter *> counterIterator(counters); QMapIterator<int, AbstractCounter *> counterIterator(counters);
while (counterIterator.hasNext()) while (counterIterator.hasNext())
counterIterator.next().value()->delCounter(); counterIterator.next().value()->delCounter();
counters.clear(); counters.clear();
playerTarget->delCounter();
} }
ArrowItem *Player::addArrow(ServerInfo_Arrow *arrow) ArrowItem *Player::addArrow(ServerInfo_Arrow *arrow)
@ -1204,15 +1217,16 @@ void Player::clearArrows()
void Player::rearrangeCounters() void Player::rearrangeCounters()
{ {
qreal marginTop = 15; qreal marginTop = 80;
qreal marginBottom = 15; qreal marginBottom = 10;
// Determine total height of bounding rectangles // Determine total height of bounding rectangles
qreal totalHeight = 0; qreal totalHeight = 0;
QMapIterator<int, Counter *> counterIterator(counters); QMapIterator<int, AbstractCounter *> counterIterator(counters);
while (counterIterator.hasNext()) { while (counterIterator.hasNext()) {
counterIterator.next(); counterIterator.next();
totalHeight += counterIterator.value()->boundingRect().height(); if (counterIterator.value()->getShownInCounterArea())
totalHeight += counterIterator.value()->boundingRect().height();
} }
// Determine free space between objects // Determine free space between objects
@ -1226,8 +1240,11 @@ void Player::rearrangeCounters()
// Place objects // Place objects
for (counterIterator.toFront(); counterIterator.hasNext(); ) { for (counterIterator.toFront(); counterIterator.hasNext(); ) {
Counter *c = counterIterator.next().value(); AbstractCounter *c = counterIterator.next().value();
if (!c->getShownInCounterArea())
continue;
QRectF br = c->boundingRect(); QRectF br = c->boundingRect();
c->setPos((counterAreaWidth - br.width()) / 2, y); c->setPos((counterAreaWidth - br.width()) / 2, y);
y += br.height() + padding; y += br.height() + padding;
@ -1431,7 +1448,7 @@ QString Player::getName() const
qreal Player::getMinimumWidth() const qreal Player::getMinimumWidth() const
{ {
qreal result = table->getMinimumWidth() + CARD_HEIGHT + 5 + counterAreaWidth + stack->boundingRect().width(); qreal result = table->getMinimumWidth() + CARD_HEIGHT + 15 + counterAreaWidth + stack->boundingRect().width();
if (!settingsCache->getHorizontalHand()) if (!settingsCache->getHorizontalHand())
result += hand->boundingRect().width(); result += hand->boundingRect().width();
return result; return result;
@ -1450,7 +1467,7 @@ void Player::processSceneSizeChange(const QSizeF &newSize)
// This will need to be changed if player areas are displayed side by side (e.g. 2x2 for a 4-player game) // This will need to be changed if player areas are displayed side by side (e.g. 2x2 for a 4-player game)
qreal fullPlayerWidth = newSize.width(); qreal fullPlayerWidth = newSize.width();
qreal tableWidth = fullPlayerWidth - CARD_HEIGHT - 5 - counterAreaWidth - stack->boundingRect().width(); qreal tableWidth = fullPlayerWidth - CARD_HEIGHT - 15 - counterAreaWidth - stack->boundingRect().width();
if (!settingsCache->getHorizontalHand()) if (!settingsCache->getHorizontalHand())
tableWidth -= hand->boundingRect().width(); tableWidth -= hand->boundingRect().width();

View file

@ -11,7 +11,7 @@ class QMenu;
class QAction; class QAction;
class ZoneViewZone; class ZoneViewZone;
class TabGame; class TabGame;
class Counter; class AbstractCounter;
class ArrowItem; class ArrowItem;
class CardZone; class CardZone;
class StackZone; class StackZone;
@ -148,7 +148,7 @@ private:
QPixmap bgPixmap; QPixmap bgPixmap;
QRectF bRect; QRectF bRect;
QMap<int, Counter *> counters; QMap<int, AbstractCounter *> counters;
QMap<int, ArrowItem *> arrows; QMap<int, ArrowItem *> arrows;
void rearrangeCounters(); void rearrangeCounters();
@ -174,7 +174,7 @@ private:
void eventDrawCards(Event_DrawCards *event); void eventDrawCards(Event_DrawCards *event);
void eventRevealCards(Event_RevealCards *event); void eventRevealCards(Event_RevealCards *event);
public: public:
static const int counterAreaWidth = 65; static const int counterAreaWidth = 55;
enum { Type = typeOther }; enum { Type = typeOther };
int type() const { return Type; } int type() const { return Type; }
@ -186,8 +186,8 @@ public:
void deleteCard(CardItem *c); void deleteCard(CardItem *c);
void addZone(CardZone *z); void addZone(CardZone *z);
Counter *addCounter(ServerInfo_Counter *counter); AbstractCounter *addCounter(ServerInfo_Counter *counter);
Counter *addCounter(int counterId, const QString &name, QColor color, int radius, int value); AbstractCounter *addCounter(int counterId, const QString &name, QColor color, int radius, int value);
void delCounter(int counterId); void delCounter(int counterId);
void clearCounters(); void clearCounters();

View file

@ -5,9 +5,49 @@
#include <QPainter> #include <QPainter>
#include <QPixmapCache> #include <QPixmapCache>
#include <QDebug> #include <QDebug>
#include <math.h>
PlayerCounter::PlayerCounter(Player *_player, int _id, const QString &_name, int _value, QGraphicsItem *parent)
: AbstractCounter(_player, _id, _name, false, _value, parent)
{
}
QRectF PlayerCounter::boundingRect() const
{
return QRectF(0, 0, 50, 30);
}
void PlayerCounter::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/)
{
const int radius = 8;
const qreal border = 1;
QPainterPath path(QPointF(50 - border / 2, border / 2));
path.lineTo(radius, border / 2);
path.arcTo(border / 2, border / 2, 2 * radius, 2 * radius, 90, 90);
path.lineTo(border / 2, 30 - border / 2);
path.lineTo(50 - border / 2, 30 - border / 2);
path.closeSubpath();
QPen pen(QColor(100, 100, 100));
pen.setWidth(border);
painter->setPen(pen);
painter->setBrush(hovered ? QColor(50, 50, 50, 160) : QColor(0, 0, 0, 160));
painter->drawPath(path);
QRectF translatedRect = painter->combinedTransform().mapRect(boundingRect());
QSize translatedSize = translatedRect.size().toSize();
painter->resetTransform();
QFont font("Serif");
font.setWeight(QFont::Bold);
font.setPixelSize(qMax((int) round(translatedSize.height() / 1.3), 9));
painter->setFont(font);
painter->setPen(Qt::white);
painter->drawText(translatedRect, Qt::AlignCenter, QString::number(value));
}
PlayerTarget::PlayerTarget(Player *_owner) PlayerTarget::PlayerTarget(Player *_owner)
: ArrowTarget(_owner, _owner) : ArrowTarget(_owner, _owner), playerCounter(0)
{ {
setCacheMode(DeviceCoordinateCache); setCacheMode(DeviceCoordinateCache);
@ -17,15 +57,17 @@ PlayerTarget::PlayerTarget(Player *_owner)
QRectF PlayerTarget::boundingRect() const QRectF PlayerTarget::boundingRect() const
{ {
return QRectF(0, 0, 100, 64); return QRectF(0, 0, 160, 64);
} }
void PlayerTarget::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/) void PlayerTarget::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/)
{ {
ServerInfo_User *info = owner->getUserInfo(); ServerInfo_User *info = owner->getUserInfo();
painter->save(); const qreal border = 2;
QRectF translatedRect = painter->combinedTransform().mapRect(boundingRect());
QRectF avatarBoundingRect = boundingRect().adjusted(border, border, -border, -border);
QRectF translatedRect = painter->combinedTransform().mapRect(avatarBoundingRect);
QSize translatedSize = translatedRect.size().toSize(); QSize translatedSize = translatedRect.size().toSize();
QPixmap cachedPixmap; QPixmap cachedPixmap;
const QString cacheKey = "avatar" + QString::number(translatedSize.width()) + "_" + QString::number(info->getUserLevel()) + "_" + QString::number(fullPixmap.cacheKey()); const QString cacheKey = "avatar" + QString::number(translatedSize.width()) + "_" + QString::number(info->getUserLevel()) + "_" + QString::number(fullPixmap.cacheKey());
@ -34,16 +76,35 @@ void PlayerTarget::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*o
#else #else
if (!QPixmapCache::find(cacheKey, cachedPixmap)) { if (!QPixmapCache::find(cacheKey, cachedPixmap)) {
#endif #endif
cachedPixmap = QPixmap(translatedSize.width(), translatedSize.height());
QPainter tempPainter(&cachedPixmap);
QRadialGradient grad(translatedRect.center(), sqrt(translatedSize.width() * translatedSize.width() + translatedSize.height() * translatedSize.height()) / 2);
grad.setColorAt(1, Qt::black);
grad.setColorAt(0, QColor(180, 180, 180));
tempPainter.fillRect(QRectF(0, 0, translatedSize.width(), translatedSize.height()), grad);
QPixmap tempPixmap;
if (fullPixmap.isNull()) if (fullPixmap.isNull())
cachedPixmap = UserLevelPixmapGenerator::generatePixmap(translatedSize.height(), info->getUserLevel()); tempPixmap = UserLevelPixmapGenerator::generatePixmap(translatedSize.height(), info->getUserLevel());
else else
cachedPixmap = fullPixmap.scaled(translatedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation); tempPixmap = fullPixmap.scaled(translatedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
tempPainter.drawPixmap((translatedSize.width() - tempPixmap.width()) / 2, (translatedSize.height() - tempPixmap.height()) / 2, tempPixmap);
QPixmapCache::insert(cacheKey, cachedPixmap); QPixmapCache::insert(cacheKey, cachedPixmap);
} }
painter->resetTransform();
painter->save();
painter->resetTransform();
painter->translate((translatedSize.width() - cachedPixmap.width()) / 2.0, 0); painter->translate((translatedSize.width() - cachedPixmap.width()) / 2.0, 0);
painter->drawPixmap(cachedPixmap.rect(), cachedPixmap, cachedPixmap.rect()); painter->drawPixmap(translatedRect, cachedPixmap, cachedPixmap.rect());
painter->restore();
QRectF nameRect = QRectF(0, boundingRect().height() - 20, 110, 20);
painter->fillRect(nameRect, QColor(0, 0, 0, 160));
QRectF translatedNameRect = painter->combinedTransform().mapRect(nameRect);
painter->save();
painter->resetTransform(); painter->resetTransform();
QString name = info->getName(); QString name = info->getName();
@ -51,14 +112,35 @@ void PlayerTarget::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*o
name = name.mid(0, 10) + "..."; name = name.mid(0, 10) + "...";
QFont font; QFont font;
font.setPixelSize(qMax(translatedSize.height() / 4, 9)); font.setPixelSize(qMax((int) round(translatedNameRect.height() / 1.5), 9));
painter->setFont(font); painter->setFont(font);
painter->setBackgroundMode(Qt::OpaqueMode);
painter->setBackground(QColor(0, 0, 0, 100));
painter->setPen(Qt::white); painter->setPen(Qt::white);
painter->drawText(translatedRect, Qt::AlignHCenter | Qt::AlignTop | Qt::TextWrapAnywhere, name); painter->drawText(translatedNameRect, Qt::AlignVCenter | Qt::AlignLeft, " " + name);
painter->restore(); painter->restore();
QPen pen(QColor(100, 100, 100));
pen.setWidth(border);
pen.setJoinStyle(Qt::RoundJoin);
painter->setPen(pen);
painter->drawRect(boundingRect().adjusted(border / 2, border / 2, -border / 2, -border / 2));
if (getBeingPointedAt()) if (getBeingPointedAt())
painter->fillRect(boundingRect(), QBrush(QColor(255, 0, 0, 100))); painter->fillRect(boundingRect(), QBrush(QColor(255, 0, 0, 100)));
} }
AbstractCounter *PlayerTarget::addCounter(int _counterId, const QString &_name, int _value)
{
if (playerCounter)
return 0;
playerCounter = new PlayerCounter(owner, _counterId, _name, _value, this);
playerCounter->setPos(boundingRect().width() - playerCounter->boundingRect().width(), boundingRect().height() - playerCounter->boundingRect().height());
connect(playerCounter, SIGNAL(destroyed()), this, SLOT(delCounter()));
return playerCounter;
}
void PlayerTarget::delCounter()
{
playerCounter = 0;
}

View file

@ -2,14 +2,27 @@
#define PLAYERTARGET_H #define PLAYERTARGET_H
#include "arrowtarget.h" #include "arrowtarget.h"
#include "abstractcounter.h"
#include <QFont> #include <QFont>
#include <QPixmap> #include <QPixmap>
class Player; class Player;
class PlayerCounter : public AbstractCounter {
Q_OBJECT
public:
PlayerCounter(Player *_player, int _id, const QString &_name, int _value, QGraphicsItem *parent = 0);
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
};
class PlayerTarget : public ArrowTarget { class PlayerTarget : public ArrowTarget {
Q_OBJECT
private: private:
QPixmap fullPixmap; QPixmap fullPixmap;
PlayerCounter *playerCounter;
public slots:
void delCounter();
public: public:
enum { Type = typePlayerTarget }; enum { Type = typePlayerTarget };
int type() const { return Type; } int type() const { return Type; }
@ -17,6 +30,8 @@ public:
PlayerTarget(Player *parent = 0); PlayerTarget(Player *parent = 0);
QRectF boundingRect() const; QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
AbstractCounter *addCounter(int _counterId, const QString &_name, int _value);
}; };
#endif #endif