-
Notifications
You must be signed in to change notification settings - Fork 0
/
QGraphicsAnimatedSprite.cpp
118 lines (113 loc) · 3.47 KB
/
QGraphicsAnimatedSprite.cpp
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
108
109
110
111
112
113
114
115
116
117
118
#include "QGraphicsAnimatedSprite.h"
#include <QPainter>
#include <QDebug>
//-----------------------------------------------------------
QGraphicsAnimatedSprite::QGraphicsAnimatedSprite(QImage &spritesheet) : mSpriteSheet(spritesheet),
mCurrentColumn(0), mCurrentLine(0), mReverseFrame(false)
{
mTimerId = startTimer(100);
}
//-----------------------------------------------------------
QGraphicsAnimatedSprite::~QGraphicsAnimatedSprite()
{
}
//-----------------------------------------------------------
void QGraphicsAnimatedSprite::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
QRect sr = QRect(mCurrentColumn * mElementWidth, mCurrentLine * mElementHeight, mElementWidth, mElementHeight);
painter->drawImage(QPoint(0,0), mSpriteSheet, sr, Qt::AutoColor);
}
//-----------------------------------------------------------
QRectF QGraphicsAnimatedSprite::boundingRect() const
{
return QRectF(0, 0, mElementWidth, mElementHeight);
}
//-----------------------------------------------------------
void QGraphicsAnimatedSprite::timerEvent(QTimerEvent *evt)
{
// Check if we are at the end or at the beginning of the animation
if (mCurrentColumn == mAnimationEndCol && mCurrentLine == mAnimationEndLine)
{
if (mReverseFrameEnabled)
{
mReverseFrame = true;
}
else
{
mCurrentColumn = mAnimationStartCol;
mCurrentLine = mAnimationStartLine;
}
}
else if (mCurrentColumn == mAnimationStartCol && mCurrentLine == mAnimationStartLine)
{
if (mReverseFrameEnabled)
{
mReverseFrame = false;
}
}
// Progress through the frames
if (!mReverseFrame) // Progress forward
{
if (mCurrentColumn == mSheetColumns-1)
{
mCurrentLine++;
mCurrentColumn = 0;
}
else
{
mCurrentColumn++;
}
}
else // Progress backwards
{
if (mCurrentColumn == 0)
{
mCurrentLine--;
mCurrentColumn = mSheetColumns-1;
}
else
{
mCurrentColumn--;
}
}
// Force redraw
this->update();
}
//-----------------------------------------------------------
void QGraphicsAnimatedSprite::setAnimationFrames(int start_col, int start_line, int end_col, int end_line)
{
mAnimationStartCol = start_col;
mAnimationStartLine = start_line;
mAnimationEndCol = end_col;
mAnimationEndLine = end_line;
// Reset animation state to avoid to go out of range
mReverseFrame = false;
mCurrentColumn = mAnimationStartCol;
mCurrentLine = mAnimationStartLine;
}
//-----------------------------------------------------------
void QGraphicsAnimatedSprite::setFrameReversion(bool reverse)
{
mReverseFrameEnabled = reverse;
}
//-----------------------------------------------------------
void QGraphicsAnimatedSprite::setSheetSize(int lines, int columns)
{
mSheetLines = lines;
mSheetColumns = columns;
}
//-----------------------------------------------------------
void QGraphicsAnimatedSprite::setElementSize(int width, int height)
{
mElementWidth = width;
mElementHeight = height;
}
//-----------------------------------------------------------
void QGraphicsAnimatedSprite::setSpeed(int timeMs)
{
killTimer(mTimerId);
mTimerId = startTimer(timeMs);
}
//-----------------------------------------------------------