00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <math.h>
00023
00024
00025 #include <QSizeF>
00026
00027
00028 #include <kdebug.h>
00029 #include <kconfig.h>
00030
00031
00032 #include "pixmapsprite.h"
00033
00034
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
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
00063
00064 void PixmapSprite::changeTheme()
00065 {
00066
00067 mFrames.clear();
00068 mHotspots.clear();
00069
00070
00071 double oldscale = this->getScale();
00072 double scale = thememanager()->getScale();
00073 setScale(scale);
00074
00075
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
00082 if (config.hasKey("zValue"))
00083 {
00084 double zValue = config.readEntry("zValue", 0.0);
00085 setZValue(zValue);
00086 }
00087
00088
00089 bool center = config.readEntry("center", false);
00090
00091
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
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
00108 QString svgid = config.readEntry("svgid");
00109
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
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
00134 setFrame(mCurrentFrame, true);
00135 update();
00136 }
00137
00138
00139
00140 double PixmapSprite::getDoubleValue(QString item)
00141 {
00142 KConfigGroup config = thememanager()->config(id());
00143 return config.readEntry(item, 0.0);
00144 }
00145
00146
00147
00148 void PixmapSprite::setPosition(QPointF pos)
00149 {
00150 mStart = pos;
00151 setPos(mStart.x()*getScale(), mStart.y()*getScale());
00152 }
00153
00154
00155
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
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
00176
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
00190 void PixmapSprite::advance(int phase)
00191 {
00192
00193 if (phase == 0)
00194 {
00195 QGraphicsItem::advance(phase);
00196 return ;
00197 }
00198
00199
00200 mTime += mAdvancePeriod;
00201
00202
00203 if (mAnimationState == Animated)
00204 {
00205
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