00001 #ifndef _BUFFERED_STEP_H
00002 #define _BUFFERED_STEP_H
00003
00004 #include "ClampedNumber.h"
00005 namespace luma
00006 {
00007 namespace numbers
00008 {
00009
00030 template <unsigned int n>
00031 class BufferedStep
00032 {
00033 private:
00034 float mUpwardsThresholds[n - 1];
00035 float mDownwardsThresholds[n - 1];
00036 ClampedNumber<int> mState;
00037 ClampedNumber<float> mFloatValue;
00038
00039 int static indexFromState(int n) { return n > 1 ? n - 1 : 0;};
00040
00041 public:
00042 BufferedStep(float mMin, float mMax, float upwardsThresholds[n - 1], float downwardsThresholds[n - 1], float interval);
00043 unsigned int getState() const;
00044 void setStateUp(bool up);
00045 void forceMin();
00046 void forceMax();
00047
00048 };
00049
00050 template <unsigned int n>
00051 BufferedStep<n>::BufferedStep(float min, float max, float upwardsThresholds[n - 1], float downwardsThresholds[n - 1], float interval):
00052 mFloatValue(min, min, max, interval),
00053 mState(0, 0, n, 1)
00054 {
00055 for(int i = 0; i < n - 1; i++)
00056 {
00057 mUpwardsThresholds[i] = upwardsThresholds[i];
00058 mDownwardsThresholds[i] = downwardsThresholds[i];
00059 }
00060 }
00061
00062 template <unsigned int n>
00063 unsigned int BufferedStep<n>::getState() const
00064 {
00065 return mState;
00066 }
00067
00068 template <unsigned int n>
00069 void BufferedStep<n>::setStateUp(bool up)
00070 {
00071 if(up)
00072 {
00073 mFloatValue++;
00074
00075 if((float) mFloatValue > mUpwardsThresholds[mState.operator int()])
00076 {
00077 mState++;
00078 }
00079 }
00080 else
00081 {
00082 mFloatValue--;
00083
00084 if((float) mFloatValue < mDownwardsThresholds[indexFromState(mState)])
00085 {
00086 mState--;
00087 }
00088 }
00089 }
00090
00091 template <unsigned int n>
00092 void BufferedStep<n>::forceMin()
00093 {
00094 mFloatValue.setValue(mFloatValue.min());
00095 mState.setValue(0);
00096 }
00097
00098 template <unsigned int n>
00099 void BufferedStep<n>::forceMax()
00100 {
00101 mFloatValue.setValue(mFloatValue.max());
00102 mState.setValue(n - 1);
00103 }
00104
00105 };};
00106 #endif//_BUFFERED_STEP_H