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

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-2018  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
1
CharacterViewNormal::CharacterViewNormal(CharSelectDialog *const widget,
37
                                         STD_VECTOR<CharacterDisplay*>
38
                                         *const entries,
39
1
                                         const int padding) :
40
    CharacterViewBase(widget, padding),
41
    mCharacterEntries(entries),
42
2
    mRows(2)
43
{
44

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

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

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