examples/animation/stickman/animation.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "animation.h"
       
    43 
       
    44 #include <QPointF>
       
    45 #include <QIODevice>
       
    46 #include <QDataStream>
       
    47 
       
    48 class Frame 
       
    49 {
       
    50 public:
       
    51     Frame() {
       
    52     }
       
    53 
       
    54     int nodeCount() const 
       
    55     {
       
    56         return m_nodePositions.size();
       
    57     }
       
    58 
       
    59     void setNodeCount(int nodeCount)
       
    60     {
       
    61         while (nodeCount > m_nodePositions.size())
       
    62             m_nodePositions.append(QPointF());            
       
    63         
       
    64         while (nodeCount < m_nodePositions.size())
       
    65             m_nodePositions.removeLast();
       
    66     }
       
    67 
       
    68     QPointF nodePos(int idx) const
       
    69     {
       
    70         return m_nodePositions.at(idx);
       
    71     }
       
    72 
       
    73     void setNodePos(int idx, const QPointF &pos)
       
    74     {
       
    75         m_nodePositions[idx] = pos;
       
    76     }
       
    77     
       
    78 private:
       
    79     QList<QPointF> m_nodePositions;
       
    80 };
       
    81 
       
    82 Animation::Animation()
       
    83 {
       
    84     m_currentFrame = 0;
       
    85     m_frames.append(new Frame);
       
    86 }
       
    87 
       
    88 Animation::~Animation() 
       
    89 {
       
    90     qDeleteAll(m_frames);
       
    91 }
       
    92 
       
    93 void Animation::setTotalFrames(int totalFrames)
       
    94 {
       
    95     while (m_frames.size() < totalFrames)
       
    96         m_frames.append(new Frame);    
       
    97 
       
    98     while (totalFrames < m_frames.size())
       
    99         delete m_frames.takeLast();    
       
   100 }
       
   101 
       
   102 int Animation::totalFrames() const
       
   103 {
       
   104     return m_frames.size();
       
   105 }
       
   106 
       
   107 void Animation::setCurrentFrame(int currentFrame)
       
   108 {
       
   109     m_currentFrame = qMax(qMin(currentFrame, totalFrames()-1), 0);
       
   110 }
       
   111 
       
   112 int Animation::currentFrame() const
       
   113 {
       
   114     return m_currentFrame;
       
   115 }
       
   116 
       
   117 void Animation::setNodeCount(int nodeCount)
       
   118 {
       
   119     Frame *frame = m_frames.at(m_currentFrame);
       
   120     frame->setNodeCount(nodeCount);
       
   121 }
       
   122 
       
   123 int Animation::nodeCount() const
       
   124 {
       
   125     Frame *frame = m_frames.at(m_currentFrame);
       
   126     return frame->nodeCount();
       
   127 }
       
   128 
       
   129 void Animation::setNodePos(int idx, const QPointF &pos)
       
   130 {
       
   131     Frame *frame = m_frames.at(m_currentFrame);
       
   132     frame->setNodePos(idx, pos);
       
   133 }
       
   134 
       
   135 QPointF Animation::nodePos(int idx) const
       
   136 {
       
   137     Frame *frame = m_frames.at(m_currentFrame);
       
   138     return frame->nodePos(idx);
       
   139 }
       
   140 
       
   141 QString Animation::name() const
       
   142 {
       
   143     return m_name;
       
   144 }
       
   145 
       
   146 void Animation::setName(const QString &name)
       
   147 {
       
   148     m_name = name;
       
   149 }
       
   150 
       
   151 void Animation::save(QIODevice *device) const
       
   152 {
       
   153     QDataStream stream(device);
       
   154     stream << m_name;
       
   155     stream << m_frames.size();
       
   156     foreach (Frame *frame, m_frames) {
       
   157         stream << frame->nodeCount();
       
   158         for (int i=0; i<frame->nodeCount(); ++i)
       
   159             stream << frame->nodePos(i);
       
   160     }
       
   161 }
       
   162 
       
   163 void Animation::load(QIODevice *device)
       
   164 {
       
   165     if (!m_frames.isEmpty())
       
   166         qDeleteAll(m_frames);
       
   167 
       
   168     m_frames.clear();
       
   169 
       
   170     QDataStream stream(device);
       
   171     stream >> m_name;
       
   172     
       
   173     int frameCount;
       
   174     stream >> frameCount;
       
   175 
       
   176     for (int i=0; i<frameCount; ++i) {
       
   177         
       
   178         int nodeCount;
       
   179         stream >> nodeCount;
       
   180         
       
   181         Frame *frame = new Frame;
       
   182         frame->setNodeCount(nodeCount);
       
   183 
       
   184         for (int j=0; j<nodeCount; ++j) {
       
   185             QPointF pos;
       
   186             stream >> pos;
       
   187 
       
   188             frame->setNodePos(j, pos);
       
   189         }
       
   190 
       
   191         m_frames.append(frame);
       
   192     }
       
   193 }