| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the QtCore module of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:BSD$
|
|---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
|---|
| 11 | **
|
|---|
| 12 | ** "Redistribution and use in source and binary forms, with or without
|
|---|
| 13 | ** modification, are permitted provided that the following conditions are
|
|---|
| 14 | ** met:
|
|---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
|---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
|---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
|---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
|---|
| 19 | ** the documentation and/or other materials provided with the
|
|---|
| 20 | ** distribution.
|
|---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
|---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
|---|
| 23 | ** products derived from this software without specific prior written
|
|---|
| 24 | ** permission.
|
|---|
| 25 | **
|
|---|
| 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
|---|
| 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:
|
|---|
| 50 | Frame() {
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | int nodeCount() const
|
|---|
| 54 | {
|
|---|
| 55 | return m_nodePositions.size();
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 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 |
|
|---|
| 67 | QPointF nodePos(int idx) const
|
|---|
| 68 | {
|
|---|
| 69 | return m_nodePositions.at(idx);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 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 |
|
|---|
| 81 | Animation::Animation()
|
|---|
| 82 | {
|
|---|
| 83 | m_currentFrame = 0;
|
|---|
| 84 | m_frames.append(new Frame);
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | Animation::~Animation()
|
|---|
| 88 | {
|
|---|
| 89 | qDeleteAll(m_frames);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 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 |
|
|---|
| 101 | int Animation::totalFrames() const
|
|---|
| 102 | {
|
|---|
| 103 | return m_frames.size();
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | void Animation::setCurrentFrame(int currentFrame)
|
|---|
| 107 | {
|
|---|
| 108 | m_currentFrame = qMax(qMin(currentFrame, totalFrames()-1), 0);
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | int Animation::currentFrame() const
|
|---|
| 112 | {
|
|---|
| 113 | return m_currentFrame;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | void Animation::setNodeCount(int nodeCount)
|
|---|
| 117 | {
|
|---|
| 118 | Frame *frame = m_frames.at(m_currentFrame);
|
|---|
| 119 | frame->setNodeCount(nodeCount);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | int Animation::nodeCount() const
|
|---|
| 123 | {
|
|---|
| 124 | Frame *frame = m_frames.at(m_currentFrame);
|
|---|
| 125 | return frame->nodeCount();
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 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 |
|
|---|
| 134 | QPointF Animation::nodePos(int idx) const
|
|---|
| 135 | {
|
|---|
| 136 | Frame *frame = m_frames.at(m_currentFrame);
|
|---|
| 137 | return frame->nodePos(idx);
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | QString Animation::name() const
|
|---|
| 141 | {
|
|---|
| 142 | return m_name;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | void Animation::setName(const QString &name)
|
|---|
| 146 | {
|
|---|
| 147 | m_name = name;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | 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 |
|
|---|
| 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 | }
|
|---|