GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/slider.cpp Lines: 94 227 41.4 %
Date: 2017-11-29 Branches: 42 198 21.2 %

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
#include "gui/widgets/slider.h"
67
68
#include "settings.h"
69
70
#include "enums/gui/slidergrid.h"
71
72
#include "gui/gui.h"
73
74
#include "utils/delete2.h"
75
76
#include "resources/imagerect.h"
77
78
#include "resources/image/image.h"
79
80
#include "render/graphics.h"
81
82
#include "render/vertexes/imagecollection.h"
83
84
#include "debug.h"
85
86
10
ImageRect Slider::buttons[2];
87
float Slider::mAlpha = 1.0;
88
int Slider::mInstances = 0;
89
90
6
static std::string const data[2] =
91
{
92
    "slider.xml",
93
    "slider_highlighted.xml"
94

10
};
95
96
12
Slider::Slider(Widget2 *const widget,
97
               const double scaleEnd,
98
12
               const double stepLength) :
99
    Widget(widget),
100
    MouseListener(),
101
    KeyListener(),
102
    mValue(0),
103
    mStepLength(stepLength),
104
    mScaleStart(0),
105
    mScaleEnd(scaleEnd),
106
    mOrientation(Orientation::HORIZONTAL),
107

12
    mVertexes(new ImageCollection),
108
    mMarkerLength(10),
109
48
    mHasMouse(false)
110
{
111
12
    init();
112
12
}
113
114
54
Slider::Slider(Widget2 *const widget,
115
               const double scaleStart,
116
               const double scaleEnd,
117
54
               const double stepLength) :
118
    Widget(widget),
119
    MouseListener(),
120
    KeyListener(),
121
    mValue(scaleStart),
122
    mStepLength(stepLength),
123
    mScaleStart(scaleStart),
124
    mScaleEnd(scaleEnd),
125
    mOrientation(Orientation::HORIZONTAL),
126

54
    mVertexes(new ImageCollection),
127
    mMarkerLength(10),
128
216
    mHasMouse(false)
