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 #include "util/log/logger.h"
00032 
00033 #include "twobutton.h"
00034 
00035 namespace gcn {
00036     static FIFE::Logger _log(LM_GUI);
00037 
00038     TwoButton::TwoButton(Image *up_file , Image *down_file, Image *hover_file, const std::string& caption):
00039         Button(),
00040         m_upImage(up_file),
00041         m_downImage(down_file),
00042         m_hoverImage(hover_file),
00043         x_downoffset(0),
00044         y_downoffset(0) {
00045         m_hoverImage = hover_file;
00046         setFrameSize(0);
00047         adjustSize();
00048         mCaption = caption;
00049     }
00050 
00051     TwoButton::~TwoButton() {
00052     }
00053 
00054     void TwoButton::setDownOffset(int32_t x, int32_t y) {
00055         x_downoffset = x;
00056         y_downoffset = y;
00057     }
00058 
00059     void TwoButton::draw(Graphics *graphics) {
00060         Image* img = m_upImage;
00061         int32_t xoffset = 0;
00062         int32_t yoffset = 0;
00063 
00064         if (isPressed()) {
00065             if( m_downImage ) {
00066                 img = m_downImage;
00067                 xoffset = x_downoffset;
00068                 yoffset = y_downoffset;
00069             }
00070         } else if(mHasMouse) {
00071             if( m_hoverImage ) {
00072                 img = m_hoverImage;
00073             }
00074         }
00075 
00076         if (img) {
00077             graphics->drawImage(img, xoffset, yoffset);
00078         }
00079 
00080         graphics->setColor(getForegroundColor());
00081         int32_t textX;
00082         int32_t textY = getHeight() / 2 - getFont()->getHeight() / 2;
00083         switch (getAlignment())
00084         {
00085             case Graphics::LEFT:
00086                 textX = 4;
00087                 break;
00088             case Graphics::CENTER:
00089                 textX = getWidth() / 2;
00090                 break;
00091             case Graphics::RIGHT:
00092                 textX = getWidth() - 4;
00093                 break;
00094             default:
00095                 textX = 4;
00096                 FL_WARN(_log, FIFE::LMsg("TwoButton::draw() - ") << "Unknown alignment: "
00097                               << getAlignment() << ".  Using the default of Graphics::LEFT");
00098         }
00099 
00100         graphics->setFont(getFont());
00101         if (mCaption.size() > 0) {
00102             if (isPressed())
00103                 graphics->drawText(getCaption(), textX + 1,
00104                         textY + 1, getAlignment());
00105             else
00106                 graphics->drawText(getCaption(), textX, textY, getAlignment());
00107         }
00108     }
00109     void TwoButton::adjustSize() {
00110         int32_t w = 0;
00111         int32_t h = w;
00112         if( m_upImage ) {
00113             w = m_upImage->getWidth();
00114             h = m_upImage->getHeight();
00115         }
00116         if( m_downImage ) {
00117             w = std::max(m_downImage->getWidth(), w);
00118             h = std::max(m_downImage->getHeight(), h);
00119         }
00120         if( m_hoverImage ) {
00121             w = std::max(m_hoverImage->getWidth(), w);
00122             h = std::max(m_hoverImage->getHeight(), h);
00123         }
00124         setWidth(w);
00125         setHeight(h);
00126     }
00127     void TwoButton::setUpImage(Image* image) {
00128         m_upImage = image;
00129         adjustSize();
00130     }
00131     void TwoButton::setDownImage(Image* image) {
00132         m_downImage = image;
00133         adjustSize();
00134     }
00135     void TwoButton::setHoverImage(Image* image) {
00136         m_hoverImage = image;
00137         adjustSize();
00138     }
00139 
00140 }
00141 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */
00142