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-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 
66 #include "gui/widgets/checkbox.h"
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 
83 int CheckBox::instances = 0;
84 Skin *CheckBox::mSkin = nullptr;
85 float CheckBox::mAlpha = 1.0;
86 
87 CheckBox::CheckBox(const Widget2 *const widget,
88  const std::string &restrict caption,
89  const bool selected,
90  ActionListener *const listener,
91  const std::string &restrict eventId) :
92  Widget(widget),
94  KeyListener(),
96  mSelected(selected),
97  mCaption(),
98  mTextChunk(),
99  mPadding(0),
100  mImagePadding(0),
101  mImageSize(9),
102  mSpacing(2),
103  mTextX(0 + 9 + 2),
104  mHasMouse(false),
105  mDrawBox(true),
106  mTextChanged(true)
107 {
108  setCaption(caption);
109  mAllowLogic = false;
110 
111  setFocusable(true);
112  addMouseListener(this);
113  addKeyListener(this);
114 
116  if (instances == 0)
117  {
118  if (theme != nullptr)
119  {
120  mSkin = theme->load("checkbox.xml",
121  "",
122  true,
124  updateAlpha();
125  }
126  }
127 
128  instances++;
129 
130  if (!eventId.empty())
131  setActionEventId(eventId);
132 
133  if (listener != nullptr)
134  addActionListener(listener);
135 
137  if (mSkin != nullptr)
138  {
140  mImagePadding = mSkin->getOption("imagePadding");
141  mImageSize = mSkin->getOption("imageSize");
142  mSpacing = mSkin->getOption("spacing");
143  mDrawBox = (mSkin->getOption("drawBox", 1) != 0);
145  }
146  adjustSize();
147 }
148 
150 {
151  if (mWindow != nullptr)
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 
166 void CheckBox::draw(Graphics *const graphics)
167 {
168  BLOCK_START("CheckBox::draw")
169  drawBox(graphics);
170 
171  Font *const font = getFont();
172 
173  if (mTextChanged)
174  {
180  font->generate(mTextChunk);
181  mTextChanged = false;
182  }
183 
184  const Image *const image = mTextChunk.img;
185  if (image != nullptr)
186  graphics->drawImage(image, mTextX, mPadding);
187 
188  BLOCK_END("CheckBox::draw")
189 }
190 
191 void CheckBox::safeDraw(Graphics *const graphics)
192 {
193  CheckBox::draw(graphics);
194 }
195 
197 {
198  const float alpha = std::max(settings.guiAlpha,
200 
201  if (mAlpha != alpha)
202  {
203  mAlpha = alpha;
204  if (mSkin != nullptr)
205  {
206  const ImageRect &rect = mSkin->getBorder();
207  for (int a = 0; a < 6; a ++)
208  {
209  Image *const image = rect.grid[a];
210  if (image != nullptr)
211  image->setAlpha(mAlpha);
212  }
213  }
214  }
215 }
216 
217 void CheckBox::drawBox(Graphics *const graphics)
218 {
219  if ((mSkin == nullptr) || !mDrawBox)
220  return;
221 
222  const ImageRect &rect = mSkin->getBorder();
223  int index = 0;
224 
225  if (mEnabled && mVisible == Visible_true)
226  {
227  if (mSelected)
228  {
229  if (mHasMouse)
230  index = 5;
231  else
232  index = 1;
233  }
234  else
235  {
236  if (mHasMouse)
237  index = 4;
238  else
239  index = 0;
240  }
241  }
242  else
243  {
244  if (mSelected)
245  index = 3;
246  else
247  index = 2;
248  }
249  const Image *const box = rect.grid[index];
250 
251  updateAlpha();
252 
253  if (box != nullptr)
254  {
255  graphics->drawImage(box,
257  (getHeight() - mImageSize) / 2);
258  }
259 }
260 
262 {
263  mHasMouse = true;
264 }
265 
267 {
268  mHasMouse = false;
270 }
271 
273 {
274  const InputActionT action = event.getActionId();
275 
276  if (action == InputAction::GUI_SELECT)
277  {
278  toggleSelected();
279  event.consume();
280  }
281 }
282 
284 {
285  setHeight(getFont()->getHeight() + 2 * mPadding);
288 }
289 
291 {
292  if (event.getButton() == MouseButton::LEFT)
293  {
294  toggleSelected();
295  event.consume();
296  }
297 }
298 
300 {
301  event.consume();
302 }
303 
305 {
306  mSelected = !mSelected;
308 }
309 
310 void CheckBox::setCaption(const std::string& caption)
311 {
312  if (caption != mCaption)
313  mTextChanged = true;
314  mCaption = caption;
315 }
316 
318 {
319  if (mWindow != nullptr)
320  mWindow->addWidgetListener(this);
321  Widget::setParent(widget);
322 }
323 
325 {
326  mTextChanged = true;
328 }
329 
330 void CheckBox::setWindow(Widget *const widget)
331 {
332  if ((widget == nullptr) && (mWindow != nullptr))
333  {
335  mWindow = nullptr;
336  }
337  else
338  {
339  Widget2::setWindow(widget);
340  }
341 }
int mTextX
Definition: checkbox.h:206
Font * getFont() const
Definition: widget.cpp:330
void drawBox(Graphics *const graphics)
Definition: checkbox.cpp:217
void toggleSelected()
Definition: checkbox.cpp:304
std::string text
Definition: textchunk.h:64
int getWidth() const
Definition: widget.h:220
Definition: skin.h:35
int mSpacing
Definition: checkbox.h:205
void setWidth(const int width)
Definition: widget.cpp:132
void unload(Skin *const skin)
Definition: theme.cpp:249
Gui * gui
Definition: gui.cpp:110
Definition: font.h:88
int mPadding
Definition: checkbox.h:202
const bool Visible_true
Definition: visible.h:29
TextChunk mTextChunk
Definition: checkbox.h:200
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:251
int mImageSize
Definition: checkbox.h:204
CheckBox(const Widget2 *const widget, const std::string &caption, const bool selected, ActionListener *const listener, const std::string &eventId)
Definition: checkbox.cpp:87
MouseButtonT getButton() const
Definition: mouseevent.h:115
float getMinimumOpacity() const
Definition: theme.h:123
#define BLOCK_START(name)
Definition: perfomance.h:78
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
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:595
void draw(Graphics *const graphics)
Definition: checkbox.cpp:166
static Skin * mSkin
Definition: checkbox.h:212
Color mForegroundColor
Definition: widget.h:1085
Font * textFont
Definition: textchunk.h:63
bool mDrawBox
Definition: checkbox.h:208
int getOption(const std::string &name) const
Definition: skin.h:105
void mouseClicked(MouseEvent &event)
Definition: checkbox.cpp:290
void updateAlpha()
Definition: checkbox.cpp:196
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:286
int getPadding() const
Definition: skin.h:99
void setParent(Widget *widget)
Definition: checkbox.cpp:317
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 keyPressed(KeyEvent &event)
Definition: checkbox.cpp:272
void adjustSize()
Definition: checkbox.cpp:283
static float mAlpha
Definition: checkbox.h:213
void mouseEntered(MouseEvent &event)
Definition: checkbox.cpp:261
void setCaption(const std::string &caption)
Definition: checkbox.cpp:310
InputAction ::T InputActionT
Definition: inputaction.h:715
void mouseDragged(MouseEvent &event)
Definition: checkbox.cpp:299
int mImagePadding
Definition: checkbox.h:203
bool mAllowLogic
Definition: widget.h:1159
void safeDraw(Graphics *const graphics)
Definition: checkbox.cpp:191
void addMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:291
void addKeyListener(KeyListener *const keyListener)
Definition: widget.cpp:271
void mouseExited(MouseEvent &event)
virtual void setParent(Widget *parent)
Definition: widget.h:625
bool mTextChanged
Definition: checkbox.h:209
void setHeight(const int height)
Definition: widget.cpp:139
void addWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:301
Theme * theme
Definition: theme.cpp:61
#define A_UNUSED
Definition: localconsts.h:151
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 mouseExited(MouseEvent &event)
Definition: checkbox.cpp:266
void removeWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:306
bool mHasMouse
Definition: checkbox.h:207
Definition: event.h:77
bool mEnabled
Definition: widget.h:1157
Definition: image.h:61
int getHeight() const
Definition: widget.h:239
Color color
Definition: textchunk.h:65
#define restrict
Definition: localconsts.h:156
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:44
void deleteImage()
Definition: textchunk.cpp:192
bool mSelected
Definition: checkbox.h:193
float guiAlpha
Definition: settings.h:129
Color color2
Definition: textchunk.h:66
std::string mCaption
Definition: checkbox.h:198
void setWindow(Widget *const widget)
Definition: checkbox.cpp:330
static int instances
Definition: checkbox.h:211
void widgetHidden(const Event &event)
Definition: checkbox.cpp:324
void generate(TextChunk &chunk)
Definition: font.cpp:428