00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 #include "guichan/widgets/slider.hpp"
00062
00063 #include "guichan/graphics.hpp"
00064 #include "guichan/key.hpp"
00065 #include "guichan/mouseinput.hpp"
00066
00067 namespace gcn
00068 {
00069 Slider::Slider(double scaleEnd)
00070 {
00071 mMouseDrag = false;
00072
00073
00074 mScaleStart = 0;
00075 mScaleEnd = scaleEnd;
00076
00077 setFocusable(true);
00078 setBorderSize(1);
00079 setOrientation(HORIZONTAL);
00080 setValue(0);
00081 setStepLength(scaleEnd / 10);
00082 setMarkerLength(10);
00083
00084 addMouseListener(this);
00085 addKeyListener(this);
00086 }
00087
00088 Slider::Slider(double scaleStart, double scaleEnd)
00089 {
00090 mMouseDrag = false;
00091
00092 mScaleStart = scaleStart;
00093 mScaleEnd = scaleEnd;
00094
00095 setFocusable(true);
00096 setBorderSize(1);
00097 setOrientation(HORIZONTAL);
00098 setValue(scaleStart);
00099 setStepLength((scaleEnd - scaleStart)/ 10);
00100 setMarkerLength(10);
00101
00102 addMouseListener(this);
00103 addKeyListener(this);
00104 }
00105
00106 void Slider::setScale(double scaleStart, double scaleEnd)
00107 {
00108 mScaleStart = scaleStart;
00109 mScaleEnd = scaleEnd;
00110 }
00111
00112 double Slider::getScaleStart() const
00113 {
00114 return mScaleStart;
00115 }
00116
00117 void Slider::setScaleStart(double scaleStart)
00118 {
00119 mScaleStart = scaleStart;
00120 }
00121
00122 double Slider::getScaleEnd() const
00123 {
00124 return mScaleEnd;
00125 }
00126
00127 void Slider::setScaleEnd(double scaleEnd)
00128 {
00129 mScaleEnd = scaleEnd;
00130 }
00131
00132 void Slider::draw(gcn::Graphics* graphics)
00133 {
00134 Color shadowColor = getBaseColor() - 0x101010;
00135 int alpha = getBaseColor().a;
00136 shadowColor.a = alpha;
00137
00138 graphics->setColor(shadowColor);
00139 graphics->fillRectangle(gcn::Rectangle(0,0,getWidth(),getHeight()));
00140
00141 drawMarker(graphics);
00142 }
00143
00144 void Slider::drawBorder(gcn::Graphics* graphics)
00145 {
00146 Color faceColor = getBaseColor();
00147 Color highlightColor, shadowColor;
00148 int alpha = getBaseColor().a;
00149 int width = getWidth() + getBorderSize() * 2 - 1;
00150 int height = getHeight() + getBorderSize() * 2 - 1;
00151 highlightColor = faceColor + 0x303030;
00152 highlightColor.a = alpha;
00153 shadowColor = faceColor - 0x303030;
00154 shadowColor.a = alpha;
00155
00156 unsigned int i;
00157 for (i = 0; i < getBorderSize(); ++i)
00158 {
00159 graphics->setColor(shadowColor);
00160 graphics->drawLine(i,i, width - i, i);
00161 graphics->drawLine(i,i + 1, i, height - i - 1);
00162 graphics->setColor(highlightColor);
00163 graphics->drawLine(width - i,i + 1, width - i, height - i);
00164 graphics->drawLine(i,height - i, width - i - 1, height - i);
00165 }
00166 }
00167
00168 void Slider::drawMarker(gcn::Graphics* graphics)
00169 {
00170 gcn::Color faceColor = getBaseColor();
00171 Color highlightColor, shadowColor;
00172 int alpha = getBaseColor().a;
00173 highlightColor = faceColor + 0x303030;
00174 highlightColor.a = alpha;
00175 shadowColor = faceColor - 0x303030;
00176 shadowColor.a = alpha;
00177
00178 graphics->setColor(faceColor);
00179
00180 if (getOrientation() == HORIZONTAL)
00181 {
00182 int v = getMarkerPosition();
00183 graphics->fillRectangle(gcn::Rectangle(v + 1, 1, getMarkerLength() - 2, getHeight() - 2));
00184 graphics->setColor(highlightColor);
00185 graphics->drawLine(v, 0, v + getMarkerLength() - 1,0);
00186 graphics->drawLine(v, 0, v, getHeight() - 1);
00187 graphics->setColor(shadowColor);
00188 graphics->drawLine(v + getMarkerLength() - 1, 1, v + getMarkerLength() - 1, getHeight() - 1);
00189 graphics->drawLine(v + 1, getHeight() - 1, v + getMarkerLength() - 1, getHeight() - 1);
00190
00191 if (isFocused())
00192 {
00193 graphics->setColor(getForegroundColor());
00194 graphics->drawRectangle(Rectangle(v + 2, 2, getMarkerLength() - 4, getHeight() - 4));
00195 }
00196 }
00197 else
00198 {
00199 int v = (getHeight() - getMarkerLength()) - getMarkerPosition();
00200 graphics->fillRectangle(gcn::Rectangle(1, v + 1, getWidth() - 2, getMarkerLength() - 2));
00201 graphics->setColor(highlightColor);
00202 graphics->drawLine(0, v, 0, v + getMarkerLength() - 1);
00203 graphics->drawLine(0, v, getWidth() - 1, v);
00204 graphics->setColor(shadowColor);
00205 graphics->drawLine(1, v + getMarkerLength() - 1, getWidth() - 1, v + getMarkerLength() - 1);
00206 graphics->drawLine(getWidth() - 1, v + 1, getWidth() - 1, v + getMarkerLength() - 1);
00207
00208 if (isFocused())
00209 {
00210 graphics->setColor(getForegroundColor());
00211 graphics->drawRectangle(Rectangle(2, v + 2, getWidth() - 4, getMarkerLength() - 4));
00212 }
00213 }
00214 }
00215
00216 void Slider::mousePress(int x, int y, int button)
00217 {
00218 if (button == gcn::MouseInput::LEFT
00219 && x >= 0 && x <= getWidth()
00220 && y >= 0 && y <= getHeight())
00221 {
00222 if (getOrientation() == HORIZONTAL)
00223 {
00224 setValue(markerPositionToValue(x - getMarkerLength() / 2));
00225 }
00226 else
00227 {
00228 setValue(markerPositionToValue(getHeight() - y - getMarkerLength() / 2));
00229 }
00230
00231 mMouseDrag = true;
00232 generateAction();
00233 }
00234 else
00235 {
00236 mMouseDrag = false;
00237 }
00238 }
00239
00240 void Slider::mouseRelease(int x, int y, int button)
00241 {
00242 mMouseDrag = false;
00243 }
00244
00245 void Slider::lostFocus()
00246 {
00247 mMouseDrag = false;
00248 }
00249
00250 void Slider::mouseMotion(int x, int y)
00251 {
00252 if (mMouseDrag)
00253 {
00254 if (getOrientation() == HORIZONTAL)
00255 {
00256 setValue(markerPositionToValue(x - getMarkerLength() / 2));
00257 }
00258 else
00259 {
00260 setValue(markerPositionToValue(getHeight() - y - getMarkerLength() / 2));
00261 }
00262
00263 generateAction();
00264 }
00265 }
00266
00267 void Slider::setValue(double value)
00268 {
00269 if (value > getScaleEnd())
00270 {
00271 mValue = getScaleEnd();
00272 return;
00273 }
00274
00275 if (value < getScaleStart())
00276 {
00277 mValue = getScaleStart();
00278 return;
00279 }
00280
00281 mValue = value;
00282 }
00283
00284 double Slider::getValue() const
00285 {
00286 return mValue;
00287 }
00288
00289 int Slider::getMarkerLength() const
00290 {
00291 return mMarkerLength;
00292 }
00293
00294 void Slider::setMarkerLength(int length)
00295 {
00296 mMarkerLength = length;
00297 }
00298
00299 void Slider::keyPress(const Key& key)
00300 {
00301 if (getOrientation() == HORIZONTAL)
00302 {
00303 if (key.getValue() == Key::RIGHT)
00304 {
00305 setValue(getValue() + getStepLength());
00306 generateAction();
00307 }
00308 else if (key.getValue() == Key::LEFT)
00309 {
00310 setValue(getValue() - getStepLength());
00311 generateAction();
00312 }
00313 }
00314 else
00315 {
00316 if (key.getValue() == Key::UP)
00317 {
00318 setValue(getValue() + getStepLength());
00319 generateAction();
00320 }
00321 else if (key.getValue() == Key::DOWN)
00322 {
00323 setValue(getValue() - getStepLength());
00324 generateAction();
00325 }
00326 }
00327 }
00328
00329 void Slider::setOrientation(unsigned int orientation)
00330 {
00331 mOrientation = orientation;
00332 }
00333
00334 unsigned int Slider::getOrientation() const
00335 {
00336 return mOrientation;
00337 }
00338
00339 double Slider::markerPositionToValue(int v) const
00340 {
00341 int w;
00342 if (getOrientation() == HORIZONTAL)
00343 {
00344 w = getWidth();
00345 }
00346 else
00347 {
00348 w = getHeight();
00349 }
00350
00351 double pos = v / ((double)w - getMarkerLength());
00352 return (1.0 - pos) * getScaleStart() + pos * getScaleEnd();
00353
00354 }
00355
00356 int Slider::valueToMarkerPosition(double value) const
00357 {
00358 int v;
00359 if (getOrientation() == HORIZONTAL)
00360 {
00361 v = getWidth();
00362 }
00363 else
00364 {
00365 v = getHeight();
00366 }
00367
00368 int w = (int)((v - getMarkerLength())
00369 * (value - getScaleStart())
00370 / (getScaleEnd() - getScaleStart()));
00371
00372 if (w < 0)
00373 {
00374 return 0;
00375 }
00376
00377 if (w > v - getMarkerLength())
00378 {
00379 return v - getMarkerLength();
00380 }
00381
00382 return w;
00383 }
00384
00385 void Slider::setStepLength(double length)
00386 {
00387 mStepLength = length;
00388 }
00389
00390 double Slider::getStepLength() const
00391 {
00392 return mStepLength;
00393 }
00394
00395 int Slider::getMarkerPosition() const
00396 {
00397 return valueToMarkerPosition(getValue());
00398 }
00399
00400 void Slider::mouseWheelUp(int x, int y)
00401 {
00402 setValue(getValue() + getStepLength());
00403 generateAction();
00404 }
00405
00406 void Slider::mouseWheelDown(int x, int y)
00407 {
00408 setValue(getValue() - getStepLength());
00409 generateAction();
00410 }
00411 }