Menu items in correct places again
[ghostsoverboard] / seascene.cpp
1 /**************************************************************************
2         Ghosts Overboard - a game for Maemo 5
3
4         Copyright (C) 2011  Heli Hyvättinen
5
6         This file is part of Ghosts Overboard
7
8         Ghosts Overboard is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 2 of the License, or
11         (at your option) any later version.
12
13         This program is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 **************************************************************************/
22
23 #include "seascene.h"
24 #include "octopus.h"
25 #include "ship.h"
26 #include <QGraphicsPixmapItem>
27 #include <QDebug>
28 #include <QMessageBox>
29 #include <QTime>
30 #include <QApplication>
31 #include <QAction>
32 #include <QPushButton>
33 #include <QLabel>
34 #include <QVBoxLayout>
35 #include <QSettings>
36 #include <QPixmap>
37
38 const QString ghostImageFilename_ = ":/pix/aave.png";
39 const QString rockImageFilename_ =":/pix/kari.png";
40 const QString octopusImageFilename_= ":/pix/tursas.png";
41
42
43 SeaScene::SeaScene(QObject *parent) :
44     QGraphicsScene(parent)
45 {
46
47     setItemPointersNull();
48
49     paused_ = false;
50     screenLitKeeper_.keepScreenLit(true);
51
52     //set background
53
54     QPixmap waves (":/pix/meri.png");
55     waves.scaled(20,20);
56     setBackgroundBrush(QBrush(waves));
57
58     //set random seed
59
60     qsrand(QTime::currentTime().msec()+2);  //+2 to avoid setting it to 1
61
62
63
64 //Setup the level list
65
66     QList<Level> levelList;
67     Level level1(5,10);
68     levelList.append(level1);
69     Level level2(5,10,2,50);
70     levelList.append(level2);
71     Level level3(5,15,2,50);
72     levelList.append(level3);
73     Level level4(5,15,4,50);
74     levelList.append(level4);
75     Level level5(5,15,5,100);
76     levelList.append(level5);
77
78     Levelset set ("Original",levelList);
79     levelset_ = set;
80     availableLevelsets_.append(set);
81
82     currentLevel_ = 0;
83
84     totalScore_ = 0;
85
86    //Create another set of levels and place it in the available levelsets list
87     levelList.clear();
88     Level set2level1(8,15,4,50);
89     levelList.append(set2level1);
90     Level set2level2(8,20,4,50);
91     levelList.append(set2level2);
92     Level set2level3(8,20,5,100);
93     levelList.append(set2level3);
94     Level set2level4(8,20,6,150);
95     levelList.append(set2level4);
96     Level set2level5(8,25,8,200);
97     levelList.append(set2level5);
98
99     Levelset set2("Difficult",levelList);
100     availableLevelsets_.append(set2);
101
102
103
104     connect(this,SIGNAL(allGhostsPicked()),this,SLOT(nextLevel()));
105
106
107     pVibrateAction_ = new QAction(tr("Vibration effects"),this);
108     pVibrateAction_->setCheckable(true);
109     connect(pVibrateAction_,SIGNAL(toggled(bool)),this,SLOT(vibrationActivate(bool)));
110     QSettings settings;
111     pVibrateAction_->setChecked(settings.value("vibration",false).toBool());
112
113
114     pPauseAction_ = new QAction(tr("Pause"),this);
115     pPauseAction_->setCheckable(true);
116     connect(pPauseAction_,SIGNAL(toggled(bool)),this,SLOT(pause(bool)));
117
118
119     deviceLockPollTimer_.setInterval(20*60);
120     connect(&deviceLockPollTimer_,SIGNAL(timeout()),this,SLOT(pollDeviceLocked()));
121     deviceLockPollTimer_.start();
122
123
124     autopauseTimer.setSingleShot(true);
125     autopauseTimer.setInterval(15*60*1000);
126     connect(&autopauseTimer,SIGNAL(timeout()),this,SLOT(turnPauseOn()));
127
128
129 }
130
131 void SeaScene::setupMap(int ghosts, int rocks, int octopuses, int octopusSpeed)
132 {
133     //empty the map
134
135     clear();
136
137     setItemPointersNull();
138
139     createMenuItems();
140
141     createAboutBoxItems();
142
143     createSelectLevelsetFromListItems();
144
145     createVictoryItems();
146
147     createLevelCompletedItems();
148
149
150     //empty the list of moving items
151
152     movingItems_.clear();
153
154     //empty the list of free slots
155     freeTiles_.clear();
156
157     //fill the list of free slots
158
159     int numberOfXTiles  = width() / 40;
160     int numberOfYTiles = height() /40;
161
162 //    qDebug() << numberOfXTiles << " slots in x direction";
163 //    qDebug() << numberOfYTiles << " slots in y rirection";
164
165     for (int i = 0; i < numberOfXTiles; i++ )
166     {
167         for (int j = 0; j < numberOfYTiles; j++)
168         {
169             freeTiles_.append(QPointF(i*40,j*40));
170         }
171     }
172
173
174     //spread the rocks
175
176     for (int i = 0; i < rocks; i++)
177     {
178         QPointF * pPosition = findRandomFreeSlot();
179
180         //If there was no room no point to continue
181         if (pPosition == NULL)
182             break;
183
184         QPixmap rockPixmap (":/pix/kari.png");
185         QGraphicsPixmapItem * pRock = addPixmap(rockPixmap);
186         pRock->setData(0,"rock");
187         pRock->setPos(*pPosition);
188         delete pPosition;
189
190     }
191
192     //spread the ghosts
193
194     ghostsLeft_ = ghosts;
195     spreadGhosts(ghosts);
196
197
198
199     //spread the octopuses
200
201     QList <Octopus*> octopusList;
202
203     for (int i=0; i < octopuses; i++)
204     {
205         QPointF * pPosition = findRandomFreeSlot();
206
207         //If there was no room no point to continue
208         if (pPosition == NULL)
209             break;
210
211     QPixmap octopusPixmap (":/pix/tursas.png");
212     Octopus * pOctopus = new Octopus(octopusPixmap,octopusSpeed);
213     pOctopus->setData(0,"octopus");
214     pOctopus->setPos(*pPosition);
215     addItem(pOctopus);
216     pOctopus->startMoving();
217     movingItems_.append(pOctopus);
218     connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving()));
219     connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving()));
220     octopusList.append(pOctopus);
221     delete pPosition;
222
223     }
224
225
226     //place the ship
227
228     QPointF * pPosition = findRandomFreeSlot();
229     if (pPosition == NULL)
230     {
231         // Game cannot begin without a free position for ship, so give an error message and return
232
233         QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
234         return;
235     }
236
237     QList<QPixmap> shipImages;
238     shipImages.append(QPixmap(":/pix/laiva.png"));
239     shipImages.append(QPixmap(":/pix/laiva_1aave.png"));
240     shipImages.append(QPixmap(":/pix/laiva_2aave.png"));
241     shipImages.append(QPixmap(":/pix/laiva_3aave.png"));
242     shipImages.append(QPixmap(":/pix/laiva_4aave.png"));
243     shipImages.append(QPixmap(":/pix/laiva_5aave.png"));
244     shipImages.append(QPixmap(":/pix/laiva_6aave.png"));
245     shipImages.append(QPixmap(":/pix/laiva_7aave.png"));
246     shipImages.append(QPixmap(":/pix/laiva_8aave.png"));
247     shipImages.append(QPixmap(":/pix/laiva_9aave.png"));
248     shipImages.append(QPixmap(":/pix/laiva_10aave.png"));
249
250     Ship * pShip = new Ship (shipImages);
251     pShip->setData(0,"ship");
252     pShip->setPos(*pPosition);
253     addItem(pShip);
254     connect(pShip,SIGNAL(pickingGhost(QGraphicsItem*)),this, SLOT(removeGhost(QGraphicsItem*)) );
255     connect(pShip,SIGNAL(droppingGhosts(int)),this,SLOT(ghostsDropped(int)));
256     connect(this,SIGNAL(vibrationActivated(bool)),pShip,SLOT(setVibrationActivate(bool)));
257     pShip->startMoving();
258     movingItems_.append(pShip);
259     connect(this,SIGNAL(pauseOn()),pShip,SLOT(stopMoving()));
260     connect(this,SIGNAL(pauseOff()),pShip,SLOT(startMoving()));
261     foreach (Octopus* pOctopus, octopusList)
262     {
263         connect(pOctopus,SIGNAL(droppingGhosts()),pShip,SLOT(dropAllGhosts()));
264     }
265     delete pPosition;
266 }
267
268 void SeaScene::setupMap(Level level)
269 {
270     setupMap(level.getNumberOfGhosts(),level.getNumberOfRocks(),level.getNumberOfOctopuses(),level.getOctopusSpeed());
271 }
272
273 void SeaScene::spreadGhosts(int ghosts)
274 {
275
276
277     //the octopuses and the ship may have moved from their original positions,
278     //so the list of free slots must be adjusted to exclude their current positions
279
280     QList<QPointF> temporarilyReservedSlots;
281
282     foreach (QGraphicsItem* pItem, movingItems_)
283     {
284         if (pItem == NULL)
285         {
286  //           qDebug() << "NULL item in movingItems_";
287             continue;
288         }
289
290         //round x and y down to fit the slot size
291         int x = pItem->x();
292         x = x/40;
293         x = x*40;
294
295         int y = pItem->y();
296         y = y/40;
297         y=y*40;
298
299
300         QPointF position (x,y);
301
302         //remove the tiles (potentially) occupied by the item from free slots and place in temp list if was in the list before
303
304         if (freeTiles_.removeOne(position))
305             temporarilyReservedSlots.append(position);
306
307
308         position.setX(x+40);
309
310         if (freeTiles_.removeOne(position))
311             temporarilyReservedSlots.append(position);
312
313         position.setY(y+40);
314
315         if (freeTiles_.removeOne(position))
316             temporarilyReservedSlots.append(position);
317
318         position.setX(x);
319
320         if (freeTiles_.removeOne(position))
321             temporarilyReservedSlots.append(position);
322
323     }
324
325
326     //spread ghosts in random free slots
327
328     for (int i=0; i < ghosts; i++)
329     {
330         QPointF * pPosition = findRandomFreeSlot();
331
332         //If there was no room no point to continue
333         if (pPosition == NULL)
334             return;
335
336         QPixmap ghostPixmap(":/pix/aave.png");
337         QGraphicsPixmapItem * pGhost = addPixmap(ghostPixmap);
338         pGhost->setData(0,"ghost");
339         pGhost->setPos(*pPosition);
340         delete pPosition;
341     }
342
343     //return the slots occupied by moving items to free slots
344     freeTiles_.append(temporarilyReservedSlots);
345
346     //clear temp for the next round
347     temporarilyReservedSlots.clear();
348 }
349
350 QPointF* SeaScene::findRandomFreeSlot()
351 {
352     if (freeTiles_.isEmpty())
353         return NULL;
354
355     int index = qrand()%freeTiles_.size();
356
357 //    qDebug()  << index << " index";
358     return new QPointF (freeTiles_.takeAt(index));
359
360 }
361
362 void SeaScene::removeGhost(QGraphicsItem *pGhost)
363 {
364     removeItem(pGhost);  //remove the item from scene
365     freeTiles_.append(pGhost->scenePos()); //add the item's position to free slots
366     delete pGhost;
367     ghostsLeft_--;
368     if (ghostsLeft_ == 0)
369     {
370         emit allGhostsPicked();
371  //       qDebug() << "All ghosts picked!";
372     }
373 }
374
375 void SeaScene::ghostsDropped(int ghosts)
376 {
377     ghostsLeft_ += ghosts;
378
379     spreadGhosts(ghosts);
380 }
381
382 void SeaScene::pause(bool paused)
383 {
384     //    qDebug() << "pause pressed " << paused;
385         if (paused_ == paused)
386                 return;
387
388         paused_ = paused;
389
390         if (paused == false)
391         {
392      //       qDebug() << "starting to move again";
393             emit fullscreenRequested();
394             emit pauseOff();
395             screenLitKeeper_.keepScreenLit(true);
396             if (pPausetextItem_)
397                 pPausetextItem_->hide();
398
399             scoreCounter_.start();
400
401             autopauseTimer.start(); //Start counting towards autopause
402             deviceLockPollTimer_.start(); //Start polling whether device is locked
403         }
404
405         else
406         {
407 //         qDebug("about to stop movement");
408             emit pauseOn();
409             screenLitKeeper_.keepScreenLit(false);
410             if (pPausetextItem_ != NULL)
411             {
412 //                qDebug() << "about to show the pause text";
413                 pPausetextItem_->show();
414 //                qDebug() << "showing pause text";
415             }
416 //                else qDebug() << "No pause text available";
417
418             levelScore_ += scoreCounter_.elapsed();
419
420             autopauseTimer.stop(); //No need to count toward autopause when already paused
421             deviceLockPollTimer_.stop(); //No need to check for unlock as no unpause anyway
422         }
423 }
424
425 void SeaScene::vibrationActivate(bool on)
426 {
427     emit vibrationActivated(on);
428 }
429
430 void SeaScene::handleScreenTapped()
431 {
432
433     //If the game is going just pause it
434     if (!paused_)
435     {
436         pPauseAction_->setChecked(true);
437         return;
438     }
439
440     //If the game is paused and about box is shown, close it and show the pause text and menu again
441
442     if(pAboutBoxItem_)
443     {
444         if(pAboutBoxItem_->isVisible())
445         {
446             pAboutBoxItem_->hide();
447             pPausetextItem_->show();
448             return;
449         }
450     }
451
452     //If the game is paused, check if the level completed item is shown
453
454     if (pLevelCompletedItem_)
455     {
456         if (pLevelCompletedItem_->isVisible())
457         {
458             pLevelCompletedItem_->hide();
459             restartLevel(); //Current level has already been set to the next one before showing the level completed item
460             pPauseAction_->setChecked(false); //unpause
461             return;
462         }
463     }
464   
465     //If the game is paused, check if the victory item is being shown
466     if(pVictoryCongratulationsItem_)
467     {
468         if (pVictoryCongratulationsItem_->isVisibleTo(NULL)) //returns visibility to scene
469         {
470             pVictoryCongratulationsItem_->hide();
471             restartGame();
472             pPauseAction_->setChecked(false); // unpause
473             return;
474         }
475     }
476
477
478     //If the game is paused and no victory, check if menu item was selected
479
480     QList<QGraphicsItem *> items = selectedItems();
481
482     //if nothing selected resume play
483
484     if (items.isEmpty())
485     {
486         pSelectLevelsetFromListItem_->hide();
487         pPauseAction_->setChecked(false);
488
489         return;
490
491     }
492
493     //If something was selected check if it was one of the menu items and act on it
494     //(Nothing else should be made selectable anyway)
495
496     //Menu functions
497
498     QGraphicsItem* pItem = items.at(0); //Selecting an item brings here, thus only selecting one item should be possible
499                                        //... so we can just take the first one
500
501     //Selection is just used to get notice of a menu item being clicked, removed after use
502
503     clearSelection();
504
505
506     //Act upon the selected item
507
508
509     if (pItem == pRestartGameItem_)
510     {
511 //        qDebug() << "game restart requested";
512         restartGame();
513         pPauseAction_->setChecked(false); //unpause game
514
515     }
516
517     else if (pItem == pRestartLevelItem_)
518     {
519 //        qDebug() << "Level restart requested";
520         restartLevel();
521         pPauseAction_->setChecked(false); //unpause game
522
523     }
524
525     else if (pItem == pSettingsItem_)
526     {
527         pVibrateAction_->toggle();
528
529         QSettings settings;
530         settings.setValue("vibration",pVibrateAction_->isChecked());
531
532         QString text = pSettingsItem_->toHtml();
533         if (pVibrateAction_->isChecked())
534             text.replace(" on"," off"); //don't remove spaces or you get vibratioff...
535         else
536             text.replace(" off"," on");
537         pSettingsItem_->setHtml(text);
538     }
539
540     else if (pItem == pAboutItem_)
541     {
542         about();
543     }
544
545     else if(pItem == pMinimizeItem_)
546     {
547         emit minimizeRequested();
548     }
549
550     else if (pItem == pQuitItem_)
551     {
552         qApp->quit();
553     }
554     else if (pItem == pChooseLevelsetItem_)
555     {
556         pPausetextItem_->hide();
557         pSelectLevelsetFromListItem_->show();
558     }
559
560     else
561     {
562         foreach (QGraphicsItem* pLevelItem, levelsetItems_)
563         {
564             if (pItem == pLevelItem)
565             {
566                 QVariant variant = pLevelItem->data(0);
567
568                 if (variant.canConvert<Levelset>())
569                 {
570                     levelset_ = variant.value<Levelset>();
571                     restartGame();
572                     pPauseAction_->setChecked(false); //unpause game
573                 }
574             }
575         }
576     }
577
578 }
579
580
581
582 void SeaScene::createMenuItems()
583 {
584
585     QFont font;
586     font.setPixelSize(35);
587
588
589
590     pPausetextItem_ = new QGraphicsTextItem;
591     pPausetextItem_->setHtml("<font size = \"5\" color = darkorange> Game paused. Tap to continue.");
592     pPausetextItem_->setZValue(1000);
593     pPausetextItem_->setPos(165,50);
594     addItem(pPausetextItem_);
595     pPausetextItem_->hide();
596
597     menuItemCount_ = 0;
598
599     QString menufonthtml = "<font size = \"4\" color = darkorange>";
600
601     pRestartGameItem_ = new QGraphicsTextItem;
602     pRestartGameItem_->setHtml(tr("Restart <br> game").prepend(menufonthtml));
603     prepareForMenu(pRestartGameItem_);
604
605     pRestartLevelItem_ = new QGraphicsTextItem;
606     pRestartLevelItem_->setHtml(tr("Restart <br> level").prepend(menufonthtml));
607     prepareForMenu(pRestartLevelItem_);
608
609     pChooseLevelsetItem_ = new QGraphicsTextItem;
610     pChooseLevelsetItem_->setHtml(tr("Choose <br> levelset").prepend(menufonthtml));
611     prepareForMenu(pChooseLevelsetItem_);
612
613     pSettingsItem_ = new QGraphicsTextItem;
614     QString vibraText(tr("Turn vibration <br> effects "));
615     QString statusText;
616     if (pVibrateAction_->isChecked())
617     {
618         statusText = "off";
619     }
620     else
621     {
622         statusText = "on";
623     }
624     vibraText.append(statusText);
625     pSettingsItem_->setHtml(vibraText.prepend(menufonthtml));
626     prepareForMenu(pSettingsItem_);
627
628     pAboutItem_ = new QGraphicsTextItem;
629     pAboutItem_->setHtml(tr("About <br> game").prepend(menufonthtml));
630     prepareForMenu(pAboutItem_);
631
632     pMinimizeItem_ = new QGraphicsTextItem;
633     pMinimizeItem_->setHtml(tr("Show <br> status bar").prepend(menufonthtml));
634     prepareForMenu(pMinimizeItem_);
635
636     pQuitItem_ = new QGraphicsTextItem;
637     pQuitItem_->setHtml(tr("Quit <br> game").prepend(menufonthtml));
638     prepareForMenu(pQuitItem_);
639
640 }
641
642 void SeaScene::prepareForMenu(QGraphicsItem * pItem)
643 {
644
645     //Menu items have pause text item as their parent and are thus added to scene automatically
646     //They are also shown and hidden with it, resulting in the menu being visble when the game is paused
647     //Their coordinates are given relative to the parent.
648
649     int itemsPerRow = 4;
650
651
652     pItem->setParentItem(pPausetextItem_);
653     pItem->setZValue(1000);
654     pItem->setFlag(QGraphicsItem::ItemIsSelectable);
655
656     int row = menuItemCount_/(itemsPerRow);
657     pItem->setY(150+row*120);
658     pItem->setX(((menuItemCount_%(itemsPerRow))*180-125));
659
660     menuItemCount_++;
661
662  }
663
664
665 void SeaScene::about()
666 {
667     pPausetextItem_->hide();
668     pAboutBoxItem_->show();
669 }
670
671
672 void SeaScene::restartLevel()
673 {
674
675     levelScore_ = 0;
676
677     setupMap(levelset_.getLevel(currentLevel_));  //getLevel() returns default constructor Level if index is invalid, so no risk of crash
678
679     scoreCounter_.start();
680
681     vibrationActivate(pVibrateAction_->isChecked());  //Vibration effects are lost without this
682    // qDebug() << pVibrateAction_->isChecked();
683     autopauseTimer.start();  //reset counting towards autopause
684
685
686 }
687
688
689
690 void SeaScene::nextLevel()
691 {
692
693     //get score for previous level
694     levelScore_ += scoreCounter_.elapsed();
695     totalScore_ += levelScore_;
696     int highscore = levelset_.getLevelHighScore(currentLevel_);
697
698     qDebug() << highscore;
699
700     QString scoretext;
701
702     if (levelScore_ >= highscore)
703     {
704         scoretext = tr("<font size=\"5\" color = darkorange>Your time: %1.%2 s<br>Best time: %3.%4 s").arg(levelScore_/1000).arg((levelScore_%1000)/100).arg(highscore/1000).arg((highscore%1000)/100);
705     }
706
707     else //New high score!
708
709     {
710         scoretext = tr("<font size=\"5\" color = darkorange>Your time %1.%2 s is<br>the new best time!").arg(levelScore_/1000).arg((levelScore_%1000)/100);
711         levelset_.setLevelHighScore(currentLevel_,levelScore_);
712     }
713
714     //pause to show the highscore or victory screen
715
716     turnPauseOn();
717     pPausetextItem_->hide();
718
719
720     //Go to the next level if available
721     currentLevel_++;
722
723     if ( currentLevel_ < levelset_.numberOfLevels() )
724     {
725
726        pLevelCompletedItem_->setHtml(scoretext);
727        pLevelCompletedItem_->show();
728 //       restartLevel();
729     }
730
731     else //Victory!
732     {
733         int totalHighsore = levelset_.getTotalHighScore();
734         if (totalScore_ >= totalHighsore)
735         {
736             scoretext.append(tr("<br>Your total time: %1.%2 s<br>Best total time:%3.%4 s").arg(totalScore_/1000).arg((totalScore_%1000)/100).arg(totalHighsore/1000).arg((totalHighsore%1000)/100));
737         }
738         else //new total high score
739         {
740             scoretext.append(tr("<br>Your total time %1.%2 s is<br>the new best time").arg(totalScore_/1000).arg((totalScore_%1000)/100));
741             levelset_.setTotalHighScore(totalScore_);
742         }
743
744         pVictoryScoreItem_->setHtml(scoretext);
745         pVictoryCongratulationsItem_->show();
746     }
747 }
748
749
750 void SeaScene::restartGame()
751 {
752     currentLevel_ = 0;
753     totalScore_ = 0;
754     restartLevel();
755 }
756
757
758 void SeaScene::forcePause()
759 {
760     //Pause without setting the pause action state
761     pause(true);
762 }
763
764 void SeaScene::softContinue()
765 {
766     //Continue if not being paused by the user
767     // Reverts forcePause()
768
769     pause(pPauseAction_->isChecked());
770 }
771
772
773 void SeaScene::createVictoryItems()
774 {
775     pVictoryCongratulationsItem_ = new QGraphicsTextItem;
776     pVictoryCongratulationsItem_->setHtml("<font size=\"6\" color = darkorange> Victory!");
777     pVictoryCongratulationsItem_->hide();
778     pVictoryCongratulationsItem_->setPos(315,30);
779     pVictoryCongratulationsItem_->setZValue(1000);
780     addItem(pVictoryCongratulationsItem_);
781
782
783     //coordinates are relative to the parent
784
785     QGraphicsTextItem * pTextItem = new QGraphicsTextItem(pVictoryCongratulationsItem_);
786     pTextItem->setHtml("<font size=\"5\" color = darkorange> Congratulations!");
787     pTextItem->setPos(-50,80);
788     pTextItem->setZValue(1000);
789
790     QGraphicsTextItem * pMiddleTextItem = new QGraphicsTextItem(pVictoryCongratulationsItem_);
791     pMiddleTextItem->setHtml("<font size=\"5\" color = darkorange> You have saved all the ghosts.");
792     pMiddleTextItem->setPos(-145,120);
793     pMiddleTextItem->setZValue(1000);
794
795
796     pVictoryScoreItem_ = new QGraphicsTextItem(pVictoryCongratulationsItem_);
797     pVictoryScoreItem_->setPos(-50,180);
798     pMiddleTextItem->setZValue(1000);
799     //Text is set at usetime!
800
801     QGraphicsTextItem * pLowestTextItem = new QGraphicsTextItem(pVictoryCongratulationsItem_);
802     pLowestTextItem->setHtml("<font size=\"5\" color = darkorange> Tap to play again");
803     pLowestTextItem->setPos(-50,360);
804     pLowestTextItem->setZValue(1000);
805 }
806
807 void SeaScene::createAboutBoxItems()
808 {
809     pAboutBoxItem_ = new QGraphicsTextItem;
810     addItem(pAboutBoxItem_);
811     pAboutBoxItem_->setPos(25,50);
812     pAboutBoxItem_->setZValue(1000);
813     pAboutBoxItem_->hide();
814
815     pAboutBoxItem_->setHtml(tr("<font color = darkorange size = \"7\">"
816                           "%1 <br> <font size = \"5\"> Version %2"
817                           "<p><font size = \"4\"> Copyright 2011 Heli Hyv&auml;ttinen"
818                           "<p><font size = \"4\"> License: General Public License v2"
819                           "<p><font size = \"3\"> Web: http://ghostsoverboard.garage.maemo.org/<br>"
820                           "Bug Reports: <br> https://bugs.maemo.org/"
821                           "enter_bug.cgi?product=Ghosts%20Overboard"
822                           ).arg(QApplication::applicationName(),QApplication::applicationVersion()));
823
824 }
825
826 void SeaScene::setItemPointersNull()
827 {
828     pPausetextItem_ = NULL;
829     pRestartLevelItem_ = NULL;
830     pRestartGameItem_ = NULL;
831     pSettingsItem_ = NULL;
832     pAboutItem_ = NULL;
833     pQuitItem_ = NULL ;
834     pMinimizeItem_ = NULL;
835     pChooseLevelsetItem_ = NULL;
836
837     pAboutBoxItem_ = NULL;
838     pVictoryCongratulationsItem_ = NULL;
839     pLevelCompletedItem_ = NULL;
840     pVictoryCongratulationsItem_ = NULL;
841     pVictoryScoreItem_ = NULL;
842
843
844 }
845
846 void SeaScene::turnPauseOn()
847 {
848     pPauseAction_->setChecked(true);
849 }
850
851 void SeaScene::handleDeviceLocked(bool isLocked)
852 {
853     //pauses if locked but does not unpause if unlocked
854     if(isLocked)
855     {
856         pPauseAction_->setChecked(true);
857     }
858 }
859
860 void SeaScene::pollDeviceLocked()
861 {
862
863     bool locked = deviceInfo_.isDeviceLocked();
864
865     if (locked)
866     {
867         if (!alreadyLocked_)
868         {
869             pPauseAction_->setChecked(true);
870             alreadyLocked_ = true;
871         }
872
873     else
874         {
875             alreadyLocked_ = false;
876         }
877     }
878 }
879
880 void SeaScene::createLevelCompletedItems()
881 {
882     pLevelCompletedItem_ = new QGraphicsTextItem;
883     addItem(pLevelCompletedItem_);
884     pLevelCompletedItem_->setPos(240,100);
885     pLevelCompletedItem_->setZValue(1000);
886     pLevelCompletedItem_->hide();
887     //The text is set at usetime
888
889     QGraphicsTextItem * pTapForNextLevelItem = new QGraphicsTextItem(pLevelCompletedItem_);
890     pTapForNextLevelItem->setPos(-60,100);
891     pTapForNextLevelItem->setZValue(1000);
892     pTapForNextLevelItem->setHtml("<font size=\"5\" color = darkorange>Tap to start the next level");
893
894 }
895 void SeaScene::createSelectLevelsetFromListItems()
896 {
897     if (availableLevelsets_.isEmpty()) //Something is badly wrong in code if this is true...
898             return;
899
900
901     pSelectLevelsetFromListItem_ = new QGraphicsTextItem;
902     addItem(pSelectLevelsetFromListItem_);
903     pSelectLevelsetFromListItem_->setPos(260,60);
904     pSelectLevelsetFromListItem_->setZValue(1000);
905     pSelectLevelsetFromListItem_->hide();
906
907     QString fontstring ("<font color = darkorange size = \"5\">");
908
909     pSelectLevelsetFromListItem_->setHtml(tr("Choose a levelset").prepend(fontstring));
910
911     int yPos = 100;
912
913     levelsetItems_.clear();
914
915
916     foreach (Levelset set, availableLevelsets_)
917     {
918         QGraphicsTextItem * pItem = new QGraphicsTextItem(pSelectLevelsetFromListItem_);
919         QString text (fontstring);
920         if (levelset_.getName() == set.getName())
921             text.append("<b>");
922         text.append(set.getName());
923         pItem->setHtml(text);
924         pItem->setPos(65,yPos);
925         yPos+=80;
926         pItem->setZValue(1000);
927         pItem->setFlag(QGraphicsItem::ItemIsSelectable);
928         pItem->setData(0,QVariant::fromValue(set));
929         levelsetItems_.append(pItem);
930
931     }
932
933   }