GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/popups/beingpopup.cpp Lines: 25 162 15.4 %
Date: 2017-11-29 Branches: 17 150 11.3 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2010  The Mana Developers
4
 *  Copyright (C) 2011-2017  The ManaPlus Developers
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/popups/beingpopup.h"
23
24
#include "being/being.h"
25
#include "being/homunculusinfo.h"
26
#include "being/petinfo.h"
27
#include "being/playerinfo.h"
28
#include "being/playerrelations.h"
29
30
#include "gui/gui.h"
31
32
#include "gui/fonts/font.h"
33
34
#include "gui/widgets/label.h"
35
36
#include "resources/chatobject.h"
37
38
#include "resources/db/groupdb.h"
39
40
#include "utils/gettext.h"
41
#include "utils/foreach.h"
42
#include "utils/stringutils.h"
43
44
#include "debug.h"
45
46
BeingPopup *beingPopup = nullptr;
47
48
4
BeingPopup::BeingPopup() :
49
    Popup("BeingPopup", "beingpopup.xml"),
50

4
    mBeingName(new Label(this, "A")),
51


48
    mLabels()
52
{
53
    // Being Name
54
4
    mBeingName->setFont(boldFont);
55
4
    mBeingName->setPosition(0, 0);
56
57
8
    const int fontHeight = mBeingName->getHeight();
58
4
    setMinHeight(fontHeight);
59
4
    addLabels(fontHeight);
60
4
}
61
62
12
BeingPopup::~BeingPopup()
63
{
64
8
}
65
66
4
void BeingPopup::postInit()
67
{
68
8
    Popup::postInit();
69
4
    add(mBeingName);
70
60
    FOR_EACH (STD_VECTOR<Label*>::iterator, it, mLabels)
71
    {
72
44
        add(*it);
73
    }
74
4
}
75
76
4
void BeingPopup::addLabels(const int fontHeight)
77
{
78
48
    for (int f = 0; f < 11; f ++)
79
    {
80

176
        Label *const label = new Label(this, "A");
81
44
        label->setPosition(0, fontHeight * (f + 1));
82
132
        label->setForegroundColorAll(getThemeColor(ThemeColorId::POPUP),
83
            getThemeColor(ThemeColorId::POPUP_OUTLINE));
84
44
        mLabels.push_back(label);
85
    }
86
4
}
87
88
void BeingPopup::show(const int x, const int y, Being *const b)
89
{
90
    if (b == nullptr)
91
    {
92
        setVisible(Visible_false);
93
        return;
94
    }
95
96
    int num = 0;
97
    Label *ptr = nullptr;
98
    b->updateComment();
99
100
    if (b->getType() == ActorType::Npc && b->getComment().empty())
101
    {
102
        setVisible(Visible_false);
103
        return;
104
    }
105
106
    mBeingName->setCaption(b->getName() + b->getGenderSignWithSpace());
107
    if (gui != nullptr)
108
    {
109
        if (playerRelations.isGoodName(b))
110
            mBeingName->setFont(boldFont);
111
        else
112
            mBeingName->setFont(gui->getSecureFont());
113
    }
114
    if (b->isAdvanced())
115
    {
116
        mBeingName->setForegroundColorAll(getThemeColor(
117
            ThemeColorId::PLAYER_ADVANCED), getThemeColor(
118
            ThemeColorId::PLAYER_ADVANCED_OUTLINE));
119
    }
120
    else
121
    {
122
        mBeingName->setForegroundColorAll(getThemeColor(ThemeColorId::POPUP),
123
            getThemeColor(ThemeColorId::POPUP_OUTLINE));
124
    }
125
126
    mBeingName->adjustSize();
127
    FOR_EACH (STD_VECTOR<Label*>::iterator, it, mLabels)
128
    {
129
        (*it)->setCaption(std::string());
130
    }
131
132
    const ActorTypeT type = b->getType();
133
    if (type == ActorType::Pet)
134
    {
135
        const PetInfo *const info = PlayerInfo::getPet();
136
        if ((info != nullptr) && info->id == b->getId())
137
        {
138
            ptr = mLabels[num];
139
            // TRANSLATORS: being popup label
140
            ptr->setCaption(strprintf(_("Hungry: %d"),
141
                info->hungry));
142
            ptr->adjustSize();
143
            num ++;
144
            ptr = mLabels[num];
145
            // TRANSLATORS: being popup label
146
            ptr->setCaption(strprintf(_("Intimacy: %d"),
147
                info->intimacy));
148
            ptr->adjustSize();
149
            num ++;
150
        }
151
    }
152
    else if (type == ActorType::Homunculus)
153
    {
154
        const HomunculusInfo *const info = PlayerInfo::getHomunculus();
155
        if ((info != nullptr) && info->id == b->getId())
156
        {
157
            ptr = mLabels[num];
158
            // TRANSLATORS: being popup label
159
            ptr->setCaption(strprintf(_("Hungry: %d"),
160
                info->hungry));
161
            ptr->adjustSize();
162
            num ++;
163
            ptr = mLabels[num];
164
            // TRANSLATORS: being popup label
165
            ptr->setCaption(strprintf(_("Intimacy: %d"),
166
                info->intimacy));
167
            ptr->adjustSize();
168
            num ++;
169
        }
170
    }
171
    else
172
    {
173
        const int groupId = b->getGroupId();
174
        const std::string &groupName = GroupDb::getLongName(groupId);
175
        if (!groupName.empty())
176
        {
177
            ptr = mLabels[num];
178
            // TRANSLATORS: being popup label
179
            ptr->setCaption(strprintf(_("Group: %s (%d)"),
180
                groupName.c_str(),
181
                groupId));
182
            ptr->adjustSize();
183
            num ++;
184
        }
185
186
        if (!(b->getPartyName().empty()))
187
        {
188
            ptr = mLabels[num];
189
            // TRANSLATORS: being popup label
190
            ptr->setCaption(strprintf(_("Party: %s"),
191
                b->getPartyName().c_str()));
192
            ptr->adjustSize();
193
            num ++;
194
        }
195
196
        if (!(b->getGuildName().empty()))
197
        {
198
            ptr = mLabels[num];
199
            // TRANSLATORS: being popup label
200
            ptr->setCaption(strprintf(_("Guild: %s"),
201
                b->getGuildName().c_str()));
202
            ptr->adjustSize();
203
            num ++;
204
        }
205
206
        if (b->getPvpRank() > 0)
207
        {
208
            ptr = mLabels[num];
209
            // TRANSLATORS: being popup label
210
            ptr->setCaption(strprintf(_("Pvp rank: %u"),
211
                b->getPvpRank()));
212
            ptr->adjustSize();
213
            num ++;
214
        }
215
216
        if (!b->getBuyBoard().empty())
217
        {
218
            ptr = mLabels[num];
219
            // TRANSLATORS: being popup label
220
            ptr->setCaption(strprintf(_("Buy shop: %s"),
221
                b->getBuyBoard().c_str()));
222
            ptr->adjustSize();
223
            num ++;
224
        }
225
226
        if (!b->getSellBoard().empty())
227
        {
228
            ptr = mLabels[num];
229
            // TRANSLATORS: being popup label
230
            ptr->setCaption(strprintf(_("Sell shop: %s"),
231
                b->getSellBoard().c_str()));
232
            ptr->adjustSize();
233
            num ++;
234
        }
235
236
        if (!b->getComment().empty())
237
        {
238
            ptr = mLabels[num];
239
            // TRANSLATORS: being popup label
240
            ptr->setCaption(strprintf(_("Comment: %s"),
241
                b->getComment().c_str()));
242
            ptr->adjustSize();
243
            num ++;
244
        }
245
246
        const std::string effects = b->getStatusEffectsString();
247
        if (!effects.empty())
248
        {
249
            ptr = mLabels[num];
250
            // TRANSLATORS: being popup label
251
            ptr->setCaption(strprintf(_("Effects: %s"),
252
                effects.c_str()));
253
            ptr->adjustSize();
254
            num ++;
255
        }
256
257
        const ChatObject *const chat = b->getChat();
258
        if (chat != nullptr)
259
        {
260
            ptr = mLabels[num];
261
            // TRANSLATORS: being popup label
262
            ptr->setCaption(strprintf(_("Chat room: %s"),
263
                chat->title.c_str()));
264
            ptr->adjustSize();
265
            num ++;
266
        }
267
    }
268
269
    const int level = b->getLevel();
270
    if (level > 1)
271
    {
272
        ptr = mLabels[num];
273
        // TRANSLATORS: being popup label
274
        ptr->setCaption(strprintf(_("Level: %d"),
275
            level));
276
        ptr->adjustSize();
277
        num ++;
278
    }
279
280
    const int maxHp = b->getMaxHP();
281
    if (maxHp > 0)
282
    {
283
        int hp = b->getHP();
284
        if (hp == 0)
285
            hp = maxHp - b->getDamageTaken();
286
        if (hp > 0)
287
        {
288
            ptr = mLabels[num];
289
            // TRANSLATORS: being popup label
290
            ptr->setCaption(strprintf(_("Hp: %d/%d"),
291
                hp,
292
                maxHp));
293
            ptr->adjustSize();
294
            num ++;
295
        }
296
    }
297
298
    ptr = mLabels[num];
299
    // TRANSLATORS: being popup label
300
    ptr->setCaption(strprintf(_("Particles: %u"),
301
        CAST_U32(b->getParticlesCount())));
302
    ptr->adjustSize();
303
    num ++;
304
305
    const size_t sz = mLabels.size();
306
    for (size_t f = num; f < sz; f ++)
307
    {
308
        mLabels[f]->setCaption(std::string());
309
    }
310
311
    int minWidth = mBeingName->getWidth();
312
    const int height1 = getFont()->getHeight();
313
    int height = height1;
314
    FOR_EACH (STD_VECTOR<Label*>::iterator, it, mLabels)
315
    {
316
        const Label *const label = *it;
317
        if (label != nullptr)
318
        {
319
            if (label->getWidth() > minWidth)
320
                minWidth = label->getWidth();
321
            if (!label->getCaption().empty())
322
                height += height1;
323
        }
324
    }
325
326
    setContentSize(minWidth, height);
327
    position(x, y);
328
4
}
329
330
#ifdef USE_PROFILER
331
void BeingPopup::logic()
332
{
333
    BLOCK_START("BeingPopup::logic")
334
    logicChildren();
335
    BLOCK_END("BeingPopup::logic")
336
}
337
#endif  // USE_PROFILER