File indexing completed on 2022-10-04 14:29:25

0001 /*
0002     SPDX-FileCopyrightText: 2007 Paolo Capriotti <p.capriotti@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "animation.h"
0008 
0009 #include "sprite.h"
0010 
0011 #include <math.h>
0012 
0013 Animation::~Animation()
0014 {
0015     Q_EMIT over();
0016 }
0017 
0018 PauseAnimation::PauseAnimation(int time)
0019 : m_time(time)
0020 {
0021 }
0022 
0023 void PauseAnimation::start(int t)
0024 {
0025     m_start = t;
0026 }
0027 
0028 bool PauseAnimation::step(int t)
0029 {
0030     return t - m_start >= m_time;
0031 }
0032 
0033 FadeAnimation::FadeAnimation(const SpritePtr& sprite, double from, double to, int time)
0034 : m_sprite(sprite)
0035 , m_from(from)
0036 , m_to(to)
0037 , m_time(time)
0038 , m_stopped(false)
0039 {
0040 }
0041 
0042 void FadeAnimation::start(int t)
0043 {
0044     m_start = t;
0045     m_sprite->setOpacityF(m_from);
0046 }
0047 
0048 bool FadeAnimation::step(int t)
0049 {
0050     if (m_stopped) {
0051         return true;
0052     }
0053     else {
0054         double val = m_from + (m_to - m_from) * (t - m_start) / m_time;
0055         m_sprite->setOpacityF(val);
0056         return t - m_start >= m_time;
0057     }
0058 }
0059 
0060 void FadeAnimation::stop()
0061 {
0062     m_sprite->setOpacityF(m_to);
0063     m_stopped = true;
0064 }
0065 
0066 
0067 MovementAnimation::MovementAnimation(const SpritePtr& sprite, const QPointF& from, 
0068                                      const QPointF& velocity, int time)
0069 : m_sprite(sprite)
0070 , m_from(from)
0071 , m_velocity(velocity)
0072 , m_time(time)
0073 {
0074 }
0075 
0076 void MovementAnimation::start(int t)
0077 {
0078     m_last = t;
0079     m_sprite->setPosition(m_from);
0080 }
0081 
0082 bool MovementAnimation::step(int t)
0083 {
0084     int delta = t - m_last;
0085     m_last = t;
0086     m_sprite->setPosition(m_sprite->position() + delta * m_velocity);
0087     m_time -= delta;
0088     return m_time <= 0;
0089 }
0090 
0091 void MovementAnimation::stop()
0092 {
0093     m_sprite->setPosition(m_from + m_velocity * m_time);
0094 }
0095 
0096 
0097 AnimationGroup::AnimationGroup()
0098 : m_last(-1)
0099 {
0100 }
0101 
0102 void AnimationGroup::add(Animation* a)
0103 {
0104     m_animations.append(a);
0105     if (m_last != -1) {
0106         a->start(m_last);
0107     }
0108 }
0109 
0110 void AnimationGroup::start(int t)
0111 {
0112     m_last = t;
0113     for (Animation* a : std::as_const(m_animations)) {
0114         a->start(t);
0115     }
0116 }
0117 
0118 bool AnimationGroup::step(int t)
0119 {
0120     m_last = t;
0121     for (List::iterator it = m_animations.begin();
0122          it != m_animations.end(); ) {
0123         if ((*it)->step(t)) {
0124             delete *it;
0125             it = m_animations.erase(it);
0126         }
0127         else {
0128             ++it;
0129         }
0130     }
0131     
0132     return m_animations.isEmpty();
0133 }
0134 
0135 void AnimationGroup::stop()
0136 {
0137     qDeleteAll(m_animations);
0138     m_animations.clear();
0139 }
0140 
0141 
0142 AnimationSequence::AnimationSequence()
0143 : m_last(-1)
0144 {
0145 }
0146 
0147 
0148 void AnimationSequence::add(Animation* a)
0149 {
0150     m_animations.enqueue(a);
0151     if (m_last != -1) {
0152         a->start(m_last);
0153     }
0154 }
0155 
0156 void AnimationSequence::start(int t)
0157 {
0158     m_last = t;
0159     if (!m_animations.isEmpty()) {
0160         m_animations.head()->start(t);
0161     }
0162 }
0163 
0164 bool AnimationSequence::step(int t)
0165 {
0166     m_last = t;
0167     while (!m_animations.isEmpty()) {
0168         if (m_animations.head()->step(t)) {
0169             delete m_animations.dequeue();
0170             if (!m_animations.isEmpty()) {
0171                 m_animations.head()->start(t);
0172             }
0173         }
0174         else {
0175             return false;
0176         }
0177     }
0178     return true;
0179 }
0180 
0181 void AnimationSequence::stop()
0182 {
0183     while (!m_animations.isEmpty()) {
0184         m_animations.head()->stop();
0185         delete m_animations.dequeue();
0186     }
0187 }
0188 
0189 
0190