comparison engine/core/gui/widgets/togglebutton.cpp @ 697:ecaa4d98f05f tip

Abstracted the GUI code and refactored the GUIChan-specific code into its own module. * Most of the GUIChan code has been refactored into its own gui/guichan module. However, references to the GuiFont class still persist in the Engine and GuiManager code and these will need further refactoring. * GuiManager is now an abstract base class which specific implementations (e.g. GUIChan) should subclass. * The GUIChan GUI code is now a concrete implementation of GuiManager, most of which is in the new GuiChanGuiManager class. * The GUI code in the Console class has been refactored out of the Console and into the GUIChan module as its own GuiChanConsoleWidget class. The rest of the Console class related to executing commands was left largely unchanged. * Existing client code may need to downcast the GuiManager pointer received from FIFE::Engine::getGuiManager() to GuiChanGuiManager, since not all functionality is represented in the GuiManager abstract base class. Python client code can use the new GuiChanGuiManager.castTo static method for this purpose.
author M. George Hansen <technopolitica@gmail.com>
date Sat, 18 Jun 2011 00:28:40 -1000
parents e201abd8c807
children
comparison
equal deleted inserted replaced
696:e201abd8c807 697:ecaa4d98f05f
1 /***************************************************************************
2 * Copyright (C) 2005-2008 by the FIFE team *
3 * http://www.fifengine.de *
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
31 // 3rd party library includes
32
33 // FIFE includes
34 // These includes are split up in two parts, separated by one empty line
35 // First block: files included from the FIFE root src directory
36 // Second block: files included from the same folder
37 #include <iostream>
38
39 #include <guichan/mouseevent.hpp>
40
41 #include "togglebutton.h"
42
43
44 namespace gcn {
45 ToggleButton::GroupMap ToggleButton::m_groupMap;
46
47 ToggleButton::ToggleButton(Image *up_file , Image *down_file, Image *hover_file, const std::string& caption, const std::string& group):
48 Button(),
49 m_upImage(up_file),
50 m_downImage(down_file),
51 m_hoverImage(hover_file),
52 x_downoffset(0),
53 y_downoffset(0),
54 m_group(group) {
55
56 m_hoverImage = hover_file;
57 setFrameSize(0);
58 setGroup(m_group);
59 adjustSize();
60 mCaption = caption;
61 m_toggled = false;
62
63 addActionListener(this);
64 }
65
66 ToggleButton::~ToggleButton() {
67 setGroup(""); // Remove button from group
68 }
69
70 void ToggleButton::setDownOffset(int x, int y) {
71 x_downoffset = x;
72 y_downoffset = y;
73 }
74
75 void ToggleButton::draw(Graphics *graphics) {
76 Color faceColor = getBaseColor();
77 Color highlightColor;
78 Color shadowColor;
79 int alpha = getBaseColor().a;
80
81 Image* img = NULL;
82 int xoffset = 0;
83 int yoffset = 0;
84
85 if (isPressed() || m_toggled) {
86 faceColor = faceColor - 0x303030;
87 faceColor.a = alpha;
88 highlightColor = faceColor - 0x303030;
89 highlightColor.a = alpha;
90 shadowColor = faceColor + 0x303030;
91 shadowColor.a = alpha;
92
93 if( m_downImage ) {
94 img = m_downImage;
95 xoffset = x_downoffset;
96 yoffset = y_downoffset;
97 }
98 } else if(mHasMouse) {
99 faceColor = faceColor + 0x303030;
100 faceColor.a = alpha;
101 highlightColor = faceColor + 0x303030;
102 highlightColor.a = alpha;
103 shadowColor = faceColor - 0x303030;
104 shadowColor.a = alpha;
105
106 if ( m_hoverImage ) {
107 img = m_hoverImage;
108 }
109 } else{
110 highlightColor = faceColor + 0x303030;
111 highlightColor.a = alpha;
112 shadowColor = faceColor - 0x303030;
113 shadowColor.a = alpha;
114
115 if (m_upImage) {
116 img = m_upImage;
117 }
118 }
119
120
121 graphics->setColor(faceColor);
122 graphics->fillRectangle(Rectangle(1, 1, getDimension().width-1, getHeight() - 1));
123
124 graphics->setColor(highlightColor);
125 graphics->drawLine(0, 0, getWidth() - 1, 0);
126 graphics->drawLine(0, 1, 0, getHeight() - 1);
127
128 graphics->setColor(shadowColor);
129 graphics->drawLine(getWidth() - 1, 1, getWidth() - 1, getHeight() - 1);
130 graphics->drawLine(1, getHeight() - 1, getWidth() - 1, getHeight() - 1);
131
132 graphics->setColor(getForegroundColor());
133
134 if (img) {
135 graphics->drawImage(img, xoffset, yoffset);
136 }
137
138 int textX;
139 int textY = getHeight() / 2 - getFont()->getHeight() / 2;
140 switch (getAlignment())
141 {
142 case Graphics::LEFT:
143 textX = 4;
144 break;
145 case Graphics::CENTER:
146 textX = getWidth() / 2;
147 break;
148 case Graphics::RIGHT:
149 textX = getWidth() - 4;
150 break;
151 default:
152 throw GCN_EXCEPTION("Unknown alignment.");
153 }
154
155 graphics->setFont(getFont());
156 if (mCaption.size() > 0) {
157 if (isPressed())
158 graphics->drawText(getCaption(), textX + 1,
159 textY + 1, getAlignment());
160 else
161 graphics->drawText(getCaption(), textX, textY, getAlignment());
162 }
163 }
164
165 void ToggleButton::action(const ActionEvent& actionEvent) {
166 setToggled(!m_toggled);
167 }
168
169 void ToggleButton::adjustSize() {
170 int w = 0;
171 int h = w;
172 if( m_upImage ) {
173 w = m_upImage->getWidth();
174 h = m_upImage->getHeight();
175 }
176 if( m_downImage ) {
177 w = std::max(m_downImage->getWidth(), w);
178 h = std::max(m_downImage->getHeight(), h);
179 }
180 if( m_hoverImage ) {
181 w = std::max(m_hoverImage->getWidth(), w);
182 h = std::max(m_hoverImage->getHeight(), h);
183 }
184
185 if( mCaption.length() > 0 ) {
186 w = std::max(static_cast<int>(getFont()->getWidth(mCaption)+2*mSpacing), w);
187 h = std::max(static_cast<int>(getFont()->getHeight()+2*mSpacing), h);
188 }
189
190 setWidth(w);
191 setHeight(h);
192 }
193
194 void ToggleButton::setUpImage(Image* image) {
195 m_upImage = image;
196 adjustSize();
197 }
198
199 void ToggleButton::setDownImage(Image* image) {
200 m_downImage = image;
201 adjustSize();
202 }
203
204 void ToggleButton::setHoverImage(Image* image) {
205 m_hoverImage = image;
206 adjustSize();
207 }
208
209 bool ToggleButton::isToggled() const {
210 return m_toggled;
211 }
212
213 void ToggleButton::setToggled(bool toggled) {
214 if (toggled && m_group != "") {
215 // untoggle all buttons in group
216 GroupIterator iter, iterEnd;
217 iterEnd = m_groupMap.upper_bound(m_group);
218
219 for (iter = m_groupMap.lower_bound(m_group); iter != iterEnd; iter++) {
220 if (iter->second->isToggled()) {
221 iter->second->setToggled(false);
222 }
223 }
224 }
225
226 m_toggled = toggled;
227 }
228
229 void ToggleButton::setGroup(const std::string &group) {
230 // Remove button from previous group
231 if (m_group != "") {
232 GroupIterator iter, iterEnd;
233 iterEnd = m_groupMap.upper_bound(m_group);
234
235 for (iter = m_groupMap.lower_bound(m_group); iter != iterEnd; iter++) {
236 if (iter->second == this) {
237 m_groupMap.erase(iter);
238 break;
239 }
240 }
241 }
242
243 // Add button to new group
244 if (group != "") {
245 m_groupMap.insert( std::pair<std::string, ToggleButton *>(group, this));
246 }
247
248 m_group = group;
249 }
250
251 const std::string &ToggleButton::getGroup() const {
252 return m_group;
253 }
254
255 int ToggleButton::getDownXOffset() const {
256 return x_downoffset;
257 }
258
259 int ToggleButton::getDownYOffset() const {
260 return y_downoffset;
261 }
262
263 }
264 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */
265