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