3
* Copyright (C) 2005 Fabien Chereau
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.
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.
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.
20
#ifndef _STELFADER_HPP_
21
#define _STELFADER_HPP_
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
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;}
52
float minValue, maxValue;
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
60
// Create and initialise
61
BooleanFader(bool initialState=false, float minimumValue=0.f, float maximumValue=1.f) : StelFader(initialState, minimumValue, maximumValue) {;}
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;}
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
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)
87
interstate = state ? maxValue : minValue;
92
// Increments the internal counter of deltaTime ticks
93
void update(int deltaTicks)
95
if (!isTransiting) return; // We are not in transition
97
if (counter>=duration)
100
isTransiting = false;
101
interstate = targetValue;
102
// state = (targetValue==maxValue) ? true : false;
106
interstate = startValue + (targetValue - startValue) * counter/duration;
110
// Get current switch state
111
float getInterstate(void) const { return interstate;}
112
float getInterstatePercentage(void) const {return 100.f * (interstate-minValue)/(maxValue-minValue);}
114
// StelFaders can be used just as bools
115
StelFader& operator=(bool s)
119
// if same end state, no changes
120
if(s == state) return *this;
122
// otherwise need to reverse course
124
counter = duration - counter;
125
float temp = startValue;
126
startValue = targetValue;
131
if(state == s) return *this; // no change
133
// set up and begin transit
135
startValue = s ? minValue : maxValue;
136
targetValue = s ? maxValue : minValue;
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;
153
float startValue, targetValue;
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
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)
168
isTransiting = false;
169
duration = _duration;
170
interstate = state ? maxValue : minValue;
173
~ParabolicFader() {;}
175
// Increments the internal counter of deltaTime ticks
176
void update(int deltaTicks)
178
if (!isTransiting) return; // We are not in transition
180
if (counter>=duration)
182
// Transition is over
183
isTransiting = false;
184
interstate = targetValue;
185
// state = (targetValue==maxValue) ? true : false;
189
interstate = startValue + (targetValue - startValue) * counter/duration;
190
interstate *= interstate;
193
// printf("Counter %d interstate %f\n", counter, interstate);
196
// Get current switch state
197
float getInterstate(void) const { return interstate;}
198
float getInterstatePercentage(void) const {return 100.f * (interstate-minValue)/(maxValue-minValue);}
200
// StelFaders can be used just as bools
201
StelFader& operator=(bool s)
205
// if same end state, no changes
206
if(s == state) return *this;
208
// otherwise need to reverse course
210
counter = duration - counter;
211
float temp = startValue;
212
startValue = targetValue;
217
if(state == s) return *this; // no change
219
// set up and begin transit
221
startValue = s ? minValue : maxValue;
222
targetValue = s ? maxValue : minValue;
229
void setDuration(int _duration) {duration = _duration;}
230
virtual float getDuration(void) {return duration;}
234
float startValue, targetValue;
240
/* better idea but not working...
241
class ParabolicFader : public LinearFader
244
ParabolicFader(int _duration=1000, float minimumValue=0.f, float maximumValue=1.f, bool initialState=false)
245
: LinearFader(_duration, minimumValue, maximumValue, initialState)
249
// Increments the internal counter of deltaTime ticks
250
void update(int deltaTicks)
253
printf("Counter %d interstate %f\n", counter, interstate);
254
if (!isTransiting) return; // We are not in transition
256
if (counter>=duration)
258
// Transition is over
259
isTransiting = false;
260
interstate = targetValue;
264
interstate = startValue + (targetValue - startValue) * counter/duration;
265
interstate *= interstate;
268
printf("Counter %d interstate %f\n", counter, interstate);
273
#endif // _STELFADER_HPP_