Stellarium 0.12.4
StelFader.hpp
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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18  */
19 
20 #ifndef _STELFADER_HPP_
21 #define _STELFADER_HPP_
22 
23 #include <QtGlobal>
24 
28 class StelFader
29 {
30 public:
31  // Create and initialise
32  StelFader(bool initialState, float minimumValue=0.f, float maximumValue=1.f) : state(initialState), minValue(minimumValue), maxValue(maximumValue) {;}
33  virtual ~StelFader() {;}
34  // Increments the internal counter of deltaTime ticks
35  virtual void update(int deltaTicks) = 0;
36  // Gets current switch state
37  virtual float getInterstate() const = 0;
38  virtual float getInterstatePercentage() const = 0;
39  // Switchors can be used just as bools
40  virtual StelFader& operator=(bool s) = 0;
41  bool operator==(bool s) const {return state==s;}
42  operator bool() const {return state;}
43  virtual void setDuration(int) {;}
44  virtual float getDuration() = 0;
45  virtual void setMinValue(float _min) {minValue = _min;}
46  virtual void setMaxValue(float _max) {maxValue = _max;}
47  float getMinValue() {return minValue;}
48  float getMaxValue() {return maxValue;}
49 protected:
50  bool state;
51  float minValue, maxValue;
52 };
53 
56 class BooleanFader : public StelFader
57 {
58 public:
59  // Create and initialise
60  BooleanFader(bool initialState=false, float minimumValue=0.f, float maximumValue=1.f) : StelFader(initialState, minimumValue, maximumValue) {;}
61  ~BooleanFader() {;}
62  // Increments the internal counter of deltaTime ticks
63  void update(int deltaTicks) {Q_UNUSED(deltaTicks);}
64  // Gets current switch state
65  float getInterstate() const {return state ? maxValue : minValue;}
66  float getInterstatePercentage() const {return state ? 100.f : 0.f;}
67  // Switchors can be used just as bools
68  StelFader& operator=(bool s) {state=s; return *this;}
69  virtual float getDuration() {return 0.f;}
70 protected:
71 };
72 
77 class LinearFader : public StelFader
78 {
79 public:
80  // Create and initialise to default
81  LinearFader(int _duration=1000, float minimumValue=0.f, float maximumValue=1.f, bool initialState=false)
82  : StelFader(initialState, minimumValue, maximumValue)
83  {
84  isTransiting = false;
85  duration = _duration;
86  interstate = state ? maxValue : minValue;
87  }
88 
89  ~LinearFader() {;}
90 
91  // Increments the internal counter of deltaTime ticks
92  void update(int deltaTicks)
93  {
94  if (!isTransiting) return; // We are not in transition
95  counter+=deltaTicks;
96  if (counter>=duration)
97  {
98  // Transition is over
99  isTransiting = false;
100  interstate = targetValue;
101  // state = (targetValue==maxValue) ? true : false;
102  }
103  else
104  {
105  interstate = startValue + (targetValue - startValue) * counter/duration;
106  }
107  }
108 
109  // Get current switch state
110  float getInterstate() const { return interstate;}
111  float getInterstatePercentage() const {return 100.f * (interstate-minValue)/(maxValue-minValue);}
112 
113  // StelFaders can be used just as bools
114  StelFader& operator=(bool s)
115  {
116 
117  if(isTransiting) {
118  // if same end state, no changes
119  if(s == state) return *this;
120 
121  // otherwise need to reverse course
122  state = s;
123  counter = duration - counter;
124  float temp = startValue;
125  startValue = targetValue;
126  targetValue = temp;
127 
128  } else {
129 
130  if(state == s) return *this; // no change
131 
132  // set up and begin transit
133  state = s;
134  startValue = s ? minValue : maxValue;
135  targetValue = s ? maxValue : minValue;
136  counter=0;
137  isTransiting = true;
138  }
139  return *this;
140  }
141 
142  void setDuration(int _duration) {duration = _duration;}
143  virtual float getDuration() {return duration;}
144  void setMaxValue(float _max) {
145  if(interstate >= maxValue) interstate =_max;
146  maxValue = _max;
147  }
148 
149 protected:
150  bool isTransiting;
151  int duration;
152  float startValue, targetValue;
153  int counter;
154  float interstate;
155 };
156 
157 
158 // Please note that state is updated instantaneously, so if you need to draw something fading in
159 // and out, you need to check the interstate value (!=0) to know to draw when on AND during transitions
160 class ParabolicFader : public StelFader
161 {
162 public:
163  // Create and initialise to default
164  ParabolicFader(int _duration=1000, float minimumValue=0.f, float maximumValue=1.f, bool initialState=false)
165  : StelFader(initialState, minimumValue, maximumValue)
166  {
167  isTransiting = false;
168  duration = _duration;
169  interstate = state ? maxValue : minValue;
170  }
171 
172  ~ParabolicFader() {;}
173 
174  // Increments the internal counter of deltaTime ticks
175  void update(int deltaTicks)
176  {
177  if (!isTransiting) return; // We are not in transition
178  counter+=deltaTicks;
179  if (counter>=duration)
180  {
181  // Transition is over
182  isTransiting = false;
183  interstate = targetValue;
184  // state = (targetValue==maxValue) ? true : false;
185  }
186  else
187  {
188  interstate = startValue + (targetValue - startValue) * counter/duration;
189  interstate *= interstate;
190  }
191 
192  // printf("Counter %d interstate %f\n", counter, interstate);
193  }
194 
195  // Get current switch state
196  float getInterstate(void) const { return interstate;}
197  float getInterstatePercentage(void) const {return 100.f * (interstate-minValue)/(maxValue-minValue);}
198 
199  // StelFaders can be used just as bools
200  StelFader& operator=(bool s)
201  {
202 
203  if(isTransiting) {
204  // if same end state, no changes
205  if(s == state) return *this;
206 
207  // otherwise need to reverse course
208  state = s;
209  counter = duration - counter;
210  float temp = startValue;
211  startValue = targetValue;
212  targetValue = temp;
213 
214  } else {
215 
216  if(state == s) return *this; // no change
217 
218  // set up and begin transit
219  state = s;
220  startValue = s ? minValue : maxValue;
221  targetValue = s ? maxValue : minValue;
222  counter=0;
223  isTransiting = true;
224  }
225  return *this;
226  }
227 
228  void setDuration(int _duration) {duration = _duration;}
229  virtual float getDuration(void) {return duration;}
230 protected:
231  bool isTransiting;
232  int duration;
233  float startValue, targetValue;
234  int counter;
235  float interstate;
236 };
237 
238 #endif // _STELFADER_HPP_