-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathanimatedlayer.cpp
More file actions
185 lines (151 loc) · 3.99 KB
/
animatedlayer.cpp
File metadata and controls
185 lines (151 loc) · 3.99 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright (C) 2016 Dmitry Morozov <dmorozov@outlook.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <QTimer>
#include "animatedlayer.h"
#include "bitmap.h"
#include "skin.h"
// TODO: deal with printf-style bitmap element ids
AnimatedLayer::AnimatedLayer(const QXmlStreamAttributes &attributes, QWidget *parent) : Layer(parent)
{
QString name, value;
Q_FOREACH (const QXmlStreamAttribute &i, attributes) {
name = i.name().toString().toLower();
value = i.value().toString();
if (!setProperty(qPrintable(name), QVariant(value))) {
if (dynamicPropertyNames().contains(qPrintable(name))) {
qWarning("%s: unknown property '%s'", Q_FUNC_INFO, qUtf8Printable(name));
setProperty(qPrintable(name), QVariant());
}
qWarning("%s: failed to set '%s' to '%s'", Q_FUNC_INFO, qUtf8Printable(name), qUtf8Printable(value));
}
}
if (m_frames.isEmpty())
setImage(attributes.value("image").toString());
}
QString AnimatedLayer::image() const
{
return m_fileId;
}
void AnimatedLayer::setImage(const QString &id)
{
if (w() == 0 || h() == 0)
return;
Bitmap *b = Skin::instance()->bitmap(id);
if (b == Q_NULLPTR) {
qWarning("%s: no '%s' bitmap found", Q_FUNC_INFO, qUtf8Printable(id));
return;
}
m_fileId = id;
int i = 0;
const QPixmap &pixmap = b->pixmap();
if ((w() == pixmap.width()) && (pixmap.height() % h() == 0)) {
m_horizontal = false;
m_numFrames = pixmap.height() / h();
while (i < pixmap.height()) {
m_frames.append(pixmap.copy(0, i, pixmap.width(), h()));
i += h();
}
} else if ((h() == pixmap.height()) && (pixmap.width() % w() == 0)) {
m_horizontal = true;
m_numFrames = pixmap.width() / w();
while (i < pixmap.width()) {
m_frames.append(pixmap.copy(i, 0, w(), pixmap.height()));
i += w();
}
} else {
qWarning("%s: don't know how to get frames from the bitmap", Q_FUNC_INFO);
return;
}
m_frames.resize(m_numFrames);
if (m_end == -1)
m_end = m_numFrames;
}
void AnimatedLayer::setFrameHeight(int value)
{
}
void AnimatedLayer::setFrameWidth(int value)
{
}
void AnimatedLayer::setElementFrames(int value)
{
}
void AnimatedLayer::setStart(int value)
{
m_start = value;
restart();
}
void AnimatedLayer::setEnd(int value)
{
m_end = value;
restart();
}
void AnimatedLayer::setSpeed(int value)
{
if (m_speed == value)
return;
m_speed = value;
restart();
}
void AnimatedLayer::setRealTime(bool enable)
{
if (m_realtime == enable)
return;
m_realtime = enable;
restart();
}
void AnimatedLayer::setAutoReplay(bool enable)
{
if (m_autoReplay == enable)
return;
m_autoReplay = enable;
restart();
}
void AnimatedLayer::setAutoPlay(bool enable)
{
if (m_autoPlay == enable)
return;
m_autoPlay = enable;
if (enable)
m_timer.start(m_speed, this);
else
m_timer.stop();
}
void AnimatedLayer::setDebug(bool enable)
{
if (m_debug == enable)
return;
m_debug = enable;
}
int AnimatedLayer::framesCount()
{
return m_numFrames;
}
void AnimatedLayer::setFrame(int number)
{
if (number >= m_frames.size())
return;
m_curFrame = number;
setPixmap(m_frames.at(m_curFrame), false, m_realtime);
}
void AnimatedLayer::restart()
{
if (m_timer.isActive())
m_timer.start(m_speed, this);
}
void AnimatedLayer::timerEvent(QTimerEvent *)
{
setPixmap(m_frames.at(m_curFrame), false, m_realtime);
m_curFrame++;
if (m_curFrame >= m_end) {
if (m_autoReplay)
m_curFrame = m_start;
else
m_timer.stop();
}
}