mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-01 11:03:54 -07:00
nicer looking counters
This commit is contained in:
parent
f6849ff02b
commit
2e90a85999
17 changed files with 3054 additions and 13 deletions
|
|
@ -1,9 +1,11 @@
|
|||
#include "counter_general.h"
|
||||
#include "pixmapgenerator.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)
|
||||
{
|
||||
setCacheMode(DeviceCoordinateCache);
|
||||
}
|
||||
|
||||
QRectF GeneralCounter::boundingRect() const
|
||||
|
|
@ -13,14 +15,22 @@ QRectF GeneralCounter::boundingRect() const
|
|||
|
||||
void GeneralCounter::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
|
||||
{
|
||||
painter->setBrush(QBrush(color));
|
||||
painter->drawEllipse(boundingRect());
|
||||
QRectF mapRect = painter->combinedTransform().mapRect(boundingRect());
|
||||
int translatedHeight = mapRect.size().height();
|
||||
qreal scaleFactor = translatedHeight / boundingRect().height();
|
||||
QPixmap pixmap = CounterPixmapGenerator::generatePixmap(translatedHeight, name, hovered);
|
||||
|
||||
painter->save();
|
||||
painter->resetTransform();
|
||||
painter->drawPixmap(QPoint(0, 0), pixmap);
|
||||
|
||||
if (value) {
|
||||
QFont f("Serif");
|
||||
f.setPixelSize(radius * 0.8);
|
||||
f.setPixelSize(qMax((int) (radius * scaleFactor), 10));
|
||||
f.setWeight(QFont::Bold);
|
||||
painter->setPen(Qt::black);
|
||||
painter->setFont(f);
|
||||
painter->drawText(boundingRect(), Qt::AlignCenter, QString::number(value));
|
||||
painter->drawText(mapRect, Qt::AlignCenter, QString::number(value));
|
||||
}
|
||||
painter->restore();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,34 @@
|
|||
#include <math.h>
|
||||
#include <QDebug>
|
||||
|
||||
QMap<QString, QPixmap> CounterPixmapGenerator::pmCache;
|
||||
|
||||
QPixmap CounterPixmapGenerator::generatePixmap(int height, QString name, bool highlight)
|
||||
{
|
||||
if (highlight)
|
||||
name.append("_highlight");
|
||||
QString key = name + QString::number(height);
|
||||
if (pmCache.contains(key))
|
||||
return pmCache.value(key);
|
||||
|
||||
QSvgRenderer svg(QString(":/resources/counters/" + name + ".svg"));
|
||||
|
||||
if (!svg.isValid()) {
|
||||
name = "general";
|
||||
if (highlight)
|
||||
name.append("_highlight");
|
||||
svg.load(QString(":/resources/counters/" + name + ".svg"));
|
||||
}
|
||||
|
||||
int width = (int) round(height * (double) svg.defaultSize().width() / (double) svg.defaultSize().height());
|
||||
QPixmap pixmap(width, height);
|
||||
pixmap.fill(Qt::transparent);
|
||||
QPainter painter(&pixmap);
|
||||
svg.render(&painter, QRectF(0, 0, width, height));
|
||||
pmCache.insert(key, pixmap);
|
||||
return pixmap;
|
||||
}
|
||||
|
||||
QPixmap PingPixmapGenerator::generatePixmap(int size, int value, int max)
|
||||
{
|
||||
int key = size * 1000000 + max * 1000 + value;
|
||||
|
|
|
|||
|
|
@ -4,6 +4,14 @@
|
|||
#include <QPixmap>
|
||||
#include <QMap>
|
||||
|
||||
class CounterPixmapGenerator {
|
||||
private:
|
||||
static QMap<QString, QPixmap> pmCache;
|
||||
public:
|
||||
static QPixmap generatePixmap(int size, QString name, bool highlight);
|
||||
static void clear() { pmCache.clear(); }
|
||||
};
|
||||
|
||||
class PingPixmapGenerator {
|
||||
private:
|
||||
static QMap<int, QPixmap> pmCache;
|
||||
|
|
|
|||
|
|
@ -1220,7 +1220,6 @@ void Player::clearArrows()
|
|||
void Player::rearrangeCounters()
|
||||
{
|
||||
qreal marginTop = 80;
|
||||
qreal marginBottom = 10;
|
||||
|
||||
// Determine total height of bounding rectangles
|
||||
qreal totalHeight = 0;
|
||||
|
|
@ -1231,15 +1230,9 @@ void Player::rearrangeCounters()
|
|||
totalHeight += counterIterator.value()->boundingRect().height();
|
||||
}
|
||||
|
||||
// Determine free space between objects
|
||||
qreal padding = (boundingRect().height() - marginTop - marginBottom - totalHeight) / (counters.size() - 1);
|
||||
const qreal padding = 10;
|
||||
qreal y = boundingRect().y() + marginTop;
|
||||
|
||||
if (counters.size() == 1) {
|
||||
padding = 0;
|
||||
y += (boundingRect().height() - marginTop - marginBottom) / 2;
|
||||
}
|
||||
|
||||
// Place objects
|
||||
for (counterIterator.toFront(); counterIterator.hasNext(); ) {
|
||||
AbstractCounter *c = counterIterator.next().value();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue