ManaPlus
radiobutton.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2018 The ManaPlus Developers
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 /* _______ __ __ __ ______ __ __ _______ __ __
24  * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\
25  * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
26  * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / /
27  * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / /
28  * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
29  * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
30  *
31  * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson
32  *
33  *
34  * Per Larsson a.k.a finalman
35  * Olof Naessén a.k.a jansem/yakslem
36  *
37  * Visit: http://guichan.sourceforge.net
38  *
39  * License: (BSD)
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  * notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  * notice, this list of conditions and the following disclaimer in
47  * the documentation and/or other materials provided with the
48  * distribution.
49  * 3. Neither the name of Guichan nor the names of its contributors may
50  * be used to endorse or promote products derived from this software
51  * without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
54  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
55  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
56  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
57  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
58  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
59  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
60  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
61  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
62  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64  */
65 
67 
68 #include "settings.h"
69 
70 #include "gui/gui.h"
71 #include "gui/skin.h"
72 
73 #include "gui/fonts/font.h"
74 
75 #include "render/graphics.h"
76 
77 #include "resources/imagerect.h"
78 
79 #include "resources/image/image.h"
80 
81 #include "debug.h"
82 
84 Skin *RadioButton::mSkin = nullptr;
85 float RadioButton::mAlpha = 1.0;
86 
88 
89 RadioButton::RadioButton(const Widget2 *const widget,
90  const std::string &restrict caption,
91  const std::string &restrict group,
92  const bool marked) :
93  Widget(widget),
94  MouseListener(),
95  KeyListener(),
97  mSelected(false),
98  mCaption(),
99  mGroup(),
100  mTextChunk(),
101  mPadding(0),
102  mImagePadding(0),
103  mImageSize(9),
104  mSpacing(2),
105  mTextX(0 + 9 + 2),
106  mHasMouse(false),
107  mTextChanged(true)
108 {
109  mAllowLogic = false;
110  setCaption(caption);
111  setGroup(group);
112  setSelected(marked);
113 
114  setFocusable(true);
115  addMouseListener(this);
116  addKeyListener(this);
117 
120  if (instances == 0)
121  {
122  if (theme != nullptr)
123  {
124  mSkin = theme->load("radio.xml",
125  "",
126  true,
128  updateAlpha();
129  }
130  }
131 
132  instances++;
133 
134  if (mSkin != nullptr)
135  {
137  mImagePadding = mSkin->getOption("imagePadding");
138  mImageSize = mSkin->getOption("imageSize");
139  mSpacing = mSkin->getOption("spacing");
141  }
142 
143  adjustSize();
144 }
145 
147 {
148  if (mWindow != nullptr)
150 
151  setGroup(std::string());
152 
153  if (gui != nullptr)
154  gui->removeDragged(this);
155 
156  instances--;
157 
158  if (instances == 0)
159  {
160  if (theme != nullptr)
161  theme->unload(mSkin);
162  }
163 }
164 
166 {
167  const float alpha = std::max(settings.guiAlpha,
169 
170  if (mAlpha != alpha)
171  {
172  mAlpha = alpha;
173  if (mSkin != nullptr)
174  {
175  const ImageRect &rect = mSkin->getBorder();
176  for (int a = 0; a < 4; a ++)
177  {
178  Image *const image = rect.grid[a];
179  if (image != nullptr)
180  image->setAlpha(mAlpha);
181  }
182  }
183  }
184 }
185 
186 void RadioButton::drawBox(Graphics *const graphics)
187 {
188  if (mSkin == nullptr)
189  return;
190 
191  const ImageRect &rect = mSkin->getBorder();
192  int index = 0;
193 
194  if (mEnabled && mVisible == Visible_true)
195  {
196  if (mSelected)
197  {
198  if (mHasMouse)
199  index = 1;
200  else
201  index = 0;
202  }
203  else
204  {
205  if (mHasMouse)
206  index = 3;
207  else
208  index = 2;
209  }
210  }
211  else
212  {
213  if (mSelected)
214  index = 0;
215  else
216  index = 2;
217  }
218 
219  const Image *const box = rect.grid[index];
220 
221  updateAlpha();
222 
223  if (box != nullptr)
224  {
225  graphics->drawImage(box,
227  (getHeight() - mImageSize) / 2);
228  }
229 }
230 
231 void RadioButton::draw(Graphics *const graphics)
232 {
233  BLOCK_START("RadioButton::draw")
234  drawBox(graphics);
235 
236  Font *const font = getFont();
237 
238  if (mTextChanged)
239  {
245  font->generate(mTextChunk);
246  mTextChanged = false;
247  }
248 
249  const Image *const image = mTextChunk.img;
250  if (image != nullptr)
251  graphics->drawImage(image, mTextX, mPadding);
252 
253  BLOCK_END("RadioButton::draw")
254 }
255 
256 void RadioButton::safeDraw(Graphics *const graphics)
257 {
258  RadioButton::draw(graphics);
259 }
260 
262 {
263  mHasMouse = true;
264 }
265 
267 {
268  mHasMouse = false;
269 }
270 
272 {
273  const InputActionT action = event.getActionId();
274  if (action == InputAction::GUI_SELECT)
275  {
276  setSelected(true);
278  event.consume();
279  }
280 }
281 
283 {
284  Font *const font = getFont();
285  setHeight(font->getHeight() + 2 * mPadding);
287  + font->getWidth(mCaption) + mPadding);
288 }
289 
290 void RadioButton::setSelected(const bool selected)
291 {
292  if (selected && !mGroup.empty())
293  {
294  for (GroupIterator iter = mGroupMap.lower_bound(mGroup),
295  iterEnd = mGroupMap.upper_bound(mGroup);
296  iter != iterEnd;
297  ++ iter)
298  {
299  if ((iter->second != nullptr) && iter->second->isSelected())
300  iter->second->setSelected(false);
301  }
302  }
303 
304  mSelected = selected;
305 }
306 
308 {
309  if (event.getButton() == MouseButton::LEFT)
310  {
311  setSelected(true);
312  event.consume();
314  }
315 }
316 
318 {
319  event.consume();
320 }
321 
322 void RadioButton::setGroup(const std::string &group)
323 {
324  if (!mGroup.empty())
325  {
326  for (GroupIterator iter = mGroupMap.lower_bound(mGroup),
327  iterEnd = mGroupMap.upper_bound(mGroup);
328  iter != iterEnd;
329  ++ iter)
330  {
331  if (iter->second == this)
332  {
333  mGroupMap.erase(iter);
334  break;
335  }
336  }
337  }
338 
339  if (!group.empty())
340  mGroupMap.insert(std::pair<std::string, RadioButton *>(group, this));
341 
342  mGroup = group;
343 }
344 
345 void RadioButton::setCaption(const std::string& caption)
346 {
347  if (caption != mCaption)
348  mTextChanged = true;
349  mCaption = caption;
350 }
351 
353 {
354  if (mWindow != nullptr)
355  mWindow->addWidgetListener(this);
356  Widget::setParent(widget);
357 }
358 
360 {
361  mTextChanged = true;
363 }
364 
365 void RadioButton::setWindow(Widget *const widget)
366 {
367  if ((widget == nullptr) && (mWindow != nullptr))
368  {
370  mWindow = nullptr;
371  }
372  else
373  {
374  Widget2::setWindow(widget);
375  }
376 }
Font * getFont() const
Definition: widget.cpp:330
void updateAlpha()
GroupMap::iterator GroupIterator
Definition: radiobutton.h:231
bool mHasMouse
Definition: radiobutton.h:243
static Skin * mSkin
Definition: radiobutton.h:203
std::string text
Definition: textchunk.h:64
Definition: skin.h:35
void setWidth(const int width)
Definition: widget.cpp:132
void unload(Skin *const skin)
Definition: theme.cpp:249
void setParent(Widget *widget)
Gui * gui
Definition: gui.cpp:110
Definition: font.h:88
const bool Visible_true
Definition: visible.h:29
void setCaption(const std::string &caption)
MouseButtonT getButton() const
Definition: mouseevent.h:115
float getMinimumOpacity() const
Definition: theme.h:123
static float mAlpha
Definition: radiobutton.h:204
#define BLOCK_START(name)
Definition: perfomance.h:78
void mouseExited(MouseEvent &event)
static std::string getThemePath()
Definition: theme.h:66
Skin * load(const std::string &filename, const std::string &filename2, const bool full, const std::string &defaultPath)
Definition: theme.cpp:178
#define BLOCK_END(name)
Definition: perfomance.h:79
RadioButton(const Widget2 *const widget, const std::string &caption, const std::string &group, const bool marked)
Definition: radiobutton.cpp:89
Color mForegroundColor
Definition: widget.h:1085
void mouseDragged(MouseEvent &event)
Font * textFont
Definition: textchunk.h:63
void widgetHidden(const Event &event)
void draw(Graphics *const graphics)
int getOption(const std::string &name) const
Definition: skin.h:105
Visible mVisible
Definition: widget.h:962
void distributeActionEvent()
Definition: widget.cpp:492
Settings settings
Definition: settings.cpp:31
virtual void setAlpha(const float alpha)
Definition: image.cpp:285
int getPadding() const
Definition: skin.h:99
std::multimap< std::string, RadioButton * > GroupMap
Definition: radiobutton.h:226
static int instances
Definition: radiobutton.h:202
virtual void setWindow(Widget *const window)
Definition: widget2.h:96
const ImageRect & getBorder() const
Definition: skin.h:67
Image * grid[9]
Definition: imagerect.h:41
Widget * mWindow
Definition: widget2.h:111
void mouseClicked(MouseEvent &event)
InputAction ::T InputActionT
Definition: inputaction.h:714
std::string mGroup
Definition: radiobutton.h:219
bool mAllowLogic
Definition: widget.h:1159
void addMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:291
bool mTextChanged
Definition: radiobutton.h:244
void addKeyListener(KeyListener *const keyListener)
Definition: widget.cpp:271
void setWindow(Widget *const widget)
virtual void setParent(Widget *parent)
Definition: widget.h:625
void safeDraw(Graphics *const graphics)
bool mSelected
Definition: radiobutton.h:209
void setHeight(const int height)
Definition: widget.cpp:139
void addWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:301
void drawBox(Graphics *const graphics)
Theme * theme
Definition: theme.cpp:61
#define A_UNUSED
Definition: localconsts.h:171
void setFocusable(const bool focusable)
Definition: widget.cpp:191
Color mForegroundColor2
Definition: widget2.h:112
Image * img
Definition: textchunk.h:62
virtual void drawImage(const Image *const image, int dstX, int dstY)=0
void removeDragged(const Widget *const widget)
Definition: gui.cpp:1160
Definition: widget.h:97
void mouseEntered(MouseEvent &event)
void removeWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:306
int getWidth(const std::string &text) const
Definition: font.cpp:333
Definition: event.h:77
bool mEnabled
Definition: widget.h:1157
Definition: image.h:61
TextChunk mTextChunk
Definition: radiobutton.h:221
int getHeight() const
Definition: widget.h:239
Color color
Definition: textchunk.h:65
void setSelected(const bool selected)
#define restrict
Definition: localconsts.h:176
void keyPressed(KeyEvent &event)
int mImagePadding
Definition: radiobutton.h:239
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:44
int getHeight() const
Definition: font.cpp:360
void deleteImage()
Definition: textchunk.cpp:192
float guiAlpha
Definition: settings.h:129
Color color2
Definition: textchunk.h:66
void setGroup(const std::string &group)
void adjustSize()
std::string mCaption
Definition: radiobutton.h:214
void generate(TextChunk &chunk)
Definition: font.cpp:428
static GroupMap mGroupMap
Definition: radiobutton.h:236