aboutsummaryrefslogtreecommitdiff
path: root/src/GameScene.cpp
blob: ed011c1641631ba1ecccda9be9f52a1388bc983d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <QPainter>
#include <QGraphicsPixmapItem>
#include <QDebug>

#include "GameView.h"

#include "GameScene.h"

GameScene::GameScene(const QString &name, GameView *parent) :
    QGraphicsScene(parent)
{
    setObjectName(name);
    m_gameView = parent;
}

GameView* GameScene::gameView() const
{
    return m_gameView;
}

void GameScene::enterScene(GameScene *)
{
    qDebug() << __FUNCTION__ << objectName();
}

void GameScene::leaveScene(GameScene *)
{
    qDebug() << __FUNCTION__ << objectName();
}

QGraphicsPixmapItem* GameScene::addTitle(const QString &title, int pointSize)
{
    //    // Draw text with a black outline and using a gradient as a brush :-)
    QLinearGradient grad;

    grad.setCoordinateMode(QGradient::ObjectBoundingMode);

    grad.setStart(0, 0);
    grad.setFinalStop(0, 1);

    /* the old gradient
    grad.setColorAt(0, Qt::white);
    grad.setColorAt(0.25, QColor(226, 174, 31));
    grad.setColorAt(0.5, QColor(149, 113, 16));
    grad.setColorAt(0.51, Qt::white);
    grad.setColorAt(1, QColor(68, 153, 213));
    */

    grad.setColorAt(0, Qt::white);
    grad.setColorAt(0.20, QColor(137, 175, 201));
    grad.setColorAt(0.35, QColor(35, 136, 207));
    grad.setColorAt(0.5, QColor(32, 98, 145));
    grad.setColorAt(0.65, QColor(35, 136, 207));
    grad.setColorAt(0.80, QColor(137, 175, 201));
    grad.setColorAt(1, Qt::white);

    QFont font("Arial", pointSize);
    font.setUnderline(true);
    font.setLetterSpacing(QFont::PercentageSpacing, 95);
    font.setWordSpacing(-20);

    QFontMetrics metrics(font);
//    QRect bbox = metrics.boundingRect(title);

//    qDebug() << metrics.boundingRect("MAZENNUS");
//    qDebug() << metrics.width("MAZENNUS") << metrics.height();

    QPainterPath path;
    //    path.addText(pixmap.width()/2-bbox.width()/2, 20+metrics.ascent(), font, "MAZENNUS");
    path.addText(0, metrics.ascent(), font, title);

    QImage img(metrics.width(title), metrics.height()+3, QImage::Format_ARGB32);

    QPainter painter(&img);

    // set dst pixels to transparent regardless of src
    painter.setCompositionMode(QPainter::CompositionMode_Clear);
    painter.fillRect(img.rect(), Qt::white);

    // switch back to normal
    painter.setCompositionMode(QPainter::CompositionMode_SourceOver);

    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(QPen(Qt::black, 3));
    painter.setBrush(grad);

    painter.drawPath(path);

    QPixmap pixmap = QPixmap::fromImage(img);

    QGraphicsPixmapItem *i = addPixmap(pixmap);

    i->setPos(sceneRect().width()/2 - pixmap.width()/2, 20);

    return i;
}