aboutsummaryrefslogtreecommitdiff
path: root/src/ParallaxScrollerItem.cpp
blob: 0480b29fc1f6b8d9c9eefed4a0da78bec1d77cd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "ParallaxScrollerItem.h"

#include <QGraphicsView>
#include <QGraphicsScene>
#include <QScrollBar>

#include <QPainter>
#include <QDebug>

ParallaxScrollerItem::ParallaxScrollerItem(QString layer, QPointF pos,
                                           qreal depthFactor, qreal speed,
                                           qreal factor, QGraphicsScene *scene,
                                           QGraphicsItem *parent)
    : QGraphicsPixmapItem(parent, scene), m_scrollSpeed(speed), m_factor(factor)
{
    m_scrollingSpeedSlowingFactor = 0;
    m_layer.load(layer);
    setPixmap(m_layer);
    setPos(pos);
    setZValue(depthFactor);
}

ParallaxScrollerItem::~ParallaxScrollerItem()
{

}

void ParallaxScrollerItem::advance(int phase)
{
    if(phase == 1)
    {
        m_scrollingSpeedSlowingFactor++;
        if(m_scrollingSpeedSlowingFactor % m_factor == 0)
        {
            m_scrollingSpeedSlowingFactor = 0;
            setPos(x()-m_scrollSpeed, y());

            QGraphicsView* v = scene()->views().first();
            QPointF p = v->mapToScene(v->viewport()->x(), 0);

            if(x() <= p.x()-pixmap().width())
            {
                setPos(p.x()+pixmap().width()-m_scrollSpeed, y());
            }
        }
    }
}