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 }
65 
67 {
69  add(mBeingName);
70  FOR_EACH (STD_VECTOR<Label*>::iterator, it, mLabels)
71  {
72  add(*it);
73  }
74 }
75 
76 void BeingPopup::addLabels(const int fontHeight)
77 {
78  for (int f = 0; f < 11; f ++)
79  {
80  Label *const label = new Label(this, "A");
81  label->setPosition(0, fontHeight * (f + 1));
82  label->setForegroundColorAll(
85  mLabels.push_back(label);
86  }
87 }
88 
89 void BeingPopup::show(const int x, const int y, Being *const b)
90 {
91  if (b == nullptr)
92  {
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  {
104  return;
105  }
106 
108  if (gui != nullptr)
109  {
112  else
114  }
115  if (b->isAdvanced())
116  {
120  }
121  else
122  {
126  }
127 
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  {
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->getPvpRank() > 0)
209  {
210  ptr = mLabels[num];
211  // TRANSLATORS: being popup label
212  ptr->setCaption(strprintf(_("Pvp rank: %u"),
213  b->getPvpRank()));
214  ptr->adjustSize();
215  num ++;
216  }
217 
218  if (!b->getBuyBoard().empty())
219  {
220  ptr = mLabels[num];
221  // TRANSLATORS: being popup label
222  ptr->setCaption(strprintf(_("Buy shop: %s"),
223  b->getBuyBoard().c_str()));
224  ptr->adjustSize();
225  num ++;
226  }
227 
228  if (!b->getSellBoard().empty())
229  {
230  ptr = mLabels[num];
231  // TRANSLATORS: being popup label
232  ptr->setCaption(strprintf(_("Sell shop: %s"),
233  b->getSellBoard().c_str()));
234  ptr->adjustSize();
235  num ++;
236  }
237 
238  if (!b->getComment().empty())
239  {
240  ptr = mLabels[num];
241  // TRANSLATORS: being popup label
242  ptr->setCaption(strprintf(_("Comment: %s"),
243  b->getComment().c_str()));
244  ptr->adjustSize();
245  num ++;
246  }
247 
248  const std::string effects = b->getStatusEffectsString();
249  if (!effects.empty())
250  {
251  ptr = mLabels[num];
252  // TRANSLATORS: being popup label
253  ptr->setCaption(strprintf(_("Effects: %s"),
254  effects.c_str()));
255  ptr->adjustSize();
256  num ++;
257  }
258 
259  const ChatObject *const chat = b->getChat();
260  if (chat != nullptr)
261  {
262  ptr = mLabels[num];
263  // TRANSLATORS: being popup label
264  ptr->setCaption(strprintf(_("Chat room: %s"),
265  chat->title.c_str()));
266  ptr->adjustSize();
267  num ++;
268  }
269  }
270 
271  const int level = b->getLevel();
272  if (level > 1)
273  {
274  ptr = mLabels[num];
275  // TRANSLATORS: being popup label
276  ptr->setCaption(strprintf(_("Level: %d"),
277  level));
278  ptr->adjustSize();
279  num ++;
280  }
281 
282  const int maxHp = b->getMaxHP();
283  if (maxHp > 0)
284  {
285  int hp = b->getHP();
286  if (hp == 0)
287  hp = maxHp - b->getDamageTaken();
288  if (hp > 0)
289  {
290  ptr = mLabels[num];
291  // TRANSLATORS: being popup label
292  ptr->setCaption(strprintf(_("Hp: %d/%d"),
293  hp,
294  maxHp));
295  ptr->adjustSize();
296  num ++;
297  }
298  }
299 
300  ptr = mLabels[num];
301  // TRANSLATORS: being popup label
302  ptr->setCaption(strprintf(_("Particles: %u"),
303  CAST_U32(b->getParticlesCount())));
304  ptr->adjustSize();
305  num ++;
306 
307  const size_t sz = mLabels.size();
308  for (size_t f = num; f < sz; f ++)
309  {
310  mLabels[f]->setCaption(std::string());
311  }
312 
313  int minWidth = mBeingName->getWidth();
314  const int height1 = getFont()->getHeight();
315  int height = height1;
316  FOR_EACH (STD_VECTOR<Label*>::iterator, it, mLabels)
317  {
318  const Label *const label = *it;
319  if (label != nullptr)
320  {
321  if (label->getWidth() > minWidth)
322  minWidth = label->getWidth();
323  if (!label->getCaption().empty())
324  height += height1;
325  }
326  }
327 
328  setContentSize(minWidth, height);
329  position(x, y);
330 }
331 
332 #ifdef USE_PROFILER
333 void BeingPopup::logic()
334 {
335  BLOCK_START("BeingPopup::logic")
336  logicChildren();
337  BLOCK_END("BeingPopup::logic")
338 }
339 #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:204
int intimacy
Definition: petinfo.h:50
Gui * gui
Definition: gui.cpp:110
const std::string & getLongName(const int id)
Definition: groupdb.cpp:345
std::string getBuyBoard() const
Definition: being.h:1012
void setVisible(Visible visible)
Definition: widget.cpp:224
BeingId getId() const
Definition: actorsprite.h:63
std::string getSellBoard() const
Definition: being.h:1007
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
size_t getParticlesCount() const
Definition: actorsprite.h:194
void postInit()
Definition: beingpopup.cpp:66
const std::string getComment() const
Definition: being.h:805
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:89
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:232
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:76
int getGroupId() const
Definition: being.h:1083
int getHP() const
Definition: being.h:752
const std::string & getName() const
Definition: being.h:231
int getDamageTaken() const
Definition: being.h:586
int getMaxHP() const
Definition: being.h:755
bool isAdvanced() const
Definition: being.h:821
std::string title
Definition: chatobject.h:50
Font * getSecureFont() const
Definition: gui.h:171
unsigned int getPvpRank() const
Definition: being.h:742
BeingPopup * beingPopup
Definition: beingpopup.cpp:46
void setFont(Font *const font)
Definition: widget.cpp:348
HomunculusInfo * getHomunculus()
Definition: playerinfo.cpp:599
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:597
int getHeight() const
Definition: font.cpp:360
PetInfo * getPet()
Definition: playerinfo.cpp:572
std::string getGenderSignWithSpace() const
Definition: being.cpp:2513
Definition: being.h:93
PlayerRelationsManager playerRelations
void adjustSize()
Definition: label.cpp:197
void updateComment()
Definition: being.cpp:4696
ChatObject * getChat() const
Definition: being.h:996
const std::string & getCaption() const
Definition: label.h:132
const std::string & getGuildName() const
Definition: being.h:258