2 Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
10 #include "animation.h"
17 Animation::~Animation()
22 PauseAnimation::PauseAnimation(int time
)
27 void PauseAnimation::start(int t
)
32 bool PauseAnimation::step(int t
)
34 return t
- m_start
>= m_time
;
37 FadeAnimation::FadeAnimation(const SpritePtr
& sprite
, double from
, double to
, int time
)
46 void FadeAnimation::start(int t
)
49 m_sprite
->setOpacityF(m_from
);
52 bool FadeAnimation::step(int t
)
58 double val
= m_from
+ (m_to
- m_from
) * (t
- m_start
) / m_time
;
59 m_sprite
->setOpacityF(val
);
60 return t
- m_start
>= m_time
;
64 void FadeAnimation::stop()
66 m_sprite
->setOpacityF(m_to
);
71 MovementAnimation::MovementAnimation(const SpritePtr
& sprite
, const QPointF
& from
,
72 const QPointF
& velocity
, int time
)
75 , m_velocity(velocity
)
80 void MovementAnimation::start(int t
)
83 m_sprite
->setPosition(m_from
);
86 bool MovementAnimation::step(int t
)
88 int delta
= t
- m_last
;
90 m_sprite
->setPosition(m_sprite
->position() + delta
* m_velocity
);
95 void MovementAnimation::stop()
97 m_sprite
->setPosition(m_from
+ m_velocity
* m_time
);
101 AnimationGroup::AnimationGroup()
106 void AnimationGroup::add(Animation
* a
)
108 m_animations
.append(a
);
114 void AnimationGroup::start(int t
)
117 foreach (Animation
* a
, m_animations
) {
122 bool AnimationGroup::step(int t
)
125 for (List::iterator it
= m_animations
.begin();
126 it
!= m_animations
.end(); ) {
127 if ((*it
)->step(t
)) {
129 it
= m_animations
.erase(it
);
136 return m_animations
.isEmpty();
139 void AnimationGroup::stop()
141 foreach (Animation
* a
, m_animations
) {
144 m_animations
.clear();
148 AnimationSequence::AnimationSequence()
154 void AnimationSequence::add(Animation
* a
)
156 m_animations
.enqueue(a
);
162 void AnimationSequence::start(int t
)
165 if (!m_animations
.isEmpty()) {
166 m_animations
.head()->start(t
);
170 bool AnimationSequence::step(int t
)
173 while (!m_animations
.isEmpty()) {
174 if (m_animations
.head()->step(t
)) {
175 delete m_animations
.dequeue();
176 if (!m_animations
.isEmpty()) {
177 m_animations
.head()->start(t
);
187 void AnimationSequence::stop()
189 while (!m_animations
.isEmpty()) {
190 m_animations
.head()->stop();
191 delete m_animations
.dequeue();
195 #include "animation.moc"