GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/slider.h Lines: 2 2 100.0 %
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_SLIDER_H
67
#define GUI_WIDGETS_SLIDER_H
68
69
#include "enums/gui/orientation.h"
70
71
#include "listeners/keylistener.h"
72
#include "listeners/mouselistener.h"
73
74
#include "gui/widgets/widget.h"
75
76
#include "localconsts.h"
77
78
class ImageCollection;
79
80
/**
81
 * Slider widget. Same as the Guichan slider but with custom look.
82
 *
83
 * \ingroup GUI
84
 */
85
class Slider final : public Widget,
86
                     public MouseListener,
87
                     public KeyListener
88
{
89
    public:
90
        /**
91
         * Constructor with scale start equal to 0.
92
         */
93
        Slider(Widget2 *const widget,
94
               const double scaleEnd,
95
               const double stepLength);
96
97
        /**
98
         * Constructor.
99
         */
100
        Slider(Widget2 *const widget,
101
               const double scaleStart,
102
               const double scaleEnd,
103
               const double stepLength);
104
105
        A_DELETE_COPY(Slider)
106
107
        /**
108
         * Destructor.
109
         */
110
        ~Slider();
111
112
        /**
113
         * Update the alpha value to the graphic components.
114
         */
115
        void updateAlpha();
116
117
        /**
118
         * Draws the slider.
119
         */
120
        void draw(Graphics *const graphics) override final A_NONNULL(2);
121
122
        void safeDraw(Graphics *const graphics) override final A_NONNULL(2);
123
124
        /**
125
         * Called when the mouse enteres the widget area.
126
         */
127
        void mouseEntered(MouseEvent& event) override final;
128
129
        /**
130
         * Called when the mouse leaves the widget area.
131
         */
132
        void mouseExited(MouseEvent& event) override final;
133
134
        void mousePressed(MouseEvent &event) override final;
135
136
        void mouseDragged(MouseEvent &event) override final;
137
138
        void mouseWheelMovedUp(MouseEvent &event) override final;
139
140
        void mouseWheelMovedDown(MouseEvent &event) override final;
141
142
        void keyPressed(KeyEvent& event) override final;
143
144
        /**
145
         * Sets the scale of the slider.
146
         *
147
         * @param scaleStart The start value of the scale.
148
         * @param scaleEnd tThe end of value the scale.
149
         * @see getScaleStart, getScaleEnd
150
         */
151
        void setScale(const double scaleStart, const double scaleEnd);
152
153
        /**
154
         * Gets the start value of the scale.
155
         *
156
         * @return The start value of the scale.
157
         * @see setScaleStart, setScale
158
         */
159
        double getScaleStart() const
160
        { return mScaleStart; }
161
162
        /**
163
         * Sets the start value of the scale.
164
         *
165
         * @param scaleStart The start value of the scale.
166
         * @see getScaleStart
167
         */
168
        void setScaleStart(const double scaleStart)
169
        { mScaleStart = scaleStart; }
170
171
        /**
172
         * Gets the end value of the scale.
173
         *
174
         * @return The end value of the scale.
175
         * @see setScaleEnd, setScale
176
         */
177
        double getScaleEnd() const
178
        { return mScaleEnd; }
179
180
        /**
181
         * Sets the end value of the scale.
182
         *
183
         * @param scaleEnd The end value of the scale.
184
         * @see getScaleEnd
185
         */
186
        void setScaleEnd(const double scaleEnd)
187
        { mScaleEnd = scaleEnd; }
188
189
        /**
190
         * Sets the current selected value.
191
         *
192
         * @param value The current selected value.
193
         * @see getValue
194
         */
195
        void setValue(const double value);
196
197
        /**
198
         * Gets the current selected value.
199
         *
200
         * @return The current selected value.
201
         * @see setValue
202
         */
203
        double getValue() const
204
        { return mValue; }
205
206
        /**
207
         * Gets the length of the marker.
208
         *
209
         * @return The length of the marker.
210
         * @see setMarkerLength
211
         */
212
        int getMarkerLength() const
213
        { return mMarkerLength; }
214
215
        /**
216
         * Sets the length of the marker.
217
         *
218
         * @param length The length for the marker.
219
         * @see getMarkerLength
220
         */
221
        void setMarkerLength(const int length)
222
66
        { mMarkerLength = length; }
223
224
        /**
225
         * Sets the orientation of the slider. A slider can be drawn vertically
226
         * or horizontally.
227
         *
228
         * @param orientation The orientation of the slider.
229
         * @see getOrientation
230
         */
231
        void setOrientation(const OrientationT orientation)
232
        { mOrientation = orientation; }
233
234
        /**
235
         * Gets the orientation of the slider. A slider can be drawn vertically
236
         * or horizontally.
237
         *
238
         * @return The orientation of the slider.
239
         * @see setOrientation
240
         */
241
        OrientationT getOrientation() const
242
        { return mOrientation; }
243
244
        /**
245
         * Sets the step length. The step length is used when the keys LEFT
246
         * and RIGHT are pressed to step in the scale.
247
         *
248
         * @param length The step length.
249
         * @see getStepLength
250
         */
251
        void setStepLength(const double length)
252
        { mStepLength = length; }
253
254
        /**
255
         * Gets the step length. The step length is used when the keys LEFT
256
         * and RIGHT are pressed to step in the scale.
257
         *
258
         * @return the step length.
259
         * @see setStepLength
260
         */
261
        double getStepLength() const
262
        { return mStepLength; }
263
264
    private:
265
        /**
266
         * Used to initialize instances.
267
         */
268
        void init();
269
270
        /**
271
         * Converts a marker position to a value in the scale.
272
         *
273
         * @param position The position to convert.
274
         * @return A scale value corresponding to the position.
275
         * @see valueToMarkerPosition
276
         */
277
        double markerPositionToValue(const int position) const;
278
279
        /**
280
         * Converts a value to a marker position.
281
         *
282
         * @param value The value to convert.
283
         * @return A marker position corresponding to the value.
284
         * @see markerPositionToValue
285
         */
286
        int valueToMarkerPosition(const double value) const;
287
288
        /**
289
         * Gets the marker position of the current selected value.
290
         *
291
         * @return The marker position of the current selected value.
292
         */
293
        int getMarkerPosition() const
294
28
        { return valueToMarkerPosition(getValue()); }
295
296
        static ImageRect buttons[2];
297
        static float mAlpha;
298
        static int mInstances;
299
300
        /**
301
         * Holds the current selected value.
302
         */
303
        double mValue;
304
305
        /**
306
         * Holds the step length. The step length is used when the keys LEFT
307
         * and RIGHT are pressed to step in the scale.
308
         */
309
        double mStepLength;
310
311
        /**
312
         * Holds the start value of the scale.
313
         */
314
        double mScaleStart;
315
316
        /**
317
         * Holds the end value of the scale.
318
         */
319
        double mScaleEnd;
320
321
        /**
322
         * Holds the orientation of the slider. A slider can be drawn
323
         * vertically or horizontally.
324
         */
325
        OrientationT mOrientation;
326
327
        ImageCollection *mVertexes A_NONNULLPOINTER;
328
329
        /**
330
         * Holds the length of the marker.
331
         */
332
        int mMarkerLength;
333
334
        bool mHasMouse;
335
};
336
337
#endif  // GUI_WIDGETS_SLIDER_H