Mana
Loading...
Searching...
No Matches
itempopup.cpp
Go to the documentation of this file.
1/*
2 * The Mana 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-2012 The Mana Developers
6 *
7 * This file is part of The Mana 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/itempopup.h"
24
25#include "configuration.h"
26#include "units.h"
27
28#include "gui/gui.h"
29
30#include "gui/widgets/icon.h"
31#include "gui/widgets/label.h"
32#include "gui/widgets/textbox.h"
33
34#include "utils/gettext.h"
35#include "utils/stringutils.h"
36
38#include "resources/theme.h"
39
40#include <guichan/font.hpp>
41
42#include <guichan/widgets/label.hpp>
43
44#define ITEMPOPUP_WRAP_WIDTH 196
45
46static int getColorIdFromItemType(ItemType type)
47{
48 switch (type)
49 {
50 case ITEM_UNUSABLE:
51 return Theme::GENERIC;
52 case ITEM_USABLE:
53 return Theme::USABLE;
55 return Theme::ONEHAND;
57 return Theme::TWOHAND;
59 return Theme::TORSO;
61 return Theme::ARMS;
63 return Theme::HEAD;
65 return Theme::LEGS;
67 return Theme::SHIELD;
69 return Theme::RING;
71 return Theme::NECKLACE;
73 return Theme::FEET;
75 return Theme::AMMO;
76 default:
78 }
79}
80
82 Popup("ItemPopup")
83{
84 setMinHeight(boldFont->getHeight());
85
86 // Item Name
87 mItemName = new Label;
88 mItemName->setFont(boldFont);
89 mItemName->setPosition(getPadding(), getPadding());
90
91 // Item Description
92 mItemDesc = new TextBox;
93 mItemDesc->setEditable(false);
94
95 // Item Effect
96 mItemEffect = new TextBox;
97 mItemEffect->setEditable(false);
98
99 // Item Weight
100 mItemWeight = new TextBox;
101 mItemWeight->setEditable(false);
102
103 mIcon = new Icon;
104
105 add(mItemName);
106 add(mItemDesc);
109 add(mIcon);
110
111 addMouseListener(this);
112}
113
114ItemPopup::~ItemPopup() = default;
115
116void ItemPopup::setEquipmentText(const std::string& text)
117{
118 mItemEquipSlot = text;
119}
120
122{
123 mIcon->setImage(nullptr);
124
125 std::string caption = _("No item");
126 if (!mItemEquipSlot.empty())
127 {
128 caption += " (";
129 caption += mItemEquipSlot;
130 caption += ")";
131 }
132 mItemName->setCaption(caption);
133 mItemName->adjustSize();
134
135 auto theme = gui->getTheme();
136 auto &palette = theme->getPalette(getSkin().palette);
137
138 mItemName->setForegroundColor(palette.getColor(Theme::GENERIC));
139 mItemName->setOutlineColor(palette.getOutlineColor(Theme::GENERIC));
140 mItemName->setPosition(0, 0);
141
142 mItemDesc->setText(std::string());
143 mItemEffect->setText(std::string());
144 mItemWeight->setText(std::string());
145
146 setContentSize(mItemName->getWidth(), mItemName->getHeight());
147}
148
149void ItemPopup::setItem(const ItemInfo &item, bool showImage)
150{
151 if (item.name == mItemName->getCaption())
152 return;
153
154 int space = 0;
155
156 if (showImage)
157 {
159 auto image = resman->getImage(paths.getStringValue("itemIcons") +
160 item.display.image);
161
162 mIcon->setImage(image);
163 if (image)
164 {
165 mIcon->setPosition(0, 0);
166 space = mIcon->getWidth();
167 }
168 }
169 else
170 {
171 mIcon->setImage(nullptr);
172 }
173
174 mItemType = item.type;
175
176 std::string caption = item.name;
177 if (!mItemEquipSlot.empty())
178 caption += " (" + mItemEquipSlot + ")";
179
180 auto theme = gui->getTheme();
181 auto &palette = theme->getPalette(getSkin().palette);
182
183 const auto typeColorId = getColorIdFromItemType(mItemType);
184
185 mItemName->setCaption(caption);
186 mItemName->adjustSize();
187 mItemName->setForegroundColor(palette.getColor(typeColorId));
188 mItemName->setOutlineColor(palette.getOutlineColor(typeColorId));
189 mItemName->setPosition(space, 0);
190
193 mItemWeight->setTextWrapped(strprintf(_("Weight: %s"),
194 Units::formatWeight(item.weight).c_str()),
196
197 int minWidth = mItemName->getWidth() + space;
198
199 if (mItemDesc->getMinWidth() > minWidth)
200 minWidth = mItemDesc->getMinWidth();
201 if (mItemEffect->getMinWidth() > minWidth)
202 minWidth = mItemEffect->getMinWidth();
203 if (mItemWeight->getMinWidth() > minWidth)
204 minWidth = mItemWeight->getMinWidth();
205
206 const int descHeight = mItemDesc->getHeight();
207 const int effectHeight = mItemEffect->getHeight();
208 const int weightHeight = mItemWeight->getHeight();
209
210 int nameHeight = std::max(mItemName->getHeight(), mIcon->getHeight());
211 nameHeight += getPadding();
212
213 if (item.effect.empty())
214 {
215 setContentSize(minWidth, nameHeight + descHeight + weightHeight + getPadding());
216
217 mItemWeight->setPosition(0, nameHeight + descHeight + getPadding());
218 }
219 else
220 {
221 setContentSize(minWidth, nameHeight + descHeight + effectHeight +
222 weightHeight + getPadding());
223
224 mItemWeight->setPosition(0, nameHeight + descHeight + effectHeight +
225 getPadding());
226 }
227
228 mItemDesc->setPosition(0, nameHeight);
229 mItemEffect->setPosition(0, nameHeight + descHeight + getPadding());
230}
231
232void ItemPopup::mouseMoved(gcn::MouseEvent &event)
233{
234 Popup::mouseMoved(event);
235
236 // When the mouse moved on top of the popup, hide it
237 setVisible(false);
238 mItemEquipSlot.clear();
239}
std::string getStringValue(const std::string &key) const
Theme * getTheme() const
The global GUI theme.
Definition gui.h:138
An icon.
Definition icon.h:36
void setImage(Image *image)
Sets the image to display.
Definition icon.cpp:41
Defines a class for storing generic item infos.
Definition iteminfo.h:100
std::string description
Short description.
Definition iteminfo.h:115
std::string name
Definition iteminfo.h:113
SpriteDisplay display
Display info (like icon)
Definition iteminfo.h:114
int weight
Weight in grams.
Definition iteminfo.h:117
std::vector< std::string > effect
Description of effects.
Definition iteminfo.h:116
ItemType type
Item type.
Definition iteminfo.h:136
Icon * mIcon
Definition itempopup.h:75
void setItem(const ItemInfo &item, bool showImage=false)
Sets the info to be displayed given a particular item.
ItemPopup()
Constructor.
Definition itempopup.cpp:81
Label * mItemName
Definition itempopup.h:69
void mouseMoved(gcn::MouseEvent &mouseEvent) override
TextBox * mItemDesc
Definition itempopup.h:70
void setEquipmentText(const std::string &text=std::string())
Tells in which equipment slot the item is equipped.
TextBox * mItemWeight
Definition itempopup.h:72
std::string mItemEquipSlot
Definition itempopup.h:73
TextBox * mItemEffect
Definition itempopup.h:71
~ItemPopup() override
Destructor.
void setNoItem()
Tells the item popup to say: No Item.
ItemType mItemType
Definition itempopup.h:74
Label widget.
Definition label.h:34
void setOutlineColor(std::optional< gcn::Color > color)
Sets the color of the outline.
Definition label.h:64
A light version of the Window class.
Definition popup.h:48
const Skin & getSkin() const
Returns the Skin used by this popup.
Definition popup.cpp:200
void setMinHeight(int height)
Sets the minimum height of the popup.
Definition popup.cpp:163
void add(gcn::Widget *widget) override
Definition popup.cpp:69
void mouseMoved(gcn::MouseEvent &event) override
Definition popup.cpp:205
int getPadding() const
Gets the padding of the popup.
Definition popup.h:132
void setContentSize(int width, int height)
Sets the size of this popup.
Definition popup.cpp:127
A class for loading and managing resources.
static ResourceManager * getInstance()
Returns an instance of the class, creating one if it does not already exist.
ResourceRef< Image > getImage(const std::string &idPath)
Loads the Image resource found at the given identifier path.
A text box, meant to be used inside a scroll area.
Definition textbox.h:36
void setTextWrapped(const std::string &text, int minDimension)
Sets the text after wrapping it to the current width of the widget.
Definition textbox.cpp:42
int getMinWidth() const
Get the minimum text width for the text box.
Definition textbox.h:54
const Palette & getPalette(size_t index) const
Definition theme.cpp:328
@ TWOHAND
Definition theme.h:258
@ LEGS
Definition theme.h:256
@ UNKNOWN_ITEM
Definition theme.h:250
@ HEAD
Definition theme.h:252
@ GENERIC
Definition theme.h:251
@ ARMS
Definition theme.h:262
@ AMMO
Definition theme.h:263
@ TORSO
Definition theme.h:254
@ USABLE
Definition theme.h:253
@ FEET
Definition theme.h:257
@ ONEHAND
Definition theme.h:255
@ SHIELD
Definition theme.h:259
@ NECKLACE
Definition theme.h:261
@ RING
Definition theme.h:260
static std::string formatWeight(int value)
Formats the given number in the correct weight/mass format.
Definition units.cpp:221
Configuration paths
XML default paths information reader.
Definition client.cpp:99
#define _(s)
Definition gettext.h:38
Gui * gui
The GUI system.
Definition gui.cpp:50
gcn::Font * boldFont
Bolded text font.
Definition gui.cpp:54
ItemType
Enumeration of available Item types.
Definition iteminfo.h:43
@ ITEM_EQUIPMENT_LEGS
Definition iteminfo.h:51
@ ITEM_EQUIPMENT_ARMS
Definition iteminfo.h:49
@ ITEM_EQUIPMENT_RING
Definition iteminfo.h:53
@ ITEM_EQUIPMENT_NECKLACE
Definition iteminfo.h:54
@ ITEM_EQUIPMENT_ONE_HAND_WEAPON
Definition iteminfo.h:46
@ ITEM_EQUIPMENT_TORSO
Definition iteminfo.h:48
@ ITEM_EQUIPMENT_HEAD
Definition iteminfo.h:50
@ ITEM_EQUIPMENT_AMMO
Definition iteminfo.h:56
@ ITEM_UNUSABLE
Definition iteminfo.h:44
@ ITEM_EQUIPMENT_TWO_HANDS_WEAPON
Definition iteminfo.h:47
@ ITEM_EQUIPMENT_FEET
Definition iteminfo.h:55
@ ITEM_EQUIPMENT_SHIELD
Definition iteminfo.h:52
@ ITEM_USABLE
Definition iteminfo.h:45
#define ITEMPOPUP_WRAP_WIDTH
Definition itempopup.cpp:44
std::string strprintf(char const *format,...)
A safe version of sprintf that returns a std::string of the result.
std::string join(const std::vector< std::string > &strings, const char *separator)
Joins a vector of strings into one string, separated by the given separator.
std::string image
Definition spritedef.h:43