Home · All Namespaces · All Classes · Functions · Coding Style · Plugins · File Structure

core/StelFader.hpp

00001 /*
00002  * Stellarium
00003  * Copyright (C) 2005 Fabien Chereau
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018  */
00019 
00020 #ifndef _STELFADER_HPP_
00021 #define _STELFADER_HPP_
00022 
00023 #include <cstdio>
00024 #include <cfloat>
00025 
00029 class StelFader
00030 {
00031 public:
00032     // Create and initialise
00033     StelFader(bool initialState, float minimumValue=0.f, float maximumValue=1.f) : state(initialState), minValue(minimumValue), maxValue(maximumValue) {;}
00034     virtual ~StelFader() {;}
00035     // Increments the internal counter of deltaTime ticks
00036     virtual void update(int deltaTicks) = 0;
00037     // Gets current switch state
00038     virtual float getInterstate(void) const = 0;
00039     virtual float getInterstatePercentage(void) const = 0;
00040     // Switchors can be used just as bools
00041     virtual StelFader& operator=(bool s) = 0;
00042     bool operator==(bool s) const {return state==s;}
00043     operator bool() const {return state;}
00044     virtual void setDuration(int _duration) {;}
00045     virtual float getDuration(void) = 0;
00046     virtual void setMinValue(float _min) {minValue = _min;}
00047     virtual void setMaxValue(float _max) {maxValue = _max;}
00048     float getMinValue(void) {return minValue;}
00049     float getMaxValue(void) {return maxValue;}
00050 protected:
00051     bool state;
00052     float minValue, maxValue;
00053 };
00054 
00057 class BooleanFader : public StelFader
00058 {
00059 public:
00060     // Create and initialise
00061     BooleanFader(bool initialState=false, float minimumValue=0.f, float maximumValue=1.f) : StelFader(initialState, minimumValue, maximumValue) {;}
00062     ~BooleanFader() {;}
00063     // Increments the internal counter of deltaTime ticks
00064     void update(int deltaTicks) {;}
00065     // Gets current switch state
00066     float getInterstate(void) const {return state ? maxValue : minValue;}
00067     float getInterstatePercentage(void) const {return state ? 100.f : 0.f;}
00068     // Switchors can be used just as bools
00069     StelFader& operator=(bool s) {state=s; return *this;}
00070     virtual float getDuration(void) {return 0.f;}
00071 protected:
00072 };
00073 
00078 class LinearFader : public StelFader
00079 {
00080 public:
00081     // Create and initialise to default
00082     LinearFader(int _duration=1000, float minimumValue=0.f, float maximumValue=1.f, bool initialState=false) 
00083         : StelFader(initialState, minimumValue, maximumValue)
00084     {
00085         isTransiting = false;
00086         duration = _duration;
00087         interstate = state ? maxValue : minValue;
00088     }
00089     
00090     ~LinearFader() {;}
00091     
00092     // Increments the internal counter of deltaTime ticks
00093     void update(int deltaTicks)
00094     {
00095         if (!isTransiting) return; // We are not in transition
00096         counter+=deltaTicks;
00097         if (counter>=duration)
00098         {
00099             // Transition is over
00100             isTransiting = false;
00101             interstate = targetValue;
00102             // state = (targetValue==maxValue) ? true : false;
00103         }
00104         else
00105         {
00106             interstate = startValue + (targetValue - startValue) * counter/duration;
00107         }
00108     }
00109     
00110     // Get current switch state
00111     float getInterstate(void) const { return interstate;}
00112     float getInterstatePercentage(void) const {return 100.f * (interstate-minValue)/(maxValue-minValue);}
00113     
00114     // StelFaders can be used just as bools
00115     StelFader& operator=(bool s)
00116     {
00117 
00118         if(isTransiting) {
00119             // if same end state, no changes
00120             if(s == state) return *this;
00121             
00122             // otherwise need to reverse course
00123             state = s;
00124             counter = duration - counter;
00125             float temp = startValue;
00126             startValue = targetValue;
00127             targetValue = temp;
00128             
00129         } else {
00130 
00131             if(state == s) return *this;  // no change
00132 
00133             // set up and begin transit
00134             state = s;
00135             startValue = s ? minValue : maxValue;
00136             targetValue = s ? maxValue : minValue;
00137             counter=0;
00138             isTransiting = true;
00139         }
00140         return *this;
00141     }
00142     
00143     void setDuration(int _duration) {duration = _duration;}
00144     virtual float getDuration(void) {return duration;}
00145     void setMaxValue(float _max) {
00146         if(interstate >=  maxValue) interstate =_max;
00147         maxValue = _max;
00148     }
00149 
00150 protected:
00151     bool isTransiting;
00152     int duration;
00153     float startValue, targetValue;
00154     int counter;
00155     float interstate;
00156 };
00157 
00158 
00159 // Please note that state is updated instantaneously, so if you need to draw something fading in
00160 // and out, you need to check the interstate value (!=0) to know to draw when on AND during transitions
00161 class ParabolicFader : public StelFader
00162 {
00163 public:
00164     // Create and initialise to default
00165     ParabolicFader(int _duration=1000, float minimumValue=0.f, float maximumValue=1.f, bool initialState=false) 
00166         : StelFader(initialState, minimumValue, maximumValue)
00167     {
00168         isTransiting = false;
00169         duration = _duration;
00170         interstate = state ? maxValue : minValue;
00171     }
00172     
00173     ~ParabolicFader() {;}
00174     
00175     // Increments the internal counter of deltaTime ticks
00176     void update(int deltaTicks)
00177     {
00178         if (!isTransiting) return; // We are not in transition
00179         counter+=deltaTicks;
00180         if (counter>=duration)
00181         {
00182             // Transition is over
00183             isTransiting = false;
00184             interstate = targetValue;
00185             // state = (targetValue==maxValue) ? true : false;
00186         }
00187         else
00188         {
00189             interstate = startValue + (targetValue - startValue) * counter/duration;
00190             interstate *= interstate;
00191         }
00192 
00193         // printf("Counter %d  interstate %f\n", counter, interstate);
00194     }
00195     
00196     // Get current switch state
00197     float getInterstate(void) const { return interstate;}
00198     float getInterstatePercentage(void) const {return 100.f * (interstate-minValue)/(maxValue-minValue);}
00199     
00200     // StelFaders can be used just as bools
00201     StelFader& operator=(bool s)
00202     {
00203 
00204         if(isTransiting) {
00205             // if same end state, no changes
00206             if(s == state) return *this;
00207             
00208             // otherwise need to reverse course
00209             state = s;
00210             counter = duration - counter;
00211             float temp = startValue;
00212             startValue = targetValue;
00213             targetValue = temp;
00214             
00215         } else {
00216 
00217             if(state == s) return *this;  // no change
00218 
00219             // set up and begin transit
00220             state = s;
00221             startValue = s ? minValue : maxValue;
00222             targetValue = s ? maxValue : minValue;
00223             counter=0;
00224             isTransiting = true;
00225         }
00226         return *this;
00227     }
00228     
00229     void setDuration(int _duration) {duration = _duration;}
00230     virtual float getDuration(void) {return duration;}
00231 protected:
00232     bool isTransiting;
00233     int duration;
00234     float startValue, targetValue;
00235     int counter;
00236     float interstate;
00237 };
00238 
00239 
00240 /* better idea but not working...
00241 class ParabolicFader : public LinearFader
00242 {
00243 public:
00244     ParabolicFader(int _duration=1000, float minimumValue=0.f, float maximumValue=1.f, bool initialState=false) 
00245         : LinearFader(_duration, minimumValue, maximumValue, initialState)
00246         {
00247         }
00248     
00249     // Increments the internal counter of deltaTime ticks
00250     void update(int deltaTicks)
00251     {
00252 
00253         printf("Counter %d  interstate %f\n", counter, interstate);
00254         if (!isTransiting) return; // We are not in transition
00255         counter+=deltaTicks;
00256         if (counter>=duration)
00257         {
00258             // Transition is over
00259             isTransiting = false;
00260             interstate = targetValue;
00261         }
00262         else
00263         {
00264             interstate = startValue + (targetValue - startValue) * counter/duration;
00265             interstate *= interstate;
00266         }
00267 
00268         printf("Counter %d  interstate %f\n", counter, interstate);
00269     }
00270 };
00271 */
00272 
00273 #endif // _STELFADER_HPP_

Generated on Mon Feb 2 17:23:47 2009 for Stellarium by  doxygen 1.5.5