Debian Codegraph @

src/game/fadeanimation.cpp

raw symbols SCIP
1 // This file is part of Dust Racing 2D.
2 // Copyright (C) 2013 Jussi Lind <[email protected]>
3 //
4 // Dust Racing 2D 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 3 of the License, or
7 // (at your option) any later version.
8 // Dust Racing 2D is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>.
15
16 #include "fadeanimation.hpp"
17 #include <cmath>
18
19 FadeAnimation::FadeAnimation(int updateFps)
20 : m_updateFps(updateFps)
21 , m_fadeValue(0.0)
22 , m_fadeIn(false)
23 {
24 m_timer.setInterval(1000 / updateFps);
25 connect(&m_timer, &QTimer::timeout, this, &FadeAnimation::updateAnimation);
26 }
27
28 bool FadeAnimation::isFading() const
29 {
30 return m_timer.isActive();
31 }
32
33 void FadeAnimation::beginFadeIn(int preDelayMSec, int msec, int postDelayMSec)
34 {
35 m_preDelayMSec = preDelayMSec / m_updateFps;
36 m_postDelayMSec = postDelayMSec / m_updateFps;
37 m_fadeValue = 0.0;
38 m_step = 1.0 / (msec / m_updateFps);
39 m_fadeIn = true;
40
41 emit fadeValueChanged(m_fadeValue);
42
43 m_timer.start();
44 }
45
46 void FadeAnimation::beginFadeOut(int preDelayMSec, int msec, int postDelayMSec)
47 {
48 m_preDelayMSec = preDelayMSec / m_updateFps;
49 m_postDelayMSec = postDelayMSec / m_updateFps;
50 m_fadeValue = 1.0;
51 m_step = m_fadeValue / (msec / m_updateFps);
52 m_fadeIn = false;
53
54 emit fadeValueChanged(m_fadeValue);
55
56 m_timer.start();
57 }
58
59 void FadeAnimation::beginFadeOutFlash(int preDelayMSec, int msec, int postDelayMSec)
60 {
61 m_preDelayMSec = preDelayMSec / m_updateFps;
62 m_postDelayMSec = postDelayMSec / m_updateFps;
63 m_fadeValue = 10.0;
64 m_step = m_fadeValue / (msec / m_updateFps);
65 m_fadeIn = false;
66
67 emit fadeValueChanged(m_fadeValue);
68
69 m_timer.start();
70 }
71
72 void FadeAnimation::updateAnimation()
73 {
74 if (m_preDelayMSec)
75 {
76 m_preDelayMSec--;
77 }
78 else if (m_fadeIn && m_fadeValue < 1.0)
79 {
80 m_fadeValue += m_step;
81 #ifdef Q_OS_ANDROID
82 emit fadeValueChanged(fmin(m_fadeValue, 1.0));
83 #else
84 emit fadeValueChanged(std::fmin(m_fadeValue, 1.0));
85 #endif
86 }
87 else if (!m_fadeIn && m_fadeValue > 0.0)
88 {
89 m_fadeValue -= m_step;
90 #ifdef Q_OS_ANDROID
91 emit fadeValueChanged(fmax(m_fadeValue, 0.0));
92 #else
93 emit fadeValueChanged(std::fmax(m_fadeValue, 0.0));
94 #endif
95 }
96 else if (m_postDelayMSec)
97 {
98 m_postDelayMSec--;
99 }
100 else
101 {
102 m_timer.stop();
103
104 if (m_fadeIn)
105 {
106 emit fadeInFinished();
107 }
108 else
109 {
110 emit fadeOutFinished();
111 }
112 }
113 }