From: Heli Hyvättinen Date: Tue, 19 Jul 2011 12:01:32 +0000 (+0300) Subject: Moved menu actions to SeaScene from MainWindow X-Git-Tag: v0.3.0_fremantle~30 X-Git-Url: http://git.maemo.org/git/?p=ghostsoverboard;a=commitdiff_plain;h=91fbdcfde625a51d4e0f6391b530e3bafafa8681 Moved menu actions to SeaScene from MainWindow Also, the levellist information is now stored in the SeaScene itself instead of MainWindow Also, when tapping the screen in paused state: if menu item is selected act upon it and clear the selection. Otherwise unpause. Also, moved checking for minimizing and maximizing events to SeaView from MainWindow. --- diff --git a/mainwindow.cpp b/mainwindow.cpp index 6aae247..0b2c61c 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -41,7 +41,7 @@ MainWindow::MainWindow(QWidget *parent) setWindowTitle("Ghosts Overboard"); pScene_ = new SeaScene (); - connect(pScene_,SIGNAL(allGhostsPicked()),this,SLOT(nextLevel())); + connect(pScene_,SIGNAL(allGhostsPicked()),pScene_,SLOT(nextLevel())); pView_ = new SeaView (); @@ -49,54 +49,17 @@ MainWindow::MainWindow(QWidget *parent) pView_->setScene(pScene_); setCentralWidget(pView_); - pPauseAction_ = new QAction(tr("Pause"),this); - pPauseAction_->setCheckable(true); - addAction(pPauseAction_); - connect(pPauseAction_,SIGNAL(toggled(bool)),pScene_,SLOT(pause(bool))); - menuBar()->addAction(pPauseAction_); - connect(pView_,SIGNAL(pauseChanged()),pPauseAction_,SLOT(toggle())); + connect(pView_,SIGNAL(screenTapped()),pScene_,SLOT(handleScreenTapped())); + connect(pView_,SIGNAL(goingBackgroung()),pScene_,SLOT(forcePause())); + connect(pView_,SIGNAL(goingForeground()),pScene_,SLOT(softContinue())); - QAction * pRestartLevelAction = new QAction(tr("Restart level"),this); - addAction(pRestartLevelAction); - connect(pRestartLevelAction,SIGNAL(triggered()),this,SLOT(restartLevel())); - menuBar()->addAction(pRestartLevelAction); - - QAction * pRestartGameAction = new QAction(tr("Restart game"),this); - addAction(pRestartGameAction); - connect(pRestartGameAction,SIGNAL(triggered()),this,SLOT(restartGame())); - menuBar()->addAction(pRestartGameAction); - - pVibrateAction_ = new QAction(tr("Vibration effects"),this); - pVibrateAction_->setCheckable(true); - addAction(pVibrateAction_); - connect(pVibrateAction_,SIGNAL(toggled(bool)),pScene_,SLOT(vibrationActivate(bool))); - menuBar()->addAction(pVibrateAction_); - - - QAction * pAboutAction = new QAction(tr("About"),this); - addAction(pAboutAction); - connect(pAboutAction,SIGNAL(triggered()),this,SLOT(about())); - menuBar()->addAction(pAboutAction); - - - - Level level1(5,10); - levelList_.append(level1); - Level level2(5,10,2,50); - levelList_.append(level2); - Level level3(5,15,2,50); - levelList_.append(level3); - Level level4(5,15,4,50); - levelList_.append(level4); - Level level5(5,15,5,100); - levelList_.append(level5); - - currentLevel_ = 0; //the boundaries of the scene are set to match the size of the view window, which is not //available in the constructor --> timer needed QTimer::singleShot(100,this,SLOT(initializeBoundaries())); + + } MainWindow::~MainWindow() @@ -122,130 +85,6 @@ void MainWindow::initializeBoundaries() // qDebug() << "Initialized boundaries" << rectangle.right() << rectangle.bottom() << pView_->width() << pView_->height(); - restartLevel(); -} - - -void MainWindow::restartLevel() -{ - pScene_->setupMap(levelList_.value(currentLevel_)); //value() returns default constructor Level if index is invalid, so no risk of crash - pScene_->vibrationActivate(pVibrateAction_->isChecked()); //Vibration effects are lost without this - // qDebug() << pVibrateAction_->isChecked(); -} - -void MainWindow::about() -{ - QMessageBox::about(this, tr("About %1").arg(QApplication::applicationName()), - tr("Version %1" - "

