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-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 
48 
50  Popup("BeingPopup", "beingpopup.xml"),
51  mBeingName(new Label(this, "A")),
52  mLabels()
53 {
54  // Being Name
56  mBeingName->setPosition(0, 0);
57 
58  const int fontHeight = mBeingName->getHeight();
59  setMinHeight(fontHeight);
60  addLabels(fontHeight);
61 }
62 
64 {
65  mBeingName = nullptr;
66 }
67 
69 {
71  add(mBeingName);
72  FOR_EACH (STD_VECTOR<Label*>::iterator, it, mLabels)
73  {
74  add(*it);
75  }
76 }
77 
78 void BeingPopup::addLabels(const int fontHeight)
79 {
80  for (int f = 0; f < 11; f ++)
81  {
82  Label *const label = new Label(this, "A");
83  label->setPosition(0, fontHeight * (f + 1));
84  label->setForegroundColorAll(
85  getThemeColor(ThemeColorId::POPUP, 255U),
86  getThemeColor(ThemeColorId::POPUP_OUTLINE, 255U));
87  mLabels.push_back(label);
88  }
89 }
90 
91 void BeingPopup::show(const int x, const int y, Being *const b)
92 {
93  if (b == nullptr)
94  {
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  {
106  return;
107  }
108 
110  if (gui != nullptr)
111  {
114  else
116  }
117  if (b->isAdvanced())
118  {
120  getThemeColor(ThemeColorId::PLAYER_ADVANCED, 255U),
121  getThemeColor(ThemeColorId::PLAYER_ADVANCED_OUTLINE, 255U));
122  }
123  else
124  {
126  getThemeColor(ThemeColorId::POPUP, 255U),
127  getThemeColor(ThemeColorId::POPUP_OUTLINE, 255U));
128  }
129 
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  {
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 }
347 
348 #ifdef USE_PROFILER
349 void BeingPopup::logic()
350 {
351  BLOCK_START("BeingPopup::logic")
352  logicChildren();
354 }
355 #endif // USE_PROFILER
ActorType ::T ActorTypeT
Definition: actortype.h:43
BeingPopup * beingPopup
Definition: beingpopup.cpp:47
#define CAST_U32
Definition: cast.h:31
size_t getParticlesCount() const
Definition: actorsprite.h:195
BeingId getId() const
Definition: actorsprite.h:64
std::string getStatusEffectsString() const
virtual void add(Widget *const widget)
virtual void logicChildren()
Label * mBeingName
Definition: beingpopup.h:63
void show(const int x, const int y, Being *const b)
Definition: beingpopup.cpp:91
void postInit()
Definition: beingpopup.cpp:68
std::vector< Label * > mLabels
Definition: beingpopup.h:64
void addLabels(const int fontHeight)
Definition: beingpopup.cpp:78
Definition: being.h:96
std::string getBuyBoard() const
Definition: being.h:1015
const std::string & getClanName() const
Definition: being.h:263
virtual int getLevel() const
Definition: being.h:604
const std::string getComment() const
Definition: being.h:812
const std::string & getPartyName() const
Definition: being.h:255
int getGroupId() const
Definition: being.h:1087
std::string getGenderSignWithSpace() const
Definition: being.cpp:2538
int getHP() const
Definition: being.h:759
ChatObject * getChat() const
Definition: being.h:999
const std::string & getName() const
Definition: being.h:232
const std::string & getGuildName() const
Definition: being.h:259
int getMaxHP() const
Definition: being.h:762
void updateComment()
Definition: being.cpp:4725
std::string getSellBoard() const
Definition: being.h:1010
bool isAdvanced() const
Definition: being.h:828
ActorTypeT getType() const
Definition: being.h:116
unsigned int getPvpRank() const
Definition: being.h:749
int getDamageTaken() const
Definition: being.h:593
int getHeight() const
Definition: font.cpp:362
Font * getSecureFont() const
Definition: gui.h:172
Definition: label.h:91
void adjustSize()
Definition: label.cpp:200
void setForegroundColorAll(const Color &color1, const Color &color2)
Definition: label.cpp:217
const std::string & getCaption() const
Definition: label.h:133
void setCaption(const std::string &caption)
Definition: label.cpp:264
bool isGoodName(Being *const being) const
Definition: popup.h:52
void setMinHeight(const int height)
Definition: popup.cpp:207
void setContentSize(int width, int height)
Definition: popup.cpp:155
void position(const int x, const int y)
Definition: popup.cpp:235
void postInit()
Definition: popup.h:177
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:45
void setVisible(Visible visible)
Definition: widget.cpp:225
void setFont(Font *const font)
Definition: widget.cpp:349
Font * getFont() const
Definition: widget.cpp:331
void setPosition(const int x, const int y)
Definition: widget.cpp:161
int getHeight() const
Definition: widget.h:240
int getWidth() const
Definition: widget.h:221
#define new
Definition: debug_new.h:147
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
#define _(s)
Definition: gettext.h:35
Gui * gui
Definition: gui.cpp:111
Font * boldFont
Definition: gui.cpp:112
bool info(InputEvent &event)
Definition: commands.cpp:57
@ Homunculus
Definition: actortype.h:39
const std::string & getLongName(const int id)
Definition: groupdb.cpp:355
HomunculusInfo * getHomunculus()
Definition: playerinfo.cpp:603
PetInfo * getPet()
Definition: playerinfo.cpp:576
#define BLOCK_END(name)
Definition: perfomance.h:80
#define BLOCK_START(name)
Definition: perfomance.h:79
PlayerRelationsManager playerRelations
std::string strprintf(const char *const format,...)
std::string title
Definition: chatobject.h:51
const bool Visible_false
Definition: visible.h:30