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

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

3
}