Menu no longer crashes randomly
[ghostsoverboard] / orientationcontrolledgraphicspixmapobject.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
24 #include "orientationcontrolledgraphicspixmapobject.h"
25 #include <QDebug>
26 #include <QGraphicsScene>
27
28 //OrientationControlledGraphicsPixmapObject::OrientationControlledGraphicsPixmapObject (QGraphicsItem *parent) :
29 //    QObject(), QGraphicsPixmapItem (parent)
30 //{
31
32 //}
33
34 OrientationControlledGraphicsPixmapObject::OrientationControlledGraphicsPixmapObject(QPixmap pixmap, QGraphicsItem *parent) :
35     QObject(), QGraphicsPixmapItem (pixmap, parent)
36 {
37
38     connect(&rotationSensor_,SIGNAL(readingChanged()),this,SLOT(readRotationSensor()));
39
40
41
42 }
43
44 void OrientationControlledGraphicsPixmapObject::startMoving()
45 {
46     rotationSensor_.start();
47 //    qDebug() << "started the sensor";
48 //    qDebug() << rotationSensor_.isActive();
49 }
50
51
52 void OrientationControlledGraphicsPixmapObject::stopMoving()
53 {
54     rotationSensor_.stop();
55 //    qDebug () << "trying to stop the sensor";
56 }
57
58 void OrientationControlledGraphicsPixmapObject::readRotationSensor()
59 {
60     if (!scene()) //no movement if this item does not belong to a scene
61         return;
62
63     QRect sceneRectangle = scene()->sceneRect().toRect();
64
65
66     QRotationReading* pSensorData = rotationSensor_.reading();
67
68     //    int deltay = pSensorData->x(); //yes, in Maemo 5 you need the "x" value from the sensor for "y" direction in the scene...
69     //    int deltax = pSensorData->y(); //...and vice versa
70
71         int deltay = -pSensorData->y(); //But in Harmattan, you need the "y" value from the sensor for "y" direction in the scene...
72         int deltax = pSensorData->x(); //...and x for x (when in landscape)
73                                       //and you need to reverse the y value
74
75  //   qDebug() << deltax << " " << deltay;
76
77     int oldx = x();
78     int oldy = y();
79
80     //this is how it works on maemo
81 //    int newx = x() + deltax/15;
82 //    int newy = y() + deltay/15;
83
84     //this is for Harmattan
85     int newx = x() + deltax/5;
86     int newy = y() + deltay/5;
87
88
89 //    qDebug() << sceneRectangle.left() << sceneRectangle.right();
90
91
92     setX(qBound(sceneRectangle.left(),newx,sceneRectangle.right()-pixmap().width()));
93     setY(qBound(sceneRectangle.top(),newy,sceneRectangle.bottom()-pixmap().height()));
94
95
96     //handle collisions and move back to the original position if false returned
97
98     if (handleCollisions() == false)
99     {
100         setX(oldx);
101         setY(oldy);
102     }
103
104 }
105
106
107 bool OrientationControlledGraphicsPixmapObject::handleCollisions()
108 {
109     return true;
110 }
111