Changed the section to user/hidden as required for Nokia Store
[ghostsoverboard] / orientationcontrolledgraphicspixmapobject.cpp
index 49d6bf1..3b41ab2 100644 (file)
@@ -1,5 +1,5 @@
 /**************************************************************************
-        Ghosts Overboard - a game for Maemo 5
+        Ghosts Overboard - a game 'Meego 1.2 Harmattan'
 
         Copyright (C) 2011  Heli Hyvättinen
 
@@ -36,15 +36,13 @@ OrientationControlledGraphicsPixmapObject::OrientationControlledGraphicsPixmapOb
     QObject(), QGraphicsPixmapItem (pixmap, parent)
 {
 
-    connect(&rotationSensor_,SIGNAL(readingChanged()),this,SLOT(readRotationSensor()));
+    rotationReadingInitialized_ = false;
+    rotationReadingTimer_.setInterval(15);
+    connect(&rotationReadingTimer_,SIGNAL(timeout()),this,SLOT(readRotationSensor()));
+    connect(&rotationSensor_,SIGNAL(readingChanged()),this,SLOT(rotationSensorReady()));
+
 
-    qrangelist rangelist = rotationSensor_.availableDataRates();
 
-    qDebug() << rangelist.length() << "ranges found";
-    foreach (qrange range, rangelist)
-    {
-        qDebug() << "Rotation sensor: " << range.first <<", " << "range.second";
-    }
 }
 
 void OrientationControlledGraphicsPixmapObject::startMoving()
@@ -58,6 +56,8 @@ void OrientationControlledGraphicsPixmapObject::startMoving()
 void OrientationControlledGraphicsPixmapObject::stopMoving()
 {
     rotationSensor_.stop();
+    rotationReadingInitialized_ = false;
+    rotationReadingTimer_.stop();
 //    qDebug () << "trying to stop the sensor";
 }
 
@@ -66,6 +66,17 @@ void OrientationControlledGraphicsPixmapObject::readRotationSensor()
     if (!scene()) //no movement if this item does not belong to a scene
         return;
 
+
+//Test reading sensor information. Since qDebug has ceased to work, uses qCritical instead...
+//        qrangelist rangelist = rotationSensor_.availableDataRates();
+
+//        qCritical() << rangelist.length() << "ranges found";
+//        foreach (qrange range, rangelist)
+//        {
+//            qCritical() << "Rotation sensor: " << range.first <<", " << "range.second";
+//        }
+//        qCritical() << "Current data date is " << rotationSensor_.dataRate();
+
     QRect sceneRectangle = scene()->sceneRect().toRect();
 
 
@@ -88,8 +99,8 @@ void OrientationControlledGraphicsPixmapObject::readRotationSensor()
 //    int newy = y() + deltay/15;
 
     //this is for Harmattan
-    int newx = x() + deltax/2;
-    int newy = y() + deltay/2;
+    int newx = x() + deltax/12;
+    int newy = y() + deltay/12;
 
 
 //    qDebug() << sceneRectangle.left() << sceneRectangle.right();
@@ -100,12 +111,13 @@ void OrientationControlledGraphicsPixmapObject::readRotationSensor()
     int finalY = qBound(sceneRectangle.top(),newy,sceneRectangle.bottom()-pixmap().height());
 
 
+    setPos(QPointF(finalX,finalY));
 
-    QPropertyAnimation * animation = new QPropertyAnimation(this,"pos",this);
-    animation->setDuration(100); //milliseconds
-    animation->setStartValue(pos());
-    animation->setEndValue( QPointF(finalX,finalY));
-    animation->start(QAbstractAnimation::DeleteWhenStopped);
+//    QPropertyAnimation * animation = new QPropertyAnimation(this,"pos",this);
+//    animation->setDuration(60); //milliseconds
+//    animation->setStartValue(pos());
+//    animation->setEndValue( QPointF(finalX,finalY));
+//    animation->start(QAbstractAnimation::DeleteWhenStopped);
 
     //handle collisions and move back to the original position if false returned
 
@@ -123,3 +135,27 @@ bool OrientationControlledGraphicsPixmapObject::handleCollisions()
     return true;
 }
 
+
+void OrientationControlledGraphicsPixmapObject::setPos(const QPointF &pos)
+{
+    QPointF oldPos = OrientationControlledGraphicsPixmapObject::pos();
+
+    QGraphicsPixmapItem::setPos(pos);
+
+    if (!handleCollisions())
+    {
+        QGraphicsPixmapItem::setPos(oldPos);
+    }
+
+}
+
+
+void OrientationControlledGraphicsPixmapObject::rotationSensorReady()
+{
+
+    if (!rotationReadingInitialized_)
+    {
+        rotationReadingInitialized_ = true;
+        rotationReadingTimer_.start();
+    }
+}