1 | /* |
2 | Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com> |
3 | |
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. |
8 | */ |
9 | |
10 | #include "animation.h" |
11 | |
12 | #include "sprite.h" |
13 | |
14 | #include <math.h> |
15 | #include <kdebug.h> |
16 | |
17 | Animation::~Animation() |
18 | { |
19 | emit over(); |
20 | } |
21 | |
22 | PauseAnimation::PauseAnimation(int time) |
23 | : m_time(time) |
24 | { |
25 | } |
26 | |
27 | void PauseAnimation::start(int t) |
28 | { |
29 | m_start = t; |
30 | } |
31 | |
32 | bool PauseAnimation::step(int t) |
33 | { |
34 | return t - m_start >= m_time; |
35 | } |
36 | |
37 | FadeAnimation::FadeAnimation(const SpritePtr& sprite, double from, double to, int time) |
38 | : m_sprite(sprite) |
39 | , m_from(from) |
40 | , m_to(to) |
41 | , m_time(time) |
42 | , m_stopped(false) |
43 | { |
44 | } |
45 | |
46 | void FadeAnimation::start(int t) |
47 | { |
48 | m_start = t; |
49 | m_sprite->setOpacityF(m_from); |
50 | } |
51 | |
52 | bool FadeAnimation::step(int t) |
53 | { |
54 | if (m_stopped) { |
55 | return true; |
56 | } |
57 | else { |
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; |
61 | } |
62 | } |
63 | |
64 | void FadeAnimation::stop() |
65 | { |
66 | m_sprite->setOpacityF(m_to); |
67 | m_stopped = true; |
68 | } |
69 | |
70 | |
71 | MovementAnimation::MovementAnimation(const SpritePtr& sprite, const QPointF& from, |
72 | const QPointF& velocity, int time) |
73 | : m_sprite(sprite) |
74 | , m_from(from) |
75 | , m_velocity(velocity) |
76 | , m_time(time) |
77 | { |
78 | } |
79 | |
80 | void MovementAnimation::start(int t) |
81 | { |
82 | m_last = t; |
83 | m_sprite->setPosition(m_from); |
84 | } |
85 | |
86 | bool MovementAnimation::step(int t) |
87 | { |
88 | int delta = t - m_last; |
89 | m_last = t; |
90 | m_sprite->setPosition(m_sprite->position() + delta * m_velocity); |
91 | m_time -= delta; |
92 | return m_time <= 0; |
93 | } |
94 | |
95 | void MovementAnimation::stop() |
96 | { |
97 | m_sprite->setPosition(m_from + m_velocity * m_time); |
98 | } |
99 | |
100 | |
101 | AnimationGroup::AnimationGroup() |
102 | : m_last(-1) |
103 | { |
104 | } |
105 | |
106 | void AnimationGroup::add(Animation* a) |
107 | { |
108 | m_animations.append(a); |
109 | if (m_last != -1) { |
110 | a->start(m_last); |
111 | } |
112 | } |
113 | |
114 | void AnimationGroup::start(int t) |
115 | { |
116 | m_last = t; |
117 | foreach (Animation* a, m_animations) { |
118 | a->start(t); |
119 | } |
120 | } |
121 | |
122 | bool AnimationGroup::step(int t) |
123 | { |
124 | m_last = t; |
125 | for (List::iterator it = m_animations.begin(); |
126 | it != m_animations.end(); ) { |
127 | if ((*it)->step(t)) { |
128 | delete *it; |
129 | it = m_animations.erase(it); |
130 | } |
131 | else { |
132 | ++it; |
133 | } |
134 | } |
135 | |
136 | return m_animations.isEmpty(); |
137 | } |
138 | |
139 | void AnimationGroup::stop() |
140 | { |
141 | qDeleteAll(m_animations); |
142 | m_animations.clear(); |
143 | } |
144 | |
145 | |
146 | AnimationSequence::AnimationSequence() |
147 | : m_last(-1) |
148 | { |
149 | } |
150 | |
151 | |
152 | void AnimationSequence::add(Animation* a) |
153 | { |
154 | m_animations.enqueue(a); |
155 | if (m_last != -1) { |
156 | a->start(m_last); |
157 | } |
158 | } |
159 | |
160 | void AnimationSequence::start(int t) |
161 | { |
162 | m_last = t; |
163 | if (!m_animations.isEmpty()) { |
164 | m_animations.head()->start(t); |
165 | } |
166 | } |
167 | |
168 | bool AnimationSequence::step(int t) |
169 | { |
170 | m_last = t; |
171 | while (!m_animations.isEmpty()) { |
172 | if (m_animations.head()->step(t)) { |
173 | delete m_animations.dequeue(); |
174 | if (!m_animations.isEmpty()) { |
175 | m_animations.head()->start(t); |
176 | } |
177 | } |
178 | else { |
179 | return false; |
180 | } |
181 | } |
182 | return true; |
183 | } |
184 | |
185 | void AnimationSequence::stop() |
186 | { |
187 | while (!m_animations.isEmpty()) { |
188 | m_animations.head()->stop(); |
189 | delete m_animations.dequeue(); |
190 | } |
191 | } |
192 | |
193 | #include "animation.moc" |
194 | |
195 | |