~ubuntu-branches/ubuntu/precise/stellarium/precise

« back to all changes in this revision

Viewing changes to src/core/StelFader.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Cédric Delfosse
  • Date: 2009-03-13 20:07:22 UTC
  • mfrom: (1.1.8 upstream) (4.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090313200722-l66s4zy2s3e8up0s
Tags: 0.10.2-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Stellarium
 
3
 * Copyright (C) 2005 Fabien Chereau
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU General Public License
 
7
 * as published by the Free Software Foundation; either version 2
 
8
 * of the License, or (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
18
 */
 
19
 
 
20
#ifndef _STELFADER_HPP_
 
21
#define _STELFADER_HPP_
 
22
 
 
23
#include <cstdio>
 
24
#include <cfloat>
 
25
 
 
26
//! @class StelFader
 
27
//! Manages a (usually smooth) transition between two states (typically ON/OFF) in function of a counter
 
28
//! It used for various purpose like smooth transitions between 
 
29
class StelFader
 
30
{
 
31
public:
 
32
        // Create and initialise
 
33
        StelFader(bool initialState, float minimumValue=0.f, float maximumValue=1.f) : state(initialState), minValue(minimumValue), maxValue(maximumValue) {;}
 
34
        virtual ~StelFader() {;}
 
35
        // Increments the internal counter of deltaTime ticks
 
36
        virtual void update(int deltaTicks) = 0;
 
37
        // Gets current switch state
 
38
        virtual float getInterstate(void) const = 0;
 
39
        virtual float getInterstatePercentage(void) const = 0;
 
40
        // Switchors can be used just as bools
 
41
        virtual StelFader& operator=(bool s) = 0;
 
42
        bool operator==(bool s) const {return state==s;}
 
43
        operator bool() const {return state;}
 
44
        virtual void setDuration(int _duration) {;}
 
45
        virtual float getDuration(void) = 0;
 
46
        virtual void setMinValue(float _min) {minValue = _min;}
 
47
        virtual void setMaxValue(float _max) {maxValue = _max;}
 
48
        float getMinValue(void) {return minValue;}
 
49
        float getMaxValue(void) {return maxValue;}
 
50
protected:
 
51
        bool state;
 
52
        float minValue, maxValue;
 
53
};
 
54
 
 
55
//! @class BooleanFader
 
56
//! Implementation of StelFader which behaves like a normal boolean, i.e. no transition between on and off.
 
57
class BooleanFader : public StelFader
 
58
{
 
59
public:
 
60
        // Create and initialise
 
61
        BooleanFader(bool initialState=false, float minimumValue=0.f, float maximumValue=1.f) : StelFader(initialState, minimumValue, maximumValue) {;}
 
62
        ~BooleanFader() {;}
 
63
        // Increments the internal counter of deltaTime ticks
 
64
        void update(int deltaTicks) {;}
 
65
        // Gets current switch state
 
66
        float getInterstate(void) const {return state ? maxValue : minValue;}
 
67
        float getInterstatePercentage(void) const {return state ? 100.f : 0.f;}
 
68
        // Switchors can be used just as bools
 
69
        StelFader& operator=(bool s) {state=s; return *this;}
 
70
        virtual float getDuration(void) {return 0.f;}
 
71
protected:
 
72
};
 
73
 
 
74
//! @class LinearFader
 
75
//! Implementation of StelFader which implements a linear transition.
 
76
//! Please note that state is updated instantaneously, so if you need to draw something fading in
 
77
//! and out, you need to check the interstate value (!=0) to know to draw when on AND during transitions.
 
78
class LinearFader : public StelFader
 
79
{
 
80
public:
 
81
        // Create and initialise to default
 
82
        LinearFader(int _duration=1000, float minimumValue=0.f, float maximumValue=1.f, bool initialState=false) 
 
83
                : StelFader(initialState, minimumValue, maximumValue)
 
84
        {
 
85
                isTransiting = false;
 
86
                duration = _duration;
 
87
                interstate = state ? maxValue : minValue;
 
88
        }
 
89
        
 
90
        ~LinearFader() {;}
 
91
        
 
92
        // Increments the internal counter of deltaTime ticks
 
93
        void update(int deltaTicks)
 
94
        {
 
95
                if (!isTransiting) return; // We are not in transition
 
96
                counter+=deltaTicks;
 
97
                if (counter>=duration)
 
98
                {
 
99
                        // Transition is over
 
100
                        isTransiting = false;
 
101
                        interstate = targetValue;
 
102
                        // state = (targetValue==maxValue) ? true : false;
 
103
                }
 
104
                else
 
105
                {
 
106
                        interstate = startValue + (targetValue - startValue) * counter/duration;
 
107
                }
 
108
        }
 
109
        
 
110
        // Get current switch state
 
111
        float getInterstate(void) const { return interstate;}
 
112
        float getInterstatePercentage(void) const {return 100.f * (interstate-minValue)/(maxValue-minValue);}
 
113
        
 
114
        // StelFaders can be used just as bools
 
115
        StelFader& operator=(bool s)
 
116
        {
 
117
 
 
118
                if(isTransiting) {
 
119
                        // if same end state, no changes
 
120
                        if(s == state) return *this;
 
121
                        
 
122
                        // otherwise need to reverse course
 
123
                        state = s;
 
124
                        counter = duration - counter;
 
125
                        float temp = startValue;
 
126
                        startValue = targetValue;
 
127
                        targetValue = temp;
 
128
                        
 
129
                } else {
 
130
 
 
131
                        if(state == s) return *this;  // no change
 
132
 
 
133
                        // set up and begin transit
 
134
                        state = s;
 
135
                        startValue = s ? minValue : maxValue;
 
136
                        targetValue = s ? maxValue : minValue;
 
137
                        counter=0;
 
138
                        isTransiting = true;
 
139
                }
 
140
                return *this;
 
141
        }
 
142
        
 
143
        void setDuration(int _duration) {duration = _duration;}
 
144
        virtual float getDuration(void) {return duration;}
 
145
        void setMaxValue(float _max) {
 
146
                if(interstate >=  maxValue) interstate =_max;
 
147
                maxValue = _max;
 
148
        }
 
149
 
 
150
protected:
 
151
        bool isTransiting;
 
152
        int duration;
 
153
        float startValue, targetValue;
 
154
        int counter;
 
155
        float interstate;
 
156
};
 
157
 
 
158
 
 
159
// Please note that state is updated instantaneously, so if you need to draw something fading in
 
160
// and out, you need to check the interstate value (!=0) to know to draw when on AND during transitions
 
161
class ParabolicFader : public StelFader
 
162
{
 
163
public:
 
164
        // Create and initialise to default
 
165
        ParabolicFader(int _duration=1000, float minimumValue=0.f, float maximumValue=1.f, bool initialState=false) 
 
166
                : StelFader(initialState, minimumValue, maximumValue)
 
167
        {
 
168
                isTransiting = false;
 
169
                duration = _duration;
 
170
                interstate = state ? maxValue : minValue;
 
171
        }
 
172
        
 
173
        ~ParabolicFader() {;}
 
174
        
 
175
        // Increments the internal counter of deltaTime ticks
 
176
        void update(int deltaTicks)
 
177
        {
 
178
                if (!isTransiting) return; // We are not in transition
 
179
                counter+=deltaTicks;
 
180
                if (counter>=duration)
 
181
                {
 
182
                        // Transition is over
 
183
                        isTransiting = false;
 
184
                        interstate = targetValue;
 
185
                        // state = (targetValue==maxValue) ? true : false;
 
186
                }
 
187
                else
 
188
                {
 
189
                        interstate = startValue + (targetValue - startValue) * counter/duration;
 
190
                        interstate *= interstate;
 
191
                }
 
192
 
 
193
                // printf("Counter %d  interstate %f\n", counter, interstate);
 
194
        }
 
195
        
 
196
        // Get current switch state
 
197
        float getInterstate(void) const { return interstate;}
 
198
        float getInterstatePercentage(void) const {return 100.f * (interstate-minValue)/(maxValue-minValue);}
 
199
        
 
200
        // StelFaders can be used just as bools
 
201
        StelFader& operator=(bool s)
 
202
        {
 
203
 
 
204
                if(isTransiting) {
 
205
                        // if same end state, no changes
 
206
                        if(s == state) return *this;
 
207
                        
 
208
                        // otherwise need to reverse course
 
209
                        state = s;
 
210
                        counter = duration - counter;
 
211
                        float temp = startValue;
 
212
                        startValue = targetValue;
 
213
                        targetValue = temp;
 
214
                        
 
215
                } else {
 
216
 
 
217
                        if(state == s) return *this;  // no change
 
218
 
 
219
                        // set up and begin transit
 
220
                        state = s;
 
221
                        startValue = s ? minValue : maxValue;
 
222
                        targetValue = s ? maxValue : minValue;
 
223
                        counter=0;
 
224
                        isTransiting = true;
 
225
                }
 
226
                return *this;
 
227
        }
 
228
        
 
229
        void setDuration(int _duration) {duration = _duration;}
 
230
        virtual float getDuration(void) {return duration;}
 
231
protected:
 
232
        bool isTransiting;
 
233
        int duration;
 
234
        float startValue, targetValue;
 
235
        int counter;
 
236
        float interstate;
 
237
};
 
238
 
 
239
 
 
240
/* better idea but not working...
 
241
class ParabolicFader : public LinearFader
 
242
{
 
243
public:
 
244
        ParabolicFader(int _duration=1000, float minimumValue=0.f, float maximumValue=1.f, bool initialState=false) 
 
245
                : LinearFader(_duration, minimumValue, maximumValue, initialState)
 
246
                {
 
247
                }
 
248
        
 
249
        // Increments the internal counter of deltaTime ticks
 
250
        void update(int deltaTicks)
 
251
        {
 
252
 
 
253
                printf("Counter %d  interstate %f\n", counter, interstate);
 
254
                if (!isTransiting) return; // We are not in transition
 
255
                counter+=deltaTicks;
 
256
                if (counter>=duration)
 
257
                {
 
258
                        // Transition is over
 
259
                        isTransiting = false;
 
260
                        interstate = targetValue;
 
261
                }
 
262
                else
 
263
                {
 
264
                        interstate = startValue + (targetValue - startValue) * counter/duration;
 
265
                        interstate *= interstate;
 
266
                }
 
267
 
 
268
                printf("Counter %d  interstate %f\n", counter, interstate);
 
269
        }
 
270
};
 
271
*/
 
272
 
 
273
#endif // _STELFADER_HPP_