File: introsprite.cpp

package info (click to toggle)
kfourinline 4%3A18.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 (439 lines) | stat: -rw-r--r-- 10,741 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
   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 "introsprite.h"
#include "kfourinline_debug.h"

// General includes
#include <cmath>


// KDE includes
#include <KConfig>

#define sign(x) ( (x)>0?1:((x)<0?-1:0) )

/**
 * Store an animation script command. The command represents certain possible
 * animation sub mentods. The possible methods are listed in the enum Cmd
 */
class AnimationCommand
{
  public:
    /**
     * The supported commands.
     */
    enum Cmd {SHOW, HIDE, PAUSE, POSITION, LINEAR_DURATION, CIRCLE_DURATION, MANHATTEN};
    /**
     * Construct an animation command
     */
    AnimationCommand() 
    {
      cmd       = PAUSE;
      duration  = 0;
      radius    = 0.0;
      velocity  = 0.0;
    }
    /** The command type.
     */
    Cmd cmd;
    /** How long does the command take [ms].
     */
    int duration;
    /** The start position [0..1, 0..1].
     */
    QPointF start;
    /** The end position [0..1, 0..1].
     */
    QPointF end;
    /** The radius  [0..1] (if necessary).
     */
    double radius;
    /** The velocity [0...1000] (if necessary).
      */
    double velocity;
};


// Constructor for the sprite
IntroSprite::IntroSprite(const QString &id, ThemeManager* theme,  int no, QGraphicsScene* scene)
    :  Themeable(id, theme), PixmapSprite(no, scene)
{
  hide();
  mDeltaT = 0;
  mDelayT = 0;
  mNo     = no;
  mTime.restart();
  mDebugTime.restart();
  mStartAnimation = true;
  if (theme) theme->updateTheme(this);
}


// Destructor
IntroSprite::~IntroSprite()
{
  // Clear animation list (deletes objects)
  clearAnimation();
}


// Change the theme
void IntroSprite::changeTheme()
{
  PixmapSprite::changeTheme();
}


// Clear the animation script
void IntroSprite::clearAnimation(bool restartTime)
{
  qDeleteAll(mAnimList);
  mAnimList.clear();

  // Reset time?
  if (restartTime)
  {
    mTime.restart();
    mDeltaT = 0;
    mDelayT = 0;
  }
  mStartAnimation = true;
}


// Add a show sprite command
AnimationCommand* IntroSprite::addShow()
{
  AnimationCommand* cmd = new AnimationCommand();
  cmd->cmd = AnimationCommand::SHOW;
  cmd->duration = 0;
  cmd->start=previousStart();
  cmd->end=previousEnd();
  mAnimList.append(cmd);
  return cmd;
}


// Add a hide sprite command
AnimationCommand* IntroSprite::addHide()
{
  AnimationCommand* cmd = new AnimationCommand();
  cmd->cmd = AnimationCommand::HIDE;
  cmd->duration = 0;
  cmd->start=previousStart();
  cmd->end=previousEnd();
  mAnimList.append(cmd);
  return cmd;
}


// Add a relative pause
AnimationCommand* IntroSprite::addPause(int duration)
{
  AnimationCommand* cmd = new AnimationCommand();
  cmd->cmd = AnimationCommand::PAUSE;
  cmd->duration = duration;
  cmd->start=previousStart();
  cmd->end=previousEnd();
  mAnimList.append(cmd);
  return cmd;
}


// Add an absoulte pause...wait until the given time
AnimationCommand* IntroSprite::addWait(int duration)
{
  int currentDuration = animationDuration();
  if (currentDuration < duration)
  {
    AnimationCommand* cmd = new AnimationCommand();
    cmd->cmd = AnimationCommand::PAUSE;
    cmd->duration = duration-currentDuration;
    cmd->start=previousStart();
    cmd->end=previousEnd();
    mAnimList.append(cmd);
    return cmd;
  }
  return 0;
}


