ManaPlus
beingpopup.cpp
Go to the documentation of this file.
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 
47 
49  Popup("BeingPopup", "beingpopup.xml"),
50  mBeingName(new Label(this, "A")),
51  mLabels()
52 {
53  // Being Name
55  mBeingName->setPosition(0, 0);
56 
57  const int fontHeight = mBeingName->getHeight();
58  setMinHeight(fontHeight);
59  addLabels(fontHeight);
60 }
61 
63 {
64  mBeingName = nullptr;
65 }
66 
68 {
70  add(mBeingName);
71  FOR_EACH (STD_VECTOR<Label*>::iterator, it, mLabels)
72  {
73  add(*it);
74  }
75 }
76 
77 void BeingPopup::addLabels(const int fontHeight)
78 {
79  for (int f = 0; f < 11; f ++)
80  {
81  Label *const label = new Label(this, "A");
82  label->setPosition(0, fontHeight * (f + 1));
83  label->setForegroundColorAll(
86  mLabels.push_back(label);
87  }
88 }
89 
90 void BeingPopup::show(const int x, const int y, Being *const b)
91 {
92  if (b == nullptr)
93  {
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  {
105  return;
106  }
107 
109  if (gui != nullptr)
110  {
113  else
115  }
116  if (b->isAdvanced())
117  {
121  }
122  else
123  {
127  }
128 
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  {
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 }
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
#define CAST_U32
Definition: cast.h:30
Font * getFont() const
Definition: widget.cpp:330
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
#define _(s)
Definition: gettext.h:34
Label * mBeingName
Definition: beingpopup.h:62
const std::string & getPartyName() const
Definition: being.h:254
BeingId id
Definition: petinfo.h:47
void setContentSize(int width, int height)
Definition: popup.cpp:154
int getWidth() const
Definition: widget.h:220
Font * boldFont
Definition: gui.cpp:111
void setMinHeight(const int height)
Definition: popup.cpp:206
int intimacy
Definition: petinfo.h:50
Gui * gui
Definition: gui.cpp:110
const std::string & getLongName(const int id)
Definition: groupdb.cpp:354
std::string getBuyBoard() const
Definition: being.h:1018
void setVisible(Visible visible)
Definition: widget.cpp:224
BeingId getId() const
Definition: actorsprite.h:63
std::string getSellBoard() const
Definition: being.h:1013
void postInit()
Definition: popup.h:176
#define BLOCK_START(name)
Definition: perfomance.h:78
std::string getStatusEffectsString() const
#define BLOCK_END(name)
Definition: perfomance.h:79
ActorType ::T ActorTypeT
Definition: actortype.h:42
std::vector< Label * > mLabels
Definition: beingpopup.h:63
const std::string & getClanName() const
Definition: being.h:262
size_t getParticlesCount() const
Definition: actorsprite.h:194
void postInit()
Definition: beingpopup.cpp:67
const std::string getComment() const
Definition: being.h:811
bool isGoodName(Being *const being) const
#define new
Definition: debug_new.h:147
void show(const int x, const int y, Being *const b)
Definition: beingpopup.cpp:90
std::string strprintf(const char *const format,...)
Definition: stringutils.cpp:99
void setCaption(const std::string &caption)
Definition: label.cpp:261
const bool Visible_false
Definition: visible.h:29
Definition: label.h:87
bool info(InputEvent &event)
Definition: commands.cpp:56
int hungry
Definition: petinfo.h:49
virtual void logicChildren()
void position(const int x, const int y)
Definition: popup.cpp:234
virtual void add(Widget *const widget)
ActorTypeT getType() const
Definition: being.h:115
void setPosition(const int x, const int y)
Definition: widget.cpp:160
void addLabels(const int fontHeight)
Definition: beingpopup.cpp:77
int getGroupId() const
Definition: being.h:1090
int getHP() const
Definition: being.h:758
const std::string & getName() const
Definition: being.h:231
int getDamageTaken() const
Definition: being.h:592
int getMaxHP() const
Definition: being.h:761
bool isAdvanced() const
Definition: being.h:827
std::string title
Definition: chatobject.h:50
Font * getSecureFont() const
Definition: gui.h:171
unsigned int getPvpRank() const
Definition: being.h:748
BeingPopup * beingPopup
Definition: beingpopup.cpp:46
void setFont(Font *const font)
Definition: widget.cpp:348
HomunculusInfo * getHomunculus()
Definition: playerinfo.cpp:600
int getHeight() const
Definition: widget.h:239
Definition: popup.h:48
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:44
void setForegroundColorAll(const Color &color1, const Color &color2)
Definition: label.cpp:214
virtual int getLevel() const
Definition: being.h:603
int getHeight() const
Definition: font.cpp:361
PetInfo * getPet()
Definition: playerinfo.cpp:573
std::string getGenderSignWithSpace() const
Definition: being.cpp:2540
Definition: being.h:93
PlayerRelationsManager playerRelations
void adjustSize()
Definition: label.cpp:197
void updateComment()
Definition: being.cpp:4725
ChatObject * getChat() const
Definition: being.h:1002
const std::string & getCaption() const
Definition: label.h:132
const std::string & getGuildName() const
Definition: being.h:258