00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <common/Keyboard.h>
00022 #include <common/DefinesString.h>
00023 #include <GLW/GLWSpinBox.h>
00024 #include <GLW/GLWFont.h>
00025
00026 REGISTER_CLASS_SOURCE(GLWSpinBox);
00027
00028 GLWSpinBox::GLWSpinBox(float x, float y, float w, int start,
00029 int minRange, int maxRange, int step) :
00030 GLWidget(x, y, w, 25.0f), value_(start), minRange_(minRange), maxRange_(maxRange),
00031 top_(x + w - 12.0f, y + 13.0f, 10.0f, 10.0f),
00032 bottom_(x + w - 12.0f, y + 2.0f, 10.0f, 10.0f),
00033 dragging_(false), step_(step)
00034 {
00035 top_.setHandler(this);
00036 bottom_.setHandler(this);
00037 }
00038
00039 GLWSpinBox::~GLWSpinBox()
00040 {
00041
00042 }
00043
00044 void GLWSpinBox::draw()
00045 {
00046 glBegin(GL_LINE_LOOP);
00047 drawShadedRoundBox(x_, y_, w_, h_, 10.0f, false);
00048 glEnd();
00049
00050 top_.draw();
00051 bottom_.draw();
00052
00053 float topOffset = 0.0f;
00054 if(top_.getPressed()) topOffset = 1.0f;
00055
00056 float botOffset = 0.0f;
00057 if(bottom_.getPressed()) botOffset = 1.0f;
00058
00059 glColor3f(0.2f, 0.2f, 0.2f);
00060 glBegin(GL_TRIANGLES);
00061 glVertex2d(x_ + w_ - 11.0f + topOffset, y_ + 15.0f - topOffset);
00062 glVertex2d(x_ + w_ - 4.0f + topOffset, y_ + 15.0f - topOffset);
00063 glVertex2d(x_ + w_ - 7.0f + topOffset, y_ + 21.0f - topOffset);
00064
00065 glVertex2d(x_ + w_ - 4.0f + botOffset, y_ + 9.0f - botOffset);
00066 glVertex2d(x_ + w_ - 10.0f + botOffset, y_ + 9.0f - botOffset);
00067 glVertex2d(x_ + w_ - 7.0f + botOffset, y_ + 4.0f - botOffset);
00068 glEnd();
00069
00070 GLWFont::instance()->getGameFont()->draw(
00071 GLWFont::widgetFontColor, 14,
00072 x_ + 5.0f, y_ + 5.0f, 0.0f,
00073 S3D::formatStringBuffer("%i", value_));
00074 }
00075
00076 void GLWSpinBox::mouseDown(int button, float x, float y, bool &skipRest)
00077 {
00078 top_.mouseDown(button, x, y, skipRest);
00079 if (skipRest) return;
00080
00081 bottom_.mouseDown(button, x, y, skipRest);
00082 if (!skipRest)
00083 {
00084 if (inBox(x, y, x_, y_, w_, h_))
00085 {
00086 dragging_ = true;
00087 skipRest = true;
00088 }
00089 }
00090 }
00091
00092 void GLWSpinBox::mouseUp(int button, float x, float y, bool &skipRest)
00093 {
00094 dragging_ = false;
00095
00096 top_.mouseUp(button, x, y, skipRest);
00097 if (skipRest) return;
00098
00099 bottom_.mouseUp(button, x, y, skipRest);
00100 }
00101
00102 void GLWSpinBox::mouseDrag(int button, float mx, float my, float x, float y, bool &skipRest)
00103 {
00104 if (dragging_)
00105 {
00106 if (y < 0) if (value_ > minRange_) value_ -= step_;
00107 if (y > 0) if (value_ < maxRange_) value_ += step_;
00108 skipRest = true;
00109 return;
00110 }
00111
00112 top_.mouseDrag(button, mx, my, x, y, skipRest);
00113 if (skipRest) return;
00114
00115 bottom_.mouseDrag(button, mx, my, x, y, skipRest);
00116 if (skipRest) return;
00117 }
00118
00119 void GLWSpinBox::keyDown(char *buffer, unsigned int keyState,
00120 KeyboardHistory::HistoryElement *history, int hisCount,
00121 bool &skipRest)
00122 {
00123 for (int i=0; i<hisCount; i++)
00124 {
00125 unsigned int dik = history[i].sdlKey;
00126 switch (dik)
00127 {
00128 case SDLK_UP:
00129 case SDLK_EQUALS:
00130 if (value_ < maxRange_) value_ += step_;
00131 skipRest = true;
00132 break;
00133 case SDLK_DOWN:
00134 case SDLK_MINUS:
00135 if (value_ > minRange_) value_ -= step_;
00136 skipRest = true;
00137 break;
00138 }
00139 }
00140 }
00141
00142 void GLWSpinBox::buttonDown(unsigned int id)
00143 {
00144 if (id == top_.getId())
00145 {
00146 if (value_ < maxRange_) value_ += step_;
00147 }
00148 else if (id == bottom_.getId())
00149 {
00150 if (value_ > minRange_) value_ -= step_;
00151 }
00152 }