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

Generated on Tue May 1 09:34:40 2007 for LSkat by  doxygen 1.4.6