Commit d28552a3 authored by Johannes Bergmeier's avatar Johannes Bergmeier
Browse files

* Added CustomGame to variants classes

* loosely integrated WelcomeScreen into KSudoku
* added .desktop files for game-variants (required)
(cleanup of KSudoku required)

svn path=/trunk/playground/games/ksudoku/; revision=658610
parent 95ac6a8a
#include "gamevariants.h"
#include "ksudokugame.h"
#include "serializer.h"
#include <QtDebug>
#include <QMessageBox>
......@@ -73,7 +74,7 @@ Game SudokuGame::createGame(int difficulty) const {
return Game(puzzle);
}
KsView* SudokuGame::createView(const Game& game) const {
KsView* SudokuGame::createView(const Game& /*game*/) const {
qDebug() << "KsView* ksudoku::SudokuGame::createView()";
return 0;
}
......@@ -110,9 +111,44 @@ Game RoxdokuGame::createGame(int difficulty) const {
return Game(puzzle);
}
KsView* RoxdokuGame::createView(const Game& game) const {
KsView* RoxdokuGame::createView(const Game& /*game*/) const {
qDebug() << "KsView* ksudoku::RoxdokuGame::createView()";
return 0;
}
///////////////////////////////////////////////////////////////////////////////
// class CustomGame
///////////////////////////////////////////////////////////////////////////////
CustomGame::CustomGame(const QString& name, const KUrl& url,
GameVariantCollection* collection)
: GameVariant(name, collection), m_url(url), m_solver(0)
{ }
bool CustomGame::canConfigure() const {
return false;
}
bool CustomGame::configure() {
return false;
}
Game CustomGame::createGame(int difficulty) const {
if(!m_solver) {
m_solver = ksudoku::Serializer::loadCustomShape(m_url, 0, 0);
if(!m_solver) return Game();
}
Puzzle* puzzle = new Puzzle(m_solver, true);
puzzle->init(difficulty, 1);
return Game(puzzle);
}
KsView* CustomGame::createView(const Game& /*game*/) const {
qDebug() << "KsView* ksudoku::CustomGame::createView()";
return 0;
}
}
......@@ -89,11 +89,8 @@ private:
};
class CustomGame : public GameVariant {
private:
CustomGame(const QString& name, GameVariantCollection* collection);
public:
static CustomGame* loadGame(const KUrl& url, GameVariantCollection* collection);
CustomGame(const QString& name, const KUrl& url, GameVariantCollection* collection=0);
public:
bool canConfigure() const;
......@@ -102,8 +99,8 @@ public:
KsView* createView(const Game& game) const;
private:
KUrl& url;
SKSolver* m_solver;
KUrl m_url;
mutable SKSolver* m_solver;
};
}
......
......@@ -18,6 +18,9 @@
#include <KStandardAction>
#include <KToggleAction>
#include <KCmdLineArgs>
#include <KAboutData>
#include <QDomDocument>
#include <kmessagebox.h>
#include <kglobal.h>
......@@ -49,6 +52,9 @@
#include <kstandarddirs.h>
#include <kio/job.h>
#include "gamevariants.h"
#include "welcomescreen.h"
// bool guidedMode;
......@@ -107,9 +113,7 @@ void KSudoku::updateStatusBar()
// }
KSudoku::KSudoku()
: KXmlGuiWindow()
// , m_tabs(0)
, m_autoDelCentralWidget(false)
: KXmlGuiWindow(), m_gameVariants(new GameVariantCollection(this, true)), m_autoDelCentralWidget(false)
{
setObjectName("ksudoku");
// m_tabs = new KTabWidget(this);
......@@ -147,7 +151,7 @@ KSudoku::KSudoku()
QMainWindow::setCentralWidget(wrapper);
wrapper->show();
m_gameSelDlg = new GameSelectionDialog(this);
// m_gameSelDlg = new GameSelectionDialog(this);
// m_tabs->insertTab(m_gameSelDlg, "Welcome");
// m_gameSelDlg->show();
// setCentralWidget(m_gameSelDlg);
......@@ -155,26 +159,26 @@ KSudoku::KSudoku()
// gameSelDlg = new GameSelectionDialog(this);
// m_tabs->insertTab(gameSelDlg, "Welcome");
// >>>>>>> .r110
QString title = i18n("Start a Game");
m_gameSelDlg->addEntry("play-sudoku", i18n("Sudoku"), title);
m_gameSelDlg->addEntry("play-roxdoku", i18n("Roxdoku (3D)"), title);
//KGlobal::dirs()->addResourceType("shapes", KStandardDirs::kde_default("data") + QString::fromLatin1("ksudoku/"));
m_welcomeScreen = new WelcomeScreen(wrapper, m_gameVariants);
connect(m_welcomeScreen, SIGNAL(newGameStarted(const Game&,GameVariant*)), this, SLOT(addGame(const Game&)));
setCentralWidget(m_welcomeScreen, false);
(void) new SudokuGame("Sudoku Standard (9x9)", 9, m_gameVariants);
(void) new SudokuGame("Sudoku 16x16", 16, m_gameVariants);
(void) new SudokuGame("Sudoku 25x25", 25, m_gameVariants);
(void) new RoxdokuGame("Roxdoku 9 (3x3x3)", 9, m_gameVariants);
(void) new RoxdokuGame("Roxdoku 16 (4x4x5)", 16, m_gameVariants);
(void) new RoxdokuGame("Roxdoku 25 (5x5x5)", 25, m_gameVariants);
// Reigster the gamevariants resource
KGlobal::dirs()->addResourceType ("gamevariant", KStandardDirs::kde_default("data") +
KCmdLineArgs::aboutData()->appName() + "/");
updateCustomShapesList();
title = i18n("Create your own Game");
m_gameSelDlg->addEntry("edit-sudoku", i18n("Sudoku"), title);
m_gameSelDlg->addEntry("edit-roxdoku", i18n("Roxdoku (3D)"), title);
// gameSelDlg->addEntry("edit-sudoku", i18n("Sudoku"), title);
// gameSelDlg->addEntry("edit-roxdoku", i18n("Roxdoku (3D)"), title);
title = i18n("MORE GAMES");
m_gameSelDlg->addEntry("shape-download", i18n("Download new shapes"), title);
m_gameSelDlg->addEntry("shape-load", i18n("Load shapes manually"), title);
// m_gameSelDlg->showOptions();
connect( m_gameSelDlg, SIGNAL( gameSelected(const QString&) ), this, SLOT( dlgSelectedGame(const QString&) ));
connect(m_gameSelDlg, SIGNAL(gameSelected(const QString&)), this, SLOT(selectGameType(const QString&)));
setCentralWidget(m_gameSelDlg, false);
QTimer *timer = new QTimer( this );
connect( timer, SIGNAL(timeout()), this, SLOT(updateStatusBar()) );
......@@ -182,59 +186,27 @@ KSudoku::KSudoku()
timer->start( 1000); //TODO PORT, false ); // 2 seconds single-shot timer
}
//This is only a testing stub
QString KSudoku::getShapeName(QString path)
{
int left = path.lastIndexOf('/');
int right = path.lastIndexOf('.');
int len = right - left;
return path.mid(left+1, len-1);
}
void KSudoku::updateCustomShapesList()
{
QString title = i18n( "Start a Game" );
QStringList mdirs = KGlobal::dirs()->findDirs("data", "ksudoku/");//, KStandardDirs::NoDuplicates);
if ( mdirs.isEmpty() ) return;
QStringList files;
for (int i = 0; i < mdirs.size(); ++i)
{
kDebug(11000) << "dir " << mdirs.at(i) << endl;
QDir dir(mdirs.at(i));
//printf("%s\n", (mdirs.at(i)).toLatin1());
QStringList temp;
temp += dir.entryList(QDir::Files, QDir::Name );
for(int j=0; j < temp.size(); j++)
if(temp.at(j) != QString( "ksudokuui.rc"))files += dir.absoluteFilePath( temp.at(j) );
}
QStringList filepaths = KGlobal::dirs()->findAllResources("gamevariant", "*.desktop", KStandardDirs::NoDuplicates); // Find files.
for (int i = 0; i < files.size(); ++i)
{
if( !m_shapes.contains(getShapeName(files.at(i))) )
{
QString err;
KUrl Url;
Url.setPath(files.at(i));
//SKSolver* sol = ksudoku::GraphCustom::createCustomSolver((*it));
SKSolver* sol = ksudoku::Serializer::loadCustomShape(Url, this, 0);
if(sol != NULL)
{
kDebug(11000) << "insert " << files.at(i) << endl;
m_shapes.insert(getShapeName(files.at(i)), sol);
m_gameSelDlg->addEntry( "custom-"+getShapeName(files.at(i)), getShapeName(files.at(i)), title );
}
else
{
//printf("Error: file %s is not a valid shape file.\n", (files.at(i)).toLatin1());
//TODO Error Message?
}
}
}
emit m_gameSelDlg->UPDATE();
QString variantName;
QString variantDescr;
QString variantDataPath;
foreach(QString filepath, filepaths) {
KConfig variantConfig(filepath, KConfig::OnlyLocal);
KConfigGroup group = variantConfig.group ("KSudokuVariant");
variantName = group.readEntry("Name", "Missing Name"); // Translated.
variantDescr = group.readEntry("Description", ""); // Translated.
variantDataPath = group.readEntry("FileName", "");
if(variantDataPath == "") continue;
variantDataPath = filepath.left(filepath.lastIndexOf("/")+1) + variantDataPath;
// TODO check whether this variant is allready on the list
(void) new CustomGame(variantName, variantDataPath, m_gameVariants);
}
}
KSudoku::~KSudoku()
......@@ -375,7 +347,7 @@ void KSudoku::setCentralWidget(QWidget* widget, bool autoDel) {
void KSudoku::showWelcomeScreen() {
m_gameOptionsDlg = 0;
setCentralWidget(m_gameSelDlg, false);
setCentralWidget(m_welcomeScreen, false);
}
void KSudoku::selectGameType(const QString& type) {
......
......@@ -34,6 +34,8 @@ class KTabWidget;
namespace ksudoku {
class GameSelectionDialog;
class GameOptionsDialog;
class GameVariantCollection;
class WelcomeScreen;
}
/**
......@@ -192,7 +194,6 @@ public:
void updateCustomShapesList();
void loadCustomShapeFromPath();
void createCustomShape();
QString getShapeName(QString path);
private:
QWidget* wrapper;
......@@ -207,7 +208,10 @@ private:
private:
// KTabWidget* m_tabs;
GameSelectionDialog* m_gameSelDlg;
// GameSelectionDialog* m_gameSelDlg;
GameVariantCollection* m_gameVariants;
WelcomeScreen* m_welcomeScreen;
bool m_autoDelCentralWidget;
......
......@@ -35,7 +35,6 @@ public:
static bool store(const Game& game, const KUrl& url, QWidget* window);
static bool storeCustomShape(const SKSolver* solver, const KUrl& url, QWidget* window);
};
}
......
[KSudokuVariant]
Encoding=UTF-8
Name=4x4
Description=4x4 shape puzzle
Author=Francesco Rossi
FileName=4x4.xml
[KSudokuVariant]
Encoding=UTF-8
Name=Jigsaw
Description=Jigsaw shape puzzle
Author=Francesco Rossi
FileName=Jigsaw.xml
\ No newline at end of file
[KSudokuVariant]
Encoding=UTF-8
Name=Samurai
Description=Samurai shape puzzle
Author=Francesco Rossi
FileName=Samurai.xml
\ No newline at end of file
[KSudokuVariant]
Encoding=UTF-8
Name=XSudoku
Description=XSudoku shape puzzle
Author=Francesco Rossi
FileName=XSudoku.xml
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