ManaPlus
button.h
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 #ifndef GUI_WIDGETS_BUTTON_H
68 #define GUI_WIDGETS_BUTTON_H
69 
70 #include "gui/fonts/textchunk.h"
71 
72 #include "gui/widgets/widget.h"
73 
75 #include "listeners/keylistener.h"
78 
79 #include "render/graphics.h"
80 
81 #include "localconsts.h"
82 
83 class Image;
84 class ImageCollection;
85 class ImageSet;
86 class Skin;
87 
88 const std::string BUTTON_PLAY = "buttonplay.png";
89 const std::string BUTTON_SKIN = "button";
90 const std::string BUTTON_PIN_SKIN = "buttonpin";
91 
97 class Button final : public Widget,
98  public MouseListener,
99  public KeyListener,
100  public FocusListener,
101  public WidgetListener
102 {
103  public:
107  Button(const Widget2 *const widget,
108  const std::string &restrict skinName);
109 
114  Button(const Widget2 *const widget,
115  const std::string &restrict caption,
116  const std::string &restrict actionEventId,
117  const std::string &restrict skinName,
118  ActionListener *const listener);
119 
124  Button(const Widget2 *const widget,
125  const std::string &restrict caption,
126  const std::string &restrict imageName,
127  const int imageWidth, const int imageHeight,
128  const std::string &restrict actionEventId,
129  const std::string &restrict skinName,
130  ActionListener *const listener);
131 
136  Button(const Widget2 *const widget,
137  const std::string &restrict imageName,
138  const int imageWidth,
139  const int imageHeight,
140  const std::string &restrict actionEventId,
141  const std::string &restrict skinName,
142  ActionListener *const listener);
143 
145 
146 
149  ~Button() override final;
150 
154  void draw(Graphics *const graphics) override final A_NONNULL(2);
155 
156  void safeDraw(Graphics *const graphics) override final A_NONNULL(2);
157 
161  void updateAlpha();
162 
163  void mouseReleased(MouseEvent& event) override final;
164 
165  void setDescription(const std::string &text)
166  { mDescription = text; }
167 
169  { return mDescription; }
170 
172  { return mClickCount; }
173 
174  void setTag(int tag)
175  { mTag = tag; }
176 
178  { return mTag; }
179 
180  void setStick(bool b)
181  { mStick = b; }
182 
183  void setPressed(bool b)
184  { mPressed = b; }
185 
186  void widgetResized(const Event &event) override final;
187 
188  void widgetMoved(const Event &event) override final;
189 
190  void widgetHidden(const Event &event) override final;
191 
192  void loadImage(const std::string &imageName);
193 
194  void loadImageSet(const std::string &imageName);
195 
196  void setImage(Image *const image);
197 
198  void adjustSize();
199 
200  void keyPressed(KeyEvent &event) override final;
201 
202  void keyReleased(KeyEvent &event) override final;
203 
204  bool isPressed2() const A_WARN_UNUSED;
205 
214  void setCaption(const std::string& caption)
215  { mCaption = caption; mTextChanged = true; }
216 
222  const std::string& getCaption() const noexcept2 A_WARN_UNUSED
223  { return mCaption; }
224 
233  { mAlignment = alignment; }
234 
242  { return mAlignment; }
243 
244  void focusLost(const Event& event) override final;
245 
246  void mousePressed(MouseEvent& event) override final;
247 
248  void mouseEntered(MouseEvent& event) override final;
249 
250  void mouseExited(MouseEvent& event) override final;
251 
252  void mouseDragged(MouseEvent& event) override final;
253 
254  void setParent(Widget *widget) override final;
255 
256  void setWindow(Widget *const widget) override final;
257 
258  void setImageWidth(const int width) noexcept2
259  { mImageWidth = width; }
260 
261  void setImageHeight(const int height) noexcept2
262  { mImageHeight = height; }
263 
264  enum
265  {
266  BUTTON_STANDARD = 0, // 0
270  BUTTON_COUNT // 4 - Must be last.
271  };
272 
273  private:
280  bool isPressed() const;
281 
282  void init();
283 
284  static float mAlpha;
285 
290  std::string mCaption;
291 
292  std::string mDescription;
293 
294  std::string mSkinName;
295 
297 
309 
314 
316 
321 
322  int mTag;
323  int mMode;
324  int mXOffset;
325  int mYOffset;
331  bool mHasMouse;
332 
337 
342 
343  bool mStick;
344  bool mPressed;
346 };
347 
348 #endif // GUI_WIDGETS_BUTTON_H
const std::string BUTTON_PIN_SKIN
Definition: button.h:90
const std::string BUTTON_SKIN
Definition: button.h:89
const std::string BUTTON_PLAY
Definition: button.h:88
Definition: button.h:102
void setImage(Image *const image)
Definition: button.cpp:379
Graphics::Alignment getAlignment() const
Definition: button.h:241
int mSpacing[BUTTON_COUNT]
Definition: button.h:320
bool mStick
Definition: button.h:343
bool mHasMouse
Definition: button.h:331
void setImageWidth(const int width)
Definition: button.h:258
int getClickCount() const
Definition: button.h:171
std::string getDescription() const
Definition: button.h:168
void setCaption(const std::string &caption)
Definition: button.h:214
int mImageHeight
Definition: button.h:327
void focusLost(const Event &event)
Definition: button.cpp:868
~Button()
Definition: button.cpp:337
Color mEnabledColor
Definition: button.h:299
void updateAlpha()
Definition: button.cpp:418
bool mTextChanged
Definition: button.h:345
void keyPressed(KeyEvent &event)
Definition: button.cpp:831
void init()
Definition: button.cpp:307
void setDescription(const std::string &text)
Definition: button.h:165
static float mAlpha
Definition: button.h:284
Button(const Widget2 *const widget, const std::string &skinName)
Definition: button.cpp:99
void setParent(Widget *widget)
Definition: button.cpp:898
ImageCollection * mVertexes2
Definition: button.h:298
void mousePressed(MouseEvent &event)
Definition: button.cpp:874
std::string mCaption
Definition: button.h:290
int mTag
Definition: button.h:322
void setAlignment(Graphics::Alignment alignment)
Definition: button.h:232
void mouseReleased(MouseEvent &event)
Definition: button.cpp:758
int mMode
Definition: button.h:323
Color mPressedColor
Definition: button.h:305
void setTag(int tag)
Definition: button.h:174
void setStick(bool b)
Definition: button.h:180
bool isPressed() const
Definition: button.cpp:861
void adjustSize()
Definition: button.cpp:799
ImageSet * mImageSet
Definition: button.h:308
@ BUTTON_DISABLED
Definition: button.h:269
@ BUTTON_COUNT
Definition: button.h:270
@ BUTTON_HIGHLIGHTED
Definition: button.h:267
@ BUTTON_STANDARD
Definition: button.h:266
@ BUTTON_PRESSED
Definition: button.h:268
bool isPressed2() const
Definition: button.cpp:856
void mouseDragged(MouseEvent &event)
Definition: button.cpp:893
void setWindow(Widget *const widget)
Definition: button.cpp:905
Color mHighlightedColor2
Definition: button.h:304
TextChunk mTextChunk
Definition: button.h:296
int mImageWidth
Definition: button.h:326
void setImageHeight(const int height)
Definition: button.h:261
void mouseExited(MouseEvent &event)
Definition: button.cpp:888
Skin * mSkin[BUTTON_COUNT]
Definition: button.h:286
Color mDisabledColor2
Definition: button.h:302
std::string mSkinName
Definition: button.h:294
void setPressed(bool b)
Definition: button.h:183
void widgetHidden(const Event &event)
Definition: button.cpp:790
Color mPressedColor2
Definition: button.h:306
void draw(Graphics *const graphics)
Definition: button.cpp:443
Color mHighlightedColor
Definition: button.h:303
void safeDraw(Graphics *const graphics)
Definition: button.cpp:622
void loadImage(const std::string &imageName)
Definition: button.cpp:366
Color mDisabledColor
Definition: button.h:301
bool mKeyPressed
Definition: button.h:336
Color mEnabledColor2
Definition: button.h:300
int getTag() const
Definition: button.h:177
int mClickCount
Definition: button.h:315
Graphics::Alignment mAlignment
Definition: button.h:313
int mYOffset
Definition: button.h:325
void mouseEntered(MouseEvent &event)
Definition: button.cpp:883
std::string mDescription
Definition: button.h:292
void loadImageSet(const std::string &imageName)
Definition: button.cpp:395
const std::string & getCaption() const
Definition: button.h:222
Image ** mImages
Definition: button.h:307
int mXOffset
Definition: button.h:324
void widgetResized(const Event &event)
Definition: button.cpp:780
void keyReleased(KeyEvent &event)
Definition: button.cpp:842
bool mPressed
Definition: button.h:344
void widgetMoved(const Event &event)
Definition: button.cpp:785
bool mMousePressed
Definition: button.h:341
Definition: color.h:76
Definition: event.h:79
Definition: skin.h:37
Definition: widget.h:99
#define restrict
Definition: localconsts.h:165
#define A_WARN_UNUSED
Definition: localconsts.h:161
#define A_NONNULL(...)
Definition: localconsts.h:168
#define A_NONNULLPOINTER
Definition: localconsts.h:266
#define noexcept2
Definition: localconsts.h:50
#define final
Definition: localconsts.h:46
#define A_DELETE_COPY(func)
Definition: localconsts.h:53