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