21 #include <KGameRenderer>
22 #include <KGameRenderedItem>
23 #include <KgThemeProvider>
24 #include <KgThemeSelector>
25 #include <KLocalizedString>
43 m_setProvider =
new KgThemeProvider(
"Theme",
this);
44 m_actorsProvider =
new KgThemeProvider(
"",
this);
47 const QMetaObject * setThemeClass = & KGrSetTheme::staticMetaObject;
48 m_setProvider->discoverThemes (
"appdata",
QLatin1String (
"themes"),
52 const QMetaObject * actorsThemeClass = & KGrActorsTheme::staticMetaObject;
53 m_actorsProvider->discoverThemes (
"appdata",
QLatin1String (
"themes"),
57 m_themeSelector =
new KgThemeSelector (m_setProvider,
58 KgThemeSelector::DefaultBehavior,
62 m_setRenderer =
new KGameRenderer (m_setProvider);
63 m_setRenderer->setParent (
this);
64 m_setRenderer->setFrameSuffix (
"_%1");
65 m_setRenderer->setFrameBaseIndex (1);
68 m_actorsRenderer =
new KGameRenderer (m_actorsProvider);
69 m_actorsRenderer->setParent (
this);
70 m_actorsRenderer->setFrameSuffix (
"_%1");
71 m_actorsRenderer->setFrameBaseIndex (1);
74 connect (m_setProvider, SIGNAL(currentThemeChanged(
const KgTheme*)),
75 this, SLOT(currentThemeChanged(
const KgTheme*)));
78 matchThemes (m_setProvider->currentTheme());
83 delete m_themeSelector;
86 void KGrRenderer::matchThemes (
const KgTheme * currentSetTheme)
91 foreach (
const KgTheme * actorsTheme, m_actorsProvider->themes()) {
92 if (actorsTheme->customData(
"Set") ==
93 currentSetTheme->customData(
"Set")) {
94 m_actorsProvider->setCurrentTheme (actorsTheme);
100 void KGrRenderer::currentThemeChanged (
const KgTheme* currentSetTheme)
102 qDebug() <<
"KGrRenderer::currentThemeChanged()" << currentSetTheme->name();
104 matchThemes (currentSetTheme);
111 m_themeSelector->showAsDialog (i18n(
"Theme Selector"));
114 KGrRenderer::PixmapSpec KGrRenderer::keyTable [] = {
115 {
ENEMY, Actors,
"enemy_1",
"", -1, -2},
116 {
HERO, Actors,
"hero_1",
"", -1, -2},
117 {
CONCRETE, Set,
"concrete",
"-%1", 0, -2},
118 {
BRICK, Set,
"brick",
"-%1", 0, -2},
119 {
FBRICK, Set,
"false_brick",
"", -1, -2},
120 {
HLADDER, Set,
"hidden_ladder",
"", -1, -2},
121 {
LADDER, Set,
"ladder",
"-%1", 0, -2},
122 {
NUGGET, Set,
"gold",
"-%1", 0, -2},
123 {
BAR, Set,
"bar",
"-%1", 0, -2},
124 {
BACKDROP, Set,
"background",
"%1", 0, -2},
125 {
FREE, Set,
"empty",
"", -1, -2}
128 void KGrRenderer::initPixmapKeys()
133 keyTable[index].frameCount = -2;
135 }
while (keyTable[index].picType !=
FREE);
143 m_scene->removeItem (currentTile);
148 if ((picType ==
FREE) || ((index = findKeyTableIndex (picType)) < 0)) {
153 QString key = getPixmapKey (picType, index);
156 m_setRenderer : m_actorsRenderer, key);
157 tile->setAcceptedMouseButtons (0);
158 m_scene->addItem (tile);
164 int index = findKeyTableIndex (picType);
169 ((picType ==
ENEMY) ?
"enemy" :
"brick");
171 m_setRenderer : m_actorsRenderer,
172 key, picType, tickTime);
173 sprite->setAcceptedMouseButtons (0);
181 if (currentBackground) {
182 m_scene->removeItem (currentBackground);
183 delete currentBackground;
186 QString key = getBackgroundKey (level);
188 background->setAcceptedMouseButtons (0);
189 m_scene->addItem (background);
198 m_scene->removeItem (currentItem);
207 item->setAcceptedMouseButtons (0);
208 m_scene->addItem (item);
214 QString s = m_setRenderer->theme()->customData(
"DrawCanvasBorder",
"0");
224 QString s = m_setRenderer->theme()->customData(
"BorderColor",
"#000000");
230 QString s = m_setRenderer->theme()->customData(
"TextColor",
"#FFFFFF");
237 int index = findKeyTableIndex (picType);
238 QString key = getPixmapKey (picType, index);
240 if (keyTable[index].picSource == Set)
241 return m_setRenderer->spritePixmap (key, m_scene->
tileSize ());
243 return m_actorsRenderer->spritePixmap (key, m_scene->
tileSize ());
246 QString KGrRenderer::getPixmapKey (
const char picType,
const int index)
250 int frameCount = (index < 0) ? -1 : keyTable[index].frameCount;
251 if (frameCount > -1) {
252 pixmapKey = keyTable[index].picKey;
253 if (frameCount > 0) {
256 pixmapKey = pixmapKey +
QString(keyTable[index].frameSuffix);
257 pixmapKey = pixmapKey.
arg (keyTable[index].frameBaseIndex +
258 (rand() % frameCount));
264 QString KGrRenderer::getBackgroundKey (
const int level)
267 int index = findKeyTableIndex (
BACKDROP);
268 int frameCount = (index < 0) ? -1 : keyTable[index].frameCount;
269 if (frameCount > -1) {
270 pixmapKey = keyTable[index].picKey;
271 if (frameCount > 0) {
273 pixmapKey = pixmapKey +
QString(keyTable[index].frameSuffix);
274 pixmapKey = pixmapKey.
arg (level % frameCount);
278 qDebug() <<
"BACKGROUND pixmap key" << pixmapKey;
282 int KGrRenderer::findKeyTableIndex (
const char picType)
286 if (keyTable[index].picType == picType) {
287 if (keyTable[index].frameCount == -2) {
288 keyTable[index].frameCount = countFrames (index);
292 else if (keyTable[index].picType ==
FREE) {
301 int KGrRenderer::countFrames (
const int index)
304 int frame = keyTable[index].frameBaseIndex;
305 KGameRenderer * r = (keyTable[index].picSource == Set) ? m_setRenderer :
307 if (r->spriteExists (keyTable[index].picKey)) {
311 if ((count == 0) && (
QString(keyTable[index].picKey) !=
QString(
"brick"))) {
321 QString(keyTable[index].frameSuffix);
322 while (r->spriteExists (pixmapKey.
arg (frame))) {
330 #include "kgrrenderer.moc"
QPixmap getPixmap(const char picType)
QSize tileSize() const
Get the current size of the squared region occupied by a single visual element (characters, ladders, bricks etc.).
KGrRenderer(KGrScene *scene)
KGameRenderedItem * getTileItem(const char picType, KGameRenderedItem *currentTile)
QColor borderColor() const
void changeTheme()
Redraw the scene whenever the current theme has changed.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
The QGraphicsScene that represents KGoldrunner on the screen.
KGameRenderedItem * getBorderItem(QString spriteKey, KGameRenderedItem *currentItem)
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
KGrSprite * getSpriteItem(const char picType, const int tickTime)
KGameRenderedItem * getBackground(const int level, KGameRenderedItem *currentBackground)