ManaPlus
checkbox.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 
67 #include "gui/widgets/checkbox.h"
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 
84 int CheckBox::instances = 0;
85 Skin *CheckBox::mSkin = nullptr;
86 float CheckBox::mAlpha = 1.0;
87 
88 CheckBox::CheckBox(const Widget2 *const widget,
89  const std::string &restrict caption,
90  const bool selected,
91  ActionListener *const listener,
92  const std::string &restrict eventId) :
93  Widget(widget),
95  KeyListener(),
97  mSelected(selected),
98  mCaption(),
99  mTextChunk(),
100  mPadding(0),
101  mImagePadding(0),
102  mImageSize(9),
103  mSpacing(2),
104  mTextX(0 + 9 + 2),
105  mHasMouse(false),
106  mDrawBox(true),
107  mTextChanged(true)
108 {
109  setCaption(caption);
110  mAllowLogic = false;
111 
112  setFocusable(true);
113  addMouseListener(this);
114  addKeyListener(this);
115 
116  mForegroundColor2 = getThemeColor(ThemeColorId::CHECKBOX_OUTLINE, 255U);
117  if (instances == 0)
118  {
119  if (theme != nullptr)
120  {
121  mSkin = theme->load("checkbox.xml",
122  "",
123  true,
125  updateAlpha();
126  }
127  }
128 
129  instances++;
130 
131  if (!eventId.empty())
132  setActionEventId(eventId);
133 
134  if (listener != nullptr)
136 
137  mForegroundColor = getThemeColor(ThemeColorId::CHECKBOX, 255U);
138  if (mSkin != nullptr)
139  {
141  mImagePadding = mSkin->getOption("imagePadding");
142  mImageSize = mSkin->getOption("imageSize");
143  mSpacing = mSkin->getOption("spacing");
144  mDrawBox = (mSkin->getOption("drawBox", 1) != 0);
146  }
147  adjustSize();
148 }
149 
151 {
152  if (mWindow != nullptr)
154 
155  if (gui != nullptr)
156  gui->removeDragged(this);
157 
158  instances--;
159 
160  if (instances == 0)
161  {
162  if (theme != nullptr)
163  theme->unload(mSkin);
164  }
165 }
166 
167 void CheckBox::draw(Graphics *const graphics)
168 {
169  BLOCK_START("CheckBox::draw")
170  drawBox(graphics);
171 
172  Font *const font = getFont();
173 
174  if (mTextChanged)
175  {
176  mTextChunk.textFont = font;
181  font->generate(mTextChunk);
182  mTextChanged = false;
183  }
184 
185  const Image *const image = mTextChunk.img;
186  if (image != nullptr)
187  graphics->drawImage(image, mTextX, mPadding);
188 
189  BLOCK_END("CheckBox::draw")
190 }
191 
192 void CheckBox::safeDraw(Graphics *const graphics)
193 {
194  CheckBox::draw(graphics);
195 }
196 
198 {
199  const float alpha = std::max(settings.guiAlpha,
201 
202  if (mAlpha != alpha)
203  {
204  mAlpha = alpha;
205  if (mSkin != nullptr)
206  {
207  const ImageRect &rect = mSkin->getBorder();
208  for (int a = 0; a < 6; a ++)
209  {
210  Image *const image = rect.grid[a];
211  if (image != nullptr)
212  image->setAlpha(mAlpha);
213  }
214  }
215  }
216 }
217 
218 void CheckBox::drawBox(Graphics *const graphics)
219 {
220  if ((mSkin == nullptr) || !mDrawBox)
221  return;
222 
223  const ImageRect &rect = mSkin->getBorder();
224  int index = 0;
225 
226  if (mEnabled && mVisible == Visible_true)
227  {
228  if (mSelected)
229  {
230  if (mHasMouse)
231  index = 5;
232  else
233  index = 1;
234  }
235  else
236  {
237  if (mHasMouse)
238  index = 4;
239  else
240  index = 0;
241  }
242  }
243  else
244  {
245  if (mSelected)
246  index = 3;
247  else
248  index = 2;
249  }
250  const Image *const box = rect.grid[index];
251 
252  updateAlpha();
253 
254  if (box != nullptr)
255  {
256  graphics->drawImage(box,
258  (getHeight() - mImageSize) / 2);
259  }
260 }
261 
263 {
264  mHasMouse = true;
265 }
266 
268 {
269  mHasMouse = false;
271 }
272 
274 {
275  const InputActionT action = event.getActionId();
276 
277  if (action == InputAction::GUI_SELECT)
278  {
279  toggleSelected();
280  event.consume();
281  }
282 }
283 
285 {
286  setHeight(getFont()->getHeight() + 2 * mPadding);
289 }
290 
292 {
293  if (event.getButton() == MouseButton::LEFT)
294  {
295  toggleSelected();
296  event.consume();
297  }
298 }
299 
301 {
302  event.consume();
303 }
304 
306 {
307  mSelected = !mSelected;
309 }
310 
311 void CheckBox::setCaption(const std::string& caption)
312 {
313  if (caption != mCaption)
314  mTextChanged = true;
315  mCaption = caption;
316 }
317 
319 {
320  if (mWindow != nullptr)
321  mWindow->addWidgetListener(this);
322  Widget::setParent(widget);
323 }
324 
326 {
327  mTextChanged = true;
329 }
330 
331 void CheckBox::setWindow(Widget *const widget)
332 {
333  if ((widget == nullptr) && (mWindow != nullptr))
334  {
336  mWindow = nullptr;
337  }
338  else
339  {
340  Widget2::setWindow(widget);
341  }
342 }
int mImageSize
Definition: checkbox.h:205
void toggleSelected()
Definition: checkbox.cpp:305
void mouseExited(MouseEvent &event)
Definition: checkbox.cpp:267
static float mAlpha
Definition: checkbox.h:214
void safeDraw(Graphics *const graphics)
Definition: checkbox.cpp:192
bool mDrawBox
Definition: checkbox.h:209
int mImagePadding
Definition: checkbox.h:204
void adjustSize()
Definition: checkbox.cpp:284
int mSpacing
Definition: checkbox.h:206
CheckBox(const Widget2 *const widget, const std::string &caption, const bool selected, ActionListener *const listener, const std::string &eventId)
Definition: checkbox.cpp:88
static int instances
Definition: checkbox.h:212
std::string mCaption
Definition: checkbox.h:199
bool mSelected
Definition: checkbox.h:194
bool mHasMouse
Definition: checkbox.h:208
void mouseEntered(MouseEvent &event)
Definition: checkbox.cpp:262
void setWindow(Widget *const widget)
Definition: checkbox.cpp:331
void keyPressed(KeyEvent &event)
Definition: checkbox.cpp:273
void setCaption(const std::string &caption)
Definition: checkbox.cpp:311
void draw(Graphics *const graphics)
Definition: checkbox.cpp:167
static Skin * mSkin
Definition: checkbox.h:213
bool mTextChanged
Definition: checkbox.h:210
void setParent(Widget *widget)
Definition: checkbox.cpp:318
TextChunk mTextChunk
Definition: checkbox.h:201
int mPadding
Definition: checkbox.h:203
void mouseClicked(MouseEvent &event)
Definition: checkbox.cpp:291
void updateAlpha()
Definition: checkbox.cpp:197
int mTextX
Definition: checkbox.h:207
void drawBox(Graphics *const graphics)
Definition: checkbox.cpp:218
void widgetHidden(const Event &event)
Definition: checkbox.cpp:325
void mouseDragged(MouseEvent &event)
Definition: checkbox.cpp:300
Definition: event.h:79
Definition: font.h:90
void generate(TextChunk &chunk)
Definition: font.cpp:430
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
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
void mouseExited(MouseEvent &event)
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
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:596
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
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:252
int getHeight() const
Definition: widget.h:240
int getWidth() const
Definition: widget.h:221
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