00001 #ifndef OglGui_UpDownNumber_h
00002 #define OglGui_UpDownNumber_h
00003
00004
00005 #ifndef OglGui_UpDownButton_h
00006 #include "OglGui/UpDownButton.h"
00007 #endif
00008
00009 #ifndef OglGui_UpDownNumberListener_h
00010 #include "OglGui/UpDownNumberListener.h"
00011 #endif
00012
00013 namespace OglGui
00014 {
00015
00016 class UpDownNumber : public Window, UpDownButtonListener
00017 {
00018 public:
00019
00020 UpDownNumber(Window* parent, int x, int y, int width, int height, int val,
00021 int upDownW=24, bool leftRight=false, int margin=1) :
00022 Window(parent, x, y, width, height)
00023 {
00024 Init(width, height, val, true, upDownW, leftRight, margin);
00025 }
00026
00027 UpDownNumber(Window* parent, int width, int height, int val,
00028 int upDownW=24, bool leftRight=false, int margin=1) :
00029 Window(parent, width, height)
00030 {
00031 Init(width, height, val, true, upDownW, leftRight, margin);
00032 }
00033
00034 UpDownNumber(Window* parent, int x, int y, int width, int height, float val,
00035 int upDownW=24, bool leftRight=false, int margin=1) :
00036 Window(parent, x, y, width, height)
00037 {
00038 Init(width, height, val, false, upDownW, leftRight, margin);
00039 }
00040
00041 UpDownNumber(Window* parent, int width, int height, float val,
00042 int upDownW=24, bool leftRight=false, int margin=1) :
00043 Window(parent, width, height)
00044 {
00045 Init(width, height, val, false, upDownW, leftRight, margin);
00046 }
00047
00048 void SetUpDownNumberListener(UpDownNumberListener* listener,
00049 void* listenerData = 0)
00050 {
00051 mListener = listener;
00052 mListenerData = listenerData;
00053 }
00054 void SetUpDownNumberListener(UpDownNumberListener* listener,
00055 int listenerData)
00056 {
00057 SetUpDownNumberListener(listener, (void *) listenerData);
00058 }
00059
00060
00061 virtual void MouseFunc(int msg, int but, int state, int x, int y)
00062 {
00063 Window::MouseFunc(msg, but, state, x, y);
00064 if (msg == oglMouseWheelUp)
00065 mUpDownBtn->UpButton()->DoButtonSelectionEvent();
00066 if (msg == oglMouseWheelDown)
00067 mUpDownBtn->DownButton()->DoButtonSelectionEvent();
00068 }
00069
00070 virtual void KeyboardFunc(int c, int state)
00071 {
00072 Window::KeyboardFunc(c,state);
00073 if (c==oglUP || c == oglRIGHT)
00074 ChangeValue(mIncrement);
00075 if (c==oglDOWN || c == oglLEFT)
00076 ChangeValue(-mIncrement);
00077 if (c==oglPAGEUP || c==oglPAGEDOWN)
00078 ChangeValue(mIncrement * ((c==oglPAGEUP) ? 5 : -5));
00079 }
00080
00081 void ChangeValue(float delta)
00082 {
00083 SetValue(mValue + delta);
00084 }
00085
00086 virtual void
00087 UpDownButtonSelectionEvent(UpDownButton *src, bool isUpBtn, void* userData)
00088 {
00089 ChangeValue(isUpBtn ? mIncrement : -mIncrement);
00090 }
00091
00092 float Value()
00093 {
00094 return mValue;
00095 }
00096
00097 float GetValue()
00098 {
00099 return mValue;
00100 }
00101
00102 void SetValue(float value)
00103 {
00104 float oldVal = mValue;
00105 char buf[40];
00106
00107 mValue = value;
00108
00109 mValue = (mValue < mMinVal) ? mMinVal : mValue;
00110 mValue = (mValue > mMaxVal) ? mMaxVal : mValue;
00111
00112 sprintf(buf, mAsInt ? "%.0f" : "%.2f", mValue);
00113 mStatTxt->SetText(buf);
00114 if ((oldVal != mValue) && mListener )
00115 mListener->UpDownNumberChangedEvent(this, mListenerData);
00116 }
00117
00118 UpDownButton* UpDownBtn()
00119 {
00120 return mUpDownBtn;
00121 }
00122
00123 StaticText* Txt()
00124 {
00125 return mStatTxt;
00126 }
00127
00128 void SetRange(float minVal, float maxVal)
00129 {
00130 mMinVal = minVal;
00131 mMaxVal = maxVal;
00132 }
00133
00134 void SetIncrement(float inc)
00135 {
00136 mIncrement = inc;
00137 }
00138
00139 private :
00140
00141 UpDownButton* mUpDownBtn;
00142 StaticText* mStatTxt;
00143 UpDownNumberListener* mListener;
00144 void* mListenerData;
00145 bool mAsInt;
00146 float mValue;
00147 float mMinVal;
00148 float mMaxVal;
00149 float mIncrement;
00150
00151 void
00152 Init(int w, int h, float val, bool asInt, int upDownW, bool leftRight,
00153 int margin)
00154 {
00155 char buf[40];
00156 int m = margin;
00157
00158 mListener = 0;
00159 mListenerData = 0;
00160 mValue = val;
00161 mAsInt = asInt;
00162 mIncrement = 1.0f;
00163 mMinVal = -1e10;
00164 mMaxVal = 1e10;
00165
00166 if (m >= w/2)
00167 m = w/2-1;
00168 if (m >= h/2)
00169 m = h/2-1;
00170
00171 mUpDownBtn = new UpDownButton(this, w-m-upDownW, m, upDownW, h-2*m,
00172 leftRight);
00173 mUpDownBtn->SetUpDownButtonListener(this);
00174 mUpDownBtn->SetRepeatMode(true);
00175
00176 sprintf(buf, mAsInt ? "%.0f" : "%.2f", mValue);
00177 mStatTxt = new StaticText(this, m, m, w-2*m-upDownW, h-2*m, buf);
00178 mStatTxt->SetNoMouseInput();
00179
00180 if (margin > 0)
00181 SetBorderType((margin<3) ? BEV_LINE : BEV_ETCHED);
00182
00183 mUpDownBtn->SetDirectionFeedback(true);
00184
00185 ScaleChildren(0,true);
00186
00187
00188 mUpDownBtn->SetAllowScaling(false);
00189 mStatTxt->SetAllowScaling(false);
00190
00191
00192 if (margin>0)
00193 {
00194 mUpDownBtn->ConnectTo(this, T2T | B2B | R2R | TPARENT);
00195 mStatTxt->ConnectTo(this, T2T | B2B | L2L | TPARENT);
00196 }
00197 }
00198 };
00199
00200 }
00201 #endif UpDownNumber_h