button.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/button.hpp"
00062 
00063 #include "guichan/exception.hpp"
00064 #include "guichan/font.hpp"
00065 #include "guichan/graphics.hpp"
00066 #include "guichan/key.hpp"
00067 #include "guichan/mouseinput.hpp"
00068 
00069 namespace gcn
00070 {
00071     Button::Button()
00072     {
00073         mAlignment = Graphics::CENTER;
00074         setFocusable(true);
00075         adjustSize();
00076         setBorderSize(1);
00077 
00078         mMouseDown = false;
00079         mKeyDown = false;
00080 
00081         addMouseListener(this);
00082         addKeyListener(this);
00083     }
00084 
00085     Button::Button(const std::string& caption)
00086     {
00087         mCaption = caption;
00088         mAlignment = Graphics::CENTER;
00089         setFocusable(true);
00090         adjustSize();
00091         setBorderSize(1);
00092 
00093         mMouseDown = false;
00094         mKeyDown = false;
00095 
00096         addMouseListener(this);
00097         addKeyListener(this);
00098     }
00099 
00100     void Button::setCaption(const std::string& caption)
00101     {
00102         mCaption = caption;
00103     }
00104 
00105     const std::string& Button::getCaption() const
00106     {
00107         return mCaption;
00108     }
00109 
00110     void Button::setAlignment(unsigned int alignment)
00111     {
00112         mAlignment = alignment;
00113     }
00114 
00115     unsigned int Button::getAlignment() const
00116     {
00117         return mAlignment;
00118     }
00119 
00120     void Button::draw(Graphics* graphics)
00121     {
00122         Color faceColor = getBaseColor();
00123         Color highlightColor, shadowColor;
00124         int alpha = getBaseColor().a;
00125 
00126         if (isPressed())
00127         {
00128             faceColor = faceColor - 0x303030;
00129             faceColor.a = alpha;
00130             highlightColor = faceColor - 0x303030;
00131             highlightColor.a = alpha;
00132             shadowColor = faceColor + 0x303030;
00133             shadowColor.a = alpha;
00134         }
00135         else
00136         {
00137             highlightColor = faceColor + 0x303030;
00138             highlightColor.a = alpha;
00139             shadowColor = faceColor - 0x303030;
00140             shadowColor.a = alpha;
00141         }
00142 
00143         graphics->setColor(faceColor);
00144         graphics->fillRectangle(Rectangle(1, 1, getDimension().width-1, getHeight() - 1));
00145 
00146         graphics->setColor(highlightColor);
00147         graphics->drawLine(0, 0, getWidth() - 1, 0);
00148         graphics->drawLine(0, 1, 0, getHeight() - 1);
00149 
00150         graphics->setColor(shadowColor);
00151         graphics->drawLine(getWidth() - 1, 1, getWidth() - 1, getHeight() - 1);
00152         graphics->drawLine(1, getHeight() - 1, getWidth() - 1, getHeight() - 1);
00153 
00154         graphics->setColor(getForegroundColor());
00155 
00156         int textX;
00157         int textY = getHeight() / 2 - getFont()->getHeight() / 2;
00158 
00159         switch (getAlignment())
00160         {
00161           case Graphics::LEFT:
00162               textX = 4;
00163               break;
00164           case Graphics::CENTER:
00165               textX = getWidth() / 2;
00166               break;
00167           case Graphics::RIGHT:
00168               textX = getWidth() - 4;
00169               break;
00170           default:
00171               throw GCN_EXCEPTION("Unknown alignment.");
00172         }
00173 
00174         graphics->setFont(getFont());
00175 
00176         if (isPressed())
00177         {
00178             graphics->drawText(getCaption(), textX + 1, textY + 1, getAlignment());
00179         }
00180         else
00181         {
00182             graphics->drawText(getCaption(), textX, textY, getAlignment());
00183 
00184             if (isFocused())
00185             {
00186                 graphics->drawRectangle(Rectangle(2, 2, getWidth() - 4,
00187                                                   getHeight() - 4));
00188             }
00189         }
00190     }
00191 
00192     void Button::drawBorder(Graphics* graphics)
00193     {
00194         Color faceColor = getBaseColor();
00195         Color highlightColor, shadowColor;
00196         int alpha = getBaseColor().a;
00197         int width = getWidth() + getBorderSize() * 2 - 1;
00198         int height = getHeight() + getBorderSize() * 2 - 1;
00199         highlightColor = faceColor + 0x303030;
00200         highlightColor.a = alpha;
00201         shadowColor = faceColor - 0x303030;
00202         shadowColor.a = alpha;
00203 
00204         unsigned int i;
00205         for (i = 0; i < getBorderSize(); ++i)
00206         {
00207             graphics->setColor(shadowColor);
00208             graphics->drawLine(i,i, width - i, i);
00209             graphics->drawLine(i,i + 1, i, height - i - 1);
00210             graphics->setColor(highlightColor);
00211             graphics->drawLine(width - i,i + 1, width - i, height - i);
00212             graphics->drawLine(i,height - i, width - i - 1, height - i);
00213         }
00214     }
00215 
00216     void Button::adjustSize()
00217     {
00218         setWidth(getFont()->getWidth(mCaption) + 8);
00219         setHeight(getFont()->getHeight() + 8);
00220     }
00221 
00222     bool Button::isPressed() const
00223     {
00224         return (hasMouse() && mMouseDown) || mKeyDown;
00225     }
00226 
00227     void Button::mouseClick(int x, int y, int button, int count)
00228     {
00229         if (button == MouseInput::LEFT)
00230         {
00231             generateAction();
00232         }
00233     }
00234 
00235     void Button::mousePress(int x, int y, int button)
00236     {
00237         if (button == MouseInput::LEFT && hasMouse())
00238         {
00239             mMouseDown = true;
00240         }
00241     }
00242 
00243     void Button::mouseRelease(int x, int y, int button)
00244     {
00245         if (button == MouseInput::LEFT)
00246         {
00247             mMouseDown = false;
00248         }
00249     }
00250 
00251     void Button::keyPress(const Key& key)
00252     {
00253         if (key.getValue() == Key::ENTER || key.getValue() == Key::SPACE)
00254         {
00255             mKeyDown = true;
00256         }
00257 
00258         mMouseDown = false;
00259     }
00260 
00261     void Button::keyRelease(const Key& key)
00262     {
00263         if ((key.getValue() == Key::ENTER || key.getValue() == Key::SPACE) && mKeyDown)
00264         {
00265             mKeyDown = false;
00266             generateAction();
00267         }
00268     }
00269 
00270     void Button::lostFocus()
00271     {
00272         mMouseDown = false;
00273         mKeyDown = false;
00274     }
00275 }

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