129
{
130
54
    init();
131
54
}
132
133
330
Slider::~Slider()
134
{
135
66
    if (gui != nullptr)
136
66
        gui->removeDragged(this);
137
138
66
    delete2(mVertexes);
139
66
    mInstances--;
140
66
    if (mInstances == 0)
141
    {
142
90
        for (int mode = 0; mode < 2; mode ++)
143
36
            Theme::unloadRect(buttons[mode]);
144
    }
145
132
}
146
147
66
void Slider::init()
148
{
149
66
    mAllowLogic = false;
150
66
    setFocusable(true);
151
132
    setFrameSize(1);
152
153
66
    addMouseListener(this);
154
66
    addKeyListener(this);
155
156
132
    setFrameSize(0);
157
158
    // Load resources
159
66
    if (mInstances == 0)
160
    {
161
18
        if (theme != nullptr)
162
        {
163
90
            for (int mode = 0; mode < 2; mode ++)
164

144
                theme->loadRect(buttons[mode], data[mode], "slider.xml", 0, 8);
165
        }
166
18
        updateAlpha();
167
    }
168
169
66
    mInstances++;
170
171
66
    if (buttons[0].grid[SliderGrid::HGRIP] != nullptr)
172
132
        setMarkerLength(buttons[0].grid[SliderGrid::HGRIP]->getWidth());
173
66
}
174
175
32
void Slider::updateAlpha()
176
{
177
    const float alpha = std::max(settings.guiAlpha,
178
96
        theme->getMinimumOpacity());
179
180
32
    if (alpha != mAlpha)
181
    {
182
        mAlpha = alpha;
183
        for (int f = 0; f < 2; f ++)
184
        {
185
            for (int d = 0; d < SliderGrid::SLIDER_MAX; d ++)
186
            {
187
                if (buttons[f].grid[d] != nullptr)
188
                    buttons[f].grid[d]->setAlpha(mAlpha);
189
            }
190
        }
191
    }
192
32
}
193
194
14
void Slider::draw(Graphics *const graphics)
195
{
196
    BLOCK_START("Slider::draw")
197

28
    if ((buttons[0].grid[SliderGrid::HSTART] == nullptr) ||
198
28
        (buttons[1].grid[SliderGrid::HSTART] == nullptr) ||
199
14
        (buttons[0].grid[SliderGrid::HEND] == nullptr))
200
    {
201
        BLOCK_END("Slider::draw")
202
        return;
203
    }
204
205
28
    int w = getWidth();
206
28
    const int h = getHeight();
207
28
    const int y = mHasMouse ?
208
        (h - buttons[1].grid[SliderGrid::HSTART]->getHeight()) / 2 :
209
42
        (h - buttons[0].grid[SliderGrid::HSTART]->getHeight()) / 2;
210
211
14
    updateAlpha();
212
213

14
    if (mRedraw || graphics->getRedraw())
214
    {
215
14
        int x = 0;
216
14
        mRedraw = false;
217
14
        mVertexes->clear();
218
14
        if (!mHasMouse)
219
        {
220
14
            graphics->calcTileCollection(mVertexes,
221
14
                buttons[0].grid[SliderGrid::HSTART],
222
14
                x, y);
223
224
28
            const int width = buttons[0].grid[SliderGrid::HSTART]->getWidth();
225
28
            w -= width + buttons[0].grid[SliderGrid::HEND]->getWidth();
226
14
            x += width;
227
228
14
            if (buttons[0].grid[SliderGrid::HMID] != nullptr)
229
            {
230
14
                const Image *const hMid = buttons[0].grid[SliderGrid::HMID];
231
14
                graphics->calcPattern(mVertexes,
232
                    hMid,
233
                    x, y,
234
14
                    w, hMid->getHeight());
235
            }
236
237
14
            x += w;
238
14
            graphics->calcTileCollection(mVertexes,
239
14
                buttons[0].grid[SliderGrid::HEND],
240
14
                x, y);
241
242
14
            const Image *const img = buttons[0].grid[SliderGrid::HGRIP];
243
14
            if (img != nullptr)
244
            {
245
28
                graphics->calcTileCollection(mVertexes,
246
                    img,
247
                    getMarkerPosition(),
248
28
                    (mDimension.height - img->getHeight()) / 2);
249
            }
250
        }
251
        else
252
        {
253
            graphics->calcTileCollection(mVertexes,
254
                buttons[1].grid[SliderGrid::HSTART],
255
                x, y);
256
257
            const int width = buttons[1].grid[SliderGrid::HSTART]->getWidth();
258
            w -= width;
259
            if (buttons[1].grid[SliderGrid::HEND] != nullptr)
260
                w -= buttons[1].grid[SliderGrid::HEND]->getWidth();
261
            x += width;
262
263
            if (buttons[1].grid[SliderGrid::HMID] != nullptr)
264
            {
265
                const Image *const hMid = buttons[1].grid[SliderGrid::HMID];
266
                graphics->calcPattern(mVertexes,
267
                    hMid,
268
                    x, y,
269
                    w, hMid->getHeight());
270
            }
271
272
            x += w;
273
            if (buttons[1].grid[SliderGrid::HEND] != nullptr)
274
            {
275
                graphics->calcTileCollection(mVertexes,
276
                    buttons[1].grid[SliderGrid::HEND], x, y);
277
            }
278
279
            const Image *const img = buttons[1].grid[SliderGrid::HGRIP];
280
            if (img != nullptr)
281
            {
282
                graphics->calcTileCollection(mVertexes,
283
                    img,
284
                    getMarkerPosition(),
285
                    (mDimension.height - img->getHeight()) / 2);
286
            }
287
        }
288
14
        graphics->finalize(mVertexes);
289
    }
290
14
    graphics->drawTileCollection(mVertexes);
291
292
    BLOCK_END("Slider::draw")
293
}
294
295
void Slider::safeDraw(Graphics *const graphics)
296
{
297
    BLOCK_START("Slider::draw")
298
    if ((buttons[0].grid[SliderGrid::HSTART] == nullptr) ||
299
        (buttons[1].grid[SliderGrid::HSTART] == nullptr) ||
300
        (buttons[0].grid[SliderGrid::HEND] == nullptr))
301
    {
302
        BLOCK_END("Slider::draw")
303
        return;
304
    }
305
306
    int w = getWidth();
307
    const int h = getHeight();
308
    int x = 0;
309
    const int y = mHasMouse ?
310
        (h - buttons[1].grid[SliderGrid::HSTART]->getHeight()) / 2 :
311
        (h - buttons[0].grid[SliderGrid::HSTART]->getHeight()) / 2;
312
313
    updateAlpha();
314
315
    if (!mHasMouse)
316
    {
317
        graphics->drawImage(buttons[0].grid[SliderGrid::HSTART], x, y);
318
        const int width = buttons[0].grid[SliderGrid::HSTART]->getWidth();
319
        w -= width + buttons[0].grid[SliderGrid::HEND]->getWidth();
320
        x += width;
321
322
        if (buttons[0].grid[SliderGrid::HMID] != nullptr)
323
        {
324
            const Image *const hMid = buttons[0].grid[SliderGrid::HMID];
325
            graphics->drawPattern(hMid, x, y, w, hMid->getHeight());
326
        }
327
328
        x += w;
329
        graphics->drawImage(buttons[0].grid[SliderGrid::HEND], x, y);
330
331
        const Image *const img = buttons[0].grid[SliderGrid::HGRIP];
332
        if (img != nullptr)
333
        {
334
            graphics->drawImage(img, getMarkerPosition(),
335
                (mDimension.height - img->getHeight()) / 2);
336
        }
337
    }
338
    else
339
    {
340
        graphics->drawImage(buttons[1].grid[SliderGrid::HSTART], x, y);
341
342
        const int width = buttons[1].grid[SliderGrid::HSTART]->getWidth();
343
        w -= width;
344
        if (buttons[1].grid[SliderGrid::HEND] != nullptr)
345
            w -= buttons[1].grid[SliderGrid::HEND]->getWidth();
346
        x += width;
347
348
        if (buttons[1].grid[SliderGrid::HMID] != nullptr)
349
        {
350
            const Image *const hMid = buttons[1].grid[SliderGrid::HMID];
351
            graphics->drawPattern(hMid, x, y, w, hMid->getHeight());
352
        }
353
354
        x += w;
355
        if (buttons[1].grid[SliderGrid::HEND] != nullptr)
356
            graphics->drawImage(buttons[1].grid[SliderGrid::HEND], x, y);
357
358
        const Image *const img = buttons[1].grid[SliderGrid::HGRIP];
359
        if (img != nullptr)
360
        {
361
            graphics->drawImage(img, getMarkerPosition(),
362
                (mDimension.height - img->getHeight()) / 2);
363
        }
364
    }
365
366
    BLOCK_END("Slider::draw")
367
}
368
369
void Slider::mouseEntered(MouseEvent& event A_UNUSED)
370
{
371
    mHasMouse = true;
372
    mRedraw = true;
373
}
374
375
void Slider::mouseExited(MouseEvent& event A_UNUSED)
376
{
377
    mHasMouse = false;
378
    mRedraw = true;
379
}
380
381
void Slider::mousePressed(MouseEvent &event)
382
{
383
    const int x = event.getX();
384
    const int y = event.getY();
385
    const int width = mDimension.width;
386
    const int height = mDimension.height;
387
388
    if (event.getButton() == MouseButton::LEFT
389
        && x >= 0 && x <= width && y >= 0 && y <= height)
390
    {
391
        event.consume();
392
        if (mOrientation == Orientation::HORIZONTAL)
393
            setValue(markerPositionToValue(x - mMarkerLength / 2));
394
        else
395
            setValue(markerPositionToValue(height - y - mMarkerLength / 2));
396
        distributeActionEvent();
397
    }
398
}
399
400
void Slider::mouseDragged(MouseEvent &event)
401
{
402
    if (mOrientation == Orientation::HORIZONTAL)
403
    {
404
        setValue(markerPositionToValue(event.getX() - mMarkerLength / 2));
405
    }
406
    else
407
    {
408
        setValue(markerPositionToValue(
409
            mDimension.height - event.getY() - mMarkerLength / 2));
410
    }
411
412
    distributeActionEvent();
413
414
    event.consume();
415
}
416
417
void Slider::mouseWheelMovedUp(MouseEvent &event)
418
{
419
    setValue(mValue + mStepLength);
420
    distributeActionEvent();
421
    event.consume();
422
}
423
424
void Slider::mouseWheelMovedDown(MouseEvent &event)
425
{
426
    setValue(mValue - mStepLength);
427
    distributeActionEvent();
428
    event.consume();
429
}
430
431
void Slider::keyPressed(KeyEvent& event)
432
{
433
    const InputActionT action = event.getActionId();
434
435
    if (mOrientation == Orientation::HORIZONTAL)
436
    {
437
        if (action == InputAction::GUI_RIGHT)
438
        {
439
            setValue(mValue + mStepLength);
440
            distributeActionEvent();
441
            event.consume();
442
        }
443
        else if (action == InputAction::GUI_LEFT)
444
        {
445
            setValue(mValue - mStepLength);
446
            distributeActionEvent();
447
            event.consume();
448
        }
449
    }
450
    else
451
    {
452
        if (action == InputAction::GUI_UP)
453
        {
454
            setValue(mValue + mStepLength);
455
            distributeActionEvent();
456
            event.consume();
457
        }
458
        else if (action == InputAction::GUI_DOWN)
459
        {
460
            setValue(mValue - mStepLength);
461
            distributeActionEvent();
462
            event.consume();
463
        }
464
    }
465
}
466
467
void Slider::setScale(const double scaleStart, const double scaleEnd)
468
{
469
    mScaleStart = scaleStart;
470
    mScaleEnd = scaleEnd;
471
}
472
473
56
void Slider::setValue(const double value)
474
{
475
56
    mRedraw = true;
476





56
    if (value > mScaleEnd)
477
        mValue = mScaleEnd;
478





56
    else if (value < mScaleStart)
479
        mValue = mScaleStart;
480
    else
481
56
        mValue = value;
482
112
    mValue = CAST_S32((mValue - mScaleStart) / mStepLength)
483
56
        * mStepLength + mScaleStart;
484
56
}
485
486
double Slider::markerPositionToValue(const int v) const
487
{
488
    int w;
489
    if (mOrientation == Orientation::HORIZONTAL)
490
        w = mDimension.width;
491
    else
492
        w = mDimension.height;
493
494
    const double pos = v / (static_cast<double>(w) - mMarkerLength);
495
    return (1.0 - pos) * mScaleStart + pos * mScaleEnd;
496
}
497
498
int Slider::valueToMarkerPosition(const double value) const
499
{
500
    int v;
501


14
    if (mOrientation == Orientation::HORIZONTAL)
502
14
        v = mDimension.width;
503
    else
504
        v = mDimension.height;
505
506
28
    const int w = CAST_S32((v - mMarkerLength)
507
14
            * (value  - mScaleStart)
508
14
            / (mScaleEnd - mScaleStart));
509
510


14
    if (w < 0)
511
        return 0;
512
513


14
    if (w > v - mMarkerLength)
514
        return v - mMarkerLength;
515
516
    return w;
517

6
}