aboutsummaryrefslogtreecommitdiff
path: root/src/SceneChanger.cpp
blob: 2aa8b868c6a19e60f57973a38824b870cb2da90c (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "SceneChanger.h"
#include "GameScene.h"
#include "GameView.h"
#include "GraphicsPixmapObject.h"

#include <QTimer>
#include <QParallelAnimationGroup>
#include <QPropertyAnimation>

SceneChanger::SceneChanger(QString name,  const QRectF &rect, GameView *parent) :
    GameScene(name, parent)
{
    setSceneRect(rect);

    m_anim = new QParallelAnimationGroup(this);
    connect(m_anim, SIGNAL(finished()), this, SLOT(animationFinished()));
    setDuration(500);

    m_updateTimer = new QTimer(this);
    connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(update()));
}

void SceneChanger::updateLogic()
{
    advance();
    update();
}

void SceneChanger::setDuration(int msecs)
{
    m_duration = msecs;
}

void SceneChanger::changeScene(GameScene *from, GameScene *to, bool rightToLeft)
{
    m_fromScene = from;
    m_toScene = to;

    int width = from->sceneRect().width();
    int height = from->sceneRect().height();

    clear();
    m_anim->clear();

    QPixmap from_pixmap(width, height);
    QPixmap to_pixmap(width, height);

    QPainter from_painter(&from_pixmap);
    QPainter to_painter(&to_pixmap);

    from->render(&from_painter);
    to->render(&to_painter);

    GraphicsPixmapObject *from_obj = new GraphicsPixmapObject(from_pixmap);
    from_obj->setPos(0, 0);

    GraphicsPixmapObject *to_obj = new GraphicsPixmapObject(to_pixmap);
    to_obj->setPos(0, 0);

    addItem(from_obj);
    addItem(to_obj);

    QPropertyAnimation *from_anim = new QPropertyAnimation(from_obj, "pos",
                                                           this);

    QPropertyAnimation *to_anim = new QPropertyAnimation(to_obj, "pos",
                                                         this);

    if( rightToLeft )
    {
        from_anim->setDuration(m_duration);
        from_anim->setStartValue(QPointF(0.0, 0.0));
        from_anim->setEndValue(QPointF(-(float)width, 0.0));
        from_anim->setEasingCurve(QEasingCurve::OutCubic); //or OutBounce, OutQuint...

        to_anim->setDuration(m_duration);
        to_anim->setStartValue(QPointF((float)width, 0.0));
        to_anim->setEndValue(QPointF(0.0, 0.0));
        to_anim->setEasingCurve(QEasingCurve::OutCubic); //or OutBounce, OutQuint...
    }
    else
    {
        from_anim->setDuration(m_duration);
        from_anim->setStartValue(QPointF(0.0, 0.0));
        from_anim->setEndValue(QPointF((float)width, 0.0));
        from_anim->setEasingCurve(QEasingCurve::OutCubic); //or OutBounce, OutQuint...

        to_anim->setDuration(m_duration);
        to_anim->setStartValue(QPointF(-(float)width, 0.0));
        to_anim->setEndValue(QPointF(0.0, 0.0));
        to_anim->setEasingCurve(QEasingCurve::OutCubic); //or OutBounce, OutQuint...

    }
    m_anim->addAnimation(from_anim);
    m_anim->addAnimation(to_anim);

    // Use a little more frequent update than normally
    m_updateTimer->start(1000 / 100);
    m_anim->start();
}

void SceneChanger::animationFinished()
{
    m_updateTimer->stop();

    gameView()->showScene(m_toScene);
}