ManaPlus
skillpopup.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2008 The Legend of Mazzeroth Development Team
4  * Copyright (C) 2008-2009 The Mana World Development Team
5  * Copyright (C) 2009-2010 The Mana Developers
6  * Copyright (C) 2011-2018 The ManaPlus Developers
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "gui/popups/skillpopup.h"
25 
26 #include "gui/gui.h"
27 
28 #include "gui/fonts/font.h"
29 
30 #include "gui/widgets/label.h"
31 #include "gui/widgets/textbox.h"
32 
33 #include "utils/gettext.h"
34 #include "utils/stringutils.h"
35 
38 
39 #include "debug.h"
40 
42 
44  Popup("SkillPopup", "skillpopup.xml"),
45  mSkillName(new Label(this)),
46  mSkillDesc(new TextBox(this)),
47  mSkillEffect(new TextBox(this)),
48  mSkillLevel(new TextBox(this)),
49  mSkillCastType(new TextBox(this)),
50  mCastType(CastType::Default),
51  mLastId(0U),
52  mLastLevel(-1),
53  mOffsetX(0),
54  mOffsetY(0)
55 {
57  mSkillName->setPosition(0, 0);
61 
62  const int fontHeight = getFont()->getHeight();
63 
64  mSkillDesc->setEditable(false);
65  mSkillDesc->setPosition(0, fontHeight);
69 
70  mSkillEffect->setEditable(false);
71  mSkillEffect->setPosition(0, 2 * fontHeight);
75 
76  mSkillLevel->setEditable(false);
77  mSkillLevel->setPosition(0, 3 * fontHeight);
81 
83  mSkillCastType->setPosition(0, 4 * fontHeight);
87 }
88 
90 {
92  add(mSkillName);
93  add(mSkillDesc);
97 
98  addMouseListener(this);
99 }
100 
102 {
103 }
104 
105 void SkillPopup::show(const SkillInfo *const skill,
106  const int level,
107  const CastTypeT castType,
108  const int offsetX,
109  const int offsetY)
110 {
111  if ((skill == nullptr) ||
112  (skill->data == nullptr) ||
113  (skill->id == mLastId &&
114  level == mLastLevel &&
115  castType == mCastType &&
116  offsetX == mOffsetX &&
117  offsetY == mOffsetY))
118  {
119  return;
120  }
121 
122  mLastId = skill->id;
123  mLastLevel = level;
124  mCastType = castType;
125  mOffsetX = offsetX;
126  mOffsetY = offsetY;
127 
130  mSkillName->setPosition(0, 0);
131 
132  std::string description = skill->data->description;
133  std::string effect = skill->skillEffect;
134  if (description.empty())
135  {
136  description = effect;
137  effect.clear();
138  }
139  mSkillDesc->setTextWrapped(description, 196);
140  mSkillEffect->setTextWrapped(effect, 196);
141  if (level != 0)
142  {
144  // TRANSLATORS: skill level
145  _("Level: %d / %d"), level, skill->level),
146  196);
147  }
148  else
149  {
150  if (skill->level != 0)
151  {
153  // TRANSLATORS: skill level
154  _("Level: %d"), skill->level),
155  196);
156  }
157  else
158  {
160  // TRANSLATORS: skill level for tmw fake skills
161  _("Level: Unknown"),
162  196);
163  }
164  }
165  std::string castStr;
166  switch (castType)
167  {
168  case CastType::Default:
169  default:
170  // TRANSLATORS: skill cast type
171  castStr = _("Default");
172  break;
173  case CastType::Target:
174  // TRANSLATORS: skill cast type
175  castStr = _("Target");
176  break;
177  case CastType::Position:
178  // TRANSLATORS: skill cast type
179  castStr = _("Mouse position");
180  break;
181  case CastType::Self:
182  // TRANSLATORS: skill cast type
183  castStr = _("Self position");
184  break;
185  }
186  if (offsetX != 0 ||
187  offsetY != 0)
188  {
189  castStr.append(strprintf(" (%+d,%+d)",
190  offsetX,
191  offsetY));
192  }
194  // TRANSLATORS: skill cast type
195  _("Cast type: %s"),
196  castStr.c_str()), 196);
197 
198  int minWidth = mSkillName->getWidth();
199 
200  if (mSkillName->getWidth() > minWidth)
201  minWidth = mSkillName->getWidth();
202  if (mSkillDesc->getMinWidth() > minWidth)
203  minWidth = mSkillDesc->getMinWidth();
204  if (mSkillEffect->getMinWidth() > minWidth)
205  minWidth = mSkillEffect->getMinWidth();
206  if (mSkillLevel->getMinWidth() > minWidth)
207  minWidth = mSkillLevel->getMinWidth();
208  if (mSkillCastType->getMinWidth() > minWidth)
209  minWidth = mSkillCastType->getMinWidth();
210 
211  const int numRowsDesc = mSkillDesc->getNumberOfRows();
212  const int numRowsEffect = mSkillEffect->getNumberOfRows();
213  const int numRowsLevel = mSkillLevel->getNumberOfRows();
214  const int numRowsCast = mSkillCastType->getNumberOfRows();
215  const int height = getFont()->getHeight();
216 
217  if (skill->skillEffect.empty())
218  {
219  setContentSize(minWidth,
220  (numRowsDesc + numRowsLevel + numRowsCast + 1) * height);
221  mSkillLevel->setPosition(0, (numRowsDesc + 1) * height);
222  mSkillCastType->setPosition(0, (numRowsDesc + 2) * height);
223  }
224  else
225  {
226  setContentSize(minWidth,
227  (numRowsDesc + numRowsLevel + numRowsEffect + numRowsCast + 1) *
228  height);
229  mSkillEffect->setPosition(0, (numRowsDesc + 1) * height);
231  (numRowsDesc + numRowsEffect + 1) * height);
233  (numRowsDesc + numRowsEffect + 2) * height);
234  }
235 
236  mSkillDesc->setPosition(0, 1 * height);
237 }
238 
240 {
241  Popup::mouseMoved(event);
242 
243  // When the mouse moved on top of the popup, hide it
245  mLastId = 0;
246 }
247 
249 {
250  mLastId = 0;
251  mLastLevel = 0;
253  mOffsetX = 0;
254  mOffsetY = 0;
255 }
Font * getFont() const
Definition: widget.cpp:330
unsigned int id
Definition: skillinfo.h:68
int mOffsetY
Definition: skillpopup.h:79
#define _(s)
Definition: gettext.h:34
void setContentSize(int width, int height)
Definition: popup.cpp:154
int getWidth() const
Definition: widget.h:220
Font * boldFont
Definition: gui.cpp:111
TextBox * mSkillLevel
Definition: skillpopup.h:73
bool skill(InputEvent &event)
Definition: commands.cpp:96
unsigned int getNumberOfRows() const
Definition: textbox.h:161
void setTextWrapped(const std::string &text, const int minDimension)
Definition: textbox.cpp:108
Label * mSkillName
Definition: skillpopup.h:70
std::string skillEffect
Definition: skillinfo.h:52
SkillPopup * skillPopup
Definition: skillpopup.cpp:41
void setVisible(Visible visible)
Definition: widget.cpp:224
void postInit()
Definition: popup.h:176
void reset()
Definition: skillpopup.cpp:248
std::string description
Definition: skilldata.h:36
unsigned int mLastId
Definition: skillpopup.h:76
CastTypeT mCastType
Definition: skillpopup.h:75
TextBox * mSkillEffect
Definition: skillpopup.h:72
void show(const SkillInfo *const skill, const int level, const CastTypeT type, const int offsetX, const int offsetY)
Definition: skillpopup.cpp:105
int mOffsetX
Definition: skillpopup.h:78
SkillData * data
Definition: skillinfo.h:62
#define new
Definition: debug_new.h:147
void setEditable(const bool editable)
Definition: textbox.cpp:649
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
void mouseMoved(MouseEvent &event)
Definition: popup.cpp:254
TextBox * mSkillCastType
Definition: skillpopup.h:74
void postInit()
Definition: skillpopup.cpp:89
virtual void add(Widget *const widget)
int getMinWidth() const
Definition: textbox.h:107
int level
Definition: skillinfo.h:63
void addMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:291
void setPosition(const int x, const int y)
Definition: widget.cpp:160
int mLastLevel
Definition: skillpopup.h:77
void setForegroundColorAll(const Color &color1, const Color &color2)
Definition: textbox.cpp:487
void mouseMoved(MouseEvent &event)
Definition: skillpopup.cpp:239
std::string dispName
Definition: skilldata.h:35
void setFont(Font *const font)
Definition: widget.cpp:348
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
TextBox * mSkillDesc
Definition: skillpopup.h:71
int getHeight() const
Definition: font.cpp:361
void adjustSize()
Definition: label.cpp:197
CastType ::T CastTypeT
Definition: casttype.h:33