Copyright 2011 Heli Hyvättinen" - "

License: General Public License v2" - "

Bug Reports: https://bugs.maemo.org/ " - "enter_bug.cgi?product=Ghosts%20Overboard" - ).arg(QApplication::applicationVersion())); - - - + pScene_->restartLevel(); } -void MainWindow::nextLevel() -{ - - currentLevel_++; - - if (levelList_.empty()) - pScene_->setupMap(Level()); - - - if ( currentLevel_ < levelList_.size() ) - { - restartLevel(); - } - - else //Victory! - { - - QDialog* pVictoryDialog = new QDialog(this); - pVictoryDialog->setWindowTitle(tr("You won!")); - - - QPushButton* pPlayAgainButton = new QPushButton(tr("Play again")); -// QPushButton* pQuitButton = new QPushButton(tr("Quit game")); - - QPixmap victoryIcon (":/pix/aavesaari.png"); - QLabel* pVictoryLabel = new QLabel(); - pVictoryLabel->setPixmap(victoryIcon); - - QLabel* pTextLabel = new QLabel(tr("Congratulations!

You have saved all the ghosts.")); - - - QVBoxLayout* pMainLayout = new QVBoxLayout; - - QHBoxLayout* pTopLayout = new QHBoxLayout; - pMainLayout->addLayout(pTopLayout); - - pTopLayout->addWidget(pVictoryLabel); - pTopLayout->addWidget(pTextLabel); - - - - QHBoxLayout* pButtonLayout = new QHBoxLayout(); - pMainLayout->addLayout(pButtonLayout); - - // pButtonLayout->addWidget(pQuitButton); - pButtonLayout->addWidget(pPlayAgainButton); - - - - pVictoryDialog->setLayout(pMainLayout); - - connect(pPlayAgainButton, SIGNAL(clicked()),pVictoryDialog,SLOT(accept())); - - pVictoryDialog->exec(); - - //Never mind if the user cancels the dialog: restart the game anyway - - restartGame(); - } -} - -bool MainWindow::event(QEvent *event) -{ - - switch (event->type()) - { - //pause if app goes to background - case QEvent::WindowDeactivate: - - if (pScene_) - pScene_->pause(true); - break; - - //un-pause if app gomes back to foreground unless it was paused before going to background - case QEvent::WindowActivate: - - - if (pPauseAction_ && !pPauseAction_->isChecked()) - { - if (pScene_) - pScene_->pause(false); - } - break; - - //Just to keep the compiler from complaining... - default: - break; - - } - - - - //pass the event to the ancestor for handling - return QMainWindow::event(event); - - } - -void MainWindow::restartGame() -{ - currentLevel_ = 0; - restartLevel(); -} diff --git a/mainwindow.h b/mainwindow.h index b96eb4a..99b7154 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -38,14 +38,11 @@ public: MainWindow(QWidget *parent = 0); ~MainWindow(); - bool event(QEvent *event); + public slots: void initializeBoundaries(); - void restartLevel(); - void about(); - void nextLevel(); - void restartGame(); + @@ -53,10 +50,9 @@ private: SeaScene * pScene_; SeaView * pView_; -QAction* pPauseAction_; -QList levelList_; -int currentLevel_; -QAction* pVibrateAction_; + + + }; diff --git a/seascene.cpp b/seascene.cpp index 28ad362..9fed8d7 100644 --- a/seascene.cpp +++ b/seascene.cpp @@ -27,6 +27,11 @@ #include #include #include +#include +#include +#include +#include +#include const QString ghostImageFilename_ = ":/pix/aave.png"; const QString rockImageFilename_ =":/pix/kari.png"; @@ -49,16 +54,30 @@ SeaScene::SeaScene(QObject *parent) : qsrand(QTime::currentTime().msec()+2); //+2 to avoid setting it to 1 - //connect selecting to menu handling (only menu items selectable) - - connect(this,SIGNAL(selectionChanged()),this,SLOT(menuClicked())); - +//Setup the level list + Level level1(5,10); + levelList_.append(level1); + Level level2(5,10,2,50); + levelList_.append(level2); + Level level3(5,15,2,50); + levelList_.append(level3); + Level level4(5,15,4,50); + levelList_.append(level4); + Level level5(5,15,5,100); + levelList_.append(level5); + currentLevel_ = 0; + pVibrateAction_ = new QAction(tr("Vibration effects"),this); + pVibrateAction_->setCheckable(true); + connect(pVibrateAction_,SIGNAL(toggled(bool)),this,SLOT(vibrationActivate(bool))); + pPauseAction_ = new QAction(tr("Pause"),this); + pPauseAction_->setCheckable(true); + connect(pPauseAction_,SIGNAL(toggled(bool)),this,SLOT(pause(bool))); } @@ -340,22 +359,36 @@ void SeaScene::vibrationActivate(bool on) emit vibrationActivated(on); } -void SeaScene::menuClicked() +void SeaScene::handleScreenTapped() { + + //If the game is going just pause it + if (!paused_) + { + pPauseAction_->setChecked(true); + return; + } + + //If the game is paused, chacl if menu item was selected + QList items = selectedItems(); - //if nothing selected (selection was removed) do nothing + //if nothing selected resume play if (items.isEmpty()) + { + pPauseAction_->setChecked(false); return; - //Tapping the screen unpaused the game, pause it again! - + } + //If something was selected check if it was one of the menu items and act on it + //(Nothing else should be made selectable anyway) //Menu functions - QGraphicsItem* pItem = items.at(0); + QGraphicsItem* pItem = items.at(0); //Selecting an item brings here, thus only selecting one item should be possible + //... so we can just take the first one if (pItem == pRestartGameItem_) @@ -376,11 +409,11 @@ void SeaScene::menuClicked() else if (pItem == pAboutItem_) { - + about(); } - //Selection is just used to get notice of being clicked, removed after use + //Selection is just used to get notice of a menu item being clicked, removed after use clearSelection(); @@ -426,3 +459,110 @@ void SeaScene::prepareForMenu(QGraphicsItem * pItem) pItem->setX(menuItemCount_++*150-250); } + +void SeaScene::about() +{ + QMessageBox::about(NULL, tr("About %1").arg(QApplication::applicationName()), + tr("Version %1" + "

