00001 #ifndef _BUFFERED_STATE_H
00002 #define _BUFFERED_STATE_H
00003
00004 #include "Numbers.h"
00005 #include "ClampedNumber.h"
00006
00007 namespace luma
00008 {
00009 namespace numbers
00010 {
00011
00032 template <unsigned int n>
00033 class BufferedState : public UpdateableNumber<unsigned int>
00034 {
00035 private:
00036
00042 class DefaultClampedFloat : public ClampedNumber<float>
00043 {
00044 public:
00048 DefaultClampedFloat(float increment = 0.1f);
00049
00053 void setIncrement(float increment);
00054
00058
00059
00060 };
00061
00062 DefaultClampedFloat mStateValues[n];
00063 float mThresholds[n];
00064 unsigned int mState;
00065
00066 public:
00067
00088 BufferedState(unsigned int initialState, float stateValues[], float thresholds[], float increment);
00089
00095 BufferedState(const BufferedState& other);
00096
00102 void setValue(unsigned int state, float ellapsedTime = 1);
00103
00107 unsigned int getValue() const;
00108
00112 void forceValue(int state);
00113 };
00114
00115 template <unsigned int n>
00116 BufferedState<n>::DefaultClampedFloat::DefaultClampedFloat(float increment):
00117 ClampedNumber<float>(0.0f, 0.0f, 1.0f, increment)
00118 {};
00119
00120 template <unsigned int n>
00121 void BufferedState<n>::DefaultClampedFloat::setIncrement(float increment)
00122 {
00123 modify(mMin, mMax, increment);
00124 }
00125
00126
00127
00128
00129
00130
00131
00132 template <unsigned int n>
00133 BufferedState<n>::BufferedState(unsigned int initialState, float stateValues[], float thresholds[], float increment):
00134 mState(initialState)
00135 {
00136 for (unsigned int i = 0; i < n; i++)
00137 {
00138 mStateValues[i].setIncrement(increment);
00139 mStateValues[i].setValue(stateValues[i]);
00140 mThresholds[i] = thresholds[i];
00141 }
00142 }
00143
00144 template <unsigned int n>
00145 BufferedState<n>::BufferedState(const BufferedState& other):
00146 mState(other.mState)
00147 {
00148 for (unsigned int i = 0; i < n; i++)
00149 {
00150 mStateValues[i] = other.mStateValues[i];
00151 mThresholds[i] = other.mThresholds[i];
00152 }
00153 }
00154
00155 template <unsigned int n>
00156 void BufferedState<n>::setValue(unsigned int state, float ellapsedTime)
00157 {
00158 mStateValues[state].inc(ellapsedTime);
00159
00160 if(mStateValues[state] > mThresholds[state])
00161 mState = state;
00162
00163 for (unsigned int i = 0; i < n; i++)
00164 {
00165 if(i == state)
00166 continue;
00167
00168 mStateValues[i].dec(ellapsedTime);
00169 }
00170 }
00171
00172 template <unsigned int n>
00173 unsigned int BufferedState<n>::getValue() const
00174 {
00175 return mState;
00176 }
00177
00178 template <unsigned int n>
00179 void BufferedState<n>::forceValue(int state)
00180 {
00181 mState = state;
00182
00183 for (int i = 0; i < n; i++)
00184 {
00185 mStateValues[i] = (i == state ? 1.0f : 0.0f);
00186 }
00187 }
00188 };};
00189
00190 #endif //_BUFFERED_N_STATE_H