slider.cpp

00001 /*      _______   __   __   __   ______   __   __   _______   __   __
00002  *     / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___  /\ /  |\/ /\
00003  *    / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
00004  *   / / /__   / / // / // / // / /    / ___  / // ___  / // /| ' / /
00005  *  / /_// /\ / /_// / // / // /_/_   / / // / // /\_/ / // / |  / /
00006  * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
00007  * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
00008  *
00009  * Copyright (c) 2004, 2005, 2006 Olof Naessén and Per Larsson
00010  *
00011  *                                                         Js_./
00012  * Per Larsson a.k.a finalman                          _RqZ{a<^_aa
00013  * Olof Naessén a.k.a jansem/yakslem                _asww7!uY`>  )\a//
00014  *                                                 _Qhm`] _f "'c  1!5m
00015  * Visit: http://guichan.darkbits.org             )Qk<P ` _: :+' .'  "{[
00016  *                                               .)j(] .d_/ '-(  P .   S
00017  * License: (BSD)                                <Td/Z <fP"5(\"??"\a.  .L
00018  * Redistribution and use in source and          _dV>ws?a-?'      ._/L  #'
00019  * binary forms, with or without                 )4d[#7r, .   '     )d`)[
00020  * modification, are permitted provided         _Q-5'5W..j/?'   -?!\)cam'
00021  * that the following conditions are met:       j<<WP+k/);.        _W=j f
00022  * 1. Redistributions of source code must       .$%w\/]Q  . ."'  .  mj$
00023  *    retain the above copyright notice,        ]E.pYY(Q]>.   a     J@\
00024  *    this list of conditions and the           j(]1u<sE"L,. .   ./^ ]{a
00025  *    following disclaimer.                     4'_uomm\.  )L);-4     (3=
00026  * 2. Redistributions in binary form must        )_]X{Z('a_"a7'<a"a,  ]"[
00027  *    reproduce the above copyright notice,       #}<]m7`Za??4,P-"'7. ).m
00028  *    this list of conditions and the            ]d2e)Q(<Q(  ?94   b-  LQ/
00029  *    following disclaimer in the                <B!</]C)d_, '(<' .f. =C+m
00030  *    documentation and/or other materials      .Z!=J ]e []('-4f _ ) -.)m]'
00031  *    provided with the distribution.          .w[5]' _[ /.)_-"+?   _/ <W"
00032  * 3. Neither the name of Guichan nor the      :$we` _! + _/ .        j?
00033  *    names of its contributors may be used     =3)= _f  (_yQmWW$#(    "
00034  *    to endorse or promote products derived     -   W,  sQQQQmZQ#Wwa]..
00035  *    from this software without specific        (js, \[QQW$QWW#?!V"".
00036  *    prior written permission.                    ]y:.<\..          .
00037  *                                                 -]n w/ '         [.
00038  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT       )/ )/           !
00039  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY         <  (; sac    ,    '
00040  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,               ]^ .-  %
00041  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF            c <   r
00042  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR            aga<  <La
00043  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE          5%  )P'-3L
00044  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR        _bQf` y`..)a
00045  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,          ,J?4P'.P"_(\?d'.,
00046  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES               _Pa,)!f/<[]/  ?"
00047  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT      _2-..:. .r+_,.. .
00048  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,     ?a.<%"'  " -'.a_ _,
00049  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION)                     ^
00050  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00051  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00052  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00053  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00054  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00055  */
00056 
00057 /*
00058  * For comments regarding functions please see the header file.
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 }

Generated on Sat Jul 29 19:38:48 2006 for Guichan by  doxygen 1.4.7