GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/popups/beingpopup.cpp Lines: 25 168 14.9 %
Date: 2018-05-19 03:07:18 Branches: 17 154 11.0 %

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