// Add a set position command
AnimationCommand* IntroSprite::addPosition(QPointF start)
{
  AnimationCommand* cmd = new AnimationCommand();
  cmd->cmd = AnimationCommand::POSITION;
  cmd->duration = 0;
  cmd->start=start;
  cmd->end=start;
  mAnimList.append(cmd);
  return cmd;
}


// Add a linear movement
AnimationCommand* IntroSprite::addLinear(QPointF start, QPointF end, int duration)
{
  AnimationCommand* cmd = new AnimationCommand();
  cmd->cmd = AnimationCommand::LINEAR_DURATION;
  cmd->duration = duration;
  cmd->start=start;
  cmd->end=end;
  mAnimList.append(cmd);
  return cmd;
}


// Add a relative linear movement, that is, starting from the previous position
AnimationCommand* IntroSprite::addRelativeLinear(QPointF end, int duration)
{
  AnimationCommand* cmd = new AnimationCommand();
  cmd->cmd = AnimationCommand::LINEAR_DURATION;
  cmd->duration = duration;
  cmd->start=previousEnd();
  cmd->end=end;
  mAnimList.append(cmd);
  return cmd;
}


// Add a circle movement
AnimationCommand* IntroSprite::addCircle(QPointF start, double radius, int duration)
{
  AnimationCommand* cmd = new AnimationCommand();
  cmd->cmd = AnimationCommand::CIRCLE_DURATION;
  cmd->duration = duration;
  cmd->start=start;
  cmd->end=start;
  cmd->radius=radius;
  mAnimList.append(cmd);
  return cmd;
}


// Add a relative manhatten move
AnimationCommand* IntroSprite::addRelativeManhatten(QPointF end, double velocity)
{
  AnimationCommand* cmd = new AnimationCommand();
  cmd->cmd = AnimationCommand::MANHATTEN;
  cmd->start = previousEnd();
  cmd->end=end;
  cmd->velocity=velocity;
  qreal dtx = fabs(cmd->end.x()-cmd->start.x())/cmd->velocity*1000.0;
  qreal dty = fabs(cmd->end.y()-cmd->start.y())/cmd->velocity*1000.0;
  cmd->duration = int(dtx+dty+1.0);
  mAnimList.append(cmd);
  return cmd;
}


// Retrieve the last commands start position or the current position if none exists
QPointF IntroSprite::previousStart()
{
  AnimationCommand* previous = mAnimList.last();
  if (previous != 0) return previous->start;
  else return QPointF(x()/getScale(), y()/getScale());
}


// Retrieve the last commands end position or the current position if none exists
QPointF IntroSprite::previousEnd()
{
  AnimationCommand* previous = mAnimList.last();
  if (previous != 0) return previous->end;
  return QPointF(x()/getScale(), y()/getScale());
}


// Retrieve the duration of the given animation command
int IntroSprite::duration(AnimationCommand* anim)
{
  return anim->duration;
}


// Retrieve the overall duration of the animation
int IntroSprite::animationDuration()
{
  int dura = 0;
  for (int i=0; i<mAnimList.size(); ++i)
  { 
    dura += mAnimList[i]->duration;
  }
  return dura;
}


// Globally delay the animation
void IntroSprite::delayAnimation(int duration)
{
  mDelayT += duration;
}


