Changed the section to user/hidden as required for Nokia Store
[ghostsoverboard] / orientationcontrolledgraphicspixmapobject.cpp
index 3bdd28c..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
 
@@ -24,6 +24,7 @@
 #include "orientationcontrolledgraphicspixmapobject.h"
 #include <QDebug>
 #include <QGraphicsScene>
+#include <QPropertyAnimation>
 
 //OrientationControlledGraphicsPixmapObject::OrientationControlledGraphicsPixmapObject (QGraphicsItem *parent) :
 //    QObject(), QGraphicsPixmapItem (parent)
@@ -35,7 +36,10 @@ 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()));
 
 
 
@@ -52,6 +56,8 @@ void OrientationControlledGraphicsPixmapObject::startMoving()
 void OrientationControlledGraphicsPixmapObject::stopMoving()
 {
     rotationSensor_.stop();
+    rotationReadingInitialized_ = false;
+    rotationReadingTimer_.stop();
 //    qDebug () << "trying to stop the sensor";
 }
 
@@ -60,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();
 
 
@@ -77,24 +94,38 @@ void OrientationControlledGraphicsPixmapObject::readRotationSensor()
     int oldx = x();
     int oldy = y();
 
-    int newx = x() + deltax/15;
-    int newy = y() + deltay/15;
+    //this is how it works on maemo
+//    int newx = x() + deltax/15;
+//    int newy = y() + deltay/15;
+
+    //this is for Harmattan
+    int newx = x() + deltax/12;
+    int newy = y() + deltay/12;
 
 
 //    qDebug() << sceneRectangle.left() << sceneRectangle.right();
 
 
-    setX(qBound(sceneRectangle.left(),newx,sceneRectangle.right()-pixmap().width()));
-    setY(qBound(sceneRectangle.top(),newy,sceneRectangle.bottom()-pixmap().height()));
 
+    int finalX = qBound(sceneRectangle.left(),newx,sceneRectangle.right()-pixmap().width());
+    int finalY = qBound(sceneRectangle.top(),newy,sceneRectangle.bottom()-pixmap().height());
+
+
+    setPos(QPointF(finalX,finalY));
+
+//    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
 
-    if (handleCollisions() == false)
-    {
-        setX(oldx);
-        setY(oldy);
-    }
+//    if (handleCollisions() == false)
+//    {
+//        setX(oldx);
+//        setY(oldy);
+//    }
 
 }
 
@@ -104,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();
+    }
+}