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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
|
/* -------------------------------------------------------------
KDE Tuberling
Play ground widget
mailto:[email protected]
------------------------------------------------------------- */
#include <stdlib.h>
#include <kmessagebox.h>
#include <klocale.h>
#include <kstandarddirs.h>
#include <kprinter.h>
#include <tqfile.h>
#include <tqpainter.h>
#include <tqimage.h>
#include <tqcursor.h>
#include <tqdom.h>
#include "playground.moc"
#include "toplevel.h"
#define XMARGIN 5
#define YMARGIN 5
// Constructor
PlayGround::PlayGround(TopLevel *parent, const char *name, uint selectedGameboard)
: TQWidget(parent, name)
{
topLevel = parent;
textsLayout = objectsLayout = 0;
textsList = soundsList = 0;
draggedCursor = 0;
toDraw.setAutoDelete(true);
history.setAutoDelete(true);
setBackgroundColor(white);
TQDomDocument layoutsDocument;
bool ok = topLevel->loadLayout(layoutsDocument);
if (ok) ok = registerPlayGrounds(layoutsDocument);
if (ok) ok = loadPlayGround(layoutsDocument, selectedGameboard);
if (!ok) loadFailure();
currentAction = 0;
setupGeometry();
}
// Destructor
PlayGround::~PlayGround()
{
delete [] textsLayout;
delete [] objectsLayout;
delete [] textsList;
delete [] soundsList;
delete draggedCursor;
}
// Reset the play ground
void PlayGround::reset()
{
toDraw.clear();
history.clear();
currentAction = 0;
}
// Change the gameboard
void PlayGround::change(uint selectedGameboard)
{
TQDomDocument layoutsDocument;
bool ok = topLevel->loadLayout(layoutsDocument);
if (ok) ok = loadPlayGround(layoutsDocument, selectedGameboard);
if (!ok) loadFailure();
toDraw.clear();
history.clear();
currentAction = 0;
setupGeometry();
update();
}
// Repaint all the editable area
void PlayGround::repaintAll()
{
TQRect dirtyArea
(editableArea.left() - 10,
editableArea.top() - 10,
editableArea.width() + 20,
editableArea.height() + 20);
repaint(dirtyArea, false);
}
// Undo last action
// Returns true if everything went fine
bool PlayGround::undo()
{
ToDraw *newObject;
Action *undone;
int zOrder;
if (!(undone = history.at(--currentAction)))
return false;
zOrder = undone->ZOrderAfter();
if (zOrder != -1)
{
// Undo an "add" or a "move" action
if (!toDraw.remove(zOrder)) return false;
}
zOrder = undone->ZOrderBefore();
if (zOrder != -1)
{
// Undo a "delete" or a "move" action
newObject = new ToDraw(undone->DrawnBefore());
if (!toDraw.insert(zOrder, newObject))
return false;
}
return true;
}
// Redo next action
// Returns true if everything went fine
bool PlayGround::redo()
{
ToDraw *newObject;
Action *undone;
int zOrder;
if (!(undone = history.at(currentAction++)))
return false;
zOrder = undone->ZOrderBefore();
if (zOrder != -1)
{
// Redo a "delete" or a "move" action
if (!toDraw.remove(zOrder)) return false;
}
zOrder = undone->ZOrderAfter();
if (zOrder != -1)
{
// Redo an "add" or a "move" action
newObject = new ToDraw(undone->DrawnAfter());
if (!toDraw.insert(zOrder, newObject))
return false;
}
return true;
}
// Save objects laid down on the editable area
bool PlayGround::saveAs(const TQString & name)
{
FILE *fp;
const ToDraw *currentObject;
if (!(fp = fopen((const char *) TQFile::encodeName(name), "w"))) return false;
fprintf(fp, "%d\n", topLevel->getSelectedGameboard());
for (currentObject = toDraw.first(); currentObject; currentObject = toDraw.next())
currentObject->save(fp);
return !fclose(fp);
}
// Print gameboard's picture
bool PlayGround::printPicture(KPrinter &printer) const
{
TQPainter artist;
TQPixmap picture(getPicture());
if (!artist.begin(&printer)) return false;
artist.drawPixmap(TQPoint(32, 32), picture);
if (!artist.end()) return false;
return true;
}
// Get a pixmap containing the current picture
TQPixmap PlayGround::getPicture() const
{
TQPixmap result(editableArea.size());
TQPainter artist(&result);
TQRect transEditableArea(editableArea);
transEditableArea.moveBy(-XMARGIN, -YMARGIN);
artist.translate(XMARGIN - editableArea.left(),
YMARGIN - editableArea.top());
drawGameboard(artist, transEditableArea);
return result;
}
// Draw some text
void PlayGround::drawText(TQPainter &artist, TQRect &area, TQString &textId) const
{
TQString label;
label=i18n(textId.latin1());
artist.drawText(area, AlignCenter, label);
}
// Paint the current picture to the given device
void PlayGround::drawGameboard( TQPainter &artist, const TQRect &area ) const
{
artist.drawPixmap(area.topLeft(), gameboard, area);
artist.setPen(white);
for (int text = 0; text < texts; text++)
drawText(artist, textsLayout[text], textsList[text]);
artist.setPen(black);
for (TQPtrListIterator<ToDraw> currentObject(toDraw);
currentObject.current();
++currentObject)
currentObject.current()->draw(artist, area, objectsLayout, &gameboard, &masks);
}
// Painting event
void PlayGround::paintEvent( TQPaintEvent *event )
{
TQPoint destination(event->rect().topLeft()),
position(destination.x() - XMARGIN,
destination.y() - YMARGIN);
TQRect area(position, TQSize(event->rect().size()));
TQPixmap cache(gameboard.size());
TQPainter artist(&cache);
if (destination.x() < XMARGIN) destination.setX(XMARGIN);
if (destination.y() < YMARGIN) destination.setY(YMARGIN);
area = TQRect(0, 0, gameboard.width(), gameboard.height()).intersect(area);
if (area.isEmpty()) return;
drawGameboard(artist, area);
bitBlt(this, destination, &cache, area, Qt::CopyROP);
}
// Mouse pressed event
void PlayGround::mousePressEvent( TQMouseEvent *event )
{
if (draggedCursor) return;
TQPoint position(event->x() - XMARGIN,
event->y() - YMARGIN);
if (!zone(position)) return;
int draggedNumber = draggedObject.getNumber();
TQPixmap object(objectsLayout[draggedNumber].size());
TQBitmap shape(objectsLayout[draggedNumber].size());
bitBlt(&object, TQPoint(0, 0), &gameboard, objectsLayout[draggedNumber], Qt::CopyROP);
bitBlt(&shape, TQPoint(0, 0), &masks, objectsLayout[draggedNumber], Qt::CopyROP);
object.setMask(shape);
draggedCursor = new TQCursor(object, position.x(), position.y());
setCursor(*draggedCursor);
topLevel->playSound(soundsList[draggedNumber]);
}
// Mouse released event
void PlayGround::mouseReleaseEvent( TQMouseEvent *event )
{
// If we are not dragging an object, ignore the event
if (!draggedCursor) return;
TQCursor arrow;
int draggedNumber = draggedObject.getNumber();
TQRect position(
event->x() - XMARGIN - draggedCursor->hotSpot().x(),
event->y() - YMARGIN - draggedCursor->hotSpot().y(),
objectsLayout[draggedNumber].width(),
objectsLayout[draggedNumber].height());
TQRect dirtyArea
(editableArea.left() - 10,
editableArea.top() - 10,
editableArea.width() + 20,
editableArea.height() + 20);
ToDraw *newObject;
Action *newAction;
// We are not anymore dragging an object
delete draggedCursor;
draggedCursor = 0;
setCursor(arrow);
// If we are not moving the object to the editable area
if (!dirtyArea.contains(event->pos()))
{
// ... then register its deletion (if coming from the editable area), and return
if (draggedZOrder == -1) return;
while (history.count() > currentAction) history.removeLast();
newAction = new Action(&draggedObject, draggedZOrder, 0, -1);
history.append(newAction);
currentAction++;
topLevel->enableUndo(true);
return;
}
// Register that we have one more object to draw
newObject = new ToDraw(draggedNumber, position);
toDraw.append(newObject);
// Forget all subsequent actions in the undo buffer, and register object's addition (or its move)
while (history.count() > currentAction) history.removeLast();
newAction = new Action(&draggedObject, draggedZOrder, newObject, toDraw.count()-1);
history.append(newAction);
currentAction++;
topLevel->enableUndo(true);
// Repaint the editable area
position.moveBy(XMARGIN, YMARGIN);
repaint(position, false);
}
// Register the various playgrounds
bool PlayGround::registerPlayGrounds(TQDomDocument &layoutDocument)
{
TQDomNodeList playGroundsList, menuItemsList, labelsList;
TQDomElement playGroundElement, menuItemElement, labelElement;
TQDomAttr actionAttribute;
playGroundsList = layoutDocument.elementsByTagName("playground");
if (playGroundsList.count() < 1)
return false;
for (uint i = 0; i < playGroundsList.count(); i++)
{
playGroundElement = (const TQDomElement &) playGroundsList.item(i).toElement();
menuItemsList = playGroundElement.elementsByTagName("menuitem");
if (menuItemsList.count() != 1)
return false;
menuItemElement = (const TQDomElement &) menuItemsList.item(0).toElement();
labelsList = menuItemElement.elementsByTagName("label");
if (labelsList.count() != 1)
return false;
labelElement = (const TQDomElement &) labelsList.item(0).toElement();
actionAttribute = menuItemElement.attributeNode("action");
topLevel->registerGameboard(labelElement.text(), actionAttribute.value().latin1());
}
return true;
}
// Load background and draggable objects masks
bool PlayGround::loadPlayGround(TQDomDocument &layoutDocument, uint toLoad)
{
TQDomNodeList playGroundsList,
editableAreasList, categoriesList, objectsList,
gameAreasList, maskAreasList, soundNamesList, labelsList;
TQDomElement playGroundElement,
editableAreaElement, categoryElement, objectElement,
gameAreaElement, maskAreaElement, soundNameElement, labelElement;
TQDomAttr gameboardAttribute, masksAttribute,
leftAttribute, topAttribute, rightAttribute, bottomAttribute,
refAttribute;
playGroundsList = layoutDocument.elementsByTagName("playground");
if (toLoad >= playGroundsList.count())
return false;
playGroundElement = (const TQDomElement &) playGroundsList.item(toLoad).toElement();
gameboardAttribute = playGroundElement.attributeNode("gameboard");
if (!gameboard.load(locate("data", "ktuberling/pics/" + gameboardAttribute.value())))
return false;
masksAttribute = playGroundElement.attributeNode("masks");
if (!masks.load(locate("data", "ktuberling/pics/" + masksAttribute.value())))
return false;
editableAreasList = playGroundElement.elementsByTagName("editablearea");
if (editableAreasList.count() != 1)
return false;
editableAreaElement = (const TQDomElement &) editableAreasList.item(0).toElement();
gameAreasList = editableAreaElement.elementsByTagName("position");
if (gameAreasList.count() != 1)
return false;
gameAreaElement = (const TQDomElement &) gameAreasList.item(0).toElement();
leftAttribute = gameAreaElement.attributeNode("left");
topAttribute = gameAreaElement.attributeNode("top");
rightAttribute = gameAreaElement.attributeNode("right");
bottomAttribute = gameAreaElement.attributeNode("bottom");
editableArea.setCoords
(XMARGIN + leftAttribute.value().toInt(),
YMARGIN + topAttribute.value().toInt(),
XMARGIN + rightAttribute.value().toInt(),
YMARGIN + bottomAttribute.value().toInt());
soundNamesList = editableAreaElement.elementsByTagName("sound");
if (soundNamesList.count() != 1)
return false;
soundNameElement = (const TQDomElement &) soundNamesList.item(0).toElement();
refAttribute = soundNameElement.attributeNode("ref");
editableSound = refAttribute.value();
categoriesList = playGroundElement.elementsByTagName("category");
texts = categoriesList.count();
if (texts < 1)
return false;
delete[] textsLayout;
textsLayout = new QRect[texts];
delete[] textsList;
textsList = new QString[texts];
for (int text = 0; text < texts; text++)
{
categoryElement = (const TQDomElement &) categoriesList.item(text).toElement();
gameAreasList = categoryElement.elementsByTagName("position");
if (gameAreasList.count() != 1)
return false;
gameAreaElement = (const TQDomElement &) gameAreasList.item(0).toElement();
leftAttribute = gameAreaElement.attributeNode("left");
topAttribute = gameAreaElement.attributeNode("top");
rightAttribute = gameAreaElement.attributeNode("right");
bottomAttribute = gameAreaElement.attributeNode("bottom");
textsLayout[text].setCoords
(leftAttribute.value().toInt(),
topAttribute.value().toInt(),
rightAttribute.value().toInt(),
bottomAttribute.value().toInt());
labelsList = categoryElement.elementsByTagName("label");
if (labelsList.count() != 1)
return false;
labelElement = (const TQDomElement &) labelsList.item(0).toElement();
textsList[text] = labelElement.text();
}
objectsList = playGroundElement.elementsByTagName("object");
decorations = objectsList.count();
if (decorations < 1)
return false;
delete[] objectsLayout;
objectsLayout = new QRect[decorations];
delete[] soundsList;
soundsList = new QString[decorations];
for (int decoration = 0; decoration < decorations; decoration++)
{
objectElement = (const TQDomElement &) objectsList.item(decoration).toElement();
gameAreasList = objectElement.elementsByTagName("position");
if (gameAreasList.count() != 1)
return false;
gameAreaElement = (const TQDomElement &) gameAreasList.item(0).toElement();
leftAttribute = gameAreaElement.attributeNode("left");
topAttribute = gameAreaElement.attributeNode("top");
rightAttribute = gameAreaElement.attributeNode("right");
bottomAttribute = gameAreaElement.attributeNode("bottom");
objectsLayout[decoration].setCoords
(leftAttribute.value().toInt(),
topAttribute.value().toInt(),
rightAttribute.value().toInt(),
bottomAttribute.value().toInt());
soundNamesList = objectElement.elementsByTagName("sound");
if (soundNamesList.count() != 1)
return false;
soundNameElement = (const TQDomElement &) soundNamesList.item(0).toElement();
refAttribute = soundNameElement.attributeNode("ref");
soundsList[decoration] = refAttribute.value();
}
return true;
}
// Report a load failure
void PlayGround::loadFailure()
{
KMessageBox::error(topLevel, i18n("Fatal error:\n"
"Unable to load the pictures, aborting."));
exit(-1);
}
// Set up play ground's geometry
void PlayGround::setupGeometry()
{
int width = gameboard.width() + 2 * XMARGIN,
height = gameboard.height() + 2 * YMARGIN;
setFixedWidth(width);
setFixedHeight(height);
}
// In which decorative object are we?
// On return, the position is the location of the cursor's hot spot
// Returns false if we aren't in any zone
bool PlayGround::zone(TQPoint &position)
{
// Scan all available decorative objects on right side because we may be adding one
int draggedNumber;
for (draggedNumber = 0;
draggedNumber < decorations;
draggedNumber++) if (objectsLayout[draggedNumber].contains(position))
{
position.setX(position.x() - objectsLayout[draggedNumber].x());
position.setY(position.y() - objectsLayout[draggedNumber].y());
draggedObject.setNumber(draggedNumber);
draggedZOrder = -1;
return true;
}
// Scan all decorative objects already layed down on editable are because we may be moving or removing one
const ToDraw *currentObject;
for (draggedZOrder = toDraw.count()-1; draggedZOrder >= 0; draggedZOrder--)
{
currentObject = toDraw.at(draggedZOrder);
if (!currentObject->getPosition().contains(position)) continue;
TQRect toUpdate(currentObject->getPosition());
draggedObject = *currentObject;
draggedNumber = draggedObject.getNumber();
TQBitmap shape(objectsLayout[draggedNumber].size());
TQPoint relative(position.x() - toUpdate.x(),
position.y() - toUpdate.y());
bitBlt(&shape, TQPoint(0, 0), &masks, objectsLayout[draggedNumber], Qt::CopyROP);
if (!shape.convertToImage().pixelIndex(relative.x(), relative.y())) continue;
toDraw.remove(draggedZOrder);
toUpdate.moveBy(XMARGIN, YMARGIN);
repaint(toUpdate, false);
position = relative;
return true;
}
// If we are on the gameboard itself, then play "tuberling" sound
if (editableArea.contains(position))
topLevel->playSound(editableSound);
return false;
}
// Load objects and lay them down on the editable area
bool PlayGround::loadFrom(const TQString &name)
{
FILE *fp;
bool eof = false;
ToDraw readObject, *newObject;
Action *newAction;
if (!(fp = fopen(TQFile::encodeName(name), "r"))) return false;
uint newGameboard;
int nitems = fscanf(fp, "%u\n", &newGameboard);
if (nitems == EOF)
{
fclose(fp);
return false;
}
topLevel->changeGameboard(newGameboard);
for (;;)
{
if (!readObject.load(fp, decorations, eof))
{
fclose(fp);
return false;
}
if (eof)
{
return !fclose(fp);
}
newObject = new ToDraw(readObject);
toDraw.append(newObject);
newAction = new Action(0, -1, newObject, toDraw.count()-1);
history.append(newAction);
currentAction++;
}
}
|