Diffstat (limited to 'noncore/games/kpacman/status.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/games/kpacman/status.cpp | 367 |
1 files changed, 367 insertions, 0 deletions
diff --git a/noncore/games/kpacman/status.cpp b/noncore/games/kpacman/status.cpp new file mode 100644 index 0000000..00d1f22 --- a/dev/null +++ b/noncore/games/kpacman/status.cpp | |||
@@ -0,0 +1,367 @@ | |||
1 | |||
2 | #include "portable.h" | ||
3 | |||
4 | #if defined( KDE2_PORT ) | ||
5 | #include <kapp.h> | ||
6 | #include <klocale.h> | ||
7 | #include <kstddirs.h> | ||
8 | #include <status.h> | ||
9 | #include <status.moc> | ||
10 | #elif defined( QPE_PORT ) | ||
11 | #include <qpe/qpeapplication.h> | ||
12 | #include "config.h" | ||
13 | #include "status.h" | ||
14 | #endif | ||
15 | |||
16 | #include <qpixmap.h> | ||
17 | #include <qbitmap.h> | ||
18 | #include <qstring.h> | ||
19 | #include <qmsgbox.h> | ||
20 | #include <qfileinfo.h> | ||
21 | |||
22 | Status::Status( QWidget *parent, const char *name, int Scheme, int Mode ) : | ||
23 | QWidget( parent, name ) | ||
24 | { | ||
25 | actualLifes = 0; | ||
26 | actualLevel = 0; | ||
27 | |||
28 | lifesPix = NULL; | ||
29 | levelPix = NULL; | ||
30 | |||
31 | scheme = Scheme; | ||
32 | mode = Mode; | ||
33 | level = 0; | ||
34 | |||
35 | confScheme(); | ||
36 | } | ||
37 | |||
38 | QList<QPixmap> *Status::loadPixmap(QWidget *parent, QString pixmapName, | ||
39 | QList<QPixmap> *pixmaps) | ||
40 | { | ||
41 | if (pixmaps == NULL) { | ||
42 | pixmaps = new QList<QPixmap>; | ||
43 | pixmaps->setAutoDelete(TRUE); | ||
44 | } | ||
45 | |||
46 | if (!pixmaps->isEmpty()) | ||
47 | pixmaps->clear(); | ||
48 | |||
49 | QPixmap PIXMAP(pixmapName); | ||
50 | if (PIXMAP.isNull() || PIXMAP.mask() == NULL) { | ||
51 | QString msg = i18n("The pixmap could not be contructed.\n\n" | ||
52 | "The file '@PIXMAPNAME@' does not exist,\n" | ||
53 | "or is of an unknown format."); | ||
54 | msg.replace(QRegExp("@PIXMAPNAME@"), pixmapName); | ||
55 | QMessageBox::information(parent, i18n("Initialization Error"), | ||
56 | (const char *) msg); | ||
57 | return 0; | ||
58 | } | ||
59 | |||
60 | int height = PIXMAP.height(); | ||
61 | int width = (height == 0) ? 0 : PIXMAP.width()/(PIXMAP.width()/height); | ||
62 | |||
63 | QBitmap BITMAP; | ||
64 | QBitmap MASK; | ||
65 | |||
66 | BITMAP = *PIXMAP.mask(); | ||
67 | MASK.resize(width, height); | ||
68 | |||
69 | for (int x = 0; x < PIXMAP.width()/width; x++) { | ||
70 | QPixmap *pixmap = new QPixmap(width, height); | ||
71 | pixmaps->append(pixmap); | ||
72 | bitBlt(pixmap, 0, 0, &PIXMAP, x*width, 0, width, height, CopyROP, TRUE); | ||
73 | bitBlt(&MASK, 0, 0, &BITMAP, x*width, 0, width, height, CopyROP, TRUE); | ||
74 | pixmap->setMask(MASK); | ||
75 | } | ||
76 | |||
77 | return pixmaps; | ||
78 | } | ||
79 | |||
80 | void Status::paintEvent( QPaintEvent *) | ||
81 | { | ||
82 | for (int x = 0; x < actualLifes && !lifesPix->isEmpty(); x++) | ||
83 | bitBlt(this, lifesPix->at(0)->width()+(lifesPix->at(0)->width()*x), | ||
84 | (height()-lifesPix->at(0)->height())/2, | ||
85 | lifesPix->at(0), 0, 0, | ||
86 | lifesPix->at(0)->width(), lifesPix->at(0)->height()); | ||
87 | |||
88 | for (int x = 0; x < actualLevel && !levelPix->isEmpty(); x++) { | ||
89 | erase((width()-levelPix->at(x)->width()*2)-(levelPix->at(x)->width()*levelPos[x]), | ||
90 | (height()-levelPix->at(x)->height())/2, | ||
91 | levelPix->at(x)->width(), levelPix->at(x)->height()); | ||
92 | bitBlt(this, (width()-levelPix->at(x)->width()*2)-(levelPix->at(x)->width()*levelPos[x]), | ||
93 | (height()-levelPix->at(x)->height())/2, | ||
94 | levelPix->at(x), 0, 0, | ||
95 | levelPix->at(x)->width(), levelPix->at(x)->height()); | ||
96 | } | ||
97 | } | ||
98 | |||
99 | void Status::initPixmaps() | ||
100 | { | ||
101 | if (lastLifesPixmapName != lifesPixmapName.at(level)) { | ||
102 | lifesPix = loadPixmap(this, lifesPixmapName.at(level), lifesPix); | ||
103 | lastLifesPixmapName = lifesPixmapName.at(level); | ||
104 | } | ||
105 | if (lastLevelPixmapName != levelPixmapName.at(level)) { | ||
106 | levelPix = loadPixmap(this, levelPixmapName.at(level), levelPix); | ||
107 | lastLevelPixmapName = levelPixmapName.at(level); | ||
108 | } | ||
109 | } | ||
110 | |||
111 | QString Status::decodeHexOctString(QString s) | ||
112 | { | ||
113 | QString value; | ||
114 | QString valids; | ||
115 | int pos, xpos = 0, opos = 0; | ||
116 | int v, len, leadin; | ||
117 | const char *ptr; | ||
118 | uchar c; | ||
119 | |||
120 | while (((xpos = s.find(QRegExp("\\\\x[0-9a-fA-F]+"), xpos)) != -1) || | ||
121 | ((opos = s.find(QRegExp("\\\\[0-7]+"), opos)) != -1)) { | ||
122 | if (xpos != -1) { | ||
123 | valids = "0123456789abcdef"; | ||
124 | leadin = 2; | ||
125 | pos = xpos; | ||
126 | } else { | ||
127 | valids = "01234567"; | ||
128 | leadin = 1; | ||
129 | pos = opos; | ||
130 | } | ||
131 | |||
132 | c = '\0'; | ||
133 | len = 0; | ||
134 | value = s.mid(pos+leadin, 3); | ||
135 | ptr = (const char *) value; | ||
136 | |||
137 | while (*ptr != '\0' && (v = valids.find(*ptr++, 0, FALSE)) != -1) { | ||
138 | c = (c * valids.length()) + v; | ||
139 | len++; | ||
140 | } | ||
141 | |||
142 | value.fill(c, 1); | ||
143 | s.replace(pos, len+leadin, value); | ||
144 | } | ||
145 | |||
146 | return s; | ||
147 | } | ||
148 | |||
149 | void Status::fillArray(QArray<int> &array, QString values, int max) | ||
150 | { | ||
151 | array.resize(max); | ||
152 | int last = 0; | ||
153 | bool ok; | ||
154 | QString value; | ||
155 | |||
156 | for (uint i = 0; i < array.size(); i++) { | ||
157 | if (values.find(',') < 0 && values.length() > 0) { | ||
158 | value = values; | ||
159 | values = ""; | ||
160 | } | ||
161 | if (values.find(',') >= 0) { | ||
162 | value = values.left(values.find(',')); | ||
163 | values.remove(0,values.find(',')+1); | ||
164 | } | ||
165 | array[i] = value.toInt(&ok); | ||
166 | if (ok) | ||
167 | last = array[i]; | ||
168 | else | ||
169 | array[i] = last; | ||
170 | } | ||
171 | } | ||
172 | |||
173 | void Status::fillStrList(QStrList &list, QString values, int max) | ||
174 | { | ||
175 | if (!list.isEmpty()) | ||
176 | list.clear(); | ||
177 | |||
178 | QString last = ""; | ||
179 | QString value; | ||
180 | |||
181 | for (uint i = 0; i < (uint) max; i++) { | ||
182 | if (values.find(',') < 0 && values.length() > 0) { | ||
183 | value = values; | ||
184 | values = ""; | ||
185 | } | ||
186 | if (values.find(',') >= 0) { | ||
187 | value = values.left(values.find(',')); | ||
188 | values.remove(0,values.find(',')+1); | ||
189 | } | ||
190 | if (!value.isEmpty()) | ||
191 | last = decodeHexOctString(value); | ||
192 | list.append(last); | ||
193 | } | ||
194 | } | ||
195 | |||
196 | void Status::fillPixmapName(QStrList &pixmapName) | ||
197 | { | ||
198 | QStrList list = pixmapName; | ||
199 | |||
200 | if (!pixmapName.isEmpty()) | ||
201 | pixmapName.clear(); | ||
202 | |||
203 | QString pixmap; | ||
204 | |||
205 | QFileInfo fileInfo; | ||
206 | |||
207 | for (uint i = 0; i < list.count(); i++) { | ||
208 | pixmap = list.at(i); | ||
209 | |||
210 | if (pixmap.left(1) != "/" && pixmap.left(1) != "~") | ||
211 | pixmap = FIND_APP_DATA( pixmapDirectory+pixmap ); | ||
212 | |||
213 | fileInfo.setFile(pixmap); | ||
214 | if (!fileInfo.isReadable() || !fileInfo.isFile()) | ||
215 | pixmap = ""; | ||
216 | |||
217 | pixmapName.append(pixmap); | ||
218 | } | ||
219 | } | ||
220 | |||
221 | void Status::confLevels(bool defGroup) | ||
222 | { | ||
223 | APP_CONFIG_BEGIN( cfg ); | ||
224 | if (defGroup || cfg->hasKey("Levels")) | ||
225 | maxLevel = cfg->readNumEntry("Levels", 13); | ||
226 | APP_CONFIG_END( cfg ); | ||
227 | } | ||
228 | |||
229 | void Status::confMisc(bool defGroup) | ||
230 | { | ||
231 | APP_CONFIG_BEGIN( cfg ); | ||
232 | if (defGroup || cfg->hasKey("LevelPosition")) | ||
233 | fillArray(levelPos, cfg->readEntry("LevelPosition", "0,1,2,3,,4,,5,,6,,7"), maxLevel); | ||
234 | |||
235 | if (defGroup || cfg->hasKey("PixmapDirectory")) { | ||
236 | pixmapDirectory = cfg->readEntry("PixmapDirectory"); | ||
237 | |||
238 | if (pixmapDirectory.left(1) != "/" && pixmapDirectory.left(1) != "~") | ||
239 | pixmapDirectory.insert(0, "pics/"); | ||
240 | if (pixmapDirectory.right(1) != "/") | ||
241 | pixmapDirectory.append("/"); | ||
242 | } | ||
243 | |||
244 | if (defGroup || cfg->hasKey("LifesPixmapName")) | ||
245 | fillStrList(lifesPixmapName, | ||
246 | cfg->readEntry("LifesPixmapName", "lifes.xpm"), maxLevel+1); | ||
247 | if (defGroup || cfg->hasKey("LevelPixmapName")) | ||
248 | fillStrList(levelPixmapName, | ||
249 | cfg->readEntry("LevelPixmapName", "level.xpm"), maxLevel+1); | ||
250 | APP_CONFIG_END( cfg ); | ||
251 | } | ||
252 | |||
253 | void Status::confScheme() | ||
254 | { | ||
255 | APP_CONFIG_BEGIN( cfg ); | ||
256 | SAVE_CONFIG_GROUP( cfg, oldgroup ); | ||
257 | QString newgroup; | ||
258 | |||
259 | // if not set, read mode and scheme from the configfile | ||
260 | if (mode == -1 && scheme == -1) { | ||
261 | scheme = cfg->readNumEntry("Scheme", -1); | ||
262 | mode = cfg->readNumEntry("Mode", -1); | ||
263 | |||
264 | // if mode is not set in the defGroup-group, lookup the scheme group | ||
265 | if (scheme != -1 || mode == -1) { | ||
266 | newgroup.sprintf("Scheme %d", scheme); | ||
267 | cfg->setGroup(newgroup); | ||
268 | |||
269 | mode = cfg->readNumEntry("Mode", -1); | ||
270 | RESTORE_CONFIG_GROUP( cfg, oldgroup ); | ||
271 | } | ||
272 | } | ||
273 | |||
274 | confLevels(); | ||
275 | |||
276 | if (mode != -1) { | ||
277 | newgroup.sprintf("Mode %d", mode); | ||
278 | cfg->setGroup(newgroup); | ||
279 | |||
280 | confLevels(FALSE); | ||
281 | } | ||
282 | |||
283 | if (scheme != -1) { | ||
284 | newgroup.sprintf("Scheme %d", scheme); | ||
285 | cfg->setGroup(newgroup); | ||
286 | |||
287 | confLevels(FALSE); | ||
288 | } | ||
289 | |||
290 | RESTORE_CONFIG_GROUP( cfg, oldgroup ); | ||
291 | |||
292 | confMisc(); | ||
293 | |||
294 | if (mode != -1) { | ||
295 | newgroup.sprintf("Mode %d", mode); | ||
296 | cfg->setGroup(newgroup); | ||
297 | |||
298 | confMisc(FALSE); | ||
299 | } | ||
300 | |||
301 | if (scheme != -1) { | ||
302 | newgroup.sprintf("Scheme %d", scheme); | ||
303 | cfg->setGroup(newgroup); | ||
304 | |||
305 | confMisc(FALSE); | ||
306 | } | ||
307 | |||
308 | fillPixmapName(lifesPixmapName); | ||
309 | fillPixmapName(levelPixmapName); | ||
310 | |||
311 | initPixmaps(); | ||
312 | |||
313 | setFixedHeight(minHeight()); | ||
314 | |||
315 | RESTORE_CONFIG_GROUP( cfg, oldgroup ); | ||
316 | APP_CONFIG_END( cfg ); | ||
317 | } | ||
318 | |||
319 | void Status::setScheme(int Scheme, int Mode) | ||
320 | { | ||
321 | mode = Mode; | ||
322 | scheme = Scheme; | ||
323 | |||
324 | confScheme(); | ||
325 | |||
326 | repaint(); | ||
327 | } | ||
328 | |||
329 | int Status::minHeight() | ||
330 | { | ||
331 | if (lifesPix->isEmpty() && levelPix->isEmpty()) | ||
332 | return 0; | ||
333 | if (levelPix->isEmpty()) | ||
334 | return lifesPix->at(0)->height(); | ||
335 | if (lifesPix->isEmpty()) | ||
336 | return levelPix->at(0)->height(); | ||
337 | return (lifesPix->at(0)->height() > levelPix->at(0)->height()) ? | ||
338 | lifesPix->at(0)->height() : levelPix->at(0)->height(); | ||
339 | } | ||
340 | |||
341 | int Status::minWidth() | ||
342 | { | ||
343 | if (lifesPix->isEmpty() && levelPix->isEmpty()) | ||
344 | return 0; | ||
345 | if (levelPix->isEmpty()) | ||
346 | return lifesPix->at(0)->width(); | ||
347 | if (lifesPix->isEmpty()) | ||
348 | return levelPix->at(0)->width(); | ||
349 | return (lifesPix->at(0)->width() > levelPix->at(0)->width()) ? | ||
350 | lifesPix->at(0)->width() : levelPix->at(0)->width(); | ||
351 | } | ||
352 | |||
353 | void Status::setLifes(int lifes) | ||
354 | { | ||
355 | actualLifes = lifes; | ||
356 | repaint(); | ||
357 | } | ||
358 | |||
359 | void Status::setLevel(int Level) | ||
360 | { | ||
361 | level = Level; | ||
362 | |||
363 | initPixmaps(); | ||
364 | |||
365 | actualLevel = (level > (int) levelPix->count()) ? (int) levelPix->count() : level; | ||
366 | repaint(); | ||
367 | } | ||