FIFE
2008.0
|
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 * Note! Group and groupmap borrows heavily from ideas of Guichan library * 00023 * version 0.8.1 * 00024 ***************************************************************************/ 00025 00026 00027 00028 // Standard C++ library includes 00029 #include <cassert> 00030 00031 // 3rd party library includes 00032 00033 // FIFE includes 00034 // These includes are split up in two parts, separated by one empty line 00035 // First block: files included from the FIFE root src directory 00036 // Second block: files included from the same folder 00037 #include <iostream> 00038 00039 #include <guichan/mouseevent.hpp> 00040 00041 #include "togglebutton.h" 00042 00043 00044 namespace gcn { 00045 ToggleButton::GroupMap ToggleButton::m_groupMap; 00046 00047 ToggleButton::ToggleButton(Image *up_file , Image *down_file, Image *hover_file, const std::string& caption, const std::string& group): 00048 Button(), 00049 m_upImage(up_file), 00050 m_downImage(down_file), 00051 m_hoverImage(hover_file), 00052 x_downoffset(0), 00053 y_downoffset(0), 00054 m_group(group) { 00055 00056 m_hoverImage = hover_file; 00057 setFrameSize(0); 00058 setGroup(m_group); 00059 adjustSize(); 00060 mCaption = caption; 00061 m_toggled = false; 00062 00063 addActionListener(this); 00064 } 00065 00066 ToggleButton::~ToggleButton() { 00067 setGroup(""); // Remove button from group 00068 } 00069 00070 void ToggleButton::setDownOffset(int x, int y) { 00071 x_downoffset = x; 00072 y_downoffset = y; 00073 } 00074 00075 void ToggleButton::draw(Graphics *graphics) { 00076 Color faceColor = getBaseColor(); 00077 Color highlightColor; 00078 Color shadowColor; 00079 int alpha = getBaseColor().a; 00080 00081 Image* img = NULL; 00082 int xoffset = 0; 00083 int yoffset = 0; 00084 00085 if (isPressed() || m_toggled) { 00086 faceColor = faceColor - 0x303030; 00087 faceColor.a = alpha; 00088 highlightColor = faceColor - 0x303030; 00089 highlightColor.a = alpha; 00090 shadowColor = faceColor + 0x303030; 00091 shadowColor.a = alpha; 00092 00093 if( m_downImage ) { 00094 img = m_downImage; 00095 xoffset = x_downoffset; 00096 yoffset = y_downoffset; 00097 } 00098 } else if(mHasMouse) { 00099 faceColor = faceColor + 0x303030; 00100 faceColor.a = alpha; 00101 highlightColor = faceColor + 0x303030; 00102 highlightColor.a = alpha; 00103 shadowColor = faceColor - 0x303030; 00104 shadowColor.a = alpha; 00105 00106 if ( m_hoverImage ) { 00107 img = m_hoverImage; 00108 } 00109 } else{ 00110 highlightColor = faceColor + 0x303030; 00111 highlightColor.a = alpha; 00112 shadowColor = faceColor - 0x303030; 00113 shadowColor.a = alpha; 00114 00115 if (m_upImage) { 00116 img = m_upImage; 00117 } 00118 } 00119 00120 00121 graphics->setColor(faceColor); 00122 graphics->fillRectangle(Rectangle(1, 1, getDimension().width-1, getHeight() - 1)); 00123 00124 graphics->setColor(highlightColor); 00125 graphics->drawLine(0, 0, getWidth() - 1, 0); 00126 graphics->drawLine(0, 1, 0, getHeight() - 1); 00127 00128 graphics->setColor(shadowColor); 00129 graphics->drawLine(getWidth() - 1, 1, getWidth() - 1, getHeight() - 1); 00130 graphics->drawLine(1, getHeight() - 1, getWidth() - 1, getHeight() - 1); 00131 00132 graphics->setColor(getForegroundColor()); 00133 00134 if (img) { 00135 graphics->drawImage(img, xoffset, yoffset); 00136 } 00137 00138 int textX; 00139 int textY = getHeight() / 2 - getFont()->getHeight() / 2; 00140 switch (getAlignment()) 00141 { 00142 case Graphics::LEFT: 00143 textX = 4; 00144 break; 00145 case Graphics::CENTER: 00146 textX = getWidth() / 2; 00147 break; 00148 case Graphics::RIGHT: 00149 textX = getWidth() - 4; 00150 break; 00151 default: 00152 throw GCN_EXCEPTION("Unknown alignment."); 00153 } 00154 00155 graphics->setFont(getFont()); 00156 if (mCaption.size() > 0) { 00157 if (isPressed()) 00158 graphics->drawText(getCaption(), textX + 1, 00159 textY + 1, getAlignment()); 00160 else 00161 graphics->drawText(getCaption(), textX, textY, getAlignment()); 00162 } 00163 } 00164 00165 void ToggleButton::action(const ActionEvent& actionEvent) { 00166 setToggled(!m_toggled); 00167 } 00168 00169 void ToggleButton::adjustSize() { 00170 int w = 0; 00171 int h = w; 00172 if( m_upImage ) { 00173 w = m_upImage->getWidth(); 00174 h = m_upImage->getHeight(); 00175 } 00176 if( m_downImage ) { 00177 w = std::max(m_downImage->getWidth(), w); 00178 h = std::max(m_downImage->getHeight(), h); 00179 } 00180 if( m_hoverImage ) { 00181 w = std::max(m_hoverImage->getWidth(), w); 00182 h = std::max(m_hoverImage->getHeight(), h); 00183 } 00184 00185 if( mCaption.length() > 0 ) { 00186 w = std::max(static_cast<int>(getFont()->getWidth(mCaption)+2*mSpacing), w); 00187 h = std::max(static_cast<int>(getFont()->getHeight()+2*mSpacing), h); 00188 } 00189 00190 setWidth(w); 00191 setHeight(h); 00192 } 00193 00194 void ToggleButton::setUpImage(Image* image) { 00195 m_upImage = image; 00196 adjustSize(); 00197 } 00198 00199 void ToggleButton::setDownImage(Image* image) { 00200 m_downImage = image; 00201 adjustSize(); 00202 } 00203 00204 void ToggleButton::setHoverImage(Image* image) { 00205 m_hoverImage = image; 00206 adjustSize(); 00207 } 00208 00209 bool ToggleButton::isToggled() const { 00210 return m_toggled; 00211 } 00212 00213 void ToggleButton::setToggled(bool toggled) { 00214 if (toggled && m_group != "") { 00215 // untoggle all buttons in group 00216 GroupIterator iter, iterEnd; 00217 iterEnd = m_groupMap.upper_bound(m_group); 00218 00219 for (iter = m_groupMap.lower_bound(m_group); iter != iterEnd; iter++) { 00220 if (iter->second->isToggled()) { 00221 iter->second->setToggled(false); 00222 } 00223 } 00224 } 00225 00226 m_toggled = toggled; 00227 } 00228 00229 void ToggleButton::setGroup(const std::string &group) { 00230 // Remove button from previous group 00231 if (m_group != "") { 00232 GroupIterator iter, iterEnd; 00233 iterEnd = m_groupMap.upper_bound(m_group); 00234 00235 for (iter = m_groupMap.lower_bound(m_group); iter != iterEnd; iter++) { 00236 if (iter->second == this) { 00237 m_groupMap.erase(iter); 00238 break; 00239 } 00240 } 00241 } 00242 00243 // Add button to new group 00244 if (group != "") { 00245 m_groupMap.insert( std::pair<std::string, ToggleButton *>(group, this)); 00246 } 00247 00248 m_group = group; 00249 } 00250 00251 const std::string &ToggleButton::getGroup() const { 00252 return m_group; 00253 } 00254 00255 int ToggleButton::getDownXOffset() const { 00256 return x_downoffset; 00257 } 00258 00259 int ToggleButton::getDownYOffset() const { 00260 return y_downoffset; 00261 } 00262 00263 } 00264 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */ 00265