GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/popups/beingpopup.cpp Lines: 27 180 15.0 %
Date: 2021-03-17 Branches: 15 156 9.6 %

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

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

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

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