Commit 6ce57ecf authored by Ian Wadham's avatar Ian Wadham

Add KGrRenderer and theme-type classes for handling dual SVG files (Set and Actors).

svn path=/branches/work/kgoldrunner-qgv/; revision=1296992
parent 0b9e48c8
......@@ -7,6 +7,8 @@ set(kgoldrunner_SRCS
kgoldrunner.cpp
kgrcanvas.cpp
kgrtheme.cpp
kgrthemetypes.cpp
kgrrenderer.cpp
kgrgame.cpp
kgrlevelplayer.cpp
kgrlevelgrid.cpp
......
/****************************************************************************
* Copyright 2012 Ian Wadham <iandw.au@gmail.com> *
* *
* This program 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 of *
* the License, or (at your option) any later version. *
* *
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************/
#include <KGameRenderer>
#include <KgThemeProvider>
#include <KgThemeSelector>
#include <KLocalizedString>
#include <QDebug>
#include <QString>
#include "kgrthemetypes.h"
#include "kgrrenderer.h"
#include "kgrglobals.h"
KGrRenderer::KGrRenderer (QWidget * view)
:
QObject (view)
{
qDebug() << "KGrRenderer called";
m_setProvider = new KgThemeProvider("Theme", this); // Saved config.
m_actorsProvider = new KgThemeProvider("", this); // Do not save.
const QMetaObject * setThemeClass = & KGrSetTheme::staticMetaObject;
m_setProvider->discoverThemes ("appdata", QLatin1String ("themes"),
QLatin1String ("egypt"), setThemeClass);
m_themeSelector = new KgThemeSelector (m_setProvider,
KgThemeSelector::DefaultBehavior,
0); // No parent: modeless dialog.
m_setRenderer = new KGameRenderer (m_setProvider);
m_setRenderer->setParent (this);
initPixmapKeys();
}
KGrRenderer::~KGrRenderer()
{
delete m_themeSelector;
}
void KGrRenderer::selectTheme()
{
m_themeSelector->showAsDialog (i18n("Theme Selector"));
}
KGrRenderer::PixmapSpec KGrRenderer::keyTable [] = {
{ENEMY, Actors, "enemy_1", "", -1, -2}, // For editor only.
{HERO, Actors, "hero_1", "", -1, -2}, // For editor only.
{CONCRETE, Set, "concrete", "-%1", 0, -2},
{BRICK, Set, "brick", "-%1", 0, -2},
{FBRICK, Set, "false_brick", "", -1, -2}, // For editor only.
{HLADDER, Set, "hidden_ladder", "", -1, -2}, // For editor only.
{LADDER, Set, "ladder", "-%1", 0, -2},
{NUGGET, Set, "gold", "-%1", 0, -2},
{BAR, Set, "bar", "-%1", 0, -2},
{FREE, Set, "", "", -1, -2} // Must be last entry.
};
void KGrRenderer::initPixmapKeys()
{
int index = 0;
while (true) {
// Start of game or change of theme: pixmap keys not counted yet.
keyTable[index].frameCount = -2;
if (keyTable[index].picType == FREE) {
break; // End of table: all done.
}
index++;
}
}
QString KGrRenderer::getPixmapKey (const char picType)
{
// TODO - Backgrounds, border tiles, display tiles.
// TODO - Add attributes to theme: HasBorderTiles, HasDisplayTiles.
QString pixmapKey = "";
int index = 0;
while (true) {
if (keyTable[index].picType == FREE) {
break; // Pixmap key not found.
}
else if (keyTable[index].picType == picType) {
if (keyTable[index].frameCount == -2) {
// TODO - Find and count the SVG elements.
// TODO - Must check for key with no suffix first.
// Use picKey + frameSuffix and start at frameBaseIndex.
}
else if (keyTable[index].frameCount == -1) {
break; // Element missing from theme.
}
else if (keyTable[index].frameCount == 0) {
pixmapKey = keyTable[index].picKey; // No suffix.
}
else {
// TODO Pick a random frame number and add a suffix.
}
break;
}
index++;
}
return pixmapKey;
}
#include "kgrrenderer.moc"
/****************************************************************************
* Copyright 2012 Ian Wadham <iandw.au@gmail.com> *
* *
* This program 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 of *
* the License, or (at your option) any later version. *
* *
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************/
#ifndef KGRRENDERER_H
#define KGRRENDERER_H
class KgThemeProvider;
class KgThemeSelector;
class KGameRenderer;
class QWidget;
class KGrRenderer : public QObject
{
Q_OBJECT
public:
KGrRenderer (QWidget * view);
virtual ~KGrRenderer();
QString getPixmapKey (const char picType);
public slots:
void selectTheme();
private:
enum PicSrc {Actors, Set};
struct PixmapSpec {
const char picType;
const PicSrc picSource;
const char * picKey;
const char * frameSuffix;
const int frameBaseIndex;
int frameCount;
};
static PixmapSpec keyTable [];
void initPixmapKeys();
KgThemeProvider * m_setProvider;
KgThemeProvider * m_actorsProvider;
KgThemeSelector * m_themeSelector;
KGameRenderer * m_setRenderer;
KGameRenderer * m_actorsRenderer;
};
#endif // KGRRENDERER_H
/****************************************************************************
* Copyright 2012 Ian Wadham <iandw.au@gmail.com> *
* *
* This program 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 of *
* the License, or (at your option) any later version. *
* *
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************/
#include <QString>
#include <QFileInfo>
#include <QDir>
#include <QDebug>
#include "kgrthemetypes.h"
// Helper: finds the absolute path of a file pointed to by the .desktop file.
QString absolutePath (const QString & path, const QString & relativePath)
{
const QFileInfo file (path);
const QDir dir = file.dir();
QString result = "";
// Add the directory part of "path" to "relativePath".
if (!relativePath.isEmpty() && QFileInfo(relativePath).isRelative()) {
result = dir.absoluteFilePath(relativePath);
}
return result;
}
KGrActorsTheme::KGrActorsTheme(const QByteArray &identifier, QObject *parent)
:
KgTheme(identifier, parent)
{
qDebug() << "KGrActorsTheme constructor";
}
KGrActorsTheme::~KGrActorsTheme()
{
qDebug() << "KGrActorsTheme destructor";
}
bool KGrActorsTheme::readFromDesktopFile(const QString& path)
{
qDebug() << "KGrActorsTheme::readFromDesktopFile" << path;
// Base class call.
if (!KgTheme::readFromDesktopFile(path))
return false;
// Customised behaviour: interprete "Actors" key as "FileName" for SVG file.
qDebug() << "Calling setGraphicsPath" << absolutePath (path, customData("Actors"));
setGraphicsPath (absolutePath (path, customData("Actors")));
return true;
}
KGrSetTheme::KGrSetTheme(const QByteArray &identifier, QObject *parent)
:
KgTheme(identifier, parent)
{
qDebug() << "KGrSetTheme constructor";
}
KGrSetTheme::~KGrSetTheme()
{
qDebug() << "KGrSetTheme destructor";
}
bool KGrSetTheme::readFromDesktopFile(const QString& path)
{
qDebug() << "KGrSetTheme::readFromDesktopFile" << path;
// Base class call.
if (!KgTheme::readFromDesktopFile(path))
return false;
// Customised behaviour: interprete "Set" key as "FileName" for SVG file.
qDebug() << "Calling setGraphicsPath" << absolutePath (path, customData("Set"));
setGraphicsPath (absolutePath (path, customData("Set")));
return true;
}
#include "kgrthemetypes.moc"
/****************************************************************************
* Copyright 2012 Ian Wadham <iandw.au@gmail.com> *
* *
* This program 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 of *
* the License, or (at your option) any later version. *
* *
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************/
#ifndef KGRTHEMETYPES_H
#define KGRTHEMETYPES_H
#include <KgTheme>
class KGrActorsTheme : public KgTheme
{
Q_OBJECT
public:
Q_INVOKABLE KGrActorsTheme(const QByteArray &identifier, QObject *parent=0);
virtual ~KGrActorsTheme();
bool readFromDesktopFile(const QString& path);
};
class KGrSetTheme : public KgTheme
{
Q_OBJECT
public:
Q_INVOKABLE KGrSetTheme(const QByteArray &identifier, QObject *parent=0);
virtual ~KGrSetTheme();
bool readFromDesktopFile(const QString& path);
};
#endif // KGRTHEMETYPES_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment