piecesprite.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 #include <QSizeF>
00026 
00027 // KDE includes
00028 #include <kdebug.h>
00029 #include <kconfig.h>
00030 
00031 // Local includes
00032 #include "piecesprite.h"
00033 
00034 // Constructor for the pixmap sprite
00035 PieceSprite::PieceSprite(QString id, ThemeManager* theme, int advancePeriod, int no, QGraphicsScene* canvas)
00036     :  Themable(id, theme), PixmapSprite(advancePeriod, no, canvas)
00037 {
00038   mNotify = new SpriteNotify(this);
00039   if (theme) theme->updateTheme(this);
00040 }
00041 
00042 // Destructor
00043 PieceSprite::~PieceSprite()
00044 {
00045   delete mNotify;
00046 }
00047 
00048 // Standard theme change function to redraw the item
00049 void PieceSprite::changeTheme()
00050 {
00051   PixmapSprite::changeTheme();
00052 }
00053 
00054 
00055 // Start a linear movement
00056 void PieceSprite::startLinear(QPointF start, QPointF end, double velocity)
00057 {
00058   mStart          = start;
00059   mEnd            = end;
00060   QPointF p       = mEnd-mStart;
00061   double dist     = sqrt(p.x()*p.x()+p.y()*p.y());
00062   if (dist > 0.0) mDuration = dist/velocity*1000.0; // Duration in [ms]
00063   else mDuration = 0.0;
00064   
00065   mMovementState = LinearMove;
00066   mTime           = 0;
00067   setPos(mStart.x()*getScale(), mStart.y()*getScale());
00068   show();
00069 }
00070 
00071 
00072 // Start linear movement from current position
00073 void PieceSprite::startLinear(QPointF end, double velocity)
00074 {
00075   mStart          = QPointF(x()/getScale(), y()/getScale());
00076   mEnd            = end;
00077   QPointF p       = mEnd-mStart;
00078   double dist     = sqrt(p.x()*p.x()+p.y()*p.y());
00079   if (dist > 0.0) mDuration = dist/velocity*1000.0; // Duration in [ms]
00080   else mDuration = 0.0;
00081   mMovementState = LinearMove;
00082   mTime           = 0;
00083   show();
00084 }
00085 
00086 
00087 // CanvasItem advance method 
00088 void PieceSprite::advance(int phase)
00089 {
00090   // Advance time and frame animation etc
00091   PixmapSprite::advance(phase);
00092   
00093   // Ignore phase 0 (collisions)
00094   if (phase == 0)
00095   {
00096     return ;
00097   }
00098 
00099   // Current scaling
00100   double scale = this->getScale();
00101 
00102   // Handle linear movement
00103   if (mMovementState == LinearMove)
00104   {
00105          // Movement over?
00106      if (mTime >= mDuration)
00107      {
00108        mMovementState = Idle;
00109        setPos(mEnd.x()*scale, mEnd.y()*scale);
00110        // Use notifier to emit signal
00111        mNotify->emitSignal(0);
00112      }
00113      else
00114      {
00115          // Continue moving
00116        double t = mTime/mDuration;
00117        qreal x = mStart.x() + t*(mEnd.x()-mStart.x());
00118        qreal y = mStart.y() + t*(mEnd.y()-mStart.y());
00119        setPos(x*scale, y*scale);
00120      }
00121   }
00122     
00123 }
00124 

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