GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/button.h Lines: 6 11 54.5 %
Date: 2017-11-29 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-2017  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
#ifndef GUI_WIDGETS_BUTTON_H
67
#define GUI_WIDGETS_BUTTON_H
68
69
#include "gui/fonts/textchunk.h"
70
71
#include "gui/widgets/widget.h"
72
73
#include "listeners/focuslistener.h"
74
#include "listeners/keylistener.h"
75
#include "listeners/mouselistener.h"
76
#include "listeners/widgetlistener.h"
77
78
#include "render/graphics.h"
79
80
#include "localconsts.h"
81
82
class Image;
83
class ImageCollection;
84
class ImageSet;
85
class Skin;
86
87
378
const std::string BUTTON_PLAY = "buttonplay.png";
88
89
/**
90
 * Button widget. Same as the Guichan button but with custom look.
91
 *
92
 * \ingroup GUI
93
 */
94
class Button final : public Widget,
95
                     public MouseListener,
96
                     public KeyListener,
97
                     public FocusListener,
98
                     public WidgetListener
99
{
100
    public:
101
        /**
102
         * Default constructor.
103
         */
104
        explicit Button(const Widget2 *const widget);
105
106
        /**
107
         * Constructor, sets the caption of the button to the given string and
108
         * adds the given action listener.
109
         */
110
        Button(const Widget2 *const widget,
111
               const std::string &restrict caption,
112
               const std::string &restrict actionEventId,
113
               ActionListener *const listener);
114
115
        /**
116
         * Constructor, sets the caption of the button to the given string and
117
         * adds the given action listener.
118
         */
119
        Button(const Widget2 *const widget,
120
               const std::string &restrict caption,
121
               const std::string &restrict imageName,
122
               const int imageWidth, const int imageHeight,
123
               const std::string &actionEventId,
124
               ActionListener *const listener);
125
126
        /**
127
         * Constructor, sets the caption of the button to the given string and
128
         * adds the given action listener.
129
         */
130
        Button(const Widget2 *const widget,
131
               const std::string &restrict imageName,
132
               const int imageWidth, const int imageHeight,
133
               const std::string &restrict actionEventId,
134
               ActionListener *const listener);
135
136
        /**
137
         * Constructor, sets the caption of the button to the given string and
138
         * adds the given action listener.
139
         */
140
        Button(const Widget2 *const widget,
141
               const std::string &restrict imageName,
142
               const std::string &restrict caption,
143
               const std::string &restrict actionEventId,
144
               ActionListener *const listener);
145
146
        A_DELETE_COPY(Button)
147
148
        /**
149
         * Destructor.
150
         */
151
        ~Button();
152
153
        /**
154
         * Draws the button.
155
         */
156
        void draw(Graphics *const graphics) override final A_NONNULL(2);
157
158
        void safeDraw(Graphics *const graphics) override final A_NONNULL(2);
159
160
        /**
161
         * Update the alpha value to the button components.
162
         */
163
        static void updateAlpha();
164
165
        void mouseReleased(MouseEvent& event) override final;
166
167
        void setDescription(const std::string &text)
168
16
        { mDescription = text; }
169
170
        std::string getDescription() const noexcept2 A_WARN_UNUSED
171
        { return mDescription; }
172
173
        int getClickCount() const noexcept2 A_WARN_UNUSED
174
        { return mClickCount; }
175
176
        void setTag(int tag)
177
48
        { mTag = tag; }
178
179
        int getTag() const noexcept2 A_WARN_UNUSED
180
        { return mTag; }
181
182
        void setStick(bool b)
183
48
        { mStick = b; }
184
185
        void setPressed(bool b)
186
8
        { mPressed = b; }
187
188
        void widgetResized(const Event &event) override final;
189
190
        void widgetMoved(const Event &event) override final;
191
192
        void widgetHidden(const Event &event) override final;
193
194
        void loadImage(const std::string &imageName);
195
196
        void loadImageSet(const std::string &imageName);
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
128
        { 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 Skin *button[BUTTON_COUNT];  /**< Button state graphics */
285
        static int mInstances;              /**< Number of button instances */
286
        static float mAlpha;
287
288
        /**
289
         * Holds the caption of the button.
290
         */
291
        std::string mCaption;
292
293
        std::string mDescription;
294
295
        TextChunk mTextChunk;
296
297
        ImageCollection *mVertexes2 A_NONNULLPOINTER;
298
        Color mEnabledColor;
299
        Color mEnabledColor2;
300
        Color mDisabledColor;
301
        Color mDisabledColor2;
302
        Color mHighlightedColor;
303
        Color mHighlightedColor2;
304
        Color mPressedColor;
305
        Color mPressedColor2;
306
        Image **mImages;
307
        ImageSet *mImageSet;
308
309
        /**
310
         * Holds the alignment of the caption.
311
         */
312
        Graphics::Alignment mAlignment;
313
314
        int mClickCount;
315
316
        /**
317
         * Holds the spacing between the border and the caption.
318
         */
319
        int mSpacing[BUTTON_COUNT];
320
321
        int mTag;
322
        int mMode;
323
        int mXOffset;
324
        int mYOffset;
325
        int mImageWidth;
326
        int mImageHeight;
327
        /**
328
         * True if the mouse is ontop of the button, false otherwise.
329
         */
330
        bool mHasMouse;
331
332
        /**
333
         * True if a key has been pressed, false otherwise.
334
         */
335
        bool mKeyPressed;
336
337
        /**
338
         * True if a mouse has been pressed, false otherwise.
339
         */
340
        bool mMousePressed;
341
342
        bool mStick;
343
        bool mPressed;
344
        bool mTextChanged;
345
};
346
347
#endif  // GUI_WIDGETS_BUTTON_H