setWindowTitle("Ghosts Overboard");
pScene_ = new SeaScene ();
- connect(pScene_,SIGNAL(allGhostsPicked()),this,SLOT(nextLevel()));
+ connect(pScene_,SIGNAL(allGhostsPicked()),pScene_,SLOT(nextLevel()));
pView_ = new SeaView ();
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()
// 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"
- "<p>Copyright 2011 Heli Hyvättinen"
- "<p>License: General Public License v2"
- "<p>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! <p>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();
-}
MainWindow(QWidget *parent = 0);
~MainWindow();
- bool event(QEvent *event);
+
public slots:
void initializeBoundaries();
- void restartLevel();
- void about();
- void nextLevel();
- void restartGame();
+
SeaScene * pScene_;
SeaView * pView_;
-QAction* pPauseAction_;
-QList<Level> levelList_;
-int currentLevel_;
-QAction* pVibrateAction_;
+
+
+
};
#include <QDebug>
#include <QMessageBox>
#include <QTime>
+#include <QApplication>
+#include <QAction>
+#include <QPushButton>
+#include <QLabel>
+#include <QVBoxLayout>
const QString ghostImageFilename_ = ":/pix/aave.png";
const QString rockImageFilename_ =":/pix/kari.png";
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)));
}
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<QGraphicsItem *> 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_)
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();
pItem->setX(menuItemCount_++*150-250);
}
+
+void SeaScene::about()
+{
+ QMessageBox::about(NULL, tr("About %1").arg(QApplication::applicationName()),
+ tr("Version %1"
+ "<p>Copyright 2011 Heli Hyvättinen"
+ "<p>License: General Public License v2"
+ "<p>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! <p>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());
+}
#include<QGraphicsItemGroup>
#include "screenlitkeeper.h"
#include "level.h"
+#include <QAction>
class SeaScene : public QGraphicsScene
{
void vibrationActivate(bool);
- void menuClicked();
+ void handleScreenTapped();
+
+ void about();
+
+ void restartLevel();
+
+ void nextLevel();
+
+ void restartGame();
+
+ void forcePause();
+
+ void softContinue();
+
protected:
QGraphicsSimpleTextItem * pSettingsItem_;
QGraphicsSimpleTextItem * pAboutItem_;
+ QList<Level> levelList_;
+
+ int currentLevel_;
+
+
+ QAction* pVibrateAction_;
+
+ QAction* pPauseAction_;
+
+ bool pauseForced_;
+
};
#include "seaview.h"
+#include <QEvent>
+
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);
+
+ }
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: