00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _STELFADER_HPP_
00021 #define _STELFADER_HPP_
00022
00023 #include <QtGlobal>
00024
00028 class StelFader
00029 {
00030 public:
00031
00032 StelFader(bool initialState, float minimumValue=0.f, float maximumValue=1.f) : state(initialState), minValue(minimumValue), maxValue(maximumValue) {;}
00033 virtual ~StelFader() {;}
00034
00035 virtual void update(int deltaTicks) = 0;
00036
00037 virtual float getInterstate() const = 0;
00038 virtual float getInterstatePercentage() const = 0;
00039
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
00060 BooleanFader(bool initialState=false, float minimumValue=0.f, float maximumValue=1.f) : StelFader(initialState, minimumValue, maximumValue) {;}
00061 ~BooleanFader() {;}
00062
00063 void update(int deltaTicks) {Q_UNUSED(deltaTicks);}
00064
00065 float getInterstate() const {return state ? maxValue : minValue;}
00066 float getInterstatePercentage() const {return state ? 100.f : 0.f;}
00067
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
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
00092 void update(int deltaTicks)
00093 {
00094 if (!isTransiting) return;
00095 counter+=deltaTicks;
00096 if (counter>=duration)
00097 {
00098
00099 isTransiting = false;
00100 interstate = targetValue;
00101
00102 }
00103 else
00104 {
00105 interstate = startValue + (targetValue - startValue) * counter/duration;
00106 }
00107 }
00108
00109
00110 float getInterstate() const { return interstate;}
00111 float getInterstatePercentage() const {return 100.f * (interstate-minValue)/(maxValue-minValue);}
00112
00113
00114 StelFader& operator=(bool s)
00115 {
00116
00117 if(isTransiting) {
00118
00119 if(s == state) return *this;
00120
00121
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;
00131
00132
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
00159
00160 class ParabolicFader : public StelFader
00161 {
00162 public:
00163
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
00175 void update(int deltaTicks)
00176 {
00177 if (!isTransiting) return;
00178 counter+=deltaTicks;
00179 if (counter>=duration)
00180 {
00181
00182 isTransiting = false;
00183 interstate = targetValue;
00184
00185 }
00186 else
00187 {
00188 interstate = startValue + (targetValue - startValue) * counter/duration;
00189 interstate *= interstate;
00190 }
00191
00192
00193 }
00194
00195
00196 float getInterstate(void) const { return interstate;}
00197 float getInterstatePercentage(void) const {return 100.f * (interstate-minValue)/(maxValue-minValue);}
00198
00199
00200 StelFader& operator=(bool s)
00201 {
00202
00203 if(isTransiting) {
00204
00205 if(s == state) return *this;
00206
00207
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;
00217
00218
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_