00001 #ifndef OglGui_Slider_h
00002 #define OglGui_Slider_h
00003
00004 #ifndef OglGui_Window_h
00005 #include "OglGui/Window.h"
00006 #endif
00007
00008 #ifndef OglGui_SliderListener_h
00009 #include "OglGui/SliderListener.h"
00010 #endif
00011
00012 namespace OglGui
00013 {
00014
00015 class Slider : public Window
00016 {
00017 public:
00018
00019 Slider(Window* parent, int w, int h, int minVal, int maxVal, int val,
00020 int borderType=BEV_ETCHED, float roundness=0) :
00021 Window(parent, w, h)
00022 {
00023 Init(minVal, maxVal, val, borderType, roundness);
00024 }
00025
00026 Slider(Window* parent, int x, int y, int w, int h, int minVal, int maxVal,
00027 int val, int borderType=BEV_ETCHED, float roundness=0) :
00028 Window(parent, x, y, w, h)
00029 {
00030 Init(minVal, maxVal, val, borderType, roundness);
00031 }
00032
00033 void
00034 SetSliderListener(SliderListener* listener, void* listenerData = 0)
00035 {
00036 mSliderListener = listener;
00037 mSliderListenerData = listenerData;
00038 }
00039
00040 void HorBackDrawing(int y, int wndW, int wndH) const
00041 {
00042 char buf[25], format[10];
00043 int tW, tH, x, oldTickX = -100;
00044
00045
00046 SetSolidLineColor(oglBLACK);
00047 DrawLine(mThumbW/2, y, wndW - mThumbW/2, y);
00048 SetSolidLineColor(oglWHITE);
00049 DrawLine(mThumbW/2, y - 1, wndW - mThumbW/2, y - 1);
00050
00051 if (mShowTicks && (wndW > mThumbW + 40) &&
00052 (mUpsideDown ? (y - 3 > 0) : (y + 3 < wndH)))
00053 {
00054 for(int i = mMinVal; i < mMaxVal+mTickInterval; i += mTickInterval)
00055 {
00056 int tickLen = 5;
00057
00058 if (i > mMaxVal) i = mMaxVal;
00059
00060 x = (int) (mThumbW/2 + ((i-mMinVal) / (float)(mMaxVal-mMinVal))
00061 * (mOglWnd->width-mThumbW));
00062
00063 if (mShowTickValue &&
00064 (mUpsideDown ? (y-11-tickLen > 0) : (y+16+tickLen < wndH)))
00065 {
00066 sprintf(format, "%%.%df", mPrecisionIdx);
00067 sprintf(buf, format, i / pow(10.0,mPrecisionIdx));
00068
00069
00070
00071
00072 oglSys.GetTextExtent(mOglWnd, buf, &tW, &tH);
00073 if (x - oldTickX > tW + 8)
00074 {
00075 tickLen += 3;
00076 oldTickX = x;
00077 int len = mUpsideDown ? -(tickLen-1+tH) : (tickLen+4);
00078 oglSys.ShadowPrintf(mOglWnd, x - tW/2 - 1, y + len,
00079 oglWHITE, oglBLACK, "%s", buf);
00080 }
00081 }
00082 int y1 = y + (mUpsideDown ? -2 : 2);
00083 int y2 = y1 + (mUpsideDown ? -tickLen : tickLen);
00084 SetSolidLineColor(oglWHITE);
00085 DrawLine(x, y1, x, y2);
00086 SetSolidLineColor(oglDARKGREY);
00087 DrawLine(x-1, y1, x-1, y2);
00088 }
00089 }
00090 }
00091
00092 void VerBackDrawing(int x, int wndW, int wndH) const
00093 {
00094 char buf[25], format[10];
00095 int tW, tH, y, oldTickY = -100;
00096
00097
00098 SetSolidLineColor(oglBLACK);
00099 DrawLine(x, mThumbH/2, x, wndH - mThumbH/2);
00100 SetSolidLineColor(oglWHITE);
00101 DrawLine(x-1, mThumbH/2, x - 1, wndH - mThumbH/2);
00102
00103 if (mShowTicks && (wndH > mThumbH + 40) &&
00104 (mUpsideDown ? (x - 3 > 0) : (x + 3 < wndW)))
00105 {
00106 for(int i = mMinVal; i < mMaxVal+mTickInterval; i += mTickInterval)
00107 {
00108 int tickLen = 5;
00109
00110 if (i > mMaxVal)
00111 i = (int) mMaxVal;
00112
00113 y = (int) (mThumbH/2 + ((i-mMinVal) /
00114 (float) (mMaxVal-mMinVal)) * (wndH-mThumbH));
00115
00116 if (mShowTickValue)
00117 {
00118 sprintf(format, "%%.%df", mPrecisionIdx);
00119 sprintf(buf, format, i / pow(10.0,mPrecisionIdx));
00120
00121 oglSys.GetTextExtent(mOglWnd, buf, &tW, &tH);
00122 if (y - oldTickY > tH/2 + 8)
00123 {
00124 tickLen += 3;
00125 oldTickY = y;
00126 int len = mUpsideDown ? -(tickLen+5+tW) : (tickLen+2);
00127 oglSys.ShadowPrintf(mOglWnd, x + len, y - 5, oglWHITE,
00128 oglBLACK, "%s", buf);
00129
00130 }
00131 }
00132 int x1 = x + (mUpsideDown ? -2 : 0);
00133 int x2 = x1 + (mUpsideDown ? -tickLen : tickLen);
00134 SetSolidLineColor(oglWHITE);
00135 DrawLine(x1, y, x2, y);
00136 SetSolidLineColor(oglDARKGREY);
00137 DrawLine(x1, y-1, x2, y-1);
00138 }
00139 }
00140 }
00141
00142
00143
00144 virtual void DisplayFunc()
00145 {
00146 int wndW = mOglWnd->width, wndH = mOglWnd->height;
00147 int x, y, tW, tH;
00148 INT bInfo[3];
00149 char buf[25], format[10];
00150
00151 Window::DisplayFunc();
00152
00153 OGC oldOGC;
00154
00155 if (GetState() == 0) return;
00156 OGCSave(&oldOGC);
00157
00158 if (mTickInterval < 1)
00159 mTickInterval = 1;
00160
00161 if (mHorizontal){
00162 x = (int) (mThumbW/2 + ((mVal-mMinVal) / (float) (mMaxVal-mMinVal))
00163 * (wndW-mThumbW));
00164 y = (int) (mUpsideDown ? wndH - mThumbH / 2 - 2: mThumbH / 2 + 2);
00165 HorBackDrawing(y, wndW, wndH);
00166 }
00167 else {
00168 y = (int) (mThumbH/2 + ((mVal-mMinVal) / (float) (mMaxVal-mMinVal))
00169 * (wndH-mThumbH));
00170 x = (int) (mUpsideDown ? wndW - mThumbW / 2: mThumbW / 2);
00171 VerBackDrawing(x, wndW, wndH);
00172 }
00173
00174 if (mDrawBlendedThumb) oglSys.StartBlend(bInfo);
00175
00176 if (mShowValNeedle){
00177 SetSolidLineColor(oglTrDARKGREY);
00178 if (mHorizontal)
00179 DrawLine(x, 3, x, wndH - 2);
00180 else
00181 DrawLine(3, y, wndW - 2, y);
00182 }
00183
00184 if (mShowVal){
00185 ULONG col = mIsPressed ? oglTrGREY : oglTrLIGHTGREY;
00186 col |= (GetState()==2) ? mStateFeedbackColor : 0;
00187 SetSolidFillColor(col);
00188
00189 if (mThumbR != 0.0f){
00190 FillRoundRectExt(x-mThumbW/2, y-mThumbH/2, mThumbW, mThumbH,
00191 mThumbR, mThumbR, mThumbR, mThumbR);
00192 DrawBevRoundRectExt(mIsPressed ? BEV_SUNKEN : BEV_RAISED,
00193 x-mThumbW/2, y-mThumbH/2, mThumbW, mThumbH,
00194 mThumbR, mThumbR, mThumbR, mThumbR);
00195 }
00196 else {
00197 FillRectangle(x - mThumbW/2, y - mThumbH/2, mThumbW, mThumbH);
00198 DrawBevRect(mIsPressed ? BEV_SUNKEN : BEV_RAISED,
00199 x-mThumbW/2, y - mThumbH/2, mThumbW, mThumbH);
00200 }
00201 }
00202
00203 if (mDrawBlendedThumb)
00204 oglSys.EndBlend(bInfo);
00205
00206 if (mShowVal)
00207 {
00208 sprintf(format, "%%.%df", mPrecisionIdx);
00209 sprintf(buf, format, mVal / pow(10.0,mPrecisionIdx));
00210 oglSys.GetTextExtent(mOglWnd, buf, &tW, &tH);
00211 oglSys.ShadowPrintf(mOglWnd, x - tW/2 - 1, y - tH/2 + 2,
00212 oglWHITE, oglBLACK, "%s", buf);
00213 }
00214
00215 OGCRestore(&oldOGC);
00216 }
00217
00218
00219 void HandleChange(int x, int y)
00220 {
00221 float nVal;
00222
00223 if (mHorizontal)
00224 {
00225 x -= mThumbW/2;
00226
00227 nVal = mMinVal + (x / (float) (mOglWnd->width-mThumbW))
00228 * (mMaxVal-mMinVal);
00229 if (nVal > mMaxVal) nVal = mMaxVal;
00230 if (nVal < mMinVal) nVal = mMinVal;
00231 if (nVal == mVal)
00232 return;
00233 }
00234 else
00235 {
00236 y -= mThumbH/2;
00237
00238 nVal = mMinVal + (y / (float) (mOglWnd->height-mThumbH))
00239 * (mMaxVal-mMinVal);
00240 if (nVal > mMaxVal) nVal = mMaxVal;
00241 if (nVal < mMinVal) nVal = mMinVal;
00242 if (nVal == mVal)
00243 return;
00244 }
00245 mVal = (int) nVal;
00246 if (mSliderListener)
00247 mSliderListener->SliderChangedEvent(this, mSliderListenerData);
00248 }
00249
00250 virtual void MouseFunc(INT msg, INT but, INT state, INT x, INT y)
00251 {
00252 Window::MouseFunc(msg, but, state, x, y);
00253
00254 if ((state & (oglAlt | oglControl | oglShift)) != 0) return;
00255
00256 if (msg == oglMouseWheelUp)
00257 SetValue(mVal - mIncrement);
00258 if (msg == oglMouseWheelDown)
00259 SetValue(mVal + mIncrement);
00260
00261 if ((msg == oglMouseDown) && (but == oglLeftButton)){
00262 mIsPressed = true;
00263 HandleChange(x, y);
00264 }
00265
00266
00267
00268
00269
00270
00271 if (msg == oglMouseMove)
00272 HandleChange(x, y);
00273
00274 if (msg == oglMouseUp)
00275 mIsPressed = false;
00276 }
00277
00278 virtual void KeyboardFunc(INT c, INT state)
00279 {
00280 Window::KeyboardFunc(c, state);
00281
00282
00283 if (c == 'a') mAutoDirection = !mAutoDirection;
00284 if (c == 'u') mUpsideDown = !mUpsideDown;
00285 if (c == 'h') mHorizontal = !mHorizontal;
00286
00287 if (c == oglLEFT || c== oglRIGHT || c == oglUP || c == oglDOWN)
00288 SetValue(mVal+((c==oglLEFT||c==oglUP) ? -mIncrement : mIncrement));
00289 if (c == oglPAGEUP || c == oglPAGEDOWN)
00290 SetValue(mVal+((c==oglPAGEUP) ? -mPageIncrement : mPageIncrement));
00291 if (c == oglHOME || c == oglEND)
00292 SetValue((c==oglHOME) ? mMinVal : mMaxVal);
00293
00294
00295
00296
00297 }
00298
00299 virtual void ViewportFunc(INT width, INT height)
00300 {
00301 Window::ViewportFunc(width, height);
00302 if (mAutoDirection)
00303 mHorizontal = width > height;
00304 }
00305
00306
00307 int Value() const
00308 {
00309 return mVal;
00310 }
00311
00312 int GetValue() const
00313 {
00314 return mVal;
00315 }
00316
00317 void SetValue(int val)
00318 {
00319 if (val < mMinVal)
00320 val = mMinVal;
00321 if (val > mMaxVal)
00322 val = mMaxVal;
00323 if (val == mVal)
00324 return;
00325
00326 mVal = val;
00327 if (mSliderListener)
00328 mSliderListener->SliderChangedEvent(this, mSliderListenerData);
00329 }
00330
00331 void SetMinMax(int min, int max)
00332 {
00333 if (min!=RETAIN)
00334 mMinVal = min;
00335 if (max!=RETAIN)
00336 mMaxVal = max;
00337 }
00338
00339 void GetMinMax(int& min, int& max)
00340 {
00341 min = mMinVal;
00342 max = mMaxVal;
00343 }
00344
00345 void SetHorizontal(bool hor=true)
00346 {
00347 mHorizontal = hor;
00348 }
00349
00350 bool IsHorizontal() const
00351 {
00352 return mHorizontal;
00353 }
00354
00355 void SetShowValNeedle(bool showNeedle=true)
00356 {
00357 mShowValNeedle = showNeedle;
00358 }
00359
00360 bool GetShowValNeedle() const
00361 {
00362 return mShowValNeedle;
00363 }
00364
00365 void SetIncrement(int inc)
00366 {
00367 mIncrement = inc;
00368 }
00369
00370 int GetIncrement() const
00371 {
00372 return mIncrement;
00373 }
00374
00375 void SetPageIncrement(int inc)
00376 {
00377 mPageIncrement = inc;
00378 }
00379
00380 int GetPageIncrement() const
00381 {
00382 return mPageIncrement;
00383 }
00384
00385 void SetThumbSizes(int w, int h)
00386 {
00387 if (w != RETAIN) mThumbW = w;
00388 if (h != RETAIN) mThumbH = h;
00389 }
00390
00391 void GetThumbSizes(int& w, int& h) const
00392 {
00393 w = mThumbW;
00394 h = mThumbH;
00395 }
00396
00397 void SetThumbRoundness(float r)
00398 {
00399 mThumbR = r;
00400 }
00401
00402 float GetThumbRoundness() const
00403 {
00404 return mThumbR;
00405 }
00406
00407 void SetShowValue(bool doShow=true)
00408 {
00409 mShowVal = doShow;
00410 }
00411
00412 bool GetShowValue() const
00413 {
00414 return mShowVal;
00415 }
00416
00417 void SetPrecisionIdx(int n)
00418 {
00419 mPrecisionIdx = n;
00420 }
00421
00422 void SetShowValPerc(bool doShow=true)
00423 {
00424 mShowValPerc = doShow;
00425 mPrecisionIdx = doShow ? 2 : 0;
00426 }
00427
00428 bool GetShowValPerc() const
00429 {
00430 return mShowValPerc;
00431 }
00432
00433 void SetShowTicks(bool doShow=true)
00434 {
00435 mShowTicks = doShow;
00436 }
00437
00438 bool GetShowTicks() const
00439 {
00440 return mShowTicks;
00441 }
00442
00443 void SetFlipOver(bool mode=true)
00444 {
00445 mUpsideDown = mode;
00446 }
00447
00448 void SetTickInterval(int inc)
00449 {
00450 if (inc == 0)
00451 inc = 1;
00452 mTickInterval = inc;
00453 }
00454
00455 int GetTickInterval() const
00456 {
00457 return mTickInterval;
00458 }
00459
00460 void SetDrawBlendedThumb(bool doBlend=true)
00461 {
00462 mDrawBlendedThumb = doBlend;
00463 }
00464
00465 bool GetDrawBlendedThumb() const
00466 {
00467 return mDrawBlendedThumb;
00468 }
00469
00470 private:
00471
00472 void Init(int minVal, int maxVal, int val, int borderType, float r)
00473 {
00474 SetBorderType(borderType);
00475 if (r != 0.0f)
00476 SetRoundness(r, r, r, r);
00477
00478 mHorizontal = true;
00479 mAutoDirection = true;
00480 mMinVal = minVal;
00481 mMaxVal = maxVal;
00482 mVal = val;
00483 mIncrement = 1;
00484 mPageIncrement = 10;
00485 mIsPressed = false;
00486 mDrawBlendedThumb = false;
00487 mThumbW = 32;
00488 mThumbH = 16;
00489 mThumbR = 10.0f;
00490 mPrecisionIdx = 2;
00491 mShowValPerc = false;
00492 mShowVal = true;
00493 mShowValNeedle = true;
00494 mUpsideDown = false;
00495 mShowTicks = true;
00496 mShowTickValue = true;
00497 mTickInterval = (maxVal-minVal) / 10;
00498 mSliderListener = 0;
00499
00500 SetDisableOGLViewKeys(true);
00501 SetDisableOGLViewMouse(true);
00502 }
00503
00504 SliderListener* mSliderListener;
00505 void* mSliderListenerData;
00506 int mMinVal, mMaxVal, mVal;
00507 int mIncrement;
00508 int mPageIncrement;
00509 int mPrecisionIdx;
00510 int mThumbW;
00511 int mThumbH;
00512 float mThumbR;
00513 bool mAutoDirection;
00514 bool mHorizontal;
00515 bool mIsPressed;
00516 bool mShowValPerc;
00517 bool mShowVal;
00518 bool mShowValNeedle;
00519 bool mUpsideDown;
00520 bool mDrawBlendedThumb;
00521 bool mShowTicks;
00522 bool mShowTickValue;
00523 int mTickInterval;
00524 };
00525
00526 }
00527 #endif