GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/button.h Lines: 8 13 61.5 %
Date: 2018-05-19 03:07:18 Branches: 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-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
#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
390
const std::string BUTTON_PLAY = "buttonplay.png";
88
390
const std::string BUTTON_SKIN = "button";
89
390
const std::string BUTTON_PIN_SKIN = "buttonpin";
90
91
/**
92
 * Button widget. Same as the Guichan button but with custom look.
93
 *
94
 * \ingroup GUI
95
 */
96
class Button final : public Widget,
97
                     public MouseListener,
98
                     public KeyListener,
99
                     public FocusListener,
100
                     public WidgetListener
101
{
102
    public:
103
        /**
104
         * Default constructor.
105
         */
106
        Button(const Widget2 *const widget,
107
               const std::string &restrict skinName);
108
109
        /**
110
         * Constructor, sets the caption of the button to the given string and
111
         * adds the given action listener.
112
         */
113
        Button(const Widget2 *const widget,
114
               const std::string &restrict caption,
115
               const std::string &restrict actionEventId,
116
               const std::string &restrict skinName,
117
               ActionListener *const listener);
118
119
        /**
120
         * Constructor, sets the caption of the button to the given string and
121
         * adds the given action listener.
122
         */
123
        Button(const Widget2 *const widget,
124
               const std::string &restrict caption,
125
               const std::string &restrict imageName,
126
               const int imageWidth, const int imageHeight,
127
               const std::string &restrict actionEventId,
128
               const std::string &restrict skinName,
129
               ActionListener *const listener);
130
131
        /**
132
         * Constructor, sets the caption of the button to the given string and
133
         * adds the given action listener.
134
         */
135
        Button(const Widget2 *const widget,
136
               const std::string &restrict imageName,
137
               const int imageWidth,
138
               const int imageHeight,
139
               const std::string &restrict actionEventId,
140
               const std::string &restrict skinName,
141
               ActionListener *const listener);
142
143
        A_DELETE_COPY(Button)
144
145
        /**
146
         * Destructor.
147
         */
148
        ~Button() override final;
149
150
        /**
151
         * Draws the button.
152
         */
153
        void draw(Graphics *const graphics) override final A_NONNULL(2);
154
155
        void safeDraw(Graphics *const graphics) override final A_NONNULL(2);
156
157
        /**
158
         * Update the alpha value to the button components.
159
         */
160
        void updateAlpha();
161
162
        void mouseReleased(MouseEvent& event) override final;
163
164
        void setDescription(const std::string &text)
165
16
        { mDescription = text; }
166
167
        std::string getDescription() const noexcept2 A_WARN_UNUSED
168
        { return mDescription; }
169
170
        int getClickCount() const noexcept2 A_WARN_UNUSED
171
        { return mClickCount; }
172
173
        void setTag(int tag)
174
48
        { mTag = tag; }
175
176
        int getTag() const noexcept2 A_WARN_UNUSED
177
        { return mTag; }
178
179
        void setStick(bool b)
180
48
        { mStick = b; }
181
182
        void setPressed(bool b)
183
8
        { mPressed = b; }
184
185
        void widgetResized(const Event &event) override final;
186
187
        void widgetMoved(const Event &event) override final;
188
189
        void widgetHidden(const Event &event) override final;
190
191
        void loadImage(const std::string &imageName);
192
193
        void loadImageSet(const std::string &imageName);
194
195
        void adjustSize();
196
197
        void keyPressed(KeyEvent &event) override final;
198
199
        void keyReleased(KeyEvent &event) override final;
200
201
        bool isPressed2() const A_WARN_UNUSED;
202
203
        /**
204
         * Sets the caption of the button. It's advisable to call
205
         * adjustSize after setting of the caption to adjust the
206
         * button's size to fit the caption.
207
         *
208
         * @param caption The caption of the button.
209
         * @see getCaption, adjustSize
210
         */
211
        void setCaption(const std::string& caption)
212
128
        { mCaption = caption; mTextChanged = true; }
213
214
        /**
215
         * Gets the caption of the button.
216
         *
217
         * @return The caption of the button.
218
         */
219
        const std::string& getCaption() const noexcept2 A_WARN_UNUSED
220
        { return mCaption; }
221
222
        /**
223
         * Sets the alignment of the caption. The alignment is relative
224
         * to the center of the button.
225
         *
226
         * @param alignment The alignment of the caption.
227
         * @see getAlignment, Graphics
228
         */
229
        void setAlignment(Graphics::Alignment alignment) noexcept2
230
        { mAlignment = alignment; }
231
232
        /**
233
         * Gets the alignment of the caption.
234
         *
235
         * @return The alignment of the caption.
236
         * @see setAlignment, Graphics
237
         */
238
        Graphics::Alignment getAlignment() const noexcept2 A_WARN_UNUSED
239
        { return mAlignment; }
240
241
        void focusLost(const Event& event) override final;
242
243
        void mousePressed(MouseEvent& event) override final;
244
245
        void mouseEntered(MouseEvent& event) override final;
246
247
        void mouseExited(MouseEvent& event) override final;
248
249
        void mouseDragged(MouseEvent& event) override final;
250
251
        void setParent(Widget *widget) override final;
252
253
        void setWindow(Widget *const widget) override final;
254
255
        void setImageWidth(const int width) noexcept2
256
        { mImageWidth = width; }
257
258
        void setImageHeight(const int height) noexcept2
259
        { mImageHeight = height; }
260
261
        enum
262
        {
263
            BUTTON_STANDARD = 0,  // 0
264
            BUTTON_HIGHLIGHTED,   // 1
265
            BUTTON_PRESSED,       // 2
266
            BUTTON_DISABLED,      // 3
267
            BUTTON_COUNT          // 4 - Must be last.
268
        };
269
270
    private:
271
        /**
272
         * Checks if the button is pressed. Convenient method to use
273
         * when overloading the draw method of the button.
274
         *
275
         * @return True if the button is pressed, false otherwise.
276
         */
277
        bool isPressed() const;
278
279
        void init();
280
281
        static float mAlpha;
282
283
        Skin *mSkin[BUTTON_COUNT];  /**< Button state graphics */
284
        /**
285
         * Holds the caption of the button.
286
         */
287
        std::string mCaption;
288
289
        std::string mDescription;
290
291
        std::string mSkinName;
292
293
        TextChunk mTextChunk;
294
295
        ImageCollection *mVertexes2 A_NONNULLPOINTER;
296
        Color mEnabledColor;
297
        Color mEnabledColor2;
298
        Color mDisabledColor;
299
        Color mDisabledColor2;
300
        Color mHighlightedColor;
301
        Color mHighlightedColor2;
302
        Color mPressedColor;
303
        Color mPressedColor2;
304
        Image **mImages;
305
        ImageSet *mImageSet;
306
307
        /**
308
         * Holds the alignment of the caption.
309
         */
310
        Graphics::Alignment mAlignment;
311
312
        int mClickCount;
313
314
        /**
315
         * Holds the spacing between the border and the caption.
316
         */
317
        int mSpacing[BUTTON_COUNT];
318
319
        int mTag;
320
        int mMode;
321
        int mXOffset;
322
        int mYOffset;
323
        int mImageWidth;
324
        int mImageHeight;
325
        /**
326
         * True if the mouse is ontop of the button, false otherwise.
327
         */
328
        bool mHasMouse;
329
330
        /**
331
         * True if a key has been pressed, false otherwise.
332
         */
333
        bool mKeyPressed;
334
335
        /**
336
         * True if a mouse has been pressed, false otherwise.
337
         */
338
        bool mMousePressed;
339
340
        bool mStick;
341
        bool mPressed;
342
        bool mTextChanged;
343
};
344
345
#endif  // GUI_WIDGETS_BUTTON_H