Stellarium 0.15.2
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  , startValue(0.)
84  , targetValue(0.)
85  , counter(0)
86  {
87  isTransiting = false;
88  duration = _duration;
89  interstate = state ? maxValue : minValue;
90  }
91 
92  ~LinearFader() {;}
93 
94  // Increments the internal counter of deltaTime ticks
95  void update(int deltaTicks)
96  {
97  if (!isTransiting) return; // We are not in transition
98  counter+=deltaTicks;
99  if (counter>=duration)
100  {
101  // Transition is over
102  isTransiting = false;
103  interstate = targetValue;
104  // state = (targetValue==maxValue) ? true : false;
105  }
106  else
107  {
108  interstate = startValue + (targetValue - startValue) * counter/duration;
109  }
110  }
111 
112  // Get current switch state
113  float getInterstate() const { return interstate;}
114  float getInterstatePercentage() const {return 100.f * (interstate-minValue)/(maxValue-minValue);}
115 
116  // StelFaders can be used just as bools
117  StelFader& operator=(bool s)
118  {
119 
120  if(isTransiting) {
121  // if same end state, no changes
122  if(s == state) return *this;
123 
124  // otherwise need to reverse course
125  state = s;
126  counter = duration - counter;
127  float temp = startValue;
128  startValue = targetValue;
129  targetValue = temp;
130 
131  } else {
132 
133  if(state == s) return *this; // no change
134 
135  // set up and begin transit
136  state = s;
137  startValue = s ? minValue : maxValue;
138  targetValue = s ? maxValue : minValue;
139  counter=0;
140  isTransiting = true;
141  }
142  return *this;
143  }
144 
145  void setDuration(int _duration) {duration = _duration;}
146  virtual float getDuration() {return duration;}
147  void setMaxValue(float _max) {
148  if(interstate >= maxValue) interstate =_max;
149  maxValue = _max;
150  }
151 
152 protected:
153  bool isTransiting;
154  int duration;
155  float startValue, targetValue;
156  int counter;
157  float interstate;
158 };
159 
160 
161 // Please note that state is updated instantaneously, so if you need to draw something fading in
162 // and out, you need to check the interstate value (!=0) to know to draw when on AND during transitions
163 class ParabolicFader : public StelFader
164 {
165 public:
166  // Create and initialise to default
167  ParabolicFader(int _duration=1000, float minimumValue=0.f, float maximumValue=1.f, bool initialState=false)
168  : StelFader(initialState, minimumValue, maximumValue)
169  {
170  isTransiting = false;
171  duration = _duration;
172  interstate = state ? maxValue : minValue;
173  }
174 
175  ~ParabolicFader() {;}
176 
177  // Increments the internal counter of deltaTime ticks
178  void update(int deltaTicks)
179  {
180  if (!isTransiting) return; // We are not in transition
181  counter+=deltaTicks;
182  if (counter>=duration)
183  {
184  // Transition is over
185  isTransiting = false;
186  interstate = targetValue;
187  // state = (targetValue==maxValue) ? true : false;
188  }
189  else
190  {
191  interstate = startValue + (targetValue - startValue) * counter/duration;
192  interstate *= interstate;
193  }
194 
195  // printf("Counter %d interstate %f\n", counter, interstate);
196  }
197 
198  // Get current switch state
199  float getInterstate(void) const { return interstate;}
200  float getInterstatePercentage(void) const {return 100.f * (interstate-minValue)/(maxValue-minValue);}
201 
202  // StelFaders can be used just as bools
203  StelFader& operator=(bool s)
204  {
205 
206  if(isTransiting) {
207  // if same end state, no changes
208  if(s == state) return *this;
209 
210  // otherwise need to reverse course
211  state = s;
212  counter = duration - counter;
213  float temp = startValue;
214  startValue = targetValue;
215  targetValue = temp;
216 
217  } else {
218 
219  if(state == s) return *this; // no change
220 
221  // set up and begin transit
222  state = s;
223  startValue = s ? minValue : maxValue;
224  targetValue = s ? maxValue : minValue;
225  counter=0;
226  isTransiting = true;
227  }
228  return *this;
229  }
230 
231  void setDuration(int _duration) {duration = _duration;}
232  virtual float getDuration(void) {return duration;}
233 protected:
234  bool isTransiting;
235  int duration;
236  float startValue, targetValue;
237  int counter;
238  float interstate;
239 };
240 
241 #endif // _STELFADER_HPP_
Implementation of StelFader which implements a linear transition.
Definition: StelFader.hpp:77
Implementation of StelFader which behaves like a normal boolean, i.e.
Definition: StelFader.hpp:56
Manages a (usually smooth) transition between two states (typically ON/OFF) in function of a counter ...
Definition: StelFader.hpp:28