File: pixmapsprite.cpp

package info (click to toggle)
kfourinline 4:18.04.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,784 kB
  • sloc: cpp: 7,339; sh: 8; makefile: 5
file content (273 lines) | stat: -rw-r--r-- 7,011 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
   This file is part of the KDE games kwin4 program
   Copyright (c) 2006 Martin Heni <kde@heni-online.de>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

// Header includes
#include "pixmapsprite.h"
#include "kfourinline_debug.h"

// General includes
#include <cmath>

// Qt includes
#include <QGraphicsScene>

// KDE includes
#include <KConfig>
#include <KConfigGroup>

// Constructor for the sprite
PixmapSprite::PixmapSprite(const QString &id, ThemeManager* theme, int no, QGraphicsScene* canvas)
    :  Themeable(id, theme), QGraphicsPixmapItem(0)
{
  canvas->addItem(this);
  hide();
  setAcceptHoverEvents(false);

  mAnimationState = Idle;
  mNo             = no;
  mCurrentFrame   = 0;
  mOffsetStatus   = true;

  if (theme) theme->updateTheme(this);
}


// Constructor for the sprite
PixmapSprite::PixmapSprite(int no, QGraphicsScene* canvas)
    :  Themeable(), QGraphicsPixmapItem(0)
{
  canvas->addItem(this);
  hide();

  mAnimationState = Idle;
  mNo             = no;
  mCurrentFrame   = 0;
  mOffsetStatus   = true;
}


// Main themeable function. Called for any theme change. The sprites needs to
// resiez and redraw here.
void PixmapSprite::changeTheme()
{
  // Clear data
  mFrames.clear();
  mHotspots.clear();
  
  // Get scaling change
  double oldscale = this->getScale();
  double scale = thememanager()->getScale();
  Themeable::setScale(scale);
 
  // Retrieve theme data from configuration
  KConfigGroup config = thememanager()->config(id());
  double width  = config.readEntry("width", 1.0);
  double height = config.readEntry("height", 0.0);
  width *= scale;
  height *= scale;
  QPointF pos = config.readEntry("pos", QPointF(1.0,1.0));
  pos *= scale;
  // Set fixed z value?
  if (config.hasKey("zValue"))
  {
    double zValue = config.readEntry("zValue", 0.0);
    setZValue(zValue);
  }

  // Centering
  bool center = config.readEntry("center", false);

  // Animation
  mStartFrame      = config.readEntry("start-frame", 0);
  mEndFrame        = config.readEntry("end-frame", 0);
  mDelay           = config.readEntry("animation-delay", 0);
  QString refframe = config.readEntry("ref-frame", QString());

  // Set fixed position or modify current position
  if (config.hasKey("pos"))
  {
    setPos(pos.x(), pos.y());
  }
  else
  {
    setPos(x()*scale/oldscale, y()*scale/oldscale);
  }

  // SVG graphics
  QString svgid = config.readEntry("svgid");
  // Read sequence of frame pixmaps when auto ID given
  QPixmap pixmap;
  if (svgid == QLatin1String("auto"))
  {
    for (int i=mStartFrame;i<=mEndFrame;i++)
    {
      QString name = QStringLiteral("frame%1").arg(i);
      svgid = config.readEntry(name);
      if (!refframe.isNull())
      {
        pixmap = thememanager()->getPixmap(svgid, refframe, width);
      }
      else if (config.hasKey("height"))
      {
        pixmap = thememanager()->getPixmap(svgid, QSize(int(width), int(height)));
      }
      else
      {
        pixmap = thememanager()->getPixmap(svgid, width);
      }
      mFrames.append(pixmap);
      if (center) mHotspots.append(QPointF(pixmap.width()/2,pixmap.height()/2));
      else mHotspots.append(QPointF(0.0,0.0));
    }
  }
  // Read only one named pixmap
  else
  {
    if (config.hasKey("height"))
    {
      pixmap = thememanager()->getPixmap(svgid, QSize(int(width), int(height)));
    }
    else
    {
      pixmap = thememanager()->getPixmap(svgid, width);
    }
    mFrames.append(pixmap);
    if (center) mHotspots.append(QPointF(pixmap.width()/2,pixmap.height()/2));
    else mHotspots.append(QPointF(0.0,0.0));
  }
  
  // Set theme offset (probably not really necesary here)
  QPoint offset = thememanager()->getOffset();
  resetTransform();
  if (mOffsetStatus) setTransform(QTransform::fromTranslate(offset.x(), offset.y()), true);

  // Set pixmap to sprite
  setFrame(mCurrentFrame, true);
  update();
}


// Debug only: Retrieve double value from configuration file
double PixmapSprite::getDoubleValue(const QString &item)
{
  KConfigGroup config = thememanager()->config(id());
  return config.readEntry(item, 0.0);
}


// Move the sprite to the given relative position
void PixmapSprite::setPosition(QPointF pos)
{
  mStart          = pos;
  setPos(mStart.x()*getScale(), mStart.y()*getScale());
}


// Handle the offset status (true: theme offset, false: no offset)
void PixmapSprite::setOffsetStatus(bool status)
{
  mOffsetStatus = status;
  changeTheme();
}


// Store the logical board coordinates for theme changes
void PixmapSprite::setLogicalPos(QPoint pos)
{
  mLPos = pos;
}


// Retrieve the logical board coordinates for theme changes
QPoint PixmapSprite::logicalPos()
{
  return mLPos;
}


// Start or stop a frame animation
void PixmapSprite::setAnimation(bool status)
{
  if (status) mAnimationState = Animated;
  else mAnimationState = Idle;
  mTime.start();
  setFrame(mStartFrame);
}


// Specify and start a frame animation
void PixmapSprite::setAnimation(int start, int end, int delay)
{
  mDelay          = delay;
  mStartFrame     = start;
  mEndFrame       = end;
  setAnimation(true);
}


// Set a new bitmap into the sprite. If the number is the same as the
// current one, nothing is done unless forcing is set to true.
void PixmapSprite::setFrame(int no, bool force)
{
  if (!force && no == mCurrentFrame) return;
  if (no<0 || no >=mFrames.count()) return;
  setPixmap(mFrames.at(no));
  
  QPoint offset = thememanager()->getOffset();
  
  // Set new item's scene transformation: Hotspot plus global theme offset
  resetTransform();
  if (mOffsetStatus)
  {
    setTransform(QTransform::fromTranslate(-mHotspots.at(no).x()+offset.x(), -mHotspots.at(no).y()+offset.y()), true);
  }
  else 
  {
    setTransform(QTransform::fromTranslate(-mHotspots.at(no).x(), -mHotspots.at(no).y()), true);
  }
  mCurrentFrame = no;
  update();
}


// Standard QGI advance method 
void PixmapSprite::advance(int phase)
{
  int elapsed = mTime.elapsed();

  // Ignore phase 0 (collisions)
  if (phase == 0)
  {
    QGraphicsItem::advance(phase);
    return ;
  }

  // Handle animation
  if (mAnimationState == Animated)
  {
    // Frame delay passed?
    int frames = elapsed / mDelay;
    int curFrame = frames % (mEndFrame-mStartFrame)+ mStartFrame;
    setFrame(curFrame);
  }

  QGraphicsItem::advance(phase);
}