FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
togglebutton.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 /***************************************************************************
22  * Note! Group and groupmap borrows heavily from ideas of Guichan library *
23  * version 0.8.1 *
24  ***************************************************************************/
25 
26 
27 
28 // Standard C++ library includes
29 #include <cassert>
30 #include <iostream>
31 
32 // 3rd party library includes
33 #include <guichan/mouseevent.hpp>
34 
35 // FIFE includes
36 // These includes are split up in two parts, separated by one empty line
37 // First block: files included from the FIFE root src directory
38 // Second block: files included from the same folder
39 #include "util/log/logger.h"
40 
41 #include "togglebutton.h"
42 
43 namespace gcn {
44  static FIFE::Logger _log(LM_GUI);
45 
47 
48  ToggleButton::ToggleButton(Image *up_file , Image *down_file, Image *hover_file, const std::string& caption, const std::string& group):
49  Button(),
50  m_upImage(up_file),
51  m_downImage(down_file),
52  m_hoverImage(hover_file),
53  x_downoffset(0),
54  y_downoffset(0),
55  m_group(group) {
56 
57  m_hoverImage = hover_file;
58  setFrameSize(0);
60  adjustSize();
61  mCaption = caption;
62  m_toggled = false;
63 
64  addActionListener(this);
65  }
66 
68  setGroup(""); // Remove button from group
69  }
70 
71  void ToggleButton::setDownOffset(int32_t x, int32_t y) {
72  x_downoffset = x;
73  y_downoffset = y;
74  }
75 
76  void ToggleButton::draw(Graphics *graphics) {
77  Color faceColor = getBaseColor();
78  Color highlightColor;
79  Color shadowColor;
80  int32_t alpha = getBaseColor().a;
81 
82  Image* img = NULL;
83  int32_t xoffset = 0;
84  int32_t yoffset = 0;
85 
86  if (isPressed() || m_toggled) {
87  faceColor = faceColor - 0x303030;
88  faceColor.a = alpha;
89  highlightColor = faceColor - 0x303030;
90  highlightColor.a = alpha;
91  shadowColor = faceColor + 0x303030;
92  shadowColor.a = alpha;
93 
94  if( m_downImage ) {
95  img = m_downImage;
96  xoffset = x_downoffset;
97  yoffset = y_downoffset;
98  }
99  } else if(mHasMouse) {
100  faceColor = faceColor + 0x303030;
101  faceColor.a = alpha;
102  highlightColor = faceColor + 0x303030;
103  highlightColor.a = alpha;
104  shadowColor = faceColor - 0x303030;
105  shadowColor.a = alpha;
106 
107  if ( m_hoverImage ) {
108  img = m_hoverImage;
109  }
110  } else{
111  highlightColor = faceColor + 0x303030;
112  highlightColor.a = alpha;
113  shadowColor = faceColor - 0x303030;
114  shadowColor.a = alpha;
115 
116  if (m_upImage) {
117  img = m_upImage;
118  }
119  }
120 
121 
122  graphics->setColor(faceColor);
123  graphics->fillRectangle(Rectangle(1, 1, getDimension().width-1, getHeight() - 1));
124 
125  graphics->setColor(highlightColor);
126  graphics->drawLine(0, 0, getWidth() - 1, 0);
127  graphics->drawLine(0, 1, 0, getHeight() - 1);
128 
129  graphics->setColor(shadowColor);
130  graphics->drawLine(getWidth() - 1, 1, getWidth() - 1, getHeight() - 1);
131  graphics->drawLine(1, getHeight() - 1, getWidth() - 1, getHeight() - 1);
132 
133  graphics->setColor(getForegroundColor());
134 
135  if (img) {
136  graphics->drawImage(img, xoffset, yoffset);
137  }
138 
139  int32_t textX;
140  int32_t textY = getHeight() / 2 - getFont()->getHeight() / 2;
141  switch (getAlignment())
142  {
143  case Graphics::LEFT:
144  textX = 4;
145  break;
146  case Graphics::CENTER:
147  textX = getWidth() / 2;
148  break;
149  case Graphics::RIGHT:
150  textX = getWidth() - 4;
151  break;
152  default:
153  //use the default of Graphics::LEFT
154  textX = 4;
155  FL_WARN(_log, FIFE::LMsg("ToggleButton::draw() - ") << "Unknown alignment: "
156  << getAlignment() << ". Using the default of Graphics::LEFT");
157  }
158 
159  graphics->setFont(getFont());
160  if (mCaption.size() > 0) {
161  if (isPressed())
162  graphics->drawText(getCaption(), textX + 1,
163  textY + 1, getAlignment());
164  else
165  graphics->drawText(getCaption(), textX, textY, getAlignment());
166  }
167  }
168 
169  void ToggleButton::action(const ActionEvent& actionEvent) {
171  }
172 
174  int32_t w = 0;
175  int32_t h = w;
176  if( m_upImage ) {
177  w = m_upImage->getWidth();
178  h = m_upImage->getHeight();
179  }
180  if( m_downImage ) {
181  w = std::max(m_downImage->getWidth(), w);
182  h = std::max(m_downImage->getHeight(), h);
183  }
184  if( m_hoverImage ) {
185  w = std::max(m_hoverImage->getWidth(), w);
186  h = std::max(m_hoverImage->getHeight(), h);
187  }
188 
189  if( mCaption.length() > 0 ) {
190  w = std::max(static_cast<int32_t>(getFont()->getWidth(mCaption)+2*mSpacing), w);
191  h = std::max(static_cast<int32_t>(getFont()->getHeight()+2*mSpacing), h);
192  }
193 
194  setWidth(w);
195  setHeight(h);
196  }
197 
198  void ToggleButton::setUpImage(Image* image) {
199  m_upImage = image;
200  adjustSize();
201  }
202 
203  void ToggleButton::setDownImage(Image* image) {
204  m_downImage = image;
205  adjustSize();
206  }
207 
208  void ToggleButton::setHoverImage(Image* image) {
209  m_hoverImage = image;
210  adjustSize();
211  }
212 
213  bool ToggleButton::isToggled() const {
214  return m_toggled;
215  }
216 
217  void ToggleButton::setToggled(bool toggled) {
218  if (toggled && m_group != "") {
219  // untoggle all buttons in group
220  GroupIterator iter, iterEnd;
221  iterEnd = m_groupMap.upper_bound(m_group);
222 
223  for (iter = m_groupMap.lower_bound(m_group); iter != iterEnd; iter++) {
224  if (iter->second->isToggled()) {
225  iter->second->setToggled(false);
226  }
227  }
228  }
229 
230  m_toggled = toggled;
231  }
232 
233  void ToggleButton::setGroup(const std::string &group) {
234  // Remove button from previous group
235  if (m_group != "") {
236  GroupIterator iter, iterEnd;
237  iterEnd = m_groupMap.upper_bound(m_group);
238 
239  for (iter = m_groupMap.lower_bound(m_group); iter != iterEnd; iter++) {
240  if (iter->second == this) {
241  m_groupMap.erase(iter);
242  break;
243  }
244  }
245  }
246 
247  // Add button to new group
248  if (group != "") {
249  m_groupMap.insert( std::pair<std::string, ToggleButton *>(group, this));
250  }
251 
252  m_group = group;
253  }
254 
255  const std::string &ToggleButton::getGroup() const {
256  return m_group;
257  }
258 
260  return x_downoffset;
261  }
262 
264  return y_downoffset;
265  }
266 
267 }
268 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */
269 
Definition: modules.h:41
#define FL_WARN(logger, msg)
Definition: logger.h:72
void setGroup(const std::string &group)
Sets the group the toggle button should belong to.
void draw(Graphics *graphics)
Draws the button.
Helper class to create log strings out from separate parts Usage: LMsg(&quot;some text&quot;) &lt;&lt; variable &lt;&lt; &quot;...
Definition: logger.h:82
bool m_toggled
Whether the button is toggled or not.
Definition: togglebutton.h:188
void setUpImage(Image *image)
Sets the image that will be displayed when the button isn&#39;t toggled.
void setHoverImage(Image *image)
Sets the image which will be displayed when the mouse cursor is over the button.
int32_t getDownXOffset() const
Gets the number of pixels the image or text will be offset from the left of button when the button is...
void action(const ActionEvent &actionEvent)
Toggle button when it is activated.
static GroupMap m_groupMap
Holds all available toggle button groups.
Definition: togglebutton.h:209
GroupMap::iterator GroupIterator
Typedef.
Definition: togglebutton.h:204
void adjustSize()
Adjust size to fit image and caption.
std::string m_group
Holds the group of the toggle button.
Definition: togglebutton.h:194
ToggleButton(Image *up_image=0, Image *down_image=0, Image *hover_image=0, const std::string &caption="", const std::string &group="")
Constructor.
~ToggleButton()
Destructor.
std::multimap< std::string, ToggleButton * > GroupMap
Typedef.
Definition: togglebutton.h:199
const std::string & getGroup() const
Gets the group the toggle button belongs to.
void setDownImage(Image *image)
Sets the image that will be displayed when the button is toggled or pressed.
bool isToggled() const
Checks if the radio button is selected.
int32_t getDownYOffset() const
Gets the number of pixels the image or text will be offset from the top of button when the button is ...
Create a Logger instance to communicate with LogManager Logger stores information about the current m...
Definition: logger.h:211
static FIFE::Logger _log(LM_GUI)
void setDownOffset(int32_t x, int32_t y)
Sets the number of pixels the image or text will be offset from the top left corner of button when th...
void setToggled(bool toggled)
Sets the radio button to selected or not.