GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/button.h Lines: 8 13 61.5 %
Date: 2021-03-17 Branches: 0 0 0.0 %

Line Branch Exec Source
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
74
#include "listeners/focuslistener.h"
75
#include "listeners/keylistener.h"
76
#include "listeners/mouselistener.h"
77
#include "listeners/widgetlistener.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
198
const std::string BUTTON_PLAY = "buttonplay.png";
89
198
const std::string BUTTON_SKIN = "button";
90
198
const std::string BUTTON_PIN_SKIN = "buttonpin";
91
92
/**
93
 * Button widget. Same as the Guichan button but with custom look.
94
 *
95
 * \ingroup GUI
96
 */
97
class Button final : public Widget,
98
                     public MouseListener,
99
                     public KeyListener,
100
                     public FocusListener,
101
                     public WidgetListener
102
{
103
    public:
104
        /**
105
         * Default constructor.
106
         */
107
        Button(const Widget2 *const widget,
108
               const std::string &restrict skinName);
109
110
        /**
111
         * Constructor, sets the caption of the button to the given string and
112
         * adds the given action listener.
113
         */
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
120
        /**
121
         * Constructor, sets the caption of the button to the given string and
122
         * adds the given action listener.
123
         */
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
132
        /**
133
         * Constructor, sets the caption of the button to the given string and
134
         * adds the given action listener.
135
         */
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
144
        A_DELETE_COPY(Button)
145
146
        /**
147
         * Destructor.
148
         */
149
        ~Button() override final;
150
151
        /**
152
         * Draws the button.
153
         */
154
        void draw(Graphics *const graphics) override final A_NONNULL(2);
155
156
        void safeDraw(Graphics *const graphics) override final A_NONNULL(2);
157
158
        /**
159
         * Update the alpha value to the button components.
160
         */
161
        void updateAlpha();
162
163
        void mouseReleased(MouseEvent& event) override final;
164
165
        void setDescription(const std::string &text)
166
8
        { mDescription = text; }
167
168
        std::string getDescription() const noexcept2 A_WARN_UNUSED
169
        { return mDescription; }
170
171
        int getClickCount() const noexcept2 A_WARN_UNUSED
172
        { return mClickCount; }
173
174
        void setTag(int tag)
175
24
        { mTag = tag; }
176
177
        int getTag() const noexcept2 A_WARN_UNUSED
178
        { return mTag; }
179
180
        void setStick(bool b)
181
24
        { mStick = b; }
182
183
        void setPressed(bool b)
184
4
        { 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
206
        /**
207
         * Sets the caption of the button. It's advisable to call
208
         * adjustSize after setting of the caption to adjust the
209
         * button's size to fit the caption.
210
         *
211
         * @param caption The caption of the button.
212
         * @see getCaption, adjustSize
213
         */
214
        void setCaption(const std::string& caption)
215
64
        { mCaption = caption; mTextChanged = true; }
216
217
        /**
218
         * Gets the caption of the button.
219
         *
220
         * @return The caption of the button.
221
         */
222
        const std::string& getCaption() const noexcept2 A_WARN_UNUSED
223
        { return mCaption; }
224
225
        /**
226
         * Sets the alignment of the caption. The alignment is relative
227
         * to the center of the button.
228
         *
229
         * @param alignment The alignment of the caption.
230
         * @see getAlignment, Graphics
231
         */
232
        void setAlignment(Graphics::Alignment alignment) noexcept2
233
        { mAlignment = alignment; }
234
235
        /**
236
         * Gets the alignment of the caption.
237
         *
238
         * @return The alignment of the caption.
239
         * @see setAlignment, Graphics
240
         */
241
        Graphics::Alignment getAlignment() const noexcept2 A_WARN_UNUSED
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
267
            BUTTON_HIGHLIGHTED,   // 1
268
            BUTTON_PRESSED,       // 2
269
            BUTTON_DISABLED,      // 3
270
            BUTTON_COUNT          // 4 - Must be last.
271
        };
272
273
    private:
274
        /**
275
         * Checks if the button is pressed. Convenient method to use
276
         * when overloading the draw method of the button.
277
         *
278
         * @return True if the button is pressed, false otherwise.
279
         */
280
        bool isPressed() const;
281
282
        void init();
283
284
        static float mAlpha;
285
286
        Skin *mSkin[BUTTON_COUNT];  /**< Button state graphics */
287
        /**
288
         * Holds the caption of the button.
289
         */
290
        std::string mCaption;
291
292
        std::string mDescription;
293
294
        std::string mSkinName;
295
296
        TextChunk mTextChunk;
297
298
        ImageCollection *mVertexes2 A_NONNULLPOINTER;
299
        Color mEnabledColor;
300
        Color mEnabledColor2;
301
        Color mDisabledColor;
302
        Color mDisabledColor2;
303
        Color mHighlightedColor;
304
        Color mHighlightedColor2;
305
        Color mPressedColor;
306
        Color mPressedColor2;
307
        Image **mImages;
308
        ImageSet *mImageSet;
309
310
        /**
311
         * Holds the alignment of the caption.
312
         */
313
        Graphics::Alignment mAlignment;
314
315
        int mClickCount;
316
317
        /**
318
         * Holds the spacing between the border and the caption.
319
         */
320
        int mSpacing[BUTTON_COUNT];
321
322
        int mTag;
323
        int mMode;
324
        int mXOffset;
325
        int mYOffset;
326
        int mImageWidth;
327
        int mImageHeight;
328
        /**
329
         * True if the mouse is ontop of the button, false otherwise.
330
         */
331
        bool mHasMouse;
332
333
        /**
334
         * True if a key has been pressed, false otherwise.
335
         */
336
        bool mKeyPressed;
337
338
        /**
339
         * True if a mouse has been pressed, false otherwise.
340
         */
341
        bool mMousePressed;
342
343
        bool mStick;
344
        bool mPressed;
345
        bool mTextChanged;
346
};
347
348
#endif  // GUI_WIDGETS_BUTTON_H