GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/popups/beingpopup.cpp Lines: 27 178 15.2 %
Date: 2018-11-12 Branches: 15 154 9.7 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2010  The Mana Developers
4
 *  Copyright (C) 2011-2018  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
2
BeingPopup::BeingPopup() :
49
    Popup("BeingPopup", "beingpopup.xml"),
50

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

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

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