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