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
QT += network svg
HEADERS += src/counter.h \
HEADERS += src/abstractcounter.h \
src/counter_general.h \
src/dlg_creategame.h \
src/dlg_connect.h \
src/dlg_create_token.h \
@ -86,7 +87,8 @@ HEADERS += src/counter.h \
../common/server_protocolhandler.h \
../common/server_arrowtarget.h
SOURCES += src/counter.cpp \
SOURCES += src/abstractcounter.cpp \
src/counter_general.cpp \
src/dlg_creategame.cpp \
src/dlg_connect.cpp \
src/dlg_create_token.cpp \

View file

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

View file

@ -1,16 +1,17 @@
#include "counter.h"
#include "abstractcounter.h"
#include "player.h"
#include "protocol_items.h"
#include <QPainter>
#include <QMenu>
#include <QAction>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsSceneHoverEvent>
Counter::Counter(Player *_player, int _id, const QString &_name, QColor _color, int _radius, 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)
AbstractCounter::AbstractCounter(Player *_player, int _id, const QString &_name, bool _shownInCounterArea, int _value, QGraphicsItem *parent)
: 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)
radius = Player::counterAreaWidth / 2;
setAcceptsHoverEvents(true);
if (player->getLocal()) {
menu = new QMenu(name);
aSet = new QAction(this);
@ -36,12 +37,12 @@ Counter::Counter(Player *_player, int _id, const QString &_name, QColor _color,
retranslateUi();
}
Counter::~Counter()
AbstractCounter::~AbstractCounter()
{
delete menu;
}
void Counter::delCounter()
void AbstractCounter::delCounter()
{
if (dialogSemaphore)
deleteAfterDialog = true;
@ -49,14 +50,14 @@ void Counter::delCounter()
deleteLater();
}
void Counter::retranslateUi()
void AbstractCounter::retranslateUi()
{
if (menu) {
aSet->setText(tr("&Set counter..."));
}
}
void Counter::setShortcutsActive()
void AbstractCounter::setShortcutsActive()
{
if (name == "life") {
aSet->setShortcut(tr("Ctrl+L"));
@ -65,7 +66,7 @@ void Counter::setShortcutsActive()
}
}
void Counter::setShortcutsInactive()
void AbstractCounter::setShortcutsInactive()
{
if (name == "life") {
aSet->setShortcut(QKeySequence());
@ -74,31 +75,13 @@ void Counter::setShortcutsInactive()
}
}
QRectF Counter::boundingRect() const
{
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)
void AbstractCounter::setValue(int _value)
{
value = _value;
update();
}
void Counter::mousePressEvent(QGraphicsSceneMouseEvent *event)
void AbstractCounter::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
player->sendGameCommand(new Command_IncCounter(-1, id, 1));
@ -114,13 +97,25 @@ void Counter::mousePressEvent(QGraphicsSceneMouseEvent *event)
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();
player->sendGameCommand(new Command_IncCounter(-1, id, delta));
}
void Counter::setCounter()
void AbstractCounter::setCounter()
{
bool ok;
dialogSemaphore = true;

View file

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

View file

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

View file

@ -13,7 +13,7 @@
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)
{
qDebug() << "ArrowItem constructor: startItem=" << startItem;
qDebug() << "ArrowItem constructor: startItem=" << static_cast<QGraphicsItem *>(startItem);
setZValue(2000000005);
if (startItem)

View file

@ -14,7 +14,7 @@ class QAction;
class QPainter;
class CardDragItem;
class CardZone : public QObject, public AbstractGraphicsItem {
class CardZone : public AbstractGraphicsItem {
Q_OBJECT
protected:
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 QPixmap;
class HandCounter : public QObject, public AbstractGraphicsItem {
class HandCounter : public AbstractGraphicsItem {
Q_OBJECT
private:
int number;

View file

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

View file

@ -1,7 +1,7 @@
#include "player.h"
#include "cardzone.h"
#include "playertarget.h"
#include "counter.h"
#include "counter_general.h"
#include "arrowitem.h"
#include "zoneviewzone.h"
#include "zoneviewwidget.h"
@ -32,26 +32,28 @@ Player::Player(ServerInfo_User *info, int _id, bool _local, TabGame *_parent)
updateBgPixmap();
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);
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);
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);
grave->setPos(base + QPointF(0, h));
grave->setPos(base + QPointF(0, h + h2 + 10));
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);
sb->setVisible(false);
HandCounter *handCounter = new HandCounter(this);
handCounter->setPos(base + QPointF(0, 3 * h + 7));
table = new TableZone(this, this);
connect(table, SIGNAL(sizeChanged()), this, SLOT(updateBoundingRect()));
@ -312,7 +314,7 @@ void Player::playerListActionTriggered()
void Player::rearrangeZones()
{
QPointF base = QPointF(CARD_HEIGHT + counterAreaWidth + 5, 0);
QPointF base = QPointF(CARD_HEIGHT + counterAreaWidth + 15, 0);
if (settingsCache->getHorizontalHand()) {
if (mirrored) {
@ -370,7 +372,7 @@ void Player::updateBgPixmap()
void Player::updateBoundingRect()
{
prepareGeometryChange();
qreal width = CARD_HEIGHT + 5 + counterAreaWidth + stack->boundingRect().width();
qreal width = CARD_HEIGHT + 15 + counterAreaWidth + stack->boundingRect().width();
if (settingsCache->getHorizontalHand()) {
qreal handHeight = hand->isVisible() ? hand->boundingRect().height() : 0;
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"));
sayMenu->setTitle(tr("S&ay"));
QMapIterator<int, Counter *> counterIterator(counters);
QMapIterator<int, AbstractCounter *> counterIterator(counters);
while (counterIterator.hasNext())
counterIterator.next().value()->retranslateUi();
@ -457,7 +459,7 @@ void Player::setShortcutsActive()
aCreateToken->setShortcut(tr("Ctrl+T"));
aCreateAnotherToken->setShortcut(tr("Ctrl+G"));
QMapIterator<int, Counter *> counterIterator(counters);
QMapIterator<int, AbstractCounter *> counterIterator(counters);
while (counterIterator.hasNext())
counterIterator.next().value()->setShortcutsActive();
}
@ -478,7 +480,7 @@ void Player::setShortcutsInactive()
aCreateToken->setShortcut(QKeySequence());
aCreateAnotherToken->setShortcut(QKeySequence());
QMapIterator<int, Counter *> counterIterator(counters);
QMapIterator<int, AbstractCounter *> counterIterator(counters);
while (counterIterator.hasNext())
counterIterator.next().value()->setShortcutsInactive();
}
@ -752,7 +754,7 @@ void Player::eventCreateCounters(Event_CreateCounters *event)
void Player::eventSetCounter(Event_SetCounter *event)
{
Counter *c = counters.value(event->getCounterId(), 0);
AbstractCounter *c = counters.value(event->getCounterId(), 0);
if (!c)
return;
int oldValue = c->getValue();
@ -1011,7 +1013,7 @@ QRectF Player::boundingRect() const
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())
painter->fillRect(QRectF(0, 0, totalWidth, boundingRect().height()), QColor(200, 200, 200));
else
@ -1111,14 +1113,22 @@ void Player::addZone(CardZone *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());
}
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);
if (countersMenu)
countersMenu->addMenu(c->getMenu());
@ -1130,9 +1140,11 @@ Counter *Player::addCounter(int counterId, const QString &name, QColor color, in
void Player::delCounter(int counterId)
{
Counter *c = counters.value(counterId, 0);
AbstractCounter *c = counters.value(counterId, 0);
if (!c)
return;
if (c->getName() == "life")
playerTarget->delCounter();
counters.remove(counterId);
c->delCounter();
rearrangeCounters();
@ -1140,10 +1152,11 @@ void Player::delCounter(int counterId)
void Player::clearCounters()
{
QMapIterator<int, Counter *> counterIterator(counters);
QMapIterator<int, AbstractCounter *> counterIterator(counters);
while (counterIterator.hasNext())
counterIterator.next().value()->delCounter();
counters.clear();
playerTarget->delCounter();
}
ArrowItem *Player::addArrow(ServerInfo_Arrow *arrow)
@ -1204,14 +1217,15 @@ void Player::clearArrows()
void Player::rearrangeCounters()
{
qreal marginTop = 15;
qreal marginBottom = 15;
qreal marginTop = 80;
qreal marginBottom = 10;
// Determine total height of bounding rectangles
qreal totalHeight = 0;
QMapIterator<int, Counter *> counterIterator(counters);
QMapIterator<int, AbstractCounter *> counterIterator(counters);
while (counterIterator.hasNext()) {
counterIterator.next();
if (counterIterator.value()->getShownInCounterArea())
totalHeight += counterIterator.value()->boundingRect().height();
}
@ -1226,7 +1240,10 @@ void Player::rearrangeCounters()
// Place objects
for (counterIterator.toFront(); counterIterator.hasNext(); ) {
Counter *c = counterIterator.next().value();
AbstractCounter *c = counterIterator.next().value();
if (!c->getShownInCounterArea())
continue;
QRectF br = c->boundingRect();
c->setPos((counterAreaWidth - br.width()) / 2, y);
@ -1431,7 +1448,7 @@ QString Player::getName() 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())
result += hand->boundingRect().width();
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)
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())
tableWidth -= hand->boundingRect().width();

View file

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

View file

@ -5,9 +5,49 @@
#include <QPainter>
#include <QPixmapCache>
#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)
: ArrowTarget(_owner, _owner)
: ArrowTarget(_owner, _owner), playerCounter(0)
{
setCacheMode(DeviceCoordinateCache);
@ -17,15 +57,17 @@ PlayerTarget::PlayerTarget(Player *_owner)
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*/)
{
ServerInfo_User *info = owner->getUserInfo();
painter->save();
QRectF translatedRect = painter->combinedTransform().mapRect(boundingRect());
const qreal border = 2;
QRectF avatarBoundingRect = boundingRect().adjusted(border, border, -border, -border);
QRectF translatedRect = painter->combinedTransform().mapRect(avatarBoundingRect);
QSize translatedSize = translatedRect.size().toSize();
QPixmap cachedPixmap;
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
if (!QPixmapCache::find(cacheKey, cachedPixmap)) {
#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())
cachedPixmap = UserLevelPixmapGenerator::generatePixmap(translatedSize.height(), info->getUserLevel());
tempPixmap = UserLevelPixmapGenerator::generatePixmap(translatedSize.height(), info->getUserLevel());
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);
}
painter->resetTransform();
painter->save();
painter->resetTransform();
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();
QString name = info->getName();
@ -51,14 +112,35 @@ void PlayerTarget::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*o
name = name.mid(0, 10) + "...";
QFont font;
font.setPixelSize(qMax(translatedSize.height() / 4, 9));
font.setPixelSize(qMax((int) round(translatedNameRect.height() / 1.5), 9));
painter->setFont(font);
painter->setBackgroundMode(Qt::OpaqueMode);
painter->setBackground(QColor(0, 0, 0, 100));
painter->setPen(Qt::white);
painter->drawText(translatedRect, Qt::AlignHCenter | Qt::AlignTop | Qt::TextWrapAnywhere, name);
painter->drawText(translatedNameRect, Qt::AlignVCenter | Qt::AlignLeft, " " + name);
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())
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
#include "arrowtarget.h"
#include "abstractcounter.h"
#include <QFont>
#include <QPixmap>
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 {
Q_OBJECT
private:
QPixmap fullPixmap;
PlayerCounter *playerCounter;
public slots:
void delCounter();
public:
enum { Type = typePlayerTarget };
int type() const { return Type; }
@ -17,6 +30,8 @@ public:
PlayerTarget(Player *parent = 0);
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
AbstractCounter *addCounter(int _counterId, const QString &_name, int _value);
};
#endif