introsprite.cpp

Go to the documentation of this file.
00001 /*
00002    This file is part of the KDE games kwin4 program
00003    Copyright (c) 2006 Martin Heni <kde@heni-online.de>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 // General includes
00022 #include <math.h>
00023 
00024 // Qt includes
00025 
00026 // KDE includes
00027 #include <kdebug.h>
00028 #include <kconfig.h>
00029 
00030 // Local includes
00031 #include "introsprite.h"
00032 
00033 // Constructor for the sprite
00034 IntroSprite::IntroSprite(QString id, ThemeManager* theme, int advancePeriod, int no, QGraphicsScene* canvas)
00035     :  Themable(id, theme), PixmapSprite(advancePeriod, no, canvas)
00036 {
00037   hide();
00038 
00039   mAdvancePeriod = advancePeriod;
00040   mNo = no;
00041   mAnimationState = Idle;
00042 
00043   if (theme) theme->updateTheme(this);
00044 }
00045 
00046 
00047 // Destructor
00048 IntroSprite::~IntroSprite()
00049 {
00050 }
00051 
00052 
00053 // Change the theme
00054 void IntroSprite::changeTheme()
00055 {
00056   PixmapSprite::changeTheme();
00057 }
00058 
00059 
00060 // Start the intro movement: linear, circle, linear
00061 void IntroSprite::startIntro(QPointF start, QPointF end, double radius, double duration, double delay)
00062 {
00063   mStart          = start;
00064   mEnd            = end;
00065   mDuration       = duration;
00066   mRadius         = radius;
00067   mDelay = delay;
00068   mAnimationState = IntroDelay;
00069   mTime           = 0;
00070   setPos(mStart.x()*getScale(), mStart.y()*getScale());
00071   show();
00072 }
00073 
00074 
00075 // Start linear movement
00076 void IntroSprite::startLinear(QPointF start, QPointF end, double duration)
00077 {
00078   mStart          = start;
00079   mEnd            = end;
00080   mDuration       = duration;
00081   mAnimationState = LinearMove;
00082   mTime           = 0;
00083   setPos(mStart.x()*getScale(), mStart.y()*getScale());
00084   show();
00085 }
00086 
00087 
00088 // Start linear movement from current position
00089 void IntroSprite::startLinear(QPointF end, double duration)
00090 {
00091   mStart          = QPointF(x()/getScale(), y()/getScale());
00092   mEnd            = end;
00093   mDuration       = duration;
00094   mAnimationState = LinearMove;
00095   mTime           = 0;
00096   show();
00097 }
00098 
00099 
00100 // CanvasItem advance method 
00101 void IntroSprite::advance(int phase)
00102 {
00103   // Ignore phase 0 (collisions)
00104   if (phase == 0)
00105   {
00106     QGraphicsItem::advance(phase);
00107     return ;
00108   }
00109 
00110   double scale = this->getScale();
00111 
00112   // Increase time
00113   mTime += mAdvancePeriod;
00114 
00115   // Delay animation
00116   if (mAnimationState == IntroDelay)
00117   {
00118      if (mTime >= mDelay)
00119      {
00120        mAnimationState = IntroLinear1;
00121        mTime = 0.0;
00122      }
00123   }
00124 
00125   // Handle first linear phase
00126   if (mAnimationState == IntroLinear1)
00127   {
00128      if (mTime >= 0.25*mDuration)
00129      {
00130        mAnimationState = IntroCircle;
00131      }
00132      else
00133      {
00134        double t = 2.0*mTime/mDuration;
00135        qreal x = mStart.x() + t*(mEnd.x()-mStart.x());
00136        qreal y = mStart.y() + t*(mEnd.y()-mStart.y());
00137        setPos(x*scale, y*scale);
00138      }
00139   }
00140 
00141   // Handle circle phase
00142   if (mAnimationState == IntroCircle)
00143   {
00144      if (mTime >= 0.75*mDuration)
00145      {
00146        mAnimationState = IntroLinear2;
00147      }
00148      else
00149      {
00150        double t = 2.0*(mTime/mDuration-0.25);
00151        double sign = 1.0;
00152        if (mStart.x() > 0.5) sign = -1.0; // Direction of turn
00153        qreal cx = mStart.x() + 0.5*(mEnd.x()-mStart.x());
00154        qreal cy = mStart.y() + 0.5*(mEnd.y()-mStart.y());
00155        qreal x = cx + mRadius*sin(sign*t*2.0*M_PI);
00156        qreal y = cy-mRadius + mRadius*cos(sign*t*2.0*M_PI);
00157        setPos(x*scale, y*scale);
00158      }
00159   }
00160 
00161   // Handle second linear phase
00162   if (mAnimationState == IntroLinear2)
00163   {
00164      if (mTime >= 1.00*mDuration)
00165      {
00166        mAnimationState = Idle;
00167        setPos(mEnd.x()*scale, mEnd.y()*scale);
00168      }
00169      else
00170      {
00171        double t = 2.0*(mTime/mDuration-0.75)+0.5;
00172        qreal x = mStart.x() + t*(mEnd.x()-mStart.x());
00173        qreal y = mStart.y() + t*(mEnd.y()-mStart.y());
00174        setPos(x*scale, y*scale);
00175      }
00176   }
00177 
00178   // Handle linear phase
00179   if (mAnimationState == LinearMove)
00180   {
00181      if (mTime >= mDuration)
00182      {
00183        mAnimationState = Idle;
00184        setPos(mEnd.x()*scale, mEnd.y()*scale);
00185      }
00186      else
00187      {
00188        double t = mTime/mDuration;
00189        qreal x = mStart.x() + t*(mEnd.x()-mStart.x());
00190        qreal y = mStart.y() + t*(mEnd.y()-mStart.y());
00191        setPos(x*scale, y*scale);
00192      }
00193   }
00194     
00195   QGraphicsItem::advance(phase);
00196 }
00197 

Generated on Sun Mar 4 10:56:43 2007 for KWin4 by  doxygen 1.4.6