Copyright 2011 Heli Hyvättinen" + "

License: General Public License v2" + "

Bug Reports: https://bugs.maemo.org/ " + "enter_bug.cgi?product=Ghosts%20Overboard" + ).arg(QApplication::applicationVersion())); + + + +} + + +void SeaScene::restartLevel() +{ + setupMap(levelList_.value(currentLevel_)); //value() returns default constructor Level if index is invalid, so no risk of crash + vibrationActivate(pVibrateAction_->isChecked()); //Vibration effects are lost without this + // qDebug() << pVibrateAction_->isChecked(); +} + + + +void SeaScene::nextLevel() +{ + + currentLevel_++; + + if (levelList_.empty()) + setupMap(Level()); + + + if ( currentLevel_ < levelList_.size() ) + { + restartLevel(); + } + + else //Victory! + { + + QDialog* pVictoryDialog = new QDialog(); + pVictoryDialog->setWindowTitle(tr("You won!")); + + + QPushButton* pPlayAgainButton = new QPushButton(tr("Play again")); +// QPushButton* pQuitButton = new QPushButton(tr("Quit game")); + + QPixmap victoryIcon (":/pix/aavesaari.png"); + QLabel* pVictoryLabel = new QLabel(); + pVictoryLabel->setPixmap(victoryIcon); + + QLabel* pTextLabel = new QLabel(tr("Congratulations!

