GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/characterdisplay.cpp Lines: 36 67 53.7 %
Date: 2021-03-17 Branches: 26 76 34.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-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#include "gui/widgets/characterdisplay.h"
25
26
#include "gui/gui.h"
27
28
#include "gui/windows/charselectdialog.h"
29
30
#include "gui/popups/textpopup.h"
31
32
#include "gui/widgets/containerplacer.h"
33
#include "gui/widgets/label.h"
34
#include "gui/widgets/layouthelper.h"
35
36
#include "utils/gettext.h"
37
#include "utils/stringutils.h"
38
39
#include "resources/db/unitsdb.h"
40
41
#include "net/character.h"
42
43
#include "debug.h"
44
45
9
CharacterDisplay::CharacterDisplay(const Widget2 *const widget,
46
9
                                   CharSelectDialog *const charSelectDialog) :
47
    Container(widget),
48
    MouseListener(),
49
    WidgetListener(),
50
    mCharacter(nullptr),
51

36
    mPlayerBox(new PlayerBox(nullptr, std::string(), std::string())),
52

72
    mName(new Label(this, "wwwwwwwwwwwwwwwwwwwwwwww"))
53
{
54
36
    mPlayerBox->setActionEventId("select");
55

9
    mPlayerBox->addActionListener(charSelectDialog);
56
36
    setActionEventId("use");
57
9
    addActionListener(charSelectDialog);
58
59
18
    LayoutHelper h(this);
60
9
    ContainerPlacer placer = h.getPlacer(0, 0);
61
62
9
    placer(0, 0, mPlayerBox, 3, 5);
63
9
    placer(0, 5, mName, 3, 1);
64
65
9
    update();
66
67
18
    mName->setAlignment(Graphics::CENTER);
68
9
    mName->adjustSize();
69
70

9
    if (mainGraphics->getWidth() > 800)
71
        setWidth(120);
72
    else
73
9
        setWidth(80);
74
9
    setHeight(120);
75
9
    addMouseListener(this);
76
9
    addWidgetListener(this);
77
9
}
78
79
27
CharacterDisplay::~CharacterDisplay()
80
{
81
9
}
82
83
9
void CharacterDisplay::setCharacter(Net::Character *const character)
84
{
85
9
    if (mCharacter == character)
86
        return;
87
88
    mCharacter = character;
89
    mPlayerBox->setPlayer(character != nullptr ? character->dummy : nullptr);
90
    update();
91
}
92
93
void CharacterDisplay::requestFocus()
94
{
95
}
96
97
void CharacterDisplay::setActive(const bool active A_UNUSED)
98
{
99
}
100
101
9
void CharacterDisplay::update()
102
{
103
9
    if (mCharacter != nullptr)
104
        mName->setCaption(mCharacter->dummy->getName());
105
    else
106
27
        mName->setCaption("");
107
18
    const int width = mPlayerBox->getWidth();
108
9
    mName->resizeTo(width, width);
109
110
9
    distributeResizedEvent();
111
9
}
112
113
9
void CharacterDisplay::widgetHidden(const Event &event A_UNUSED)
114
{
115
9
    if (textPopup != nullptr)
116
        textPopup->setVisible(Visible_false);
117
9
}
118
119
void CharacterDisplay::mouseExited(MouseEvent &event A_UNUSED)
120
{
121
    if (textPopup != nullptr)
122
        textPopup->setVisible(Visible_false);
123
}
124
125
void CharacterDisplay::mouseMoved(MouseEvent &event A_UNUSED)
126
{
127
    if (gui == nullptr ||
128
        textPopup == nullptr ||
129
        mCharacter == nullptr)
130
    {
131
        return;
132
    }
133
134
    int mouseX = 0;
135
    int mouseY = 0;
136
    Gui::getMouseState(mouseX, mouseY);
137
    const std::string &name = mName->getCaption();
138
    if (!name.empty())
139
    {
140
        textPopup->show(mouseX, mouseY,
141
            name,
142
            // TRANSLATORS: character level
143
            strprintf(_("Level: %u"),
144
            CAST_U32(
145
            mCharacter->data.mAttributes[Attributes::PLAYER_BASE_LEVEL])),
146
            // TRANSLATORS: character money
147
            strprintf(_("Money: %s"), UnitsDb::formatCurrency64(
148
            mCharacter->data.mAttributes[Attributes::MONEY]).c_str()));
149
    }
150
    else
151
    {
152
        textPopup->setVisible(Visible_false);
153
    }
154
}
155
156
void CharacterDisplay::mousePressed(MouseEvent &event)
157
{
158
    event.consume();
159
    if (event.getClickCount() == 2)
160
        distributeActionEvent();
161
2
}