summaryrefslogtreecommitdiff
path: root/noncore/games/kpacman/kpacman.cpp
blob: 4a3b87034f20e128b75503fd5057695fbedca6ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363

#include "portable.h"

#if defined( KDE2_PORT )
#include <kpacman.h>
#include <kpacman.moc>
#include <kcolordlg.h>
#elif defined( QPE_PORT )
#include <qmenubar.h>
#include <qpe/config.h>
#include <qapplication.h>
#include "kpacman.h"
#endif


Kpacman::Kpacman(QWidget *parent, const char *name, WFlags fl)
        : KTMainWindow(parent, name,fl)
{
    schemesPopup = new QList<QPopupMenu>;
    schemesPopup->setAutoDelete(TRUE);

    menu();

	m_view = new QWidget( this, "m_view" );
    m_view->setBackgroundColor( black );
	m_layout = new QGridLayout( m_view );
	m_layout->setMargin( 7 );

	view = new KpacmanWidget( this, QString(name)+"widget");
	m_layout->addWidget( view, 0, 0 );

    setCaption( tr("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();

    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 QPE_PORT
    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 QPE_PORT
    _menuBar->insertItem(tr("&Help"), helpPopup);
#endif
}

int Kpacman::lookupSchemes()
{
    APP_CONFIG_BEGIN( cfg );
    int ModeCount = cfg->readNumEntry("ModeCount", 0);
    int Mode = cfg->readNumEntry("Mode", 0);
    int SchemeCount = cfg->readNumEntry("SchemeCount", 0);
    int Scheme = cfg->readNumEntry("Scheme", 0);

	/*
    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();
}