// CanvasItem advance method 
void IntroSprite::advance(int phase)
{
  // Ignore phase 0 (collisions)
  if (phase == 0)
  {
    QGraphicsItem::advance(phase);
    return ;
  }

  // No animation pending
  if (mAnimList.isEmpty())
  {
    QGraphicsItem::advance(phase);
    return ;
  }

  // First time animation start
  if (mStartAnimation)
  {
    mTime.restart();
    mStartAnimation = false;
  }
  int elapsed     = mTime.elapsed()+mDeltaT-mDelayT;
  if (elapsed<0) elapsed = 0;


  // Get the current/first command
  AnimationCommand* anim = mAnimList.first();

  // Execute commands who were passed in the time since the
  // last call
  while (anim != 0 &&  anim->duration <= elapsed)
  {
      executeCmd(anim, anim->duration);
      mAnimList.removeFirst();
      elapsed -= anim->duration;
      if (!mAnimList.isEmpty()) anim = mAnimList.first();
      else anim = 0;
  }

  // Handle current command
  if (anim != 0)
  {
    executeCmd(anim, elapsed);
  }

  // Restart timer and store remaining time
  mTime.restart();
  mDeltaT = elapsed;
  mDelayT = 0;

  // Parent advance
  QGraphicsItem::advance(phase);

}


// Execute the given animation command. The given time is the time elapsed 
// since start of the animation sequence.
void IntroSprite::executeCmd(AnimationCommand* anim, int elapsed)
{
  if (anim == 0) return;

  // Scale
  double scale = this->getScale();

  // Pause command
  if (anim->cmd == AnimationCommand::PAUSE)
  {
    // Do nothing
  }
  // Show sprite command
  if (anim->cmd == AnimationCommand::SHOW)
  {
    show();
  }
  // Hide sprite command
  if (anim->cmd == AnimationCommand::HIDE)
  {
    hide();
  }
  // Set position command
  if (anim->cmd == AnimationCommand::POSITION)
  {
    qreal x   = anim->end.x();
    qreal y   = anim->end.y();
    setPos(x*scale, y*scale);
  }
  // Linear move command
  if (anim->cmd == AnimationCommand::LINEAR_DURATION)
  {
    double qt = double(elapsed)/double(anim->duration);
    qreal x   = anim->start.x() + qt*(anim->end.x()-anim->start.x());
    qreal y   = anim->start.y() + qt*(anim->end.y()-anim->start.y());
    setPos(x*scale, y*scale);
  }
  // Circle move command
  if (anim->cmd == AnimationCommand::CIRCLE_DURATION)
  {
    double qt   = double(elapsed)/double(anim->duration);
    double sign = 1.0;
    if (anim->start.x() > 0.5) sign = -1.0; // Direction of turn
    qreal cx = anim->start.x(); 
    qreal cy = anim->start.y();
    qreal x = cx + anim->radius*sin(sign*qt*2.0*M_PI);
    qreal y = cy-anim->radius + anim->radius*cos(sign*qt*2.0*M_PI);
    setPos(x*scale, y*scale);
  }
  // Manhatten move command
  if (anim->cmd == AnimationCommand::MANHATTEN)
  {
     if ( (fabs(anim->end.x()-x()/scale) <= 0.00001 && 
         fabs(anim->end.y()-y()/scale) <= 0.00001 ) ||
         (elapsed >= anim->duration) )
     {
       setPos(anim->end.x()*scale, anim->end.y()*scale);
     }
     // x-edge
     else if (fabs(anim->end.x()-x()/scale) > 0.00001)
     {
       qreal x = anim->start.x() + sign(anim->end.x()-anim->start.x())*anim->velocity*double(elapsed)/1000.0;
       // Finished?
       if ( (anim->end.x() > anim->start.x() && x >= anim->end.x()) ||
            (anim->end.x() < anim->start.x() && x <= anim->end.x()) )
       {
         x = anim->end.x();
       }
       setPos(x*scale, anim->start.y()*scale);
     }
     // y-edge
     else
     {
       qreal dtx = fabs(anim->end.x()-anim->start.x())/anim->velocity*1000.0;
       qreal y = anim->start.y() + sign(anim->end.y()-anim->start.y())*anim->velocity*double(elapsed-dtx)/1000.0;
       // Finished?
       if ( (anim->end.y() > anim->start.y() && y >= anim->end.y()) ||
            (anim->end.y() < anim->start.y() && y <= anim->end.y()) )
       {
         y = anim->end.y();
       }
       setPos(anim->end.x()*scale, y*scale);
     }
  }

}