summaryrefslogtreecommitdiff
authorimm <imm>2002-09-21 12:24:59 (UTC)
committer imm <imm>2002-09-21 12:24:59 (UTC)
commit99d546ce72f40e28b04f09201d92d58294401591 (patch) (side-by-side diff)
tree4589ba17c622a114efb4811334e0adce02cedeca
parent279f025b03efdade9c211dd9441eec0c2a91ad94 (diff)
downloadopie-99d546ce72f40e28b04f09201d92d58294401591.zip
opie-99d546ce72f40e28b04f09201d92d58294401591.tar.gz
opie-99d546ce72f40e28b04f09201d92d58294401591.tar.bz2
center widget
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/kpacman/kpacman.cpp23
-rw-r--r--noncore/games/kpacman/kpacman.h18
-rw-r--r--noncore/games/kpacman/kpacmanwidget.cpp9
-rw-r--r--noncore/games/kpacman/referee.cpp4
-rw-r--r--noncore/games/kpacman/score.cpp61
5 files changed, 14 insertions, 101 deletions
diff --git a/noncore/games/kpacman/kpacman.cpp b/noncore/games/kpacman/kpacman.cpp
index 7df6ef0..d9d2d69 100644
--- a/noncore/games/kpacman/kpacman.cpp
+++ b/noncore/games/kpacman/kpacman.cpp
@@ -1,369 +1,364 @@
#include "portable.h"
#if defined( KDE2_PORT )
#include <kpacman.h>
#include <kpacman.moc>
#include <kcolordlg.h>
#elif defined( QPE_PORT )
#include <qmenubar.h>
#include "config.h"
#include <qapplication.h>
#include "kpacman.h"
#endif
#include <qkeycode.h>
#include <qcolor.h>
#include <qstring.h>
#include <qpopmenu.h>
#include <qmsgbox.h>
Kpacman::Kpacman(QWidget *parent, const char *name)
: KTMainWindow(parent, name)
{
schemesPopup = new QList<QPopupMenu>;
schemesPopup->setAutoDelete(TRUE);
menu();
- view = new KpacmanWidget( this, QString(name)+"widget");
+ m_view = new QWidget( this, "m_view" );
+ m_view->setBackgroundColor( black );
+ m_layout = new QGridLayout( m_view );
+ m_layout->setMargin( 7 );
-#ifndef QWS
- setFixedSize(view->width(), view->height());
-#else
- setCaption( "Kpacman" );
-#endif
+ view = new KpacmanWidget( this, QString(name)+"widget");
+ m_layout->addWidget( view, 0, 0 );
+
+ setCaption( "KPacman" );
view->referee->setFocus();
connect(view->referee, SIGNAL(setScore(int, int)),
view->score, SLOT(setScore(int, int)));
connect(view->referee, SIGNAL(setPoints(int)),
view->score, SLOT(set(int)));
connect(view->referee, SIGNAL(setLifes(int)),
view->status, SLOT(setLifes(int)));
connect(view->referee, SIGNAL(setLevel(int)),
view->status, SLOT(setLevel(int)));
connect(view->referee, SIGNAL(forcedHallOfFame(bool)),
this, SLOT(forcedHallOfFame(bool)));
connect(view->referee, SIGNAL(togglePaused()), this, SLOT(togglePaused()));
connect(view->referee, SIGNAL(toggleNew()), this, SLOT(toggleNew()));
connect(view->score, SIGNAL(toggleNew()), this, SLOT(toggleNew()));
connect(view->score, SIGNAL(forcedHallOfFame(bool)),
this, SLOT(forcedHallOfFame(bool)));
APP_CONFIG_BEGIN( cfg );
focusOutPause = !cfg->readBoolEntry("FocusOutPause", TRUE);
focusInContinue = !cfg->readBoolEntry("FocusInContinue", TRUE);
hideMouseCursor = !cfg->readBoolEntry("HideMouseCursor", TRUE);
APP_CONFIG_END( cfg );
toggleFocusOutPause();
toggleFocusInContinue();
toggleHideMouseCursor();
-#ifndef QWS
- menuBar->show();
- view->show();
- setMenu(menuBar);
- setView(view);
-#else
- setCentralWidget( view );
-#endif
+ setCentralWidget( m_view );
}
Kpacman::~Kpacman()
{
APP_CONFIG_BEGIN( cfg );
cfg->writeEntry("FocusOutPause", focusOutPause);
cfg->writeEntry("FocusInContinue", focusInContinue);
cfg->writeEntry("HideMouseCursor", hideMouseCursor);
APP_CONFIG_END( cfg );
delete _menuBar;
}
void Kpacman::menu()
{
gamePopup = new QPopupMenu();
CHECK_PTR( gamePopup );
newID = gamePopup->insertItem(tr("&New"), this, SLOT(newKpacman()),Key_F2);
pauseID = gamePopup->insertItem(tr("&Pause"),
this, SLOT(pauseKpacman()), Key_F3);
hofID = gamePopup->insertItem(tr("&Hall of fame"),
this, SLOT(toggleHallOfFame()), Key_F4);
gamePopup->insertSeparator();
gamePopup->insertItem(tr("&Quit"), this, SLOT(quitKpacman()), CTRL+Key_Q);
gamePopup->setCheckable(TRUE);
optionsPopup = new QPopupMenu();
CHECK_PTR(optionsPopup);
modesPopup = new QPopupMenu();
CHECK_PTR(modesPopup);
hideMouseCursorID = optionsPopup->insertItem(tr("&Hide Mousecursor"),
this, SLOT(toggleHideMouseCursor()),
CTRL+Key_H);
optionsPopup->insertSeparator();
if (lookupSchemes() > 0) {
optionsPopup->insertItem(tr("&Select graphic scheme"), modesPopup);
optionsPopup->insertSeparator();
}
focusOutPauseID = optionsPopup->insertItem(tr("&Pause in Background"),
this, SLOT(toggleFocusOutPause()));
focusInContinueID = optionsPopup->insertItem(tr("&Continue in Foreground"),
this, SLOT(toggleFocusInContinue()));
optionsPopup->insertSeparator();
optionsPopup->insertItem(tr("Change &keys..."), this, SLOT(confKeys()));
#ifndef QWS
QString aboutText = tr("@PACKAGE@ - @VERSION@\n\n"
"Joerg Thoennissen (joe@dsite.de)\n\n"
"A pacman game for the KDE Desktop\n\n"
"The program based on the source of ksnake\n"
"by Michel Filippi (mfilippi@sade.rhein-main.de).\n"
"The design was strongly influenced by the pacman\n"
"(c) 1980 MIDWAY MFG.CO.\n\n"
"I like to thank my girlfriend Elke Krueers for\n"
"the last 10 years of her friendship.\n");
aboutText.replace(QRegExp("@PACKAGE@"), PACKAGE);
aboutText.replace(QRegExp("@VERSION@"), VERSION);
QPopupMenu *helpPopup = helpMenu(aboutText, FALSE);
#endif
//_menuBar = new KMenuBar(this);
//CHECK_PTR( _menuBar );
//_menuBar->insertItem(tr("&Game"), gamePopup);
//_menuBar->insertItem(tr("&Options"), optionsPopup);
//_menuBar->insertSeparator();
#ifndef QWS
_menuBar->insertItem(tr("&Help"), helpPopup);
#endif
}
int Kpacman::lookupSchemes()
{
APP_CONFIG_BEGIN( cfg );
int ModeCount = cfg->readNumEntry("ModeCount", -1);
int Mode = cfg->readNumEntry("Mode", -1);
int SchemeCount = cfg->readNumEntry("SchemeCount");
int Scheme = cfg->readNumEntry("Scheme", -1);
if (SchemeCount == 0 || Scheme == -1) {
QMessageBox::warning(this, tr("Configuration Error"),
tr("There are no schemes defined,\n"
"or no scheme is selected."));
APP_CONFIG_END( cfg );
return 0;
}
connect(modesPopup, SIGNAL(activated(int)), this, SLOT(schemeChecked(int)));
modeID.resize(ModeCount > 0 ? ModeCount : 0);
if (!schemesPopup->isEmpty())
schemesPopup->clear();
SAVE_CONFIG_GROUP( cfg, oldgroup );
QString ModeGroup;
QString ModeName;
for (int m = 0; m < ModeCount; m++) {
ModeGroup.sprintf("Mode %d", m);
cfg->setGroup(ModeGroup);
ModeName = cfg->readEntry("Description", ModeGroup);
QPopupMenu *p = new QPopupMenu;
p->setCheckable(TRUE);
connect(p, SIGNAL(activated(int)), this, SLOT(schemeChecked(int)));
schemesPopup->append(p);
modeID[m] = modesPopup->insertItem(ModeName, schemesPopup->at(m));
modesPopup->setItemEnabled(modeID[m], FALSE);
modesPopup->setItemChecked(modeID[m], m == Mode);
}
schemeID.resize(SchemeCount);
schemeMode.resize(SchemeCount);
QString SchemeGroup;
QString SchemeName;
int SchemeMode;
for (int i = 0; i < SchemeCount; i++) {
SchemeGroup.sprintf("Scheme %d", i);
cfg->setGroup(SchemeGroup);
SchemeName = cfg->readEntry("Description", SchemeGroup);
SchemeMode = cfg->readNumEntry("Mode", -1);
schemeMode[i] = SchemeMode;
if (SchemeMode == -1) {
schemeID[i] = modesPopup->insertItem(SchemeName);
modesPopup->setItemChecked(schemeID[i], i == Scheme);
} else {
schemeID[i] = schemesPopup->at(SchemeMode)->insertItem(SchemeName);
schemesPopup->at(SchemeMode)->
setItemChecked(schemeID[i], i == Scheme);
modesPopup->setItemEnabled(modeID[SchemeMode], TRUE);
}
}
RESTORE_CONFIG_GROUP( cfg, oldgroup );
APP_CONFIG_END( cfg );
return SchemeCount;
}
void Kpacman::quitKpacman()
{
APP_QUIT();
}
void Kpacman::newKpacman()
{
if (!gamePopup->isItemEnabled(hofID))
gamePopup->setItemEnabled(hofID, TRUE);
if (gamePopup->isItemChecked(hofID))
toggleHallOfFame();
if (gamePopup->isItemChecked(pauseID))
pauseKpacman();
view->referee->play();
}
void Kpacman::pauseKpacman()
{
view->referee->pause();
view->score->setPause(gamePopup->isItemChecked(pauseID));
}
void Kpacman::toggleHallOfFame()
{
gamePopup->setItemChecked(hofID, !gamePopup->isItemChecked(hofID));
view->referee->toggleHallOfFame();
if (gamePopup->isItemChecked(hofID)) {
view->referee->lower();
view->status->lower();
} else {
view->status->raise();
view->referee->raise();
view->referee->setFocus();
}
}
/*
* Disable or enable the "Hall of fame"-menuitem if the referee says so.
* This is done, to disable turning off the "hall of fame"-display, in the automated
* sequence of displaying the introduction, the demonstration (or playing) and the
* hall of fame.
* If on == TRUE then also lower the referee and the status widgets.
*/
void Kpacman::forcedHallOfFame(bool on)
{
if (!on && !gamePopup->isItemChecked(hofID))
return;
gamePopup->setItemEnabled(hofID, !on);
gamePopup->setItemChecked(hofID, on);
view->referee->toggleHallOfFame();
if (on) {
view->referee->lower();
view->status->lower();
} else {
view->status->raise();
view->referee->raise();
view->referee->setFocus();
view->referee->intro();
}
}
void Kpacman::togglePaused()
{
static bool checked = FALSE;
checked = !checked;
gamePopup->setItemChecked( pauseID, checked );
view->score->setPause(gamePopup->isItemChecked(pauseID));
}
/*
* This disables the "New Game" menuitem to prevent interruptions of the current
* play.
*/
void Kpacman::toggleNew()
{
gamePopup->setItemEnabled(newID, !gamePopup->isItemEnabled(newID));
}
void Kpacman::toggleHideMouseCursor()
{
hideMouseCursor = !hideMouseCursor;
optionsPopup->setItemChecked(hideMouseCursorID, hideMouseCursor);
if (hideMouseCursor)
view->setCursor(blankCursor);
else
view->setCursor(arrowCursor);
}
void Kpacman::toggleFocusOutPause()
{
focusOutPause = !focusOutPause;
optionsPopup->setItemChecked(focusOutPauseID, focusOutPause);
view->referee->setFocusOutPause(focusOutPause);
}
void Kpacman::toggleFocusInContinue()
{
focusInContinue = !focusInContinue;
optionsPopup->setItemChecked(focusInContinueID, focusInContinue);
view->referee->setFocusInContinue(focusInContinue);
}
void Kpacman::confKeys()
{
Keys *keys = new Keys();
if (keys->exec() == QDialog::Accepted) {
view->referee->initKeys();
view->score->initKeys();
}
delete keys;
}
void Kpacman::schemeChecked(int id)
{
int mode = 0, scheme = -1;
for (uint s = 0; s < schemeID.size(); s++) {
if (schemeID[s] == id) {
scheme = s;
mode = schemeMode[s];
}
if (schemeMode[s] == -1) {
modesPopup->setItemChecked(schemeID[s], schemeID[s] == id);
} else {
modesPopup->setItemChecked(modeID[schemeMode[s]], schemeMode[s] == mode);
schemesPopup->at(schemeMode[s])->setItemChecked(schemeID[s], schemeID[s] == id);
}
}
APP_CONFIG_BEGIN( cfg );
cfg->writeEntry("Scheme", scheme);
cfg->writeEntry("Mode", mode);
APP_CONFIG_END( cfg );
view->setScheme(scheme, mode);
view->updateGeometry();
updateGeometry();
update();
repaint(TRUE);
show();
}
diff --git a/noncore/games/kpacman/kpacman.h b/noncore/games/kpacman/kpacman.h
index d7de9de..2c1de70 100644
--- a/noncore/games/kpacman/kpacman.h
+++ b/noncore/games/kpacman/kpacman.h
@@ -1,95 +1,83 @@
#ifndef KPACMAN_H
#define KPACMAN_H
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "portable.h"
-#if defined( KDE2_PORT )
-#include <kapp.h>
-#include <klocale.h>
-#include <ktmainwindow.h>
-#include <kmenubar.h>
-#elif defined( QPE_PORT )
#include <qmainwindow.h>
class QMenuBar;
-#endif
-
#include <qregexp.h>
#include "kpacmanwidget.h"
#include <qpopmenu.h>
#include <qlist.h>
#include <qfileinf.h>
+#include <qlayout.h>
-#if defined( KDE2_PORT )
-#include <referee.h>
-#include <status.h>
-#include <score.h>
-#include <keys.h>
-#elif defined( QPE_PORT )
#include "referee.h"
#include "status.h"
#include "score.h"
#include "keys.h"
-#endif
class Kpacman : public KTMainWindow
{
Q_OBJECT
public:
Kpacman(QWidget *parent = 0, const char *name = 0);
virtual ~Kpacman();
public slots:
void forcedHallOfFame(bool);
private slots:
void newKpacman();
void pauseKpacman();
void toggleHallOfFame();
void toggleNew();
void togglePaused();
void quitKpacman();
void schemeChecked(int);
void toggleFocusOutPause();
void toggleFocusInContinue();
void toggleHideMouseCursor();
void confKeys();
protected:
private:
KpacmanWidget *view;
void menu();
int lookupSchemes();
+ QGridLayout *m_layout;
+ QWidget *m_view;
KMenuBar *_menuBar;
QPopupMenu *gamePopup;
QPopupMenu *optionsPopup;
QPopupMenu *modesPopup;
QList<QPopupMenu> *schemesPopup;
int newID;
int pauseID;
int hofID;
QArray<int> modeID;
QArray<int> schemeID;
QArray<int> schemeMode;
int focusOutPauseID;
int focusInContinueID;
int hideMouseCursorID;
bool focusOutPause;
bool focusInContinue;
bool hideMouseCursor;
};
#endif // KPACMAN_H
diff --git a/noncore/games/kpacman/kpacmanwidget.cpp b/noncore/games/kpacman/kpacmanwidget.cpp
index 215dcda..feb782c 100644
--- a/noncore/games/kpacman/kpacmanwidget.cpp
+++ b/noncore/games/kpacman/kpacmanwidget.cpp
@@ -1,162 +1,153 @@
#include "portable.h"
#if defined( KDE2_PORT )
#include <kapp.h>
#include <kconfig.h>
#include <kstddirs.h>
#include <kpacmanwidget.h>
#include <kpacmanwidget.moc>
#elif defined( QPE_PORT )
#include <qpe/qpeapplication.h>
#include "config.h"
#include "kpacmanwidget.h"
#endif
#include <qmessagebox.h>
#include "bitfont.h"
#include "score.h"
#include "referee.h"
#include "status.h"
KpacmanWidget::KpacmanWidget( QWidget *parent, const char *name)
: QWidget( parent, name )
{
bitfont = NULL;
fontName = "";
scheme = mode = -1;
confScheme();
score = new Score(this, name, scheme, mode, bitfont);
referee = new Referee( this, name, scheme, mode, bitfont);
status = new Status(this, name, scheme, mode);
-#ifndef QWS
- setFixedSize(referee->width(), bitfont->height()*3 + referee->height() + status->height());
-#else
setBackgroundColor( black );
-#endif
}
KpacmanWidget::~KpacmanWidget()
{
}
void KpacmanWidget::confMisc(bool defGroup)
{
APP_CONFIG_BEGIN( cfg );
//KStandardDirs *dirs = KGlobal::dirs();
QString findPath;
if (defGroup || cfg->hasKey("Font")) {
fontName = cfg->readEntry("Font");
if (fontName.left(1) != "/" && fontName.left(1) != "~")
fontName.insert(0, "fonts/");
if (fontName.right(1) == "/")
fontName.append("font.xbm");
//findPath = dirs->findResource("appdata", fontName);
findPath = FIND_APP_DATA( fontName );
if (!findPath.isEmpty())
fontName = findPath;
bitfontFirstChar = cfg->readNumEntry("FontFirstChar", 0x0e);
bitfontLastChar = cfg->readNumEntry("FontLastChar", 0x5f);
}
APP_CONFIG_END( cfg );
}
void KpacmanWidget::confScheme()
{
APP_CONFIG_BEGIN( cfg );
QString lastFontName = fontName;
SAVE_CONFIG_GROUP( cfg, oldgroup );
QString newgroup;
// if not set, read mode and scheme from the configfile
if (mode == -1 && scheme == -1) {
scheme = cfg->readNumEntry("Scheme", -1);
mode = cfg->readNumEntry("Mode", -1);
// if mode is not set in the defGroup-group, lookup the scheme group
if (scheme != -1 || mode == -1) {
newgroup.sprintf("Scheme %d", scheme);
cfg->setGroup(newgroup);
mode = cfg->readNumEntry("Mode", -1);
RESTORE_CONFIG_GROUP( cfg, oldgroup );
}
}
confMisc();
if (mode != -1) {
newgroup.sprintf("Mode %d", mode);
cfg->setGroup(newgroup);
confMisc(FALSE);
}
if (scheme != -1) {
newgroup.sprintf("Scheme %d", scheme);
cfg->setGroup(newgroup);
confMisc(FALSE);
}
if (lastFontName != fontName) {
if (bitfont != 0)
delete bitfont;
bitfont = new Bitfont(fontName, bitfontFirstChar, bitfontLastChar);
if (bitfont->width() == 0 || bitfont->height() == 0) {
QString msg = tr("The bitfont could not be contructed.\n\n"
"The file '@FONTNAME@' does not exist,\n"
"or is of an unknown format.");
msg.replace(QRegExp("@FONTNAME@"), fontName);
// QMessageBox::critical(this, tr("Initialization Error"), msg);
printf("%s\n", msg.data());
}
}
RESTORE_CONFIG_GROUP( cfg, oldgroup );
APP_CONFIG_END( cfg );
}
void KpacmanWidget::setScheme(int Scheme, int Mode)
{
mode = Mode;
scheme = Scheme;
confScheme();
score->setScheme(Scheme, Mode, bitfont);
referee->setScheme(Scheme, Mode, bitfont);
status->setScheme(Scheme, Mode);
-#ifndef QWS
- setFixedSize(referee->width(),
- bitfont->height()*3 + referee->height() + status->height());
-#endif
-
score->repaint(FALSE);
referee->repaint(FALSE);
status->repaint(FALSE);
}
void KpacmanWidget::resizeEvent( QResizeEvent * )
{
referee->setGeometry(0, bitfont->height()*3, referee->width(), referee->height());
referee->setBackgroundColor(BLACK);
status->setGeometry(0, bitfont->height()*3+referee->height(), referee->width(),
status->height());
status->setBackgroundColor(BLACK);
score->setGeometry(0, 0, referee->width(), bitfont->height()*3+referee->height()+status->height());
score->setBackgroundColor(BLACK);
}
diff --git a/noncore/games/kpacman/referee.cpp b/noncore/games/kpacman/referee.cpp
index 70916f0..81f331b 100644
--- a/noncore/games/kpacman/referee.cpp
+++ b/noncore/games/kpacman/referee.cpp
@@ -350,772 +350,772 @@ void Referee::timerEvent( QTimerEvent *e )
if (eated > 0 && !gameState.testBit(Dying)) {
timerCount = eated;
eaten();
}
if (gameState.testBit(Introducing) && moved)
introPlay();
}
void Referee::repaintFigures()
{
pacmanRect = pix->rect(pacman->position(), PacmanPix);
for (int e = 0; e < board->energizers(); e++) {
*energizerRect->at(e) = pix->rect(board->position(energizer, e), EnergizerPix);
if (pacmanRect.intersects(*energizerRect->at(e))) {
pacmanRect = pix->rect(pacmanRect, *energizerRect->at(e));
energizerRect->at(e)->setRect(0, 0, 0, 0);
} else
for (int ie = e+1; ie < board->energizers(); ie++)
if (energizerRect->at(e)->intersects(*energizerRect->at(ie))) {
*energizerRect->at(e) = pix->rect(*energizerRect->at(e), *energizerRect->at(ie));
energizerRect->at(ie)->setRect(0, 0, 0, 0);
}
}
if (fruit->pix() != -1 && fruit->state() != active)
fruitRect = pix->rect(fruit->position(), FruitScorePix, fruit->pix());
else
fruitRect = pix->rect(fruit->position(), FruitPix, fruit->pix());
if (pacmanRect.intersects(fruitRect)) {
pacmanRect = pix->rect(pacmanRect, fruitRect);
fruitRect.setRect(0, 0, 0, 0);
}
for (int m = 0; m < board->monsters(); m++) {
*monsterRect->at(m) = pix->rect(board->position(monster, m), MonsterPix);
if (pacmanRect.intersects(*monsterRect->at(m))) {
pacmanRect = pix->rect(pacmanRect, *monsterRect->at(m));
monsterRect->at(m)->setRect(0, 0, 0, 0);
} else
for (int im = m+1; im < board->monsters(); im++)
if (monsterRect->at(m)->intersects(*monsterRect->at(im))) {
*monsterRect->at(m) = pix->rect(*monsterRect->at(m), *monsterRect->at(im));
monsterRect->at(im)->setRect(0, 0, 0, 0);
}
}
if (!pacmanRect.isNull())
repaint(pacmanRect, FALSE);
if (!fruitRect.isNull())
repaint(fruitRect, FALSE);
for (int m = 0; m < board->monsters(); m++)
if (!monsterRect->at(m)->isNull())
repaint(*monsterRect->at(m), FALSE);
for (int e = 0; e < board->energizers(); e++)
if (!energizerRect->at(e)->isNull())
repaint(*energizerRect->at(e), FALSE);
}
void Referee::initKeys()
{
APP_CONFIG_BEGIN( cfg );
QString up("Up");
up = cfg->readEntry("upKey", (const char*) up);
UpKey = KAccel::stringToKey(up);
QString down("Down");
down = cfg->readEntry("downKey", (const char*) down);
DownKey = KAccel::stringToKey(down);
QString left("Left");
left = cfg->readEntry("leftKey", (const char*) left);
LeftKey = KAccel::stringToKey(left);
QString right("Right");
right = cfg->readEntry("rightKey", (const char*) right);
RightKey = KAccel::stringToKey(right);
APP_CONFIG_END( cfg );
}
void Referee::fillArray(QArray<int> &array, QString values, int max)
{
if (max < 0)
max = values.contains(',')+1;
array.resize(max);
int last = 0;
bool ok;
QString value;
for (uint i = 0; i < array.size(); i++) {
if (values.find(',') < 0 && values.length() > 0) {
value = values;
values = "";
}
if (values.find(',') >= 0) {
value = values.left(values.find(','));
values.remove(0,values.find(',')+1);
}
array[i] = value.toInt(&ok);
if (ok)
last = array[i];
else
array[i] = last;
}
}
void Referee::fillStrList(QStrList &list, QString values, int max)
{
if (!list.isEmpty())
list.clear();
QString last = "";
QString value;
for (uint i = 0; i < (uint) max; i++) {
if (values.find(',') < 0 && values.length() > 0) {
value = values;
values = "";
}
if (values.find(',') >= 0) {
value = values.left(values.find(','));
values.remove(0,values.find(',')+1);
}
if (!value.isEmpty())
last = value;
list.append(last);
}
}
void Referee::fillMapName()
{
QStrList list = mapName;
if (!mapName.isEmpty())
mapName.clear();
QString map;
QFileInfo fileInfo;
for (uint i = 0; i < list.count(); i++) {
map = list.at(i);
if (map.left(1) != "/" && map.left(1) != "~")
map = FIND_APP_DATA( mapDirectory+map );
fileInfo.setFile(map);
if (!fileInfo.isReadable())
map = "";
mapName.append(map);
}
}
void Referee::confLevels(bool defGroup)
{
APP_CONFIG_BEGIN( cfg );
if (defGroup || cfg->hasKey("Levels"))
maxLevel = cfg->readNumEntry("Levels", 13);
APP_CONFIG_END( cfg );
}
void Referee::confMisc(bool defGroup)
{
APP_CONFIG_BEGIN( cfg );
if (defGroup || cfg->hasKey("PixmapDirectory")) {
pixmapDirectory = cfg->readEntry("PixmapDirectory");
if (pixmapDirectory.left(1) != "/" && pixmapDirectory.left(1) != "~")
pixmapDirectory.insert(0, "pics/");
if (pixmapDirectory.right(1) != "/")
pixmapDirectory.append("/");
}
if (defGroup || cfg->hasKey("MapDirectory")) {
mapDirectory = cfg->readEntry("MapDirectory");
if (mapDirectory.left(1) != "/" && mapDirectory.left(1) != "~")
mapDirectory.insert(0, "maps/");
if (mapDirectory.right(1) != "/")
mapDirectory.append("/");
}
if (defGroup || cfg->hasKey("MapName"))
fillStrList(mapName, cfg->readEntry("MapName", "map"), maxLevel+1);
if (defGroup || cfg->hasKey("MonsterIQ"))
fillArray(monsterIQ, cfg->readEntry("MonsterIQ", "0,170,180,170,180,170,180"), maxLevel+1);
if (defGroup || cfg->hasKey("FruitIQ"))
fillArray(fruitIQ, cfg->readEntry("FruitIQ", "0,170,180,170,180,170,180"), maxLevel+1);
if (defGroup || cfg->hasKey("FruitIndex"))
fillArray(fruitIndex, cfg->readEntry("FruitIndex", "0"), maxLevel+1);
APP_CONFIG_END( cfg );
}
void Referee::confTiming(bool defGroup)
{
APP_CONFIG_BEGIN( cfg );
if (defGroup || cfg->hasKey("SpeedMS"))
fillArray(speed, cfg->readEntry("SpeedMS", "20"), maxLevel+1);
if (defGroup || cfg->hasKey("PacmanTicks"))
fillArray(pacmanTicks,cfg->readEntry("PacmanTicks", "3"), maxLevel+1);
if (defGroup || cfg->hasKey("RemTicks"))
fillArray(remTicks, cfg->readEntry("RemTicks", "1"), maxLevel+1);
if (defGroup || cfg->hasKey("DangerousTicks"))
fillArray(dangerousTicks, cfg->readEntry("DangerousTicks", "3"), maxLevel+1);
if (defGroup || cfg->hasKey("HarmlessTicks"))
fillArray(harmlessTicks, cfg->readEntry("HarmlessTicks", "7,6,,5,,4"), maxLevel+1);
if (defGroup || cfg->hasKey("HarmlessDurationTicks"))
fillArray(harmlessDurTicks, cfg->readEntry("HarmlessDurationTicks", "375,,,300,,250,200,150"), maxLevel+1);
if (defGroup || cfg->hasKey("HarmlessWarningTicks"))
fillArray(harmlessWarnTicks, cfg->readEntry("HarmlessWarningTicks", "135"), maxLevel+1);
if (defGroup || cfg->hasKey("ArrestTicks"))
fillArray(arrestTicks, cfg->readEntry("ArrestTicks", "6"), maxLevel+1);
if (defGroup || cfg->hasKey("ArrestDurationTicks"))
fillArray(arrestDurTicks, cfg->readEntry("ArrestDurationTicks", "200,,,150"), maxLevel+1);
if (defGroup || cfg->hasKey("FruitTicks"))
fillArray(fruitTicks, cfg->readEntry("FruitTicks", "7,6,,5,,4"), maxLevel+1);
if (defGroup || cfg->hasKey("FruitAppearsTicks"))
fillArray(fruitAppearsTicks, cfg->readEntry("FruitAppearsTicks", "1000,,1500,2000,2500,3000,3500,4000"), maxLevel+1);
if (defGroup || cfg->hasKey("FruitDurationTicks"))
fillArray(fruitDurTicks, cfg->readEntry("FruitDurationTicks", "500,,,400,350,300,,250,200,150"), maxLevel+1);
if (defGroup || cfg->hasKey("FruitScoreDurationTicks"))
fillArray(fruitScoreDurTicks, cfg->readEntry("FruitScoreDurationTicks", "150"), maxLevel+1);
if (defGroup || cfg->hasKey("MonsterScoreDurationMS"))
monsterScoreDurMS = cfg->readNumEntry("MonsterScoreDurationMS", 1000);
if (defGroup || cfg->hasKey("PlayerDurationMS"))
playerDurMS = cfg->readNumEntry("PlayerDurationMS", 3000);
if (defGroup || cfg->hasKey("ReadyDurationMS"))
readyDurMS = cfg->readNumEntry("ReadyDurationMS", 2000);
if (defGroup || cfg->hasKey("GameOverDurationMS"))
gameOverDurMS = cfg->readNumEntry("GameOverDurationMS", 3000);
if (defGroup || cfg->hasKey("AfterPauseMS"))
afterPauseMS = cfg->readNumEntry("AfterPauseMS", 1000);
if (defGroup || cfg->hasKey("DyingPreAnimationMS"))
dyingPreAnimationMS = cfg->readNumEntry("DyingPreAnimationMS", 1000);
if (defGroup || cfg->hasKey("DyingAnimationMS"))
dyingAnimationMS = cfg->readNumEntry("DyingAnimationMS", 100);
if (defGroup || cfg->hasKey("DyingPostAnimationMS"))
dyingPostAnimationMS = cfg->readNumEntry("DyingPostAnimationMS", 500);
if (defGroup || cfg->hasKey("IntroAnimationMS"))
introAnimationMS = cfg->readNumEntry("IntroAnimationMS", 800);
if (defGroup || cfg->hasKey("IntroPostAnimationMS"))
introPostAnimationMS = cfg->readNumEntry("IntroPostAnimationMS", 1000);
if (defGroup || cfg->hasKey("LevelUpPreAnimationMS"))
levelUpPreAnimationMS = cfg->readNumEntry("LevelUpPreAnimationMS", 2000);
if (defGroup || cfg->hasKey("LevelUpAnimationMS"))
levelUpAnimationMS = cfg->readNumEntry("LevelUpAnimationMS", 2000);
if (defGroup || cfg->hasKey("EnergizerAnimationMS"))
energizerAnimationMS = cfg->readNumEntry("EnergizerAnimationMS", 200);
APP_CONFIG_END( cfg );
}
void Referee::confScoring(bool defGroup)
{
APP_CONFIG_BEGIN( cfg );
if (defGroup || cfg->hasKey("PointScore"))
pointScore = cfg->readNumEntry("PointScore", 10);
if (defGroup || cfg->hasKey("EnergizerScore"))
energizerScore = cfg->readNumEntry("EnergizerScore", 50);
if (defGroup || cfg->hasKey("FruitScore"))
fillArray(fruitScore, cfg->readEntry("FruitScore", "100,300,500,,700,,1000,,2000,,3000,,5000"), maxLevel+1);
if (defGroup || cfg->hasKey("MonsterScore"))
fillArray(monsterScore, cfg->readEntry("MonsterScore", "200,400,800,1600"), 4);
if (defGroup || cfg->hasKey("ExtraLifeScore"))
fillArray(extraLifeScore, cfg->readEntry("ExtraLifeScore", "10000"), -1);
APP_CONFIG_END( cfg );
}
void Referee::confScheme()
{
APP_CONFIG_BEGIN( cfg );
SAVE_CONFIG_GROUP( cfg, oldgroup );
QString newgroup;
// if not set, read mode and scheme from the configfile
if (mode == -1 && scheme == -1) {
scheme = cfg->readNumEntry("Scheme", -1);
mode = cfg->readNumEntry("Mode", -1);
// if mode is not set in the defGroup-group, lookup the scheme group
if (scheme != -1 || mode == -1) {
newgroup.sprintf("Scheme %d", scheme);
cfg->setGroup(newgroup);
mode = cfg->readNumEntry("Mode", -1);
RESTORE_CONFIG_GROUP( cfg, oldgroup );
}
}
confLevels();
if (mode != -1) {
newgroup.sprintf("Mode %d", mode);
cfg->setGroup(newgroup);
confLevels(FALSE);
}
if (scheme != -1) {
newgroup.sprintf("Scheme %d", scheme);
cfg->setGroup(newgroup);
confLevels(FALSE);
}
RESTORE_CONFIG_GROUP( cfg, oldgroup );
confMisc();
confTiming();
confScoring();
if (mode != -1) {
newgroup.sprintf("Mode %d", mode);
cfg->setGroup(newgroup);
confMisc(FALSE);
confTiming(FALSE);
confScoring(FALSE);
}
if (scheme != -1) {
newgroup.sprintf("Scheme %d", scheme);
cfg->setGroup(newgroup);
confMisc(FALSE);
confTiming(FALSE);
confScoring(FALSE);
}
fillMapName();
RESTORE_CONFIG_GROUP( cfg, oldgroup );
APP_CONFIG_END( cfg );
}
void Referee::setScheme(int Scheme, int Mode, Bitfont *font)
{
mode = Mode;
scheme = Scheme;
confScheme();
pix->setScheme(scheme, mode, font);
pacman->setMaxPixmaps(pix->maxPixmaps(PacmanPix));
fruit->setMaxPixmaps(pix->maxPixmaps(FruitPix));
for (Monster *m = monsters->first(); m != 0; m = monsters->next())
if (m)
m->setMaxPixmaps(pix->maxPixmaps(MonsterPix), pix->maxPixmaps(EyesPix));
for (Energizer *e = energizers->first(); e != 0; e = energizers->next())
if (e)
e->setMaxPixmaps(pix->maxPixmaps(EnergizerPix));
if (gameState.testBit(Introducing))
for (int i = 0; i < (gameState.testBit(Init) ? timerCount : 15); i++)
introPaint(i);
setFixedSize(pix->levelPix().size());
repaint();
}
void Referee::keyPressEvent( QKeyEvent *k )
{
uint key = k->key();
#ifdef QWS
// "OK" => new game
if ( key == Key_F33 || key == Key_F2 || key == Key_Enter )
play();
else if ( !gameState.testBit(Playing) &&
- key == Key_Up ||
+ ( key == Key_Up ||
key == Key_Down ||
key == Key_Right ||
- key == Key_Left )
+ key == Key_Left ))
play();
// "Space" => pause
//else if ( key == Key_Space || key == Key_F3 )
// pause();
// "Menu" => hall of fame
//else if ( key == Key_F11 || key == Key_F4 )
// toggleHallOfFame();
#endif
if (gameState.testBit(Paused) || gameState.testBit(HallOfFame) ||
gameState.testBit(Demonstration) || gameState.testBit(Dying) ||
gameState.testBit(Ready) || gameState.testBit(LevelDone) ||
!gameState.testBit(Playing))
return;
if (key == UpKey)
pacman->setDirection(N);
else if (key == DownKey)
pacman->setDirection(S);
else if (key == RightKey)
pacman->setDirection(E);
else if (key == LeftKey)
pacman->setDirection(W);
#ifdef CHEATS
else if (key == Key_L) { printf("levelUp()\n"); levelUp(); }
else if (key == Key_F) { printf("fruit->move(TRUE)\n"); fruit->move(TRUE); repaint(FALSE); }
else if (key == Key_E) { printf("setLifes(++lifes)\n"); emit setLifes(++lifes); }
#endif
else {
k->ignore();
return;
}
k->accept();
}
void Referee::score(int p)
{
if (!gameState.testBit(Playing))
return;
if ((points += p) < 0)
points = 0;
emit setPoints(points);
if (points >= nextExtraLifeScore) {
emit setLifes(++lifes);
if (extraLifeScoreIndex < (int) extraLifeScore.size()-1)
extraLifeScoreIndex++;
if (extraLifeScore[extraLifeScoreIndex] < 0)
nextExtraLifeScore = extraLifeScore[extraLifeScoreIndex] * -1;
else
nextExtraLifeScore += extraLifeScore[extraLifeScoreIndex];
}
}
void Referee::eaten()
{
if (gameState.testBit(Ready))
return;
stop();
if (monstersEaten < 4)
monstersEaten++;
gameState.setBit(Scoring);
score(monsterScore[monstersEaten-1]);
repaint(pix->rect(pix->rect(pacman->position(), MonsterPix),
pix->rect(pacman->position(), MonsterScorePix, monstersEaten-1)));
if (--timerCount > 0)
QTimer::singleShot( monsterScoreDurMS, this, SLOT(eaten()));
else {
for (Monster *m = monsters->first(); m != 0; m = monsters->next())
if (m && m->direction() == X && !gameState.testBit(Introducing))
m->setDirection(N);
if (monstersEaten != 4 || !gameState.testBit(Introducing))
QTimer::singleShot( monsterScoreDurMS, this, SLOT(start()));
}
}
void Referee::toggleHallOfFame()
{
gameState.toggleBit(HallOfFame);
}
void Referee::hallOfFame()
{
if (gameState.testBit(HallOfFame)) // If the HallOfFame is switched on manually, toggle the
toggleHallOfFame(); // bit twice.
emit setLevel(0); // Clear status display for hall of fame
emit setScore(level, 0);
emit forcedHallOfFame(TRUE);
}
void Referee::pause()
{
static int pausedTimer = 0;
if (!gameState.testBit(Paused)) {
pausedTimer = gameTimer;
stop();
stopEnergizer();
gameState.setBit(Paused);
repaint(pix->rect((BoardWidth*BoardHeight)/2-BoardWidth, tr("PAUSED")), FALSE);
} else {
gameState.clearBit(Paused);
repaint(pix->rect((BoardWidth*BoardHeight)/2-BoardWidth, tr("PAUSED")), FALSE);
if (pausedTimer) {
pausedTimer = 0;
start();
}
}
emit togglePaused();
}
void Referee::intro()
{
stop();
stopEnergizer();
bool paused = gameState.testBit(Paused);
gameState.fill(FALSE);
gameState.setBit(Introducing);
gameState.setBit(Init);
if (paused)
gameState.setBit(Paused);
level = 0;
emit setLevel(level);
board->init(Intro);
pix->setLevel(level);
initPacman();
initFruit();
initMonsters();
initEnergizers();
repaint();
monstersEaten = 0;
timerCount = 0;
introPlay();
}
void Referee::introMonster(int id)
{
Monster *m = new Monster(board, id);
m->setPosition((10+id*6)*BoardWidth+10);
m->setDirection(E);
m->setDangerous(dangerousTicks[level], monsterIQ[level]);
m->setMaxPixmaps(pix->maxPixmaps(MonsterPix), pix->maxPixmaps(EyesPix));
if (m->body() != -1)
pix->draw(m->position(), RoomPix, MonsterPix, m->body());
if (m->eyes() != -1)
pix->draw(m->position(), RoomPix, EyesPix, m->eyes());
repaint(pix->rect(m->position(), MonsterPix), FALSE);
m->setPosition(OUT);
}
void Referee::introPaint(int t)
{
QString pts;
switch (t) {
case 0 : repaint(pix->draw(16, 6, RoomPix, tr("CHARACTER"), WHITE, QColor(), AlignLeft), FALSE);
repaint(pix->draw(36, 6, RoomPix, tr("/"), WHITE, QColor(), AlignLeft), FALSE);
repaint(pix->draw(40, 6, RoomPix, tr("NICKNAME"), WHITE, QColor(), AlignLeft), FALSE);
break;
case 1 : introMonster(0);
break;
case 2 : repaint(pix->draw(16, 10, RoomPix, tr("-SHADOW"), RED, QColor(), AlignLeft), FALSE);
break;
case 3 : repaint(pix->draw(38, 10, RoomPix, tr("\"BLINKY\""), RED, QColor(), AlignLeft), FALSE);
break;
case 4 : introMonster(1);
break;
case 5 : repaint(pix->draw(16, 16, RoomPix, tr("-SPEEDY"), PINK, QColor(), AlignLeft), FALSE);
break;
case 6 : repaint(pix->draw(38, 16, RoomPix, tr("\"PINKY\""), PINK, QColor(), AlignLeft), FALSE);
break;
case 7 : introMonster(2);
break;
case 8 : repaint(pix->draw(16, 22, RoomPix, tr("-BASHFUL"), CYAN, QColor(), AlignLeft), FALSE);
break;
case 9 : repaint(pix->draw(38, 22, RoomPix, tr("\"INKY\""), CYAN, QColor(), AlignLeft), FALSE);
break;
case 10 : introMonster(3);
break;
case 11 : repaint(pix->draw(16, 28, RoomPix, tr("-POKEY"), ORANGE, QColor(), AlignLeft), FALSE);
break;
case 12 : repaint(pix->draw(38, 28, RoomPix, tr("\"CLYDE\""), ORANGE, QColor(), AlignLeft), FALSE);
break;
case 13 : pts.sprintf("%d", pointScore);
repaint(pix->draw(28, 44, RoomPix, pts.data(), WHITE, QColor(), AlignRight), FALSE);
repaint(pix->draw(31, 44, RoomPix, "\x1C\x1D\x1E", WHITE, QColor(), AlignLeft), FALSE);
pts.sprintf("%d", energizerScore);
repaint(pix->draw(28, 48, RoomPix, pts.data(), WHITE, QColor(), AlignRight), FALSE);
repaint(pix->draw(31, 48, RoomPix, "\x1C\x1D\x1E", WHITE, QColor(), AlignLeft), FALSE);
break;
case 14 : // "@ 1980 MIDWAY MFG.CO."
#if defined( KDE2_PORT )
repaint(pix->draw(30, 58, RoomPix, "© 1998-2002 J.THÖNNISSEN", PINK), FALSE);
#elif defined( QPE_PORT )
repaint(pix->draw(30, 55, RoomPix, "© 1998-2002 J.THÖNNISSEN", PINK), FALSE);
repaint(pix->draw(29, 58, RoomPix, tr("QTOPIA PORT: CATALIN CLIMOV"), GREEN), FALSE);
repaint(pix->draw(29, 61, RoomPix, tr("PRESS CURSOR TO START"), GREEN), FALSE);
#endif
break;
}
}
void Referee::introPlay()
{
if (!gameState.testBit(Introducing) || gameState.testBit(Ready))
return;
if (gameState.testBit(Paused) || gameState.testBit(HallOfFame)) {
QTimer::singleShot(afterPauseMS, this, SLOT(introPlay()));
return;
}
if (!gameState.testBit(Init)) {
if (monstersEaten == 4) {
stop();
QTimer::singleShot(introPostAnimationMS, this, SLOT(demo()));
}
if (pacman->direction() == W) {
int id = -1;
if (pacman->position() == 37*BoardWidth-6)
id = 0;
else
if (board->isMonster(37*BoardWidth-6))
for (Monster *m = monsters->first(); m != 0; m = monsters->next())
if (m && m->position() == 37*BoardWidth-6) {
id = m->id();
id++;
break;
}
if (id >= 0 && id <= 4)
for (Monster *m = monsters->first(); m != 0; m = monsters->next())
if (m && m->id() == id && m->position() == OUT) {
m->setPosition(37*BoardWidth-1);
m->setDirection(W);
m->setDangerous(dangerousTicks[level], monsterIQ[level]);
board->set(37*BoardWidth-1, monsterhome, id);
repaint(pix->rect(m->position(), MonsterPix));
break;
}
}
return;
}
if (timerCount < 15)
introPaint(timerCount);
switch (timerCount) {
case 13 : board->set(44*BoardWidth+22, Point);
pix->drawPoint(44*BoardWidth+22);
repaint(pix->rect(44*BoardWidth+22, PointPix), FALSE);
energizers->at(0)->setPosition(48*BoardWidth+22);
energizers->at(0)->setOn();
repaint(pix->rect(48*BoardWidth+22, EnergizerPix), FALSE);
break;
case 14 : energizers->at(1)->setPosition(36*BoardWidth+10);
energizers->at(1)->setOn();
repaint(pix->rect(36*BoardWidth+10, EnergizerPix), FALSE);
for (int pos = 8; pos < BoardWidth; pos++) {
board->set(34*BoardWidth+pos, out);
board->set(38*BoardWidth+pos, out);
}
board->set(36*BoardWidth+8, out);
break;
case 15 : gameState.clearBit(Init);
initPacman();
pacman->setDemo(TRUE);
pacman->setPosition(37*BoardWidth-1);
repaintFigures();
start();
return;
}
if (timerCount++ < 15)
QTimer::singleShot(introAnimationMS, this, SLOT(introPlay()));
}
void Referee::demo()
{
if (gameState.testBit(Ready))
return;
if (gameState.testBit(Paused) || gameState.testBit(HallOfFame)) {
QTimer::singleShot(afterPauseMS, this, SLOT(demo()));
return;
}
stop();
stopEnergizer();
gameState.fill(FALSE);
gameState.setBit(Init);
gameState.setBit(Demonstration);
level = 0;
emit setLevel(level);
board->init(Demo, mapName.at(0));
pix->setLevel(level);
initPacman();
initFruit();
initMonsters();
initEnergizers();
gameState.clearBit(Init);
repaint();
timerCount = 0;
QTimer::singleShot(playerDurMS, this, SLOT(start()));
}
void Referee::play()
{
stop();
stopEnergizer();
gameState.fill(FALSE);
gameState.setBit(Init);
gameState.setBit(Playing);
gameState.setBit(Player);
gameState.setBit(Ready);
lifes = 3;
level = 1;
points = 0;
extraLifeScoreIndex = 0;
nextExtraLifeScore = extraLifeScore[extraLifeScoreIndex];
if (nextExtraLifeScore < 0)
nextExtraLifeScore *= -1;
board->init(Level, mapName.at(level));
pix->setLevel(level);
initPacman();
initFruit();
initMonsters();
initEnergizers();
repaint();
emit toggleNew();
emit setLifes(lifes);
emit setLevel(level);
emit setPoints(points);
repaint(pix->rect(board->position(monsterhome, 0), tr("PLAYER ONE")), FALSE);
repaint(pix->rect(board->position(fruithome), tr("READY!")), FALSE);
timerCount = 0;
QTimer::singleShot(playerDurMS, this, SLOT(ready()));
}
void Referee::ready()
{
if (gameState.testBit(Paused) || gameState.testBit(HallOfFame)) {
QTimer::singleShot(afterPauseMS, this, SLOT(ready()));
return;
}
if (gameState.testBit(Player)) {
emit setLifes(--lifes);
gameState.clearBit(Player);
gameState.clearBit(Init);
diff --git a/noncore/games/kpacman/score.cpp b/noncore/games/kpacman/score.cpp
index 17dbf0a..b3ff3d9 100644
--- a/noncore/games/kpacman/score.cpp
+++ b/noncore/games/kpacman/score.cpp
@@ -97,546 +97,485 @@ void Score::paintEvent( QPaintEvent *e)
pix = bitfont->text(tr(" 2UP "), fg, BLACK);
bitBlt(this, x(21), y(0), &pix);
}
QString s;
s.sprintf("%6d0", playerScore[0]/10);
if (rect(0, 1, s).intersects(e->rect())) {
QPixmap pix = bitfont->text(s, WHITE, BLACK);
bitBlt(this, x(0), y(1), &pix);
}
s.sprintf("%8d0", HighScore/10);
if (rect(8, 1, s).intersects(e->rect())) {
QPixmap pix = bitfont->text(s, WHITE, BLACK);
bitBlt(this, x(8), y(1), &pix);
}
if (lastScore >= 0) {
if (rect(1, 4*1.25, tr(" CONGRATULATIONS ")).intersects(e->rect())) {
QPixmap pix = bitfont->text(tr(" CONGRATULATIONS "), YELLOW, BLACK);
bitBlt(this, x(1), y(4*1.25), &pix);
}
if (rect(1, 6*1.25, tr(" YOU HAVE ARCHIEVED ")).intersects(e->rect())) {
QPixmap pix = bitfont->text(tr(" YOU HAVE ARCHIEVED "), CYAN, BLACK);
bitBlt(this, x(1), y(6*1.25), &pix);
}
if (rect(1, 7*1.25, tr(" A SCORE IN THE TOP 10. ")).intersects(e->rect())) {
QPixmap pix = bitfont->text(tr(" A SCORE IN THE TOP 10. "), CYAN, BLACK);
bitBlt(this, x(1), y(7*1.25), &pix);
}
if (rect(1, 8*1.25, tr(" ")).intersects(e->rect())) {
QPixmap pix = bitfont->text(tr(" "), CYAN, BLACK);
bitBlt(this, x(1), y(8*1.25), &pix);
}
}
if (rect(1, 9.5*1.25, tr("RNK SCORE NAME DATE")).intersects(e->rect())) {
QPixmap pix = bitfont->text(tr("RNK SCORE NAME DATE"), WHITE, BLACK);
bitBlt(this, x(1), y(9.5*1.25), &pix);
}
for (int i = 0; i < 10; i++) {
s.sprintf("%2d%9d %-3.3s %-8.8s",
i+1, hallOfFame[i].points, hallOfFame[i].name.utf8().data(),
formatDate(hallOfFame[i].moment.date()).data());
if (rect(1, (11+i)*1.25, s).intersects(e->rect())) {
QPixmap pix = bitfont->text(s, (i == lastScore) ? YELLOW : WHITE, BLACK);
bitBlt(this, x(1), y((11+i)*1.25), &pix);
}
}
if (cursor.x != -1 && cursor.y != -1 && cursor.on) {
if (rect(cursor.x, (cursor.y*1.25), cursor.chr).intersects(e->rect())) {
QPixmap pix = bitfont->text(cursor.chr, BLACK, YELLOW);
bitBlt(this, x(cursor.x), y(cursor.y*1.25), &pix);
}
}
if (paused) {
QPixmap pix = bitfont->text(tr("PAUSED"), RED, BLACK);
QRect r = bitfont->rect(tr("PAUSED"));
r.moveCenter(QPoint(this->width()/2, this->height()/2));
bitBlt(this, r.x(), r.y(), &pix);
}
}
void Score::timerEvent(QTimerEvent*)
{
cursor.on = !cursor.on;
if (paused)
return;
if (cursor.x != -1 && cursor.y != -1)
repaint(rect(cursor.x, cursor.y*1.25, cursor.chr), FALSE);
scrollRepeat = FALSE;
if (lastPlayer == 0)
repaint(rect(1, 0, tr(" 1UP ")), FALSE);
if (lastPlayer == 1)
repaint(rect(21, 0, tr(" 2UP ")), FALSE);
}
void Score::keyPressEvent(QKeyEvent *k)
{
if (lastScore < 0 || lastPlayer < 0 || lastPlayer >= maxPlayer || paused) {
k->ignore();
return;
}
int x = cursor.x;
int y = cursor.y;
uint key = k->key();
if (scrollRepeat && (key == UpKey || key == Key_Up || key == DownKey || key == Key_Down)) {
k->ignore();
return;
}
if (key != Key_Return) {
if (key == RightKey || key == Key_Right)
if (++cursor.x > 16)
cursor.x = 14;
if (key == LeftKey || key == Key_Left)
if (--cursor.x < 14)
cursor.x = 16;
if (key == UpKey || key == Key_Up)
if (cursor.chr.unicode() < bitfont->lastChar())
cursor.chr = cursor.chr.unicode()+1;
else
cursor.chr = bitfont->firstChar();
if (key == DownKey || key == Key_Down)
if (cursor.chr.unicode() > bitfont->firstChar())
cursor.chr = cursor.chr.unicode()-1;
else
cursor.chr = bitfont->lastChar();
if (cursor.x == x && cursor.y == y &&
cursor.chr == hallOfFame[lastScore].name.at(cursor.x-14)) {
uint ascii = k->ascii();
if (ascii < bitfont->firstChar() || ascii > bitfont->lastChar())
ascii = toupper(ascii);
if (ascii >= bitfont->firstChar() && ascii <= bitfont->lastChar()) {
cursor.chr = ascii;
hallOfFame[lastScore].name.at(cursor.x-14) = cursor.chr;
if (++cursor.x > 16)
cursor.x = 14;
}
}
}
if (key == Key_Return) {
playerName[lastPlayer] = hallOfFame[lastScore].name;
write();
read();
lastScore = -1;
cursor.x = -1;
cursor.y = -1;
// killTimers();
emit toggleNew();
end();
}
if (x != cursor.x || y != cursor.y) {
if (cursor.x != -1)
cursor.chr = hallOfFame[lastScore].name.at(cursor.x-14);
scrollRepeat = FALSE;
repaint(rect(x, y*1.25, cursor.chr), FALSE);
} else
hallOfFame[lastScore].name.at(cursor.x-14) = cursor.chr;
if (key == UpKey || key == Key_Up || key == DownKey || key == Key_Down)
scrollRepeat = TRUE;
else
repaint(rect(cursor.x, cursor.y*1.25, cursor.chr), FALSE);
}
void Score::initKeys()
{
APP_CONFIG_BEGIN( cfg );
QString up("Up");
up = cfg->readEntry("upKey", (const char*) up);
UpKey = KAccel::stringToKey(up);
QString down("Down");
down = cfg->readEntry("downKey", (const char*) down);
DownKey = KAccel::stringToKey(down);
QString left("Left");
left = cfg->readEntry("leftKey", (const char*) left);
LeftKey = KAccel::stringToKey(left);
QString right("Right");
right = cfg->readEntry("rightKey", (const char*) right);
RightKey = KAccel::stringToKey(right);
APP_CONFIG_END( cfg );
}
void Score::confTiming(bool defGroup)
{
APP_CONFIG_BEGIN( cfg );
if (defGroup || cfg->hasKey("CursorBlinkMS"))
cursorBlinkMS = cfg->readNumEntry("CursorBlinkMS", 250);
if (defGroup || cfg->hasKey("HallOfFameMS"))
hallOfFameMS = cfg->readNumEntry("HallOfFameMS", 7000);
if (defGroup || cfg->hasKey("AfterPauseMS"))
afterPauseMS = cfg->readNumEntry("AfterPauseMS", 1000);
APP_CONFIG_END( cfg );
}
void Score::confScheme()
{
APP_CONFIG_BEGIN( cfg );
SAVE_CONFIG_GROUP( cfg, oldgroup );
QString newgroup;
// if not set, read mode and scheme from the configfile
if (mode == -1 && scheme == -1) {
scheme = cfg->readNumEntry("Scheme", -1);
mode = cfg->readNumEntry("Mode", -1);
// if mode is not set in the defGroup-group, lookup the scheme group
if (scheme != -1 || mode == -1) {
newgroup.sprintf("Scheme %d", scheme);
cfg->setGroup(newgroup);
mode = cfg->readNumEntry("Mode", -1);
RESTORE_CONFIG_GROUP( cfg, oldgroup );
}
}
int oldCursorBlinkMS = cursorBlinkMS;
confTiming();
if (mode != -1) {
newgroup.sprintf("Mode %d", mode);
cfg->setGroup(newgroup);
confTiming(FALSE);
}
if (scheme != -1) {
newgroup.sprintf("Scheme %d", scheme);
cfg->setGroup(newgroup);
confTiming(FALSE);
}
if (cursorBlinkMS != oldCursorBlinkMS) {
if (cursorBlinkTimer)
killTimer(cursorBlinkTimer);
cursorBlinkTimer = startTimer(cursorBlinkMS);
}
RESTORE_CONFIG_GROUP( cfg, oldgroup );
APP_CONFIG_END( cfg );
}
void Score::setScheme(int Scheme, int Mode, Bitfont *font)
{
mode = Mode;
scheme = Scheme;
confScheme();
bitfont = font;
for (int p = 0; p < maxPlayer; p++)
for (uint i = 0; i < playerName[p].length(); i++)
if (playerName[p].at(i) < bitfont->firstChar() ||
playerName[p].at(i) > bitfont->lastChar())
playerName[p].at(i) = playerName[p].at(i).upper();
for (int i = 0; i < 10; i++)
for (uint j = 0; j < hallOfFame[i].name.length(); j++)
if (hallOfFame[i].name.at(j) < bitfont->firstChar() ||
hallOfFame[i].name.at(j) > bitfont->lastChar())
hallOfFame[i].name.at(j) = hallOfFame[i].name.at(j).upper();
if (cursor.chr.unicode() < bitfont->firstChar() ||
cursor.chr.unicode() > bitfont->lastChar())
cursor.chr = cursor.chr.upper();
}
void Score::set(int score)
{
set(score, 0);
}
void Score::set(int score, int player)
{
if (player < 0 || player >= maxPlayer)
return;
lastPlayer = player;
playerScore[lastPlayer] = score;
QString s;
s.sprintf("%6d0", playerScore[lastPlayer]/10);
repaint(rect(0, 1, s), FALSE);
if (score > HighScore) {
HighScore = score;
s.sprintf("%8d0", HighScore/10);
repaint(rect(8, 1, s), FALSE);
}
}
/*
* Set the score for player after the game if over. If the score is in the
* high scores then the hall of fame is updated (shifted) and the scoreboard
* is shown.
*/
void Score::setScore(int level, int player)
{
lastScore = -1;
if (player < 0 || player >= maxPlayer || level == 0) {
if (level != 0)
emit toggleNew();
QTimer::singleShot(hallOfFameMS, this, SLOT(end()));
return;
}
lastPlayer = player;
for (int i = 0; i < 10; i++)
if ( playerScore[lastPlayer] > hallOfFame[i].points) {
lastScore = i;
break;
}
if (lastScore < 0) {
emit toggleNew();
QTimer::singleShot(hallOfFameMS, this, SLOT(end()));
return;
}
for (int i = 9; i > lastScore && i > 0; i--)
hallOfFame[i] = hallOfFame[i-1];
hallOfFame[lastScore].points = playerScore[lastPlayer];
hallOfFame[lastScore].levels = level;
hallOfFame[lastScore].moment = QDateTime::currentDateTime();
hallOfFame[lastScore].name = playerName[lastPlayer];
cursor.x = 14;
cursor.y = 11+lastScore;
cursor.chr = hallOfFame[lastScore].name.at(cursor.x-14);
// startTimer(cursorBlinkMS);
setFocus();
}
/*
* Read the highscores, if no file or a file shorter than 4 bytes (versions before 0.2.4 stores only
* the points of one highscore) exists - the highscores were initialized with default values.
*/
void Score::read()
{
if (highscoreFile.exists() && highscoreFile.size() > 4) {
if (highscoreFile.open(IO_ReadOnly)) {
QDataStream s(&highscoreFile);
char *name;
for (int i = 0; i < 10; i++) {
s >> hallOfFame[i].points >> hallOfFame[i].levels >> hallOfFame[i].duration >>
hallOfFame[i].moment >> name;
hallOfFame[i].name = QString::fromLatin1(name);
delete(name);
}
highscoreFile.close();
}
} else {
for (int i = 0; i < 10; i++) {
hallOfFame[i].points = 5000;
hallOfFame[i].levels = 0;
hallOfFame[i].duration = QTime();
hallOfFame[i].moment = QDateTime();
hallOfFame[i].name = "???";
}
// write();
}
for (int i = 0; i < 10; i++)
for (uint j = 0; j < hallOfFame[i].name.length(); j++)
if (hallOfFame[i].name.at(j) < bitfont->firstChar() ||
hallOfFame[i].name.at(j) > bitfont->lastChar())
hallOfFame[i].name.at(j) = hallOfFame[i].name.at(j).upper();
HighScore = hallOfFame[0].points;
}
void Score::write()
{
-#ifndef QWS
- if (!highscoreFile.exists() && highscoreFile.name() == systemHighscoreFileInfo.filePath())
- KMessageBox::information(0,
- tr("You're going to create the highscore-file\n"
- "'%1'\n"
- "for your maschine, that should be used systemwide.\n"
- "\n"
- "To grant access to the other users, set the appropriate rights (a+w)\n"
- "on that file or ask your systemadministator for that favor.\n"
- "\n"
- "To use a different directory or filename for the highscores,"
- "specify them in the configfile (kpacmanrc:highscoreFilePath)."
- ).arg(systemHighscoreFileInfo.filePath()));
-
- if (highscoreFile.name() == privateHighscoreFileInfo.filePath())
- KMessageBox::information(0,
- tr("You're using a private highscore-file, that's mostly because of\n"
- "missing write-access to the systemwide file\n"
- "'%1' .\n"
- "\n"
- "Ask your systemadministrator for granting you access to that file,\n"
- "by setting the appropriate rights (a+w) on it.\n"
- "\n"
- "To use a different directory or filename for the highscores,"
- "specify them in the configfile (kpacmanrc:highscoreFilePath)."
- ).arg(systemHighscoreFileInfo.filePath()),
- QString::null, "PrivateHighscore");
-#endif
if (highscoreFile.open(IO_WriteOnly)) {
QDataStream s(&highscoreFile);
for (int i = 0; i < 10; i++)
s << hallOfFame[i].points << hallOfFame[i].levels << hallOfFame[i].duration <<
hallOfFame[i].moment << hallOfFame[i].name.latin1();
highscoreFile.close();
}
}
void Score::setPause(bool Paused)
{
paused = Paused;
QRect r = bitfont->rect(tr("PAUSED"));
r.moveCenter(QPoint(this->width()/2, this->height()/2));
repaint(r, TRUE);
// repaint 1UP or 2UP
repaint(FALSE);
}
void Score::end()
{
if (paused) {
QTimer::singleShot(afterPauseMS, this, SLOT(end()));
return;
}
// repaint 1UP or 2UP
lastPlayer = -1;
repaint(FALSE);
emit forcedHallOfFame(FALSE);
}
/*
* Return the date in a formatted QString. The format can be changed using internationalization
* of the string "YY/MM/DD". Invalid QDate's where returned as "00/00/00".
*/
QString Score::formatDate(QDate date)
{
QString s = tr("@YY@/@MM@/@DD@");
QString dd;
dd.sprintf("%02d", date.isValid() ? date.year() % 100 : 0);
s.replace(QRegExp("@YY@"), dd);
dd.sprintf("%02d", date.isValid() ? date.month() : 0);
s.replace(QRegExp("@MM@"), dd);
dd.sprintf("%02d", date.isValid() ? date.day() : 0);
s.replace(QRegExp("@DD@"), dd);
return s;
}
QRect Score::rect(int col, float row, QString str, int align)
{
QRect r = bitfont->rect(str);
r.moveBy(x(col), y(row));
int dx = 0;
int dy = 0;
if (align & AlignLeft || align & AlignRight) {
dx = (str.length()-1) * (bitfont->width()/2);
if (align & AlignRight)
dx *= -1;
}
if (align & AlignTop || align & AlignBottom) {
dy = bitfont->height()/2;
if (align & AlignBottom)
dy *= -1;
}
if (dx != 0 || dy != 0)
r.moveBy(dx, dy);
return r;
}
int Score::x(int col)
{
return col*bitfont->width();
}
int Score::y(float row)
{
return (int) (row*(bitfont->height()+bitfont->height()/4));
}
/**
* Ermittelt die zu benutzende "highscore"-Datei, in die auch geschrieben werden kann.
* Über den "highscoreFilePath"-KConfig-Eintrag, kann abweichend von der Standardlokation
* der Standort der "highscore"-Datei spezifiziert werden.
* Wenn die systemweite "highscore"-Datei nicht beschrieben werden kann, wird mit einer
* privaten Datei gearbeitet.
*/
QFileInfo Score::locateHighscoreFilePath()
{
-#ifndef QWS
- QFileInfo systemHighscoreDirPath;
- QStringList systemHighscoreDirs;
-
- // Schreibfähige "private" highscore-Datei ermitteln für den fallback.
- privateHighscoreFileInfo.setFile(KGlobal::dirs()->saveLocation("appdata")+highscoreName);
-
- // FilePath aus der Konfigurationsdatei benutzen
- systemHighscoreFileInfo.setFile(cfg->readEntry("HighscoreFilePath"));
-
- // Kein Wert aus der Konfiguration erhalten, dann die "system"-Datei suchen.
- if (systemHighscoreFileInfo.filePath().isEmpty())
- systemHighscoreDirs = KGlobal::dirs()->resourceDirs("appdata");
- else
- systemHighscoreDirs = QStringList(systemHighscoreFileInfo.filePath());
-
- for (QStringList::Iterator i = systemHighscoreDirs.begin(); i != systemHighscoreDirs.end(); ++i) {
-
- systemHighscoreFileInfo.setFile(*i);
- if (systemHighscoreFileInfo.fileName().isEmpty())
- systemHighscoreFileInfo.setFile(systemHighscoreFileInfo.dirPath()+"/"+highscoreName);
-
- // privateHighscoreFileInfo für die "system" Suche ignorieren
- if (systemHighscoreFileInfo.filePath() != privateHighscoreFileInfo.filePath())
- if (!systemHighscoreFileInfo.exists()) {
- systemHighscoreDirPath.setFile(systemHighscoreFileInfo.dirPath());
- if (systemHighscoreDirPath.exists() && systemHighscoreDirPath.isWritable())
- return systemHighscoreFileInfo;
- } else
- if (systemHighscoreFileInfo.isWritable())
- return systemHighscoreFileInfo;
- }
-#endif
return privateHighscoreFileInfo;
}