The score counting now stops during pauses
[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     connect(&rotationSensor_,SIGNAL(readingChanged()),this,SLOT(readRotationSensor()));
40
41 //    qrangelist rangelist = rotationSensor_.availableDataRates();
42
43 //    qDebug() << rangelist.length() << "ranges found";
44 //    foreach (qrange range, rangelist)
45 //    {
46 //        qDebug() << "Rotation sensor: " << range.first <<", " << "range.second";
47 //    }
48 }
49
50 void OrientationControlledGraphicsPixmapObject::startMoving()
51 {
52     rotationSensor_.start();
53 //    qDebug() << "started the sensor";
54 //    qDebug() << rotationSensor_.isActive();
55 }
56
57
58 void OrientationControlledGraphicsPixmapObject::stopMoving()
59 {
60     rotationSensor_.stop();
61 //    qDebug () << "trying to stop the sensor";
62 }
63
64 void OrientationControlledGraphicsPixmapObject::readRotationSensor()
65 {
66     if (!scene()) //no movement if this item does not belong to a scene
67         return;
68
69     QRect sceneRectangle = scene()->sceneRect().toRect();
70
71
72     QRotationReading* pSensorData = rotationSensor_.reading();
73
74     //    int deltay = pSensorData->x(); //yes, in Maemo 5 you need the "x" value from the sensor for "y" direction in the scene...
75     //    int deltax = pSensorData->y(); //...and vice versa
76
77         int deltay = -pSensorData->y(); //But in Harmattan, you need the "y" value from the sensor for "y" direction in the scene...
78         int deltax = pSensorData->x(); //...and x for x (when in landscape)
79                                       //and you need to reverse the y value
80
81  //   qDebug() << deltax << " " << deltay;
82
83     int oldx = x();
84     int oldy = y();
85
86     //this is how it works on maemo
87 //    int newx = x() + deltax/15;
88 //    int newy = y() + deltay/15;
89
90     //this is for Harmattan
91     int newx = x() + deltax/3;
92     int newy = y() + deltay/3;
93
94
95 //    qDebug() << sceneRectangle.left() << sceneRectangle.right();
96
97
98
99     int finalX = qBound(sceneRectangle.left(),newx,sceneRectangle.right()-pixmap().width());
100     int finalY = qBound(sceneRectangle.top(),newy,sceneRectangle.bottom()-pixmap().height());
101
102
103
104     QPropertyAnimation * animation = new QPropertyAnimation(this,"pos",this);
105     animation->setDuration(60); //milliseconds
106     animation->setStartValue(pos());
107     animation->setEndValue( QPointF(finalX,finalY));
108     animation->start(QAbstractAnimation::DeleteWhenStopped);
109
110     //handle collisions and move back to the original position if false returned
111
112 //    if (handleCollisions() == false)
113 //    {
114 //        setX(oldx);
115 //        setY(oldy);
116 //    }
117
118 }
119
120
121 bool OrientationControlledGraphicsPixmapObject::handleCollisions()
122 {
123     return true;
124 }
125
126
127 void OrientationControlledGraphicsPixmapObject::setPos(const QPointF &pos)
128 {
129     QPointF oldPos = OrientationControlledGraphicsPixmapObject::pos();
130
131     QGraphicsPixmapItem::setPos(pos);
132
133     if (!handleCollisions())
134     {
135         QGraphicsPixmapItem::setPos(oldPos);
136     }
137
138 }