summaryrefslogtreecommitdiff
path: root/noncore/games/kpacman/kpacman.cpp
Unidiff
Diffstat (limited to 'noncore/games/kpacman/kpacman.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/kpacman/kpacman.cpp369
1 files changed, 369 insertions, 0 deletions
diff --git a/noncore/games/kpacman/kpacman.cpp b/noncore/games/kpacman/kpacman.cpp
new file mode 100644
index 0000000..4077085
--- a/dev/null
+++ b/noncore/games/kpacman/kpacman.cpp
@@ -0,0 +1,369 @@
1
2#include "portable.h"
3
4#if defined( KDE2_PORT )
5#include <kpacman.h>
6#include <kpacman.moc>
7#include <kcolordlg.h>
8#elif defined( QPE_PORT )
9#include <qmenubar.h>
10#include "config.h"
11#include <qapplication.h>
12#include "kpacman.h"
13#endif
14
15#include <qkeycode.h>
16#include <qcolor.h>
17#include <qstring.h>
18#include <qpopmenu.h>
19#include <qmsgbox.h>
20
21Kpacman::Kpacman(QWidget *parent, const char *name)
22 : KTMainWindow(parent, name)
23{
24 schemesPopup = new QList<QPopupMenu>;
25 schemesPopup->setAutoDelete(TRUE);
26
27 menu();
28
29 view = new KpacmanWidget( this, QString(name)+"widget");
30
31#ifndef QWS
32 setFixedSize(view->width(), view->height());
33#else
34 setCaption( "Kpacman" );
35#endif
36
37 view->referee->setFocus();
38
39 connect(view->referee, SIGNAL(setScore(int, int)),
40 view->score, SLOT(setScore(int, int)));
41 connect(view->referee, SIGNAL(setPoints(int)),
42 view->score, SLOT(set(int)));
43 connect(view->referee, SIGNAL(setLifes(int)),
44 view->status, SLOT(setLifes(int)));
45 connect(view->referee, SIGNAL(setLevel(int)),
46 view->status, SLOT(setLevel(int)));
47 connect(view->referee, SIGNAL(forcedHallOfFame(bool)),
48 this, SLOT(forcedHallOfFame(bool)));
49 connect(view->referee, SIGNAL(togglePaused()), this, SLOT(togglePaused()));
50 connect(view->referee, SIGNAL(toggleNew()), this, SLOT(toggleNew()));
51
52 connect(view->score, SIGNAL(toggleNew()), this, SLOT(toggleNew()));
53 connect(view->score, SIGNAL(forcedHallOfFame(bool)),
54 this, SLOT(forcedHallOfFame(bool)));
55
56 APP_CONFIG_BEGIN( cfg );
57 focusOutPause = !cfg->readBoolEntry("FocusOutPause", TRUE);
58 focusInContinue = !cfg->readBoolEntry("FocusInContinue", TRUE);
59 hideMouseCursor = !cfg->readBoolEntry("HideMouseCursor", TRUE);
60 APP_CONFIG_END( cfg );
61
62 toggleFocusOutPause();
63 toggleFocusInContinue();
64 toggleHideMouseCursor();
65
66#ifndef QWS
67 menuBar->show();
68 view->show();
69 setMenu(menuBar);
70 setView(view);
71#else
72 setCentralWidget( view );
73#endif
74}
75
76Kpacman::~Kpacman()
77{
78 APP_CONFIG_BEGIN( cfg );
79 cfg->writeEntry("FocusOutPause", focusOutPause);
80 cfg->writeEntry("FocusInContinue", focusInContinue);
81 cfg->writeEntry("HideMouseCursor", hideMouseCursor);
82 APP_CONFIG_END( cfg );
83 delete _menuBar;
84}
85
86void Kpacman::menu()
87{
88 gamePopup = new QPopupMenu();
89 CHECK_PTR( gamePopup );
90 newID = gamePopup->insertItem(i18n("&New"), this, SLOT(newKpacman()),Key_F2);
91 pauseID = gamePopup->insertItem(i18n("&Pause"),
92 this, SLOT(pauseKpacman()), Key_F3);
93 hofID = gamePopup->insertItem(i18n("&Hall of fame"),
94 this, SLOT(toggleHallOfFame()), Key_F4);
95 gamePopup->insertSeparator();
96 gamePopup->insertItem(i18n("&Quit"), this, SLOT(quitKpacman()), CTRL+Key_Q);
97 gamePopup->setCheckable(TRUE);
98
99 optionsPopup = new QPopupMenu();
100 CHECK_PTR(optionsPopup);
101
102 modesPopup = new QPopupMenu();
103 CHECK_PTR(modesPopup);
104
105 hideMouseCursorID = optionsPopup->insertItem(i18n("&Hide Mousecursor"),
106 this, SLOT(toggleHideMouseCursor()),
107 CTRL+Key_H);
108 optionsPopup->insertSeparator();
109
110 if (lookupSchemes() > 0) {
111 optionsPopup->insertItem(i18n("&Select graphic scheme"), modesPopup);
112 optionsPopup->insertSeparator();
113 }
114
115 focusOutPauseID = optionsPopup->insertItem(i18n("&Pause in Background"),
116 this, SLOT(toggleFocusOutPause()));
117 focusInContinueID = optionsPopup->insertItem(i18n("&Continue in Foreground"),
118 this, SLOT(toggleFocusInContinue()));
119 optionsPopup->insertSeparator();
120
121 optionsPopup->insertItem(i18n("Change &keys..."), this, SLOT(confKeys()));
122
123#ifndef QWS
124 QString aboutText = i18n("@PACKAGE@ - @VERSION@\n\n"
125 "Joerg Thoennissen (joe@dsite.de)\n\n"
126 "A pacman game for the KDE Desktop\n\n"
127 "The program based on the source of ksnake\n"
128 "by Michel Filippi (mfilippi@sade.rhein-main.de).\n"
129 "The design was strongly influenced by the pacman\n"
130 "(c) 1980 MIDWAY MFG.CO.\n\n"
131 "I like to thank my girlfriend Elke Krueers for\n"
132 "the last 10 years of her friendship.\n");
133 aboutText.replace(QRegExp("@PACKAGE@"), PACKAGE);
134 aboutText.replace(QRegExp("@VERSION@"), VERSION);
135 QPopupMenu *helpPopup = helpMenu(aboutText, FALSE);
136#endif
137
138 //_menuBar = new KMenuBar(this);
139 //CHECK_PTR( _menuBar );
140 //_menuBar->insertItem(i18n("&Game"), gamePopup);
141 //_menuBar->insertItem(i18n("&Options"), optionsPopup);
142 //_menuBar->insertSeparator();
143#ifndef QWS
144 _menuBar->insertItem(i18n("&Help"), helpPopup);
145#endif
146}
147
148int Kpacman::lookupSchemes()
149{
150 APP_CONFIG_BEGIN( cfg );
151 int ModeCount = cfg->readNumEntry("ModeCount", -1);
152 int Mode = cfg->readNumEntry("Mode", -1);
153 int SchemeCount = cfg->readNumEntry("SchemeCount");
154 int Scheme = cfg->readNumEntry("Scheme", -1);
155
156 if (SchemeCount == 0 || Scheme == -1) {
157 QMessageBox::warning(this, i18n("Configuration Error"),
158 i18n("There are no schemes defined,\n"
159 "or no scheme is selected."));
160 APP_CONFIG_END( cfg );
161 return 0;
162 }
163
164 connect(modesPopup, SIGNAL(activated(int)), this, SLOT(schemeChecked(int)));
165 modeID.resize(ModeCount > 0 ? ModeCount : 0);
166
167 if (!schemesPopup->isEmpty())
168 schemesPopup->clear();
169
170 SAVE_CONFIG_GROUP( cfg, oldgroup );
171
172 QString ModeGroup;
173 QString ModeName;
174
175 for (int m = 0; m < ModeCount; m++) {
176 ModeGroup.sprintf("Mode %d", m);
177 cfg->setGroup(ModeGroup);
178
179 ModeName = cfg->readEntry("Description", ModeGroup);
180
181 QPopupMenu *p = new QPopupMenu;
182 p->setCheckable(TRUE);
183 connect(p, SIGNAL(activated(int)), this, SLOT(schemeChecked(int)));
184 schemesPopup->append(p);
185
186 modeID[m] = modesPopup->insertItem(ModeName, schemesPopup->at(m));
187 modesPopup->setItemEnabled(modeID[m], FALSE);
188 modesPopup->setItemChecked(modeID[m], m == Mode);
189 }
190
191 schemeID.resize(SchemeCount);
192 schemeMode.resize(SchemeCount);
193
194 QString SchemeGroup;
195 QString SchemeName;
196 int SchemeMode;
197
198 for (int i = 0; i < SchemeCount; i++) {
199 SchemeGroup.sprintf("Scheme %d", i);
200 cfg->setGroup(SchemeGroup);
201
202 SchemeName = cfg->readEntry("Description", SchemeGroup);
203 SchemeMode = cfg->readNumEntry("Mode", -1);
204
205 schemeMode[i] = SchemeMode;
206 if (SchemeMode == -1) {
207 schemeID[i] = modesPopup->insertItem(SchemeName);
208 modesPopup->setItemChecked(schemeID[i], i == Scheme);
209 } else {
210 schemeID[i] = schemesPopup->at(SchemeMode)->insertItem(SchemeName);
211 schemesPopup->at(SchemeMode)->
212 setItemChecked(schemeID[i], i == Scheme);
213 modesPopup->setItemEnabled(modeID[SchemeMode], TRUE);
214 }
215 }
216
217 RESTORE_CONFIG_GROUP( cfg, oldgroup );
218
219 APP_CONFIG_END( cfg );
220 return SchemeCount;
221}
222
223void Kpacman::quitKpacman()
224{
225 APP_QUIT();
226}
227
228void Kpacman::newKpacman()
229{
230 if (!gamePopup->isItemEnabled(hofID))
231 gamePopup->setItemEnabled(hofID, TRUE);
232
233 if (gamePopup->isItemChecked(hofID))
234 toggleHallOfFame();
235
236 if (gamePopup->isItemChecked(pauseID))
237 pauseKpacman();
238
239 view->referee->play();
240}
241
242void Kpacman::pauseKpacman()
243{
244 view->referee->pause();
245 view->score->setPause(gamePopup->isItemChecked(pauseID));
246}
247
248void Kpacman::toggleHallOfFame()
249{
250 gamePopup->setItemChecked(hofID, !gamePopup->isItemChecked(hofID));
251 view->referee->toggleHallOfFame();
252
253 if (gamePopup->isItemChecked(hofID)) {
254 view->referee->lower();
255 view->status->lower();
256 } else {
257 view->status->raise();
258 view->referee->raise();
259 view->referee->setFocus();
260 }
261}
262
263/*
264 * Disable or enable the "Hall of fame"-menuitem if the referee says so.
265 * This is done, to disable turning off the "hall of fame"-display, in the automated
266 * sequence of displaying the introduction, the demonstration (or playing) and the
267 * hall of fame.
268 * If on == TRUE then also lower the referee and the status widgets.
269 */
270void Kpacman::forcedHallOfFame(bool on)
271{
272 if (!on && !gamePopup->isItemChecked(hofID))
273 return;
274
275 gamePopup->setItemEnabled(hofID, !on);
276 gamePopup->setItemChecked(hofID, on);
277
278 view->referee->toggleHallOfFame();
279 if (on) {
280 view->referee->lower();
281 view->status->lower();
282 } else {
283 view->status->raise();
284 view->referee->raise();
285 view->referee->setFocus();
286 view->referee->intro();
287 }
288}
289
290void Kpacman::togglePaused()
291{
292 static bool checked = FALSE;
293 checked = !checked;
294 gamePopup->setItemChecked( pauseID, checked );
295 view->score->setPause(gamePopup->isItemChecked(pauseID));
296}
297
298/*
299 * This disables the "New Game" menuitem to prevent interruptions of the current
300 * play.
301 */
302void Kpacman::toggleNew()
303{
304 gamePopup->setItemEnabled(newID, !gamePopup->isItemEnabled(newID));
305}
306
307void Kpacman::toggleHideMouseCursor()
308{
309 hideMouseCursor = !hideMouseCursor;
310 optionsPopup->setItemChecked(hideMouseCursorID, hideMouseCursor);
311 if (hideMouseCursor)
312 view->setCursor(blankCursor);
313 else
314 view->setCursor(arrowCursor);
315}
316
317void Kpacman::toggleFocusOutPause()
318{
319 focusOutPause = !focusOutPause;
320 optionsPopup->setItemChecked(focusOutPauseID, focusOutPause);
321 view->referee->setFocusOutPause(focusOutPause);
322}
323
324void Kpacman::toggleFocusInContinue()
325{
326 focusInContinue = !focusInContinue;
327 optionsPopup->setItemChecked(focusInContinueID, focusInContinue);
328 view->referee->setFocusInContinue(focusInContinue);
329}
330
331void Kpacman::confKeys()
332{
333 Keys *keys = new Keys();
334 if (keys->exec() == QDialog::Accepted) {
335 view->referee->initKeys();
336 view->score->initKeys();
337 }
338 delete keys;
339}
340
341void Kpacman::schemeChecked(int id)
342{
343 int mode = 0, scheme = -1;
344
345 for (uint s = 0; s < schemeID.size(); s++) {
346 if (schemeID[s] == id) {
347 scheme = s;
348 mode = schemeMode[s];
349 }
350 if (schemeMode[s] == -1) {
351 modesPopup->setItemChecked(schemeID[s], schemeID[s] == id);
352 } else {
353 modesPopup->setItemChecked(modeID[schemeMode[s]], schemeMode[s] == mode);
354 schemesPopup->at(schemeMode[s])->setItemChecked(schemeID[s], schemeID[s] == id);
355 }
356 }
357
358 APP_CONFIG_BEGIN( cfg );
359 cfg->writeEntry("Scheme", scheme);
360 cfg->writeEntry("Mode", mode);
361 APP_CONFIG_END( cfg );
362
363 view->setScheme(scheme, mode);
364 view->updateGeometry();
365 updateGeometry();
366 update();
367 repaint(TRUE);
368 show();
369}