Mana
Loading...
Searching...
No Matches
itemshortcutcontainer.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2007-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
23
24#include "graphics.h"
25#include "inventory.h"
26#include "item.h"
27#include "itemshortcut.h"
28#include "keyboardconfig.h"
29#include "playerinfo.h"
30
31#include "gui/inventorywindow.h"
32#include "gui/itempopup.h"
33#include "gui/viewport.h"
34
35#include "resources/image.h"
36#include "resources/theme.h"
37
38#include "utils/stringutils.h"
39
45
47
49{
50 auto *g = static_cast<Graphics*>(graphics);
51 auto theme = gui->getTheme();
52 auto &skin = theme->getSkin(SkinType::ShortcutBox);
53
54 graphics->setFont(getFont());
55
56 for (int i = 0; i < mMaxItems; i++)
57 {
58 WidgetState state;
59 state.x = (i % mGridWidth) * mBoxWidth;
60 state.y = (i / mGridWidth) * mBoxHeight;
61 skin.draw(g, state);
62
63 // Draw item keyboard shortcut.
64 const char *key = SDL_GetKeyName(
67 g->drawText(key,
68 state.x + skin.padding + 2,
69 state.y + skin.padding + 2,
70 gcn::Graphics::LEFT);
71
72 const int itemId = itemShortcut->getItem(i);
73 if (itemId < 0)
74 continue;
75
76 if (Item *item = PlayerInfo::getInventory()->findItem(itemId))
77 {
78 // Draw item icon.
79 if (Image *image = item->getImage())
80 {
81 std::string caption;
82 if (item->getQuantity() > 1)
83 caption = toString(item->getQuantity());
84 else if (item->isEquipped())
85 caption = "Eq.";
86
87 image->setAlpha(1.0f);
88 g->drawImage(image, state.x + skin.padding, state.y + skin.padding);
89 if (item->isEquipped())
91 g->drawText(caption,
92 state.x + mBoxWidth / 2,
93 state.y + mBoxHeight - 14,
94 gcn::Graphics::CENTER);
95 }
96 }
97 }
98
99 if (mItemMoved)
100 {
101 // Draw the item image being dragged by the cursor.
102 if (Image *image = mItemMoved->getImage())
103 {
104 const int tPosX = mCursorPosX - (image->getWidth() / 2);
105 const int tPosY = mCursorPosY - (image->getHeight() / 2);
106
107 g->drawImage(image, tPosX, tPosY);
108 g->drawText(toString(mItemMoved->getQuantity()),
109 tPosX + mBoxWidth / 2, tPosY + mBoxHeight - 14,
110 gcn::Graphics::CENTER);
111 }
112 }
113}
114
115void ItemShortcutContainer::mouseDragged(gcn::MouseEvent &event)
116{
117 if (event.getButton() == gcn::MouseEvent::LEFT)
118 {
119 if (!mItemMoved && mItemClicked)
120 {
121 const int index = getIndexFromGrid(event.getX(), event.getY());
122 if (index == -1)
123 return;
124
125 const int itemId = itemShortcut->getItem(index);
126 if (itemId < 0)
127 return;
128
129 if (Item *item = PlayerInfo::getInventory()->findItem(itemId))
130 {
131 mItemMoved = item;
132 itemShortcut->removeItem(index);
133 }
134 }
135
136 if (mItemMoved)
137 {
138 mCursorPosX = event.getX();
139 mCursorPosY = event.getY();
140 }
141 }
142}
143
144void ItemShortcutContainer::mousePressed(gcn::MouseEvent &event)
145{
146 const int index = getIndexFromGrid(event.getX(), event.getY());
147
148 if (index == -1)
149 return;
150
151 if (event.getButton() == gcn::MouseEvent::LEFT)
152 {
153 // Stores the selected item if theirs one.
154 if (itemShortcut->isItemSelected() && inventoryWindow->isVisible())
155 {
156 itemShortcut->setItem(index);
158 }
159 else if (itemShortcut->getItem(index))
160 mItemClicked = true;
161 }
162 else if (event.getButton() == gcn::MouseEvent::RIGHT)
163 {
165 findItem(itemShortcut->getItem(index));
166
167 if (!item)
168 return;
169
170 // Convert relative to the window coordinates to absolute screen
171 // coordinates.
172 viewport->showPopup(nullptr, viewport->getMouseX(), viewport->getMouseY(), item);
173 }
174}
175
176void ItemShortcutContainer::mouseReleased(gcn::MouseEvent &event)
177{
178 if (event.getButton() == gcn::MouseEvent::LEFT)
179 {
182
183 const int index = getIndexFromGrid(event.getX(), event.getY());
184 if (index == -1)
185 {
186 mItemMoved = nullptr;
187 return;
188 }
189 if (mItemMoved)
190 {
192 mItemMoved = nullptr;
193 }
194 else if (itemShortcut->getItem(index) && mItemClicked)
195 {
196 itemShortcut->useItem(index);
197 }
198
199 if (mItemClicked)
200 mItemClicked = false;
201 }
202}
203
204// Show ItemTooltip
205void ItemShortcutContainer::mouseMoved(gcn::MouseEvent &event)
206{
207 if (Item *item = getItemAt(event.getX(), event.getY()))
208 {
209 mItemPopup->setItem(item->getInfo());
211 }
212 else
213 {
214 mItemPopup->setVisible(false);
215 }
216}
217
219{
220 const int index = getIndexFromGrid(x, y);
221 if (index == -1)
222 return nullptr;
223
224 const int itemId = itemShortcut->getItem(index);
225 if (itemId < 0)
226 return nullptr;
227
228 return PlayerInfo::getInventory()->findItem(itemId);
229}
230
231// Hide ItemTooltip
232void ItemShortcutContainer::mouseExited(gcn::MouseEvent &event)
233{
234 mItemPopup->setVisible(false);
235}
A central point of control for graphics.
Definition graphics.h:78
void setColor(const gcn::Color &color) override
Definition graphics.h:255
Theme * getTheme() const
The global GUI theme.
Definition gui.h:138
Defines a class for loading and storing images.
Definition image.h:45
Item * findItem(int itemId) const
Searches for the specified item by it's id.
Definition inventory.cpp:52
A popup that displays information about an item.
Definition itempopup.h:39
Item * getItemAt(int x, int y) const
void mousePressed(gcn::MouseEvent &event) override
Handles mouse when pressed.
void draw(gcn::Graphics *graphics) override
Draws the items.
void mouseReleased(gcn::MouseEvent &event) override
Handles mouse release.
void mouseMoved(gcn::MouseEvent &event) override
void mouseExited(gcn::MouseEvent &event) override
void mouseDragged(gcn::MouseEvent &event) override
Handles mouse when dragged.
std::unique_ptr< ItemPopup > mItemPopup
~ItemShortcutContainer() override
int getItemCount() const
Returns the amount of shortcut items.
void useItem(int index)
Try to use the item specified by the index.
int getItem(int index) const
Returns the shortcut item ID specified by the index.
void setItems(int index, int itemId)
Adds an item to the items store specified by the index.
void setItem(int index)
Adds the selected item ID to the items specified by the index.
void removeItem(int index)
Remove a item from the shortcut.
bool isItemSelected()
A flag to check if the item is selected.
void setItemSelected(int itemId)
Set the item that is selected.
Represents one or more instances of a certain item type.
Definition item.h:35
int getQuantity() const
Returns the number of items.
Definition item.h:69
int getId() const
Returns the item id.
Definition item.h:49
Image * getImage() const
Returns the item image.
Definition item.h:54
int getKeyValue(int index) const
Obtain the value stored in memory.
int getIndexFromGrid(int pointX, int pointY) const
Gets the index from the grid provided the point is in an item box.
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
@ ITEM_EQUIPPED
Definition theme.h:235
@ TEXT
Definition theme.h:214
int getMouseY() const
Returns mouse y in pixels.
Definition viewport.h:136
int getMouseX() const
Returns mouse x in pixels.
Definition viewport.h:131
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
Graphics * graphics
Definition client.cpp:104
KeyboardConfig keyboard
Definition client.cpp:101
InventoryWindow * inventoryWindow
Definition game.cpp:96
Viewport * viewport
Viewport on the map.
Definition game.cpp:115
Gui * gui
The GUI system.
Definition gui.cpp:50
ItemShortcut * itemShortcut
Inventory * getInventory()
Returns the player's inventory.
std::string toString(const T &arg)
Converts the given value to a string using std::stringstream.
Definition stringutils.h:68