GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/characterviewnormal.cpp Lines: 47 49 95.9 %
Date: 2017-11-29 Branches: 24 42 57.1 %

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/characterviewnormal.h"
22
23
#include "configuration.h"
24
25
#include "gui/widgets/characterdisplay.h"
26
27
#include "utils/foreach.h"
28
29
#include "debug.h"
30
31
namespace
32
{
33
    int perRowCount = 5;
34
}  // namespace
35
36
2
CharacterViewNormal::CharacterViewNormal(CharSelectDialog *const widget,
37
                                         STD_VECTOR<CharacterDisplay*>
38
                                         *const entries,
39
2
                                         const int padding) :
40
    CharacterViewBase(widget, padding),
41
    mCharacterEntries(entries),
42
4
    mRows(2)
43
{
44

2
    addKeyListener(widget);
45
2
    if (entries != nullptr)
46
    {
47
24
        FOR_EACHP (STD_VECTOR<CharacterDisplay*>::iterator,
48
                   it, entries)
49
        {
50
18
            CharacterDisplay *const character = *it;
51
18
            add(character);
52
18
            character->setVisible(Visible_true);
53
        }
54
4
        const size_t sz = mCharacterEntries->size();
55

2
        if (mSelected >= 0 && mSelected < CAST_S32(sz))
56
        {
57
4
            CharacterDisplay *const display = (*mCharacterEntries)[mSelected];
58
2
            if (display != nullptr)
59
                display->setSelect(false);
60
        }
61
2
        if (sz > 0)
62
        {
63
2
            mSelected = 0;
64
2
            CharacterDisplay *const display = (*mCharacterEntries)[0];
65
2
            display->setSelect(true);
66
4
            setWidth(display->getWidth() * perRowCount + mPadding * 2);
67
        }
68
        else
69
        {
70
            mSelected = -1;
71
        }
72
2
        mRows = CAST_S32(sz / perRowCount);
73
74
2
        if (mRows * perRowCount != CAST_S32(sz))
75
2
            mRows ++;
76
    }
77
78

8
    setHeight((105 + config.getIntValue("fontSize")) * mRows);
79
2
}
80
81
8
CharacterViewNormal::~CharacterViewNormal()
82
{
83
2
    removeKeyListener(mParent);
84
4
}
85
86
2
void CharacterViewNormal::show(const int i)
87
{
88
4
    const int sz = CAST_S32(mCharacterEntries->size());
89
2
    if (i >= 0 && i < sz)
90
    {
91
2
        if (mSelected >= 0)
92
4
            (*mCharacterEntries)[mSelected]->setSelect(false);
93
2
        mSelected = i;
94
4
        (*mCharacterEntries)[i]->setSelect(true);
95
    }
96
2
}
97
98
2
void CharacterViewNormal::resize()
99
{
100
4
    const size_t sz = mCharacterEntries->size();
101
2
    if (sz == 0)
102
        return;
103
2
    const CharacterDisplay *const firtChar = (*mCharacterEntries)[0];
104
2
    int y = 0;
105
4
    const int width = firtChar->getWidth();
106
4
    const int height = firtChar->getHeight();
107
2
    int x = 0;
108
20
    for (size_t f = 0; f < sz; f ++, x ++)
109
    {
110
18
        if (x >= perRowCount)
111
        {
112
2
            x = 0;
113
2
            y += height;
114
        }
115
36
        (*mCharacterEntries)[f]->setPosition(x * width, y);
116
    }
117
}
118
119
void CharacterViewNormal::action(const ActionEvent &event A_UNUSED)
120
{
121
}