Collision detection works
[ghostsoverboard] / mainwindow.cpp
1 #include "mainwindow.h"
2 #include <QPixmap>
3 #include <QTimer>
4 #include <QDebug>
5 #include <QAction>
6 #include <QMenuBar>
7
8
9
10 MainWindow::MainWindow(QWidget *parent)
11     : QMainWindow(parent)
12 {
13     paused_ = false;
14
15     pScene_ = new QGraphicsScene ();
16     pView_  = new QGraphicsView ();
17
18     QPixmap waves (":/pix/meri.png");
19     pScene_->setBackgroundBrush(QBrush(waves));
20
21     pTursas_ = new OrientationControlledGraphicsPixmapObject(QPixmap(":/pix/tursas.png"));
22     pScene_->addItem(pTursas_);
23
24     pView_->setScene(pScene_);
25     setCentralWidget(pView_);
26
27
28     //the boundaries of the scene are set to match the size of the view window, which is not
29     //available in the constructor --> timer needed
30     QTimer::singleShot(100,this,SLOT(initializeBoundaries()));
31
32
33     QAction * pPauseAction = new QAction(tr("Pause"),this);
34     pPauseAction->setCheckable(true);
35     addAction(pPauseAction);
36     connect(pPauseAction,SIGNAL(triggered(bool)),this,SLOT(pause(bool)));
37     menuBar()->addAction(pPauseAction);
38
39     QGraphicsPixmapItem * pGhost = pScene_->addPixmap(QPixmap(":/pix/aave.png"));
40     pGhost->setData(0,"ghost");
41     QGraphicsPixmapItem * pRock =  pScene_->addPixmap(QPixmap(":/pix/kari.png"));
42         QGraphicsPixmapItem * pRock2 =  pScene_->addPixmap(QPixmap(":/pix/kari.png"));
43     pRock->moveBy(40,40);
44     pRock->setData(0,"rock");
45     pRock2->moveBy(80,80);
46     pRock2->setData(0,"rock");
47
48
49 }
50
51 MainWindow::~MainWindow()
52 {
53
54 }
55
56 void MainWindow::initializeBoundaries()
57 {
58         //the boundaries of the scene are set to match the size of the view window, and
59         //the view is set to show exactly the whole scene area
60
61     QPoint topleft (0,0);
62     QSize windowsize = pView_->size();
63     QRectF rectangle (topleft,windowsize);
64
65
66     pScene_->setSceneRect(rectangle);
67     pView_->setSceneRect(rectangle);
68     pTursas_->setBoundaries(rectangle);
69     pTursas_->startMoving();
70
71     qDebug() << "Initialized boundaries" << rectangle.left() << rectangle.right() << pView_->width();
72 }
73
74 void MainWindow::pause(bool paused)
75 {
76 //    qDebug() << "pause pressed " << paused;
77     if (paused_ == paused)
78             return;
79
80     paused_ = paused;
81
82     if (paused == false)
83     {
84  //       qDebug() << "starting to move again";
85         pTursas_->startMoving();
86     }
87
88     else
89     {
90         qDebug("about to stop movement");
91         pTursas_->stopMoving();
92     }
93 }