1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
19 ** distribution.
20 ** * Neither the name of The Qt Company Ltd nor the names of its
21 ** contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include "animation.h"
42
43 #include <QPointF>
44 #include <QIODevice>
45 #include <QDataStream>
46
47 class Frame
48 {
49 public:
Frame()50 Frame() {
51 }
52
nodeCount() const53 int nodeCount() const
54 {
55 return m_nodePositions.size();
56 }
57
setNodeCount(int nodeCount)58 void setNodeCount(int nodeCount)
59 {
60 while (nodeCount > m_nodePositions.size())
61 m_nodePositions.append(QPointF());
62
63 while (nodeCount < m_nodePositions.size())
64 m_nodePositions.removeLast();
65 }
66
nodePos(int idx) const67 QPointF nodePos(int idx) const
68 {
69 return m_nodePositions.at(idx);
70 }
71
setNodePos(int idx,const QPointF & pos)72 void setNodePos(int idx, const QPointF &pos)
73 {
74 m_nodePositions[idx] = pos;
75 }
76
77 private:
78 QList<QPointF> m_nodePositions;
79 };
80
Animation()81 Animation::Animation()
82 {
83 m_currentFrame = 0;
84 m_frames.append(new Frame);
85 }
86
~Animation()87 Animation::~Animation()
88 {
89 qDeleteAll(m_frames);
90 }
91
setTotalFrames(int totalFrames)92 void Animation::setTotalFrames(int totalFrames)
93 {
94 while (m_frames.size() < totalFrames)
95 m_frames.append(new Frame);
96
97 while (totalFrames < m_frames.size())
98 delete m_frames.takeLast();
99 }
100
totalFrames() const101 int Animation::totalFrames() const
102 {
103 return m_frames.size();
104 }
105
setCurrentFrame(int currentFrame)106 void Animation::setCurrentFrame(int currentFrame)
107 {
108 m_currentFrame = qMax(qMin(currentFrame, totalFrames()-1), 0);
109 }
110
currentFrame() const111 int Animation::currentFrame() const
112 {
113 return m_currentFrame;
114 }
115
setNodeCount(int nodeCount)116 void Animation::setNodeCount(int nodeCount)
117 {
118 Frame *frame = m_frames.at(m_currentFrame);
119 frame->setNodeCount(nodeCount);
120 }
121
nodeCount() const122 int Animation::nodeCount() const
123 {
124 Frame *frame = m_frames.at(m_currentFrame);
125 return frame->nodeCount();
126 }
127
setNodePos(int idx,const QPointF & pos)128 void Animation::setNodePos(int idx, const QPointF &pos)
129 {
130 Frame *frame = m_frames.at(m_currentFrame);
131 frame->setNodePos(idx, pos);
132 }
133
nodePos(int idx) const134 QPointF Animation::nodePos(int idx) const
135 {
136 Frame *frame = m_frames.at(m_currentFrame);
137 return frame->nodePos(idx);
138 }
139
name() const140 QString Animation::name() const
141 {
142 return m_name;
143 }
144
setName(const QString & name)145 void Animation::setName(const QString &name)
146 {
147 m_name = name;
148 }
149
save(QIODevice * device) const150 void Animation::save(QIODevice *device) const
151 {
152 QDataStream stream(device);
153 stream << m_name;
154 stream << m_frames.size();
155 foreach (Frame *frame, m_frames) {
156 stream << frame->nodeCount();
157 for (int i=0; i<frame->nodeCount(); ++i)
158 stream << frame->nodePos(i);
159 }
160 }
161
load(QIODevice * device)162 void Animation::load(QIODevice *device)
163 {
164 if (!m_frames.isEmpty())
165 qDeleteAll(m_frames);
166
167 m_frames.clear();
168
169 QDataStream stream(device);
170 stream >> m_name;
171
172 int frameCount;
173 stream >> frameCount;
174
175 for (int i=0; i<frameCount; ++i) {
176
177 int nodeCount;
178 stream >> nodeCount;
179
180 Frame *frame = new Frame;
181 frame->setNodeCount(nodeCount);
182
183 for (int j=0; j<nodeCount; ++j) {
184 QPointF pos;
185 stream >> pos;
186
187 frame->setNodePos(j, pos);
188 }
189
190 m_frames.append(frame);
191 }
192 }
193