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