Reads rotation sensor on timer instead whenever new reading arrives
[ghostsoverboard] / orientationcontrolledgraphicspixmapobject.cpp
1 /**************************************************************************
2         Ghosts Overboard - a game 'Meego 1.2 Harmattan'
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 #include <QPropertyAnimation>
28
29 //OrientationControlledGraphicsPixmapObject::OrientationControlledGraphicsPixmapObject (QGraphicsItem *parent) :
30 //    QObject(), QGraphicsPixmapItem (parent)
31 //{
32
33 //}
34
35 OrientationControlledGraphicsPixmapObject::OrientationControlledGraphicsPixmapObject(QPixmap pixmap, QGraphicsItem *parent) :
36     QObject(), QGraphicsPixmapItem (pixmap, parent)
37 {
38
39     rotationReadingInitialized_ = false;
40     rotationReadingTimer_.setInterval(100);
41     connect(&rotationSensor_,SIGNAL(readingChanged()),this,SLOT(rotationSensorReady()));
42
43
44
45 }
46
47 void OrientationControlledGraphicsPixmapObject::startMoving()
48 {
49     rotationSensor_.start();
50 //    qDebug() << "started the sensor";
51 //    qDebug() << rotationSensor_.isActive();
52 }
53
54
55 void OrientationControlledGraphicsPixmapObject::stopMoving()
56 {
57     rotationSensor_.stop();
58     rotationReadingInitialized_ = false;
59     rotationReadingTimer_.stop();
60 //    qDebug () << "trying to stop the sensor";
61 }
62
63 void OrientationControlledGraphicsPixmapObject::readRotationSensor()
64 {
65     if (!scene()) //no movement if this item does not belong to a scene
66         return;
67
68
69 //Test reading sensor information. Since qDebug has ceased to work, uses qCritical instead...
70 //        qrangelist rangelist = rotationSensor_.availableDataRates();
71
72 //        qCritical() << rangelist.length() << "ranges found";
73 //        foreach (qrange range, rangelist)
74 //        {
75 //            qCritical() << "Rotation sensor: " << range.first <<", " << "range.second";
76 //        }
77 //        qCritical() << "Current data date is " << rotationSensor_.dataRate();
78
79     QRect sceneRectangle = scene()->sceneRect().toRect();
80
81
82     QRotationReading* pSensorData = rotationSensor_.reading();
83
84     //    int deltay = pSensorData->x(); //yes, in Maemo 5 you need the "x" value from the sensor for "y" direction in the scene...
85     //    int deltax = pSensorData->y(); //...and vice versa
86
87         int deltay = -pSensorData->y(); //But in Harmattan, you need the "y" value from the sensor for "y" direction in the scene...
88         int deltax = pSensorData->x(); //...and x for x (when in landscape)
89                                       //and you need to reverse the y value
90
91  //   qDebug() << deltax << " " << deltay;
92
93     int oldx = x();
94     int oldy = y();
95
96     //this is how it works on maemo
97 //    int newx = x() + deltax/15;
98 //    int newy = y() + deltay/15;
99
100     //this is for Harmattan
101     int newx = x() + deltax/3;
102     int newy = y() + deltay/3;
103
104
105 //    qDebug() << sceneRectangle.left() << sceneRectangle.right();
106
107
108
109     int finalX = qBound(sceneRectangle.left(),newx,sceneRectangle.right()-pixmap().width());
110     int finalY = qBound(sceneRectangle.top(),newy,sceneRectangle.bottom()-pixmap().height());
111
112
113
114     QPropertyAnimation * animation = new QPropertyAnimation(this,"pos",this);
115     animation->setDuration(60); //milliseconds
116     animation->setStartValue(pos());
117     animation->setEndValue( QPointF(finalX,finalY));
118     animation->start(QAbstractAnimation::DeleteWhenStopped);
119
120     //handle collisions and move back to the original position if false returned
121
122 //    if (handleCollisions() == false)
123 //    {
124 //        setX(oldx);
125 //        setY(oldy);
126 //    }
127
128 }
129
130
131 bool OrientationControlledGraphicsPixmapObject::handleCollisions()
132 {
133     return true;
134 }
135
136
137 void OrientationControlledGraphicsPixmapObject::setPos(const QPointF &pos)
138 {
139     QPointF oldPos = OrientationControlledGraphicsPixmapObject::pos();
140
141     QGraphicsPixmapItem::setPos(pos);
142
143     if (!handleCollisions())
144     {
145         QGraphicsPixmapItem::setPos(oldPos);
146     }
147
148 }
149
150
151 void OrientationControlledGraphicsPixmapObject::rotationSensorReady()
152 {
153
154     if (!rotationReadingInitialized_)
155     {
156         connect(&rotationReadingTimer_,SIGNAL(timeout()),this,SLOT(readRotationSensor()));
157         rotationReadingInitialized_ = true;
158         rotationReadingTimer_.start();
159     }
160 }