pixmapsprite.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 "pixmapsprite.h"
00033 
00034 // Constructor for the sprite
00035 PixmapSprite::PixmapSprite(QString id, ThemeManager* theme, int advancePeriod, int no, QGraphicsScene* canvas)
00036     :  Themable(id, theme), QGraphicsPixmapItem(0, canvas)
00037 {
00038   hide();
00039 
00040   mAnimationState = Idle;
00041   mAdvancePeriod  = advancePeriod;
00042   mNo             = no;
00043   mCurrentFrame   = 0;
00044 
00045   if (theme) theme->updateTheme(this);
00046 }
00047 
00048 
00049 // Constructor for the sprite
00050 PixmapSprite::PixmapSprite(int advancePeriod, int no, QGraphicsScene* canvas)
00051     :  Themable(), QGraphicsPixmapItem(0, canvas)
00052 {
00053   hide();
00054 
00055   mAnimationState = Idle;
00056   mAdvancePeriod  = advancePeriod;
00057   mNo             = no;
00058   mCurrentFrame   = 0;
00059 }
00060 
00061 
00062 // Main themable function. Called for any theme change. The sprites needs to
00063 // resiez and redraw here.
00064 void PixmapSprite::changeTheme()
00065 {
00066   // Clear data
00067   mFrames.clear();
00068   mHotspots.clear();
00069   
00070   // Get scaling change
00071   double oldscale = this->getScale();
00072   double scale = thememanager()->getScale();
00073   setScale(scale);
00074 
00075   // Retrieve theme data from configuration
00076   KConfigGroup config = thememanager()->config(id());
00077   double width = config.readEntry("width", 1.0);
00078   width *= scale;
00079   QPointF pos = config.readEntry("pos", QPointF(1.0,1.0));
00080   pos *= scale;
00081   // Set fixed z value?
00082   if (config.hasKey("zValue"))
00083   {
00084     double zValue = config.readEntry("zValue", 0.0);
00085     setZValue(zValue);
00086   }
00087 
00088   // Centering
00089   bool center = config.readEntry("center", false);
00090 
00091   // Animation
00092   mStartFrame      = config.readEntry("start-frame", 0);
00093   mEndFrame        = config.readEntry("end-frame", 0);
00094   mDelay           = config.readEntry("animation-delay", 0);
00095   QString refframe = config.readEntry("ref-frame", QString());
00096 
00097   // Set fixed position or modify current position
00098   if (config.hasKey("pos"))
00099   {
00100     setPos(pos.x(), pos.y());
00101   }
00102   else
00103   {
00104     setPos(x()*scale/oldscale, y()*scale/oldscale);
00105   }
00106 
00107   // SVG graphics
00108   QString svgid = config.readEntry("svgid");
00109   // Read sequence of frame pixmaps when auto ID given
00110   if (svgid == "auto")
00111   {
00112     for (int i=mStartFrame;i<=mEndFrame;i++)
00113     {
00114       QString name = QString("frame%1").arg(i);
00115       svgid = config.readEntry(name.toUtf8());
00116       QPixmap pixmap;
00117       if (refframe.isNull()) pixmap = thememanager()->getPixmap(svgid, width);
00118       else pixmap = thememanager()->getPixmap(svgid, refframe, width);
00119       mFrames.append(pixmap);
00120       if (center) mHotspots.append(QPointF(pixmap.width()/2,pixmap.height()/2));
00121       else mHotspots.append(QPointF(0.0,0.0));
00122     }
00123   }
00124   // Read only one named pixmap
00125   else
00126   {
00127     QPixmap pixmap = thememanager()->getPixmap(svgid, width);
00128     mFrames.append(pixmap);
00129     if (center) mHotspots.append(QPointF(pixmap.width()/2,pixmap.height()/2));
00130     else mHotspots.append(QPointF(0.0,0.0));
00131   }
00132 
00133   // Set pixmap to sprite
00134   setFrame(mCurrentFrame, true);
00135   update();
00136 }
00137 
00138 
00139 // Debug only: Retrieve double value from configuration file
00140 double PixmapSprite::getDoubleValue(QString item)
00141 {
00142   KConfigGroup config = thememanager()->config(id());
00143   return config.readEntry(item, 0.0);
00144 }
00145 
00146 
00147 // Move the sprite to the given relative position
00148 void PixmapSprite::setPosition(QPointF pos)
00149 {
00150   mStart          = pos;
00151   setPos(mStart.x()*getScale(), mStart.y()*getScale());
00152 }
00153 
00154 
00155 // Start or stop a frame animation
00156 void PixmapSprite::setAnimation(bool status)
00157 {
00158   if (status) mAnimationState = Animated;
00159   else mAnimationState = Idle;
00160   mTime           = 0;
00161   setFrame(mStartFrame);
00162 }
00163 
00164 
00165 // Specify and start a frame animation
00166 void PixmapSprite::setAnimation(int start, int end, int delay)
00167 {
00168   mDelay          = delay;
00169   mStartFrame     = start;
00170   mEndFrame       = end;
00171   setAnimation(true);
00172 }
00173 
00174 
00175 // Set a new bitmap into the sprite. If the number is the same as the
00176 // current one, nothing is done unless forcing is set to true.
00177 void PixmapSprite::setFrame(int no, bool force)
00178 {
00179   if (!force && no == mCurrentFrame) return;
00180   if (no<0 || no >=mFrames.count()) return;
00181   setPixmap(mFrames.at(no));
00182   resetMatrix();
00183   translate(-mHotspots.at(no).x(), -mHotspots.at(no).y());
00184   mCurrentFrame = no;
00185   update();
00186 }
00187 
00188 
00189 // Standard QGI advance method 
00190 void PixmapSprite::advance(int phase)
00191 {
00192   // Ignore phase 0 (collisions)
00193   if (phase == 0)
00194   {
00195     QGraphicsItem::advance(phase);
00196     return ;
00197   }
00198 
00199   // Increase time
00200   mTime += mAdvancePeriod;
00201 
00202   // Handle animation
00203   if (mAnimationState == Animated)
00204   {
00205         // Frame delay passed?
00206     if (mTime>mDelay)
00207     {
00208       mTime = 0;
00209       int frame = mCurrentFrame+1;
00210       if (frame > mEndFrame) setFrame(mStartFrame);
00211       else setFrame(frame);
00212     }
00213   }
00214 
00215   QGraphicsItem::advance(phase);
00216 }
00217 

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