GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/characterdisplay.cpp Lines: 36 67 53.7 %
Date: 2017-11-29 Branches: 29 82 35.4 %

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

18
    mPlayerBox(new PlayerBox(nullptr)),
51


234
    mName(new Label(this, "wwwwwwwwwwwwwwwwwwwwwwww"))
52
{
53
90
    mPlayerBox->setActionEventId("select");
54

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

18
    if (mainGraphics->getWidth() > 800)
70
        setWidth(120);
71
    else
72
18
        setWidth(80);
73
18
    setHeight(120);
74
18
    addMouseListener(this);
75
18
    addWidgetListener(this);
76
18
}
77
78
54
CharacterDisplay::~CharacterDisplay()
79
{
80
18
}
81
82
18
void CharacterDisplay::setCharacter(Net::Character *const character)
83
{
84
18
    if (mCharacter == character)
85
        return;
86
87
    mCharacter = character;
88
    mPlayerBox->setPlayer(character != nullptr ? character->dummy : nullptr);
89
    update();
90
}
91
92
void CharacterDisplay::requestFocus()
93
{
94
}
95
96
void CharacterDisplay::setActive(const bool active A_UNUSED)
97
{
98
}
99
100
18
void CharacterDisplay::update()
101
{
102
18
    if (mCharacter != nullptr)
103
        mName->setCaption(mCharacter->dummy->getName());
104
    else
105

72
        mName->setCaption("");
106
36
    const int width = mPlayerBox->getWidth();
107
18
    mName->resizeTo(width, width);
108
109
18
    distributeResizedEvent();
110
18
}
111
112
18
void CharacterDisplay::widgetHidden(const Event &event A_UNUSED)
113
{
114
18
    if (textPopup != nullptr)
115
        textPopup->setVisible(Visible_false);
116
18
}
117
118
void CharacterDisplay::mouseExited(MouseEvent &event A_UNUSED)
119
{
120
    if (textPopup != nullptr)
121
        textPopup->setVisible(Visible_false);
122
}
123
124
void CharacterDisplay::mouseMoved(MouseEvent &event A_UNUSED)
125
{
126
    if (gui == nullptr ||
127
        textPopup == nullptr ||
128
        mCharacter == nullptr)
129
    {
130
        return;
131
    }
132
133
    int mouseX = 0;
134
    int mouseY = 0;
135
    Gui::getMouseState(mouseX, mouseY);
136
    const std::string &name = mName->getCaption();
137
    if (!name.empty())
138
    {
139
        textPopup->show(mouseX, mouseY,
140
            name,
141
            // TRANSLATORS: character level
142
            strprintf(_("Level: %u"),
143
            CAST_U32(
144
            mCharacter->data.mAttributes[Attributes::PLAYER_BASE_LEVEL])),
145
            // TRANSLATORS: character money
146
            strprintf(_("Money: %s"), UnitsDb::formatCurrency64(
147
            mCharacter->data.mAttributes[Attributes::MONEY]).c_str()));
148
    }
149
    else
150
    {
151
        textPopup->setVisible(Visible_false);
152
    }
153
}
154
155
void CharacterDisplay::mousePressed(MouseEvent &event)
156
{
157
    event.consume();
158
    if (event.getClickCount() == 2)
159
        distributeActionEvent();
160
4
}