Home · All Namespaces · All Classes · Functions · Coding Style · Scripting · 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 
00026 class StelFader
00027 {
00028 public:
00029     // Create and initialise
00030     StelFader(bool initialState, float minimumValue=0.f, float maximumValue=1.f) : state(initialState), minValue(minimumValue), maxValue(maximumValue) {;}
00031     virtual ~StelFader() {;}
00032     // Increments the internal counter of deltaTime ticks
00033     virtual void update(int deltaTicks) = 0;
00034     // Gets current switch state
00035     virtual float getInterstate(void) const = 0;
00036     virtual float getInterstatePercentage(void) const = 0;
00037     // Switchors can be used just as bools
00038     virtual StelFader& operator=(bool s) = 0;
00039     bool operator==(bool s) const {return state==s;}
00040     operator bool() const {return state;}
00041     virtual void setDuration(int _duration) {;}
00042     virtual float getDuration(void) = 0;
00043     virtual void setMinValue(float _min) {minValue = _min;}
00044     virtual void setMaxValue(float _max) {maxValue = _max;}
00045     float getMinValue(void) {return minValue;}
00046     float getMaxValue(void) {return maxValue;}
00047 protected:
00048     bool state;
00049     float minValue, maxValue;
00050 };
00051 
00054 class BooleanFader : public StelFader
00055 {
00056 public:
00057     // Create and initialise
00058     BooleanFader(bool initialState=false, float minimumValue=0.f, float maximumValue=1.f) : StelFader(initialState, minimumValue, maximumValue) {;}
00059     ~BooleanFader() {;}
00060     // Increments the internal counter of deltaTime ticks
00061     void update(int deltaTicks) {;}
00062     // Gets current switch state
00063     float getInterstate(void) const {return state ? maxValue : minValue;}
00064     float getInterstatePercentage(void) const {return state ? 100.f : 0.f;}
00065     // Switchors can be used just as bools
00066     StelFader& operator=(bool s) {state=s; return *this;}
00067     virtual float getDuration(void) {return 0.f;}
00068 protected:
00069 };
00070 
00075 class LinearFader : public StelFader
00076 {
00077 public:
00078     // Create and initialise to default
00079     LinearFader(int _duration=1000, float minimumValue=0.f, float maximumValue=1.f, bool initialState=false) 
00080         : StelFader(initialState, minimumValue, maximumValue)
00081     {
00082         isTransiting = false;
00083         duration = _duration;
00084         interstate = state ? maxValue : minValue;
00085     }
00086     
00087     ~LinearFader() {;}
00088     
00089     // Increments the internal counter of deltaTime ticks
00090     void update(int deltaTicks)
00091     {
00092         if (!isTransiting) return; // We are not in transition
00093         counter+=deltaTicks;
00094         if (counter>=duration)
00095         {
00096             // Transition is over
00097             isTransiting = false;
00098             interstate = targetValue;
00099             // state = (targetValue==maxValue) ? true : false;
00100         }
00101         else
00102         {
00103             interstate = startValue + (targetValue - startValue) * counter/duration;
00104         }
00105     }
00106     
00107     // Get current switch state
00108     float getInterstate(void) const { return interstate;}
00109     float getInterstatePercentage(void) const {return 100.f * (interstate-minValue)/(maxValue-minValue);}
00110     
00111     // StelFaders can be used just as bools
00112     StelFader& operator=(bool s)
00113     {
00114 
00115         if(isTransiting) {
00116             // if same end state, no changes
00117             if(s == state) return *this;
00118             
00119             // otherwise need to reverse course
00120             state = s;
00121             counter = duration - counter;
00122             float temp = startValue;
00123             startValue = targetValue;
00124             targetValue = temp;
00125             
00126         } else {
00127 
00128             if(state == s) return *this;  // no change
00129 
00130             // set up and begin transit
00131             state = s;
00132             startValue = s ? minValue : maxValue;
00133             targetValue = s ? maxValue : minValue;
00134             counter=0;
00135             isTransiting = true;
00136         }
00137         return *this;
00138     }
00139     
00140     void setDuration(int _duration) {duration = _duration;}
00141     virtual float getDuration(void) {return duration;}
00142     void setMaxValue(float _max) {
00143         if(interstate >=  maxValue) interstate =_max;
00144         maxValue = _max;
00145     }
00146 
00147 protected:
00148     bool isTransiting;
00149     int duration;
00150     float startValue, targetValue;
00151     int counter;
00152     float interstate;
00153 };
00154 
00155 
00156 // Please note that state is updated instantaneously, so if you need to draw something fading in
00157 // and out, you need to check the interstate value (!=0) to know to draw when on AND during transitions
00158 class ParabolicFader : public StelFader
00159 {
00160 public:
00161     // Create and initialise to default
00162     ParabolicFader(int _duration=1000, float minimumValue=0.f, float maximumValue=1.f, bool initialState=false) 
00163         : StelFader(initialState, minimumValue, maximumValue)
00164     {
00165         isTransiting = false;
00166         duration = _duration;
00167         interstate = state ? maxValue : minValue;
00168     }
00169     
00170     ~ParabolicFader() {;}
00171     
00172     // Increments the internal counter of deltaTime ticks
00173     void update(int deltaTicks)
00174     {
00175         if (!isTransiting) return; // We are not in transition
00176         counter+=deltaTicks;
00177         if (counter>=duration)
00178         {
00179             // Transition is over
00180             isTransiting = false;
00181             interstate = targetValue;
00182             // state = (targetValue==maxValue) ? true : false;
00183         }
00184         else
00185         {
00186             interstate = startValue + (targetValue - startValue) * counter/duration;
00187             interstate *= interstate;
00188         }
00189 
00190         // printf("Counter %d  interstate %f\n", counter, interstate);
00191     }
00192     
00193     // Get current switch state
00194     float getInterstate(void) const { return interstate;}
00195     float getInterstatePercentage(void) const {return 100.f * (interstate-minValue)/(maxValue-minValue);}
00196     
00197     // StelFaders can be used just as bools
00198     StelFader& operator=(bool s)
00199     {
00200 
00201         if(isTransiting) {
00202             // if same end state, no changes
00203             if(s == state) return *this;
00204             
00205             // otherwise need to reverse course
00206             state = s;
00207             counter = duration - counter;
00208             float temp = startValue;
00209             startValue = targetValue;
00210             targetValue = temp;
00211             
00212         } else {
00213 
00214             if(state == s) return *this;  // no change
00215 
00216             // set up and begin transit
00217             state = s;
00218             startValue = s ? minValue : maxValue;
00219             targetValue = s ? maxValue : minValue;
00220             counter=0;
00221             isTransiting = true;
00222         }
00223         return *this;
00224     }
00225     
00226     void setDuration(int _duration) {duration = _duration;}
00227     virtual float getDuration(void) {return duration;}
00228 protected:
00229     bool isTransiting;
00230     int duration;
00231     float startValue, targetValue;
00232     int counter;
00233     float interstate;
00234 };
00235 
00236 #endif // _STELFADER_HPP_

Generated on Mon Mar 22 09:55:37 2010 for Stellarium by  doxygen 1.5.5