You have saved all the ghosts.")); + + + QVBoxLayout* pMainLayout = new QVBoxLayout; + + QHBoxLayout* pTopLayout = new QHBoxLayout; + pMainLayout->addLayout(pTopLayout); + + pTopLayout->addWidget(pVictoryLabel); + pTopLayout->addWidget(pTextLabel); + + + + QHBoxLayout* pButtonLayout = new QHBoxLayout(); + pMainLayout->addLayout(pButtonLayout); + + // pButtonLayout->addWidget(pQuitButton); + pButtonLayout->addWidget(pPlayAgainButton); + + + + pVictoryDialog->setLayout(pMainLayout); + + connect(pPlayAgainButton, SIGNAL(clicked()),pVictoryDialog,SLOT(accept())); + + pVictoryDialog->exec(); + + //Never mind if the user cancels the dialog: restart the game anyway + + restartGame(); + } +} + + +void SeaScene::restartGame() +{ + currentLevel_ = 0; + restartLevel(); +} + + +void SeaScene::forcePause() +{ + //Pause without setting the pause action state + pause(true); +} + +void::SeaScene::softContinue() +{ + //Continue if not being paused by the user + // Reverts forcePause() + + pause(pPauseAction_->isChecked()); +} diff --git a/seascene.h b/seascene.h index 7690d57..08e6a4e 100644 --- a/seascene.h +++ b/seascene.h @@ -28,6 +28,7 @@ #include #include "screenlitkeeper.h" #include "level.h" +#include class SeaScene : public QGraphicsScene { @@ -64,7 +65,20 @@ public slots: void vibrationActivate(bool); - void menuClicked(); + void handleScreenTapped(); + + void about(); + + void restartLevel(); + + void nextLevel(); + + void restartGame(); + + void forcePause(); + + void softContinue(); + protected: @@ -103,6 +117,17 @@ protected: QGraphicsSimpleTextItem * pSettingsItem_; QGraphicsSimpleTextItem * pAboutItem_; + QList levelList_; + + int currentLevel_; + + + QAction* pVibrateAction_; + + QAction* pPauseAction_; + + bool pauseForced_; + }; diff --git a/seaview.cpp b/seaview.cpp index 8d6564e..1ea801b 100644 --- a/seaview.cpp +++ b/seaview.cpp @@ -22,16 +22,51 @@ #include "seaview.h" +#include + SeaView::SeaView(QWidget *parent) : QGraphicsView(parent) { + } void SeaView::mousePressEvent(QMouseEvent *event) { QGraphicsView::mousePressEvent(event); - emit pauseChanged(); + emit screenTapped(); } + +bool SeaView::event(QEvent *event) +{ + if (!event) + return false; + + switch (event->type()) + { + //pause if app goes to background + case QEvent::WindowDeactivate: + + emit goingBackgroung(); + break; + + //un-pause if app gomes back to foreground unless it was paused before going to background + case QEvent::WindowActivate: + + emit goingForeground(); + break; + + //Just to keep the compiler from complaining... + default: + break; + + } + + + + //pass the event to the ancestor for handling + return QGraphicsView::event(event); + + } diff --git a/seaview.h b/seaview.h index dcbd566..332cc90 100644 --- a/seaview.h +++ b/seaview.h @@ -31,11 +31,17 @@ class SeaView : public QGraphicsView public: explicit SeaView(QWidget *parent = 0); - virtual void mousePressEvent(QMouseEvent *event); + virtual void mousePressEvent(QMouseEvent *event); + + bool event(QEvent *event); signals: - void pauseChanged(); + void screenTapped(); + + void goingBackgroung(); + + void goingForeground(); public slots: