File: renderer.cpp

package info (click to toggle)
klines 4%3A18.04.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,812 kB
  • sloc: cpp: 1,410; xml: 158; makefile: 5; sh: 2
file content (188 lines) | stat: -rw-r--r-- 5,266 bytes parent folder | download | duplicates (2)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*******************************************************************
*
* Copyright 2006-2007 Dmitry Suzdalev <dimsuz@gmail.com>
*
* This file is part of the KDE project "KLines"
*
* KLines is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* KLines is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KLines; see the file COPYING.  If not, write to
* the Free Software Foundation, 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
********************************************************************/
#include "renderer.h"

#include <KGameRenderer>
#include <KgThemeProvider>

#include <QFileInfo>

// note: this should be in sync with svg
static inline char color2char(BallColor col)
{
    switch (col) {
    case Blue:
        return 'b';
    case Brown:
        return 'e';
    case Cyan:
        return 'c';
    case Green:
        return 'g';
    case Red:
        return 'r';
    case Violet:
        return 'p';
    case Yellow:
        return 'y';
    default:
        return 'x'; // error
    }
}

int KLinesRenderer::m_cellSize = 0;
KGameRenderer *KLinesRenderer::m_renderer;
int KLinesRenderer::m_numBornFrames(0);
int KLinesRenderer::m_numSelFrames(0);
int KLinesRenderer::m_numDieFrames(0);
int KLinesRenderer::m_bornDuration(0);
int KLinesRenderer::m_selDuration(0);
int KLinesRenderer::m_dieDuration(0);
int KLinesRenderer::m_moveDuration(0);

KLinesRenderer  *g_KLinesRenderer = NULL;

void KLinesRenderer::Init()
{
    g_KLinesRenderer = new KLinesRenderer();
}

void KLinesRenderer::UnInit()
{
    delete g_KLinesRenderer;
}

KLinesRenderer::KLinesRenderer()
{
    KgThemeProvider* provider = new KgThemeProvider;
    provider->discoverThemes("appdata", QStringLiteral("themes"));
    //the default theme is marked with a key "Default=true"
    foreach (const KgTheme* theme, provider->themes())
    {
        if (theme->customData(QStringLiteral("Default")) == QLatin1String("true"))
        {
            provider->setDefaultTheme(theme);
            break;
        }
    }
    m_renderer = new KGameRenderer(provider);
    loadTheme();
}

KLinesRenderer::~KLinesRenderer()
{
    delete m_renderer;
}

QString KLinesRenderer::ballPixmapId(BallColor color)
{
    return QLatin1Char(color2char(color)) + QLatin1String("_rest");
}

QPixmap KLinesRenderer::ballPixmap(BallColor color)
{
    return getPixmap(ballPixmapId(color));
}

QString KLinesRenderer::animationFrameId(AnimationType type, BallColor color, int frame)
{
    switch (type) {
    case BornAnim:
        return QLatin1Char(color2char(color)) + QLatin1String("_born_") + QString::number(frame + 1);
    case SelectedAnim:
        return QLatin1Char(color2char(color)) + QLatin1String("_select_") + QString::number(frame + 1);
    case DieAnim:
        return QLatin1Char(color2char(color)) + QLatin1String("_die_") + QString::number(frame + 1);
    case MoveAnim:
        qDebug() << "Move animation type isn't supposed to be handled by KLinesRenderer!";
        return QString();
    default:
        qDebug() << "Warning! Animation type not handled in switch!";
        return QString();
    }
}

QPixmap KLinesRenderer::backgroundTilePixmap()
{
    return getPixmap(QStringLiteral("field_cell"));
}

QPixmap KLinesRenderer::backgroundPixmap(const QSize& size)
{
    return getPixmap(QStringLiteral("background"), size);
}

QPixmap KLinesRenderer::previewPixmap()
{
    return getPixmap(QStringLiteral("preview"), QSize(m_cellSize, m_cellSize * 3));
}

bool KLinesRenderer::loadTheme()
{
    const KgTheme* theme = m_renderer->theme();

    m_numBornFrames = theme->customData(QStringLiteral("NumBornFrames")).toInt();
    m_numSelFrames = theme->customData(QStringLiteral("NumSelectedFrames")).toInt();
    m_numDieFrames = theme->customData(QStringLiteral("NumDieFrames")).toInt();

    m_bornDuration = theme->customData(QStringLiteral("BornAnimDuration")).toInt();
    m_selDuration = theme->customData(QStringLiteral("SelectedAnimDuration")).toInt();
    m_dieDuration = theme->customData(QStringLiteral("DieAnimDuration")).toInt();
    m_moveDuration = theme->customData(QStringLiteral("MoveAnimDuration")).toInt();

    return true;
}

void KLinesRenderer::setCellSize(int cellSize)
{
    if (m_cellSize == cellSize)
        return;

    m_cellSize = cellSize;
}

QPixmap KLinesRenderer::getPixmap(const QString& svgName, const QSize& customSize)
{
    if (m_cellSize == 0)
        return QPixmap();

    QSize sz = customSize.isValid() ? customSize : cellExtent();

    QPixmap pix = m_renderer->spritePixmap(svgName, sz);

    return pix;
}

QPixmap KLinesRenderer::backgroundBorderPixmap(const QSize& size)
{
    if (!hasBorderElement())
        return QPixmap();

    return getPixmap(QStringLiteral("border"), size);
}

bool KLinesRenderer::hasBorderElement()
{
    return m_renderer->spriteExists(QStringLiteral("border"));

}