twobutton.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 #include <cassert>
00024 
00025 // 3rd party library includes
00026 
00027 // FIFE includes
00028 // These includes are split up in two parts, separated by one empty line
00029 // First block: files included from the FIFE root src directory
00030 // Second block: files included from the same folder
00031 
00032 #include "twobutton.h"
00033 
00034 namespace gcn {
00035     TwoButton::TwoButton(Image *up_file , Image *down_file, Image *hover_file, const std::string& caption): 
00036         Button(),
00037         m_upImage(up_file),
00038         m_downImage(down_file),
00039         m_hoverImage(hover_file),
00040         x_downoffset(0),
00041         y_downoffset(0) {
00042         m_hoverImage = hover_file;
00043         setFrameSize(0);
00044         adjustSize();
00045         mCaption = caption;
00046     }
00047     
00048     TwoButton::~TwoButton() {
00049     }
00050     
00051     void TwoButton::setDownOffset(int x, int y) {
00052         x_downoffset = x;
00053         y_downoffset = y;
00054     }
00055     
00056     void TwoButton::draw(Graphics *graphics) {
00057         Image* img = m_upImage;
00058         int xoffset = 0;
00059         int yoffset = 0;
00060         
00061         if (isPressed()) {
00062             if( m_downImage ) {
00063                 img = m_downImage;
00064                 xoffset = x_downoffset;
00065                 yoffset = y_downoffset;
00066             }
00067         } else if(mHasMouse) {
00068             if( m_hoverImage ) {
00069                 img = m_hoverImage;
00070             }
00071         }
00072         
00073         if (img) {
00074             graphics->drawImage(img, xoffset, yoffset);
00075         }
00076 
00077         graphics->setColor(getForegroundColor());
00078         int textX;
00079         int textY = getHeight() / 2 - getFont()->getHeight() / 2;
00080         switch (getAlignment())
00081         {
00082             case Graphics::LEFT:
00083                 textX = 4;
00084                 break;
00085             case Graphics::CENTER:
00086                 textX = getWidth() / 2;
00087                 break;
00088             case Graphics::RIGHT:
00089                 textX = getWidth() - 4;
00090                 break;
00091             default:
00092                 throw GCN_EXCEPTION("Unknown alignment.");
00093         }
00094 
00095         graphics->setFont(getFont());
00096         if (mCaption.size() > 0) {
00097             if (isPressed())
00098                 graphics->drawText(getCaption(), textX + 1, 
00099                         textY + 1, getAlignment());
00100             else
00101                 graphics->drawText(getCaption(), textX, textY, getAlignment());
00102         }
00103     }
00104     void TwoButton::adjustSize() {
00105         int w = 0;
00106         int h = w;
00107         if( m_upImage ) {
00108             w = m_upImage->getWidth();
00109             h = m_upImage->getHeight();
00110         }
00111         if( m_downImage ) {
00112             w = std::max(m_downImage->getWidth(), w);
00113             h = std::max(m_downImage->getHeight(), h);
00114         }
00115         if( m_hoverImage ) {
00116             w = std::max(m_hoverImage->getWidth(), w);
00117             h = std::max(m_hoverImage->getHeight(), h);
00118         }
00119         setWidth(w);
00120         setHeight(h);
00121     }
00122     void TwoButton::setUpImage(Image* image) {
00123         m_upImage = image;
00124         adjustSize();
00125     }
00126     void TwoButton::setDownImage(Image* image) {
00127         m_downImage = image;
00128         adjustSize();
00129     }
00130     void TwoButton::setHoverImage(Image* image) {
00131         m_hoverImage = image;
00132         adjustSize();
00133     }
00134         
00135 }
00136 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */
00137 
Generated by  doxygen 1.6.2-20100208