1/*******************************************************************
2*
3* Copyright 2006-2007 Dmitry Suzdalev <dimsuz@gmail.com>
4*
5* This file is part of the KDE project "KLines"
6*
7* KLines is free software; you can redistribute it and/or modify
8* it under the terms of the GNU General Public License as published by
9* the Free Software Foundation; either version 2, or (at your option)
10* any later version.
11*
12* KLines is distributed in the hope that it will be useful,
13* but WITHOUT ANY WARRANTY; without even the implied warranty of
14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15* GNU General Public License for more details.
16*
17* You should have received a copy of the GNU General Public License
18* along with KLines; see the file COPYING. If not, write to
19* the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20* Boston, MA 02110-1301, USA.
21*
22********************************************************************/
23#include "renderer.h"
24
25#include <KDebug>
26#include <KGameRenderer>
27#include <KgThemeProvider>
28
29#include <QFileInfo>
30
31// note: this should be in sync with svg
32static inline char color2char(BallColor col)
33{
34 switch (col) {
35 case Blue:
36 return 'b';
37 case Brown:
38 return 'e';
39 case Cyan:
40 return 'c';
41 case Green:
42 return 'g';
43 case Red:
44 return 'r';
45 case Violet:
46 return 'p';
47 case Yellow:
48 return 'y';
49 default:
50 return 'x'; // error
51 }
52}
53
54int KLinesRenderer::m_cellSize = 0;
55KGameRenderer *KLinesRenderer::m_renderer;
56int KLinesRenderer::m_numBornFrames(0);
57int KLinesRenderer::m_numSelFrames(0);
58int KLinesRenderer::m_numDieFrames(0);
59int KLinesRenderer::m_bornDuration(0);
60int KLinesRenderer::m_selDuration(0);
61int KLinesRenderer::m_dieDuration(0);
62int KLinesRenderer::m_moveDuration(0);
63
64KLinesRenderer *g_KLinesRenderer = NULL;
65
66void KLinesRenderer::Init()
67{
68 g_KLinesRenderer = new KLinesRenderer();
69}
70
71void KLinesRenderer::UnInit()
72{
73 delete g_KLinesRenderer;
74}
75
76KLinesRenderer::KLinesRenderer()
77{
78 KgThemeProvider* provider = new KgThemeProvider;
79 provider->discoverThemes("appdata", QLatin1String("themes"));
80 //the default theme is marked with a key "Default=true"
81 foreach (const KgTheme* theme, provider->themes())
82 {
83 if (theme->customData(QLatin1String("Default")) == QLatin1String("true"))
84 {
85 provider->setDefaultTheme(theme);
86 break;
87 }
88 }
89 m_renderer = new KGameRenderer(provider);
90 loadTheme();
91}
92
93KLinesRenderer::~KLinesRenderer()
94{
95 delete m_renderer;
96}
97
98QString KLinesRenderer::ballPixmapId(BallColor color)
99{
100 return QLatin1Char(color2char(color)) + QLatin1String("_rest");
101}
102
103QPixmap KLinesRenderer::ballPixmap(BallColor color)
104{
105 return getPixmap(ballPixmapId(color));
106}
107
108QString KLinesRenderer::animationFrameId(AnimationType type, BallColor color, int frame)
109{
110 switch (type) {
111 case BornAnim:
112 return QLatin1Char(color2char(color)) + QLatin1String("_born_") + QString::number(frame + 1);
113 case SelectedAnim:
114 return QLatin1Char(color2char(color)) + QLatin1String("_select_") + QString::number(frame + 1);
115 case DieAnim:
116 return QLatin1Char(color2char(color)) + QLatin1String("_die_") + QString::number(frame + 1);
117 case MoveAnim:
118 kDebug() << "Move animation type isn't supposed to be handled by KLinesRenderer!";
119 return QString();
120 default:
121 kDebug() << "Warning! Animation type not handled in switch!";
122 return QString();
123 }
124}
125
126QPixmap KLinesRenderer::backgroundTilePixmap()
127{
128 return getPixmap(QLatin1String("field_cell"));
129}
130
131QPixmap KLinesRenderer::backgroundPixmap(const QSize& size)
132{
133 return getPixmap(QLatin1String("background"), size);
134}
135
136QPixmap KLinesRenderer::previewPixmap()
137{
138 return getPixmap(QLatin1String("preview"), QSize(m_cellSize, m_cellSize * 3));
139}
140
141bool KLinesRenderer::loadTheme()
142{
143 const KgTheme* theme = m_renderer->theme();
144
145 m_numBornFrames = theme->customData(QLatin1String("NumBornFrames")).toInt();
146 m_numSelFrames = theme->customData(QLatin1String("NumSelectedFrames")).toInt();
147 m_numDieFrames = theme->customData(QLatin1String("NumDieFrames")).toInt();
148
149 m_bornDuration = theme->customData(QLatin1String("BornAnimDuration")).toInt();
150 m_selDuration = theme->customData(QLatin1String("SelectedAnimDuration")).toInt();
151 m_dieDuration = theme->customData(QLatin1String("DieAnimDuration")).toInt();
152 m_moveDuration = theme->customData(QLatin1String("MoveAnimDuration")).toInt();
153
154 return true;
155}
156
157void KLinesRenderer::setCellSize(int cellSize)
158{
159 if (m_cellSize == cellSize)
160 return;
161
162 m_cellSize = cellSize;
163}
164
165QPixmap KLinesRenderer::getPixmap(const QString& svgName, const QSize& customSize)
166{
167 if (m_cellSize == 0)
168 return QPixmap();
169
170 QSize sz = customSize.isValid() ? customSize : cellExtent();
171
172 QPixmap pix = m_renderer->spritePixmap(svgName, sz);
173
174 return pix;
175}
176
177QPixmap KLinesRenderer::backgroundBorderPixmap(const QSize& size)
178{
179 if (!hasBorderElement())
180 return QPixmap();
181
182 return getPixmap(QLatin1String("border"), size);
183}
184
185bool KLinesRenderer::hasBorderElement()
186{
187 return m_renderer->spriteExists(QLatin1String("border"));
188
189}
190