comparison engine/core/gui/widgets/togglebutton.cpp @ 177:3fb17daa1b27

* Added ToggleButton widget * Modified editor to use togglebuttons in toolbox
author cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 25 Jan 2009 20:17:41 +0000
parents
children d76169461729
comparison
equal deleted inserted replaced
176:542213eebe73 177:3fb17daa1b27
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_helptext(""),
55 m_group(group) {
56
57 m_hoverImage = hover_file;
58 setFrameSize(0);
59 setGroup(m_group);
60 adjustSize();
61 mCaption = caption;
62 m_toggled = false;
63
64 addActionListener(this);
65 }
66
67 ToggleButton::~ToggleButton() {
68 setGroup(""); // Remove button from group
69 }
70
71 void ToggleButton::setDownOffset(int x, int 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 int alpha = getBaseColor().a;
81
82 Image* img = NULL;
83 int xoffset = 0;
84 int 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 int textX;
140 int 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 throw GCN_EXCEPTION("Unknown alignment.");
154 }
155
156 graphics->setFont(getFont());
157 if (mCaption.size() > 0) {
158 if (isPressed())
159 graphics->drawText(getCaption(), textX + 1,
160 textY + 1, getAlignment());
161 else
162 graphics->drawText(getCaption(), textX, textY, getAlignment());
163 }
164 }
165
166 void ToggleButton::action(const ActionEvent& actionEvent) {
167 setToggled(!m_toggled);
168 }
169
170 void ToggleButton::adjustSize() {
171 int w = 0;
172 int h = w;
173 if( m_upImage ) {
174 w = m_upImage->getWidth();
175 h = m_upImage->getHeight();
176 }
177 if( m_downImage ) {
178 w = std::max(m_downImage->getWidth(), w);
179 h = std::max(m_downImage->getHeight(), h);
180 }
181 if( m_hoverImage ) {
182 w = std::max(m_hoverImage->getWidth(), w);
183 h = std::max(m_hoverImage->getHeight(), h);
184 }
185 setWidth(w);
186 setHeight(h);
187 }
188
189 void ToggleButton::setUpImage(Image* image) {
190 m_upImage = image;
191 adjustSize();
192 }
193
194 void ToggleButton::setDownImage(Image* image) {
195 m_downImage = image;
196 adjustSize();
197 }
198
199 void ToggleButton::setHoverImage(Image* image) {
200 m_hoverImage = image;
201 adjustSize();
202 }
203
204 bool ToggleButton::isToggled() const {
205 return m_toggled;
206 }
207
208 void ToggleButton::setToggled(bool toggled) {
209 if (toggled && m_group != "") {
210 // untoggle all buttons in group
211 GroupIterator iter, iterEnd;
212 iterEnd = m_groupMap.upper_bound(m_group);
213
214 for (iter = m_groupMap.lower_bound(m_group); iter != iterEnd; iter++) {
215 if (iter->second->isToggled()) {
216 iter->second->setToggled(false);
217 }
218 }
219 }
220
221 m_toggled = toggled;
222 }
223
224 void ToggleButton::setGroup(const std::string &group) {
225 // Remove button from previous group
226 if (m_group != "") {
227 GroupIterator iter, iterEnd;
228 iterEnd = m_groupMap.upper_bound(m_group);
229
230 for (iter = m_groupMap.lower_bound(m_group); iter != iterEnd; iter++) {
231 if (iter->second == this) {
232 m_groupMap.erase(iter);
233 break;
234 }
235 }
236 }
237
238 // Add button to new group
239 if (group != "") {
240 m_groupMap.insert( std::pair<std::string, ToggleButton *>(group, this));
241 }
242
243 m_group = group;
244 }
245
246 const std::string &ToggleButton::getGroup() const {
247 return m_group;
248 }
249
250 int ToggleButton::getDownXOffset() const {
251 return x_downoffset;
252 }
253
254 int ToggleButton::getDownYOffset() const {
255 return y_downoffset;
256 }
257
258 void ToggleButton::setHelpText(const std::string& txt) {
259 m_helptext = txt;
260 }
261
262 const std::string& ToggleButton::getHelpText() {
263 return m_helptext;
264 }
265 }
266 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */
267