Mana
Loading...
Searching...
No Matches
equipmentwindow.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana 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/widgets/button.h"
23
24#include "equipment.h"
25#include "graphics.h"
26#include "inventory.h"
27#include "item.h"
28#include "localplayer.h"
29
30#include "gui/equipmentwindow.h"
31#include "gui/itempopup.h"
32#include "gui/setup.h"
33#include "gui/viewport.h"
34
36
38#include "net/net.h"
39
40#include "resources/image.h"
41#include "resources/iteminfo.h"
43#include "resources/theme.h"
44
45#include "utils/gettext.h"
46#include "utils/stringutils.h"
47
48#include <guichan/font.hpp>
49
51 Window(_("Equipment")),
52 mEquipment(equipment)
53{
56
57 // Control that shows the Player
58 auto *playerBox = new PlayerBox;
59 playerBox->setDimension(gcn::Rectangle(50, 80, 74, 123));
60 playerBox->setPlayer(local_player);
61
62 setWindowName("Equipment");
63 setCloseButton(true);
64 setSaveVisible(true);
65 setContentSize(175, 290);
66 setDefaultSize(getWidth(), getHeight(), WindowAlignment::Center);
68
69 mUnequip = new Button(_("Unequip"), "unequip", this);
70 const gcn::Rectangle &area = getChildrenArea();
71 mUnequip->setPosition(area.width - mUnequip->getWidth() - getPadding(),
72 area.height - mUnequip->getHeight() - getPadding());
73 mUnequip->setEnabled(false);
74
75 add(playerBox);
76 add(mUnequip);
77}
78
83
84void EquipmentWindow::draw(gcn::Graphics *graphics)
85{
87
88 auto *g = static_cast<Graphics*>(graphics);
89
90 auto theme = gui->getTheme();
91 auto &boxSkin = theme->getSkin(SkinType::EquipmentBox);
92
93 // Draw equipment boxes
94 const int boxCount = mEquipment->getSlotNumber();
95 for (int i = 0; i < boxCount; ++i)
96 {
98 boxPos.x += getPadding();
99 boxPos.y += getTitleBarHeight();
100
101 WidgetState boxState(gcn::Rectangle(boxPos.x, boxPos.y, boxSkin.width, boxSkin.height));
102 if (i == mSelected)
103 boxState.flags |= STATE_SELECTED;
104
105 boxSkin.draw(g, boxState);
106
107 if (Item *item = mEquipment->getEquipment(i))
108 {
109 if (Image *image = item->getImage())
110 {
111 image->setAlpha(1.0f);
112 g->drawImage(image,
113 boxPos.x + boxSkin.padding,
114 boxPos.y + boxSkin.padding);
115 }
116
118 {
119 g->setColor(Theme::getThemeColor(Theme::TEXT));
120 graphics->drawText(toString(item->getQuantity()),
121 boxPos.x + boxSkin.width / 2,
122 boxPos.y - getFont()->getHeight(),
123 gcn::Graphics::CENTER);
124 }
125 }
126 else
127 {
128 auto &icon = Net::getInventoryHandler()->getBoxIcon(i);
129 if (!icon.empty())
130 if (auto image = theme->getIcon(icon))
131 g->drawImage(image, boxPos.x + boxSkin.padding, boxPos.y + boxSkin.padding);
132 }
133 }
134}
135
136void EquipmentWindow::action(const gcn::ActionEvent &event)
137{
138 if (event.getId() == "unequip" && mSelected > -1)
139 {
141 setSelected(-1);
142 }
143}
144
149int EquipmentWindow::getBoxIndex(int x, int y) const
150{
151 auto &boxSkin = gui->getTheme()->getSkin(SkinType::EquipmentBox);
152
153 // Translate coordinates to content area
154 const auto childrenArea = const_cast<EquipmentWindow*>(this)->getChildrenArea();
155 x -= childrenArea.x;
156 y -= childrenArea.y;
157
158 const int boxCount = mEquipment->getSlotNumber();
159 for (int i = 0; i < boxCount; ++i)
160 {
162 const gcn::Rectangle tRect(boxPos.x, boxPos.y, boxSkin.width, boxSkin.height);
163 if (tRect.isPointInRect(x, y))
164 return i;
165 }
166
167 return -1;
168}
169
170Item *EquipmentWindow::getItem(int x, int y) const
171{
172 const int index = getBoxIndex(x, y);
173 return index != -1 ? mEquipment->getEquipment(index) : nullptr;
174}
175
176std::string EquipmentWindow::getSlotName(int x, int y) const
177{
178 const int index = getBoxIndex(x, y);
179 return index != -1 ? mEquipment->getSlotName(index) : std::string();
180}
181
182void EquipmentWindow::mousePressed(gcn::MouseEvent& mouseEvent)
183{
184 Window::mousePressed(mouseEvent);
185
186 const int x = mouseEvent.getX();
187 const int y = mouseEvent.getY();
188 Item *item = nullptr;
189
190 const int index = getBoxIndex(x, y);
191 if (index != -1)
192 {
193 item = mEquipment->getEquipment(index);
194 if (item)
195 setSelected(index);
196 }
197
198 if (mouseEvent.getButton() == gcn::MouseEvent::RIGHT)
199 {
200 if (item)
201 {
202 /* Convert relative to the window coordinates to absolute screen
203 * coordinates.
204 */
205 const int mx = x + getX();
206 const int my = y + getY();
207 viewport->showPopup(this, mx, my, item, true, false);
208 }
209 }
210}
211
212void EquipmentWindow::mouseMoved(gcn::MouseEvent &event)
213{
214 Window::mouseMoved(event);
215
216 const int x = event.getX();
217 const int y = event.getY();
218
219 // Show ItemTooltip
220 std::string slotName = getSlotName(x, y);
221 if (!slotName.empty())
222 {
223 mItemPopup->setEquipmentText(slotName);
224
225 if (Item *item = getItem(x, y))
226 mItemPopup->setItem(item->getInfo());
227 else
229
230 mItemPopup->position(x + getX(), y + getY());
231 }
232 else
233 {
234 mItemPopup->setVisible(false);
235 }
236}
237
238void EquipmentWindow::mouseExited(gcn::MouseEvent &event)
239{
240 Window::mouseExited(event);
241
242 mItemPopup->setVisible(false);
243}
244
246{
247 mSelected = index;
248 mUnequip->setEnabled(mSelected != -1);
249}
Button widget.
Definition button.h:38
Equipment dialog.
EquipmentWindow(Equipment *equipment)
~EquipmentWindow() override
void setSelected(int index)
gcn::Button * mUnequip
void action(const gcn::ActionEvent &event) override
void mouseMoved(gcn::MouseEvent &event) override
int mSelected
Index of selected item.
Item * getItem(int x, int y) const
void mouseExited(gcn::MouseEvent &event) override
Equipment * mEquipment
int getBoxIndex(int x, int y) const
Returns an index of an equipment box at the given position, or -1 if there is no box.
std::string getSlotName(int x, int y) const
void draw(gcn::Graphics *graphics) override
Draws the equipment window.
ItemPopup * mItemPopup
void mousePressed(gcn::MouseEvent &mouseEvent) override
void triggerUnequip(int slotIndex) const
Definition equipment.h:62
Item * getEquipment(int slotIndex) const
Get equipment at the given slot.
Definition equipment.h:53
int getSlotNumber() const
Definition equipment.h:59
std::string getSlotName(int slotIndex) const
Definition equipment.h:56
A central point of control for graphics.
Definition graphics.h:78
void drawText(const std::string &text, int x, int y, gcn::Graphics::Alignment alignment, const gcn::Color &color, gcn::Font *font, bool outline=false, bool shadow=false, const std::optional< gcn::Color > &outlineColor={}, const std::optional< gcn::Color > &shadowColor={})
Definition graphics.cpp:176
Theme * getTheme() const
The global GUI theme.
Definition gui.h:138
Defines a class for loading and storing images.
Definition image.h:45
A popup that displays information about an item.
Definition itempopup.h:39
void setItem(const ItemInfo &item, bool showImage=false)
Sets the info to be displayed given a particular item.
void setEquipmentText(const std::string &text=std::string())
Tells in which equipment slot the item is equipped.
void setNoItem()
Tells the item popup to say: No Item.
Represents one or more instances of a certain item type.
Definition item.h:35
virtual Position getBoxPosition(unsigned int slotIndex) const
virtual const std::string & getBoxIcon(unsigned int slotIndex) const
A box showing a player character.
Definition playerbox.h:34
void setPlayer(const Being *being)
Sets a new player character to be displayed by this box.
Definition playerbox.h:47
void position(int x, int y)
Sets the location to display the popup.
Definition popup.cpp:183
void registerWindowForReset(Window *window)
Enables the Reset Windows button.
Definition setup.cpp:108
const Skin & getSkin(SkinType skinType) const
Definition theme.cpp:434
static const gcn::Color & getThemeColor(int type)
Gets the color associated with the type in the default palette (0).
Definition theme.cpp:313
@ TEXT
Definition theme.h:214
void showPopup(Window *parent, int x, int y, Item *item, bool isInventory=true, bool canDrop=true)
Shows a popup for an item.
Definition viewport.cpp:538
A window.
Definition window.h:59
void draw(gcn::Graphics *graphics) override
Draws the window contents.
Definition window.cpp:103
void setContentSize(int width, int height)
Sets the size of this window.
Definition window.cpp:151
void setWindowName(const std::string &name)
Sets the name of the window.
Definition window.h:272
void mouseMoved(gcn::MouseEvent &event) override
Implements custom cursor image changing context, based on mouse relative position.
Definition window.cpp:352
void setSaveVisible(bool save)
Sets whether the window will save it's visibility.
Definition window.h:225
void mouseExited(gcn::MouseEvent &event) override
When the mouse leaves the window this ensures that the custom cursor is restored back to it's standar...
Definition window.cpp:344
void setCloseButton(bool flag)
Sets whether or not the window has a close button.
Definition window.cpp:262
void setDefaultSize()
Set the default win pos and size to the current ones.
Definition window.cpp:545
void loadWindowState()
Reads the position (and the size for resizable windows) in the configuration based on the given strin...
Definition window.cpp:467
void mousePressed(gcn::MouseEvent &event) override
Starts window resizing when appropriate.
Definition window.cpp:304
Graphics * graphics
Definition client.cpp:104
Viewport * viewport
Viewport on the map.
Definition game.cpp:115
#define _(s)
Definition gettext.h:38
Gui * gui
The GUI system.
Definition gui.cpp:50
LocalPlayer * local_player
InventoryHandler * getInventoryHandler()
Definition net.cpp:90
@ EQUIP_PROJECTILE_SLOT
Definition iteminfo.h:187
Setup * setupWindow
Definition setup.cpp:120
std::string toString(const T &arg)
Converts the given value to a string using std::stringstream.
Definition stringutils.h:68
A position along a being's path.
Definition position.h:31
int y
Definition position.h:37
int x
Definition position.h:36
uint8_t flags
Definition theme.h:150
@ STATE_SELECTED
Definition theme.h:106