GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/characterviewsmall.cpp Lines: 1 67 1.5 %
Date: 2017-11-29 Branches: 2 80 2.5 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-2017  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "gui/widgets/characterviewsmall.h"
22
23
#include "gui/widgets/button.h"
24
#include "gui/widgets/characterdisplay.h"
25
#include "gui/widgets/label.h"
26
27
#include "utils/foreach.h"
28
#include "utils/stringutils.h"
29
30
#include "debug.h"
31
32
CharacterViewSmall::CharacterViewSmall(CharSelectDialog *const widget,
33
                                       STD_VECTOR<CharacterDisplay*>
34
                                       *const entries,
35
                                       const int padding) :
36
    CharacterViewBase(widget, padding),
37
    mSelectedEntry(nullptr),
38
    mPrevious(new Button(this, "<", "prev", this)),
39
    mNext(new Button(this, ">", "next", this)),
40
    mNumber(new Label(this, "??")),
41
    mCharacterEntries(entries)
42
{
43
    addKeyListener(widget);
44
    if (entries != nullptr)
45
    {
46
        FOR_EACHP (STD_VECTOR<CharacterDisplay*>::iterator,
47
                   it, entries)
48
        {
49
            add(*it);
50
        }
51
        const int sz = CAST_S32(mCharacterEntries->size());
52
        if (sz > 0)
53
        {
54
            mSelected = 0;
55
            mSelectedEntry = (*mCharacterEntries)[mSelected];
56
            mSelectedEntry->setVisible(Visible_true);
57
            mNumber->setCaption(strprintf("%d / %d", mSelected + 1, sz));
58
            mNumber->adjustSize();
59
        }
60
        else
61
        {
62
            mSelected = -1;
63
            mSelectedEntry = nullptr;
64
            mNumber->setCaption("0 / 0");
65
            mNumber->adjustSize();
66
        }
67
    }
68
    add(mPrevious);
69
    add(mNext);
70
    add(mNumber);
71
72
    setHeight(200);
73
}
74
75
CharacterViewSmall::~CharacterViewSmall()
76
{
77
    removeKeyListener(mParent);
78
}
79
80
void CharacterViewSmall::show(const int i)
81
{
82
    const int sz = CAST_S32(mCharacterEntries->size());
83
    if (sz <= 0)
84
        return;
85
    if (mSelectedEntry != nullptr)
86
        mSelectedEntry->setVisible(Visible_false);
87
    if (i >= sz)
88
        mSelected = 0;
89
    else if (i < 0)
90
        mSelected = sz - 1;
91
    else
92
        mSelected = i;
93
    mSelectedEntry = (*mCharacterEntries)[mSelected];
94
    mSelectedEntry->setVisible(Visible_true);
95
    mNumber->setCaption(strprintf("%d / %d", mSelected + 1, sz));
96
    mNumber->adjustSize();
97
}
98
99
void CharacterViewSmall::resize()
100
{
101
    const int sz = CAST_S32(mCharacterEntries->size());
102
    if (sz <= 0)
103
        return;
104
    const CharacterDisplay *const firtChar = (*mCharacterEntries)[0];
105
    const int w = mDimension.width;
106
    const int h = mDimension.height;
107
    const int x = (w - firtChar->getWidth()) / 2;
108
    const int y = (h - firtChar->getHeight()) / 2;
109
    FOR_EACHP (STD_VECTOR<CharacterDisplay*>::iterator,
110
               it, mCharacterEntries)
111
    {
112
        (*it)->setPosition(x, y);
113
    }
114
    const int y2 = (h - mPrevious->getHeight()) / 2;
115
    const int y3 = y2 - 55;
116
    mPrevious->setPosition(x - mPrevious->getWidth() - 10, y3);
117
    mNext->setPosition(w - x + 10, y3);
118
    mNumber->setPosition(10, y2);
119
}
120
121
void CharacterViewSmall::action(const ActionEvent &event)
122
{
123
    const std::string &eventId = event.getId();
124
    if (eventId == "next")
125
    {
126
        mSelected ++;
127
        show(mSelected);
128
        mParent->updateState();
129
    }
130
    else if (eventId == "prev")
131
    {
132
        mSelected --;
133
        show(mSelected);
134
        mParent->updateState();
135
    }
136

6
}