ManaPlus
virtshortcutcontainer.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 Andrei Karas
5  * Copyright (C) 2011-2018 The ManaPlus Developers
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 
24 
25 #include "dragdrop.h"
26 #include "settings.h"
27 
28 #include "being/playerinfo.h"
29 
30 #include "gui/viewport.h"
31 
32 #include "gui/fonts/font.h"
33 
35 
36 #include "gui/popups/itempopup.h"
37 #include "gui/popups/popupmenu.h"
38 
40 
41 #include "utils/stringutils.h"
42 
43 #include "debug.h"
44 
46  ShortcutBase *const shortcut) :
47  ShortcutContainer(widget),
48  mItemClicked(false),
49  mEquipedColor(getThemeColor(ThemeColorId::ITEM_EQUIPPED, 255U)),
50  mEquipedColor2(getThemeColor(ThemeColorId::ITEM_EQUIPPED_OUTLINE, 255U)),
51  mUnEquipedColor(getThemeColor(ThemeColorId::ITEM_NOT_EQUIPPED, 255U)),
52  mUnEquipedColor2(getThemeColor(ThemeColorId::ITEM_NOT_EQUIPPED_OUTLINE,
53  255U)),
54  mShortcut(shortcut)
55 {
56  if (mShortcut != nullptr)
58  else
59  mMaxItems = 0;
60 }
61 
63 {
64 }
65 
66 void VirtShortcutContainer::setSkin(const Widget2 *const widget,
67  Skin *const skin)
68 {
69  ShortcutContainer::setSkin(widget, skin);
74  255U);
75 }
76 
78 {
79  if (mShortcut == nullptr)
80  return;
81 
82  BLOCK_START("VirtShortcutContainer::draw")
83  if (settings.guiAlpha != mAlpha)
84  {
86  if (mBackgroundImg != nullptr)
88  }
89 
90  drawBackground(graphics);
91 
92  const Inventory *const inv = PlayerInfo::getInventory();
93  if (inv == nullptr)
94  {
95  BLOCK_END("VirtShortcutContainer::draw")
96  return;
97  }
98 
99  Font *const font = getFont();
100 
101  for (unsigned i = 0; i < mMaxItems; i++)
102  {
103  const int itemX = (i % mGridWidth) * mBoxWidth;
104  const int itemY = (i / mGridWidth) * mBoxHeight;
105 
106  if (mShortcut->getItem(i) < 0)
107  continue;
108 
109  const Item *const item = inv->findItem(mShortcut->getItem(i),
110  mShortcut->getItemColor(i));
111 
112  if (item != nullptr)
113  {
114  // Draw item icon.
115  Image *const image = item->getImage();
116 
117  if (image != nullptr)
118  {
119  std::string caption;
120  if (item->getQuantity() > 1)
121  caption = toString(item->getQuantity());
122  else if (item->isEquipped() == Equipped_true)
123  caption = "Eq.";
124 
125  image->setAlpha(1.0F);
126  graphics->drawImage(image,
127  itemX + mImageOffsetX,
128  itemY + mImageOffsetY);
129  if (item->isEquipped() == Equipped_true)
130  {
131  font->drawString(graphics,
134  caption,
135  itemX + (mBoxWidth - font->getWidth(caption)) / 2,
136  itemY + mBoxHeight - 14);
137  }
138  else
139  {
140  font->drawString(graphics,
143  caption,
144  itemX + (mBoxWidth - font->getWidth(caption)) / 2,
145  itemY + mBoxHeight - 14);
146  }
147  }
148  }
149  }
150  BLOCK_END("VirtShortcutContainer::draw")
151 }
152 
154 {
155  if (mShortcut == nullptr)
156  return;
157 
158  BLOCK_START("VirtShortcutContainer::safeDraw")
159  if (settings.guiAlpha != mAlpha)
160  {
162  if (mBackgroundImg != nullptr)
164  }
165 
166  safeDrawBackground(graphics);
167 
168  const Inventory *const inv = PlayerInfo::getInventory();
169  if (inv == nullptr)
170  {
171  BLOCK_END("VirtShortcutContainer::safeDraw")
172  return;
173  }
174 
175  Font *const font = getFont();
176 
177  for (unsigned i = 0; i < mMaxItems; i++)
178  {
179  const int itemX = (i % mGridWidth) * mBoxWidth;
180  const int itemY = (i / mGridWidth) * mBoxHeight;
181 
182  if (mShortcut->getItem(i) < 0)
183  continue;
184 
185  const Item *const item = inv->findItem(mShortcut->getItem(i),
186  mShortcut->getItemColor(i));
187 
188  if (item != nullptr)
189  {
190  // Draw item icon.
191  Image *const image = item->getImage();
192 
193  if (image != nullptr)
194  {
195  std::string caption;
196  if (item->getQuantity() > 1)
197  caption = toString(item->getQuantity());
198  else if (item->isEquipped() == Equipped_true)
199  caption = "Eq.";
200 
201  image->setAlpha(1.0F);
202  graphics->drawImage(image,
203  itemX + mImageOffsetX,
204  itemY + mImageOffsetY);
205  if (item->isEquipped() == Equipped_true)
206  {
207  font->drawString(graphics,
210  caption,
211  itemX + (mBoxWidth - font->getWidth(caption)) / 2,
212  itemY + mBoxHeight - 14);
213  }
214  else
215  {
216  font->drawString(graphics,
219  caption,
220  itemX + (mBoxWidth - font->getWidth(caption)) / 2,
221  itemY + mBoxHeight - 14);
222  }
223  }
224  }
225  }
226  BLOCK_END("VirtShortcutContainer::safeDraw")
227 }
228 
230 {
231  if (mShortcut == nullptr)
232  return;
233 
234  if (event.getButton() == MouseButton::LEFT)
235  {
236  if (dragDrop.isEmpty() && mItemClicked)
237  {
238  const int index = getIndexFromGrid(event.getX(), event.getY());
239 
240  if (index == -1)
241  return;
242 
243  const int itemId = mShortcut->getItem(index);
244  const ItemColor itemColor = mShortcut->getItemColor(index);
245 
246  if (itemId < 0)
247  return;
248 
249  const Inventory *const inv = PlayerInfo::getInventory();
250  if (inv == nullptr)
251  return;
252 
253  Item *const item = inv->findItem(itemId, itemColor);
254 
255  if (item != nullptr)
256  {
258  mShortcut->removeItem(index);
259  }
260  else
261  {
262  dragDrop.clear();
263  }
264  }
265  }
266 }
267 
269 {
270  if ((mShortcut == nullptr) || (inventoryWindow == nullptr))
271  return;
272 
273  const int index = getIndexFromGrid(event.getX(), event.getY());
274 
275  if (index == -1)
276  return;
277 
278  event.consume();
279 
280  const MouseButtonT eventButton = event.getButton();
281  if (eventButton == MouseButton::LEFT)
282  {
283  if (mShortcut->getItem(index) > 0)
284  {
285  mItemClicked = true;
286  }
287  else
288  {
289  if (dragDrop.isSelected())
290  {
293  dragDrop.deselect();
294  }
295  }
296  }
297  else if (eventButton == MouseButton::RIGHT)
298  {
299  const Inventory *const inv = PlayerInfo::getInventory();
300  if (inv == nullptr)
301  return;
302 
303  Item *const item = inv->findItem(mShortcut->getItem(index),
304  mShortcut->getItemColor(index));
305 
306  if (popupMenu != nullptr)
307  {
309  viewport->mMouseY,
310  item);
311  }
312  }
313 }
314 
316 {
317  if (mShortcut == nullptr)
318  return;
319 
320  if (event.getButton() == MouseButton::LEFT)
321  {
322  if (mShortcut->isItemSelected())
324 
325  const int index = getIndexFromGrid(event.getX(), event.getY());
326  if (index == -1)
327  {
328  dragDrop.clear();
329  return;
330  }
331  if (!dragDrop.isEmpty())
332  {
334  {
337  dragDrop.clear();
338  dragDrop.deselect();
339  }
340  }
341 
342  mItemClicked = false;
343  }
344 }
345 
346 // Show ItemTooltip
348 {
349  if (mShortcut == nullptr)
350  return;
351 
352  const int index = getIndexFromGrid(event.getX(), event.getY());
353 
354  if (index == -1)
355  return;
356 
357  const int itemId = mShortcut->getItem(index);
358  const ItemColor itemColor = mShortcut->getItemColor(index);
359 
360  if (itemId < 0)
361  return;
362 
363  const Inventory *const inv = PlayerInfo::getInventory();
364  if (inv == nullptr)
365  return;
366 
367  const Item *const item = inv->findItem(itemId, itemColor);
368 
369  if ((item != nullptr) && (viewport != nullptr))
370  {
371  itemPopup->setItem(item, false);
373  }
374  else
375  {
377  }
378 }
379 
381 {
382  if (itemPopup != nullptr)
384 }
385 
387 {
388  if (itemPopup != nullptr)
390 }
Font * getFont() const
Definition: widget.cpp:330
Definition: skin.h:35
Definition: font.h:88
void clear()
Definition: dragdrop.h:182
bool isSelected() const
Definition: dragdrop.h:224
Item * findItem(const int itemId, const ItemColor color) const
Definition: inventory.cpp:93
void setVisible(Visible visible)
Definition: widget.cpp:224
Equipped isEquipped() const
Definition: item.h:128
void setItemSelected(const int itemId)
Definition: shortcutbase.h:112
MouseButtonT getButton() const
Definition: mouseevent.h:115
Inventory * getInventory()
Definition: playerinfo.cpp:193
#define BLOCK_START(name)
Definition: perfomance.h:78
DragDrop dragDrop
#define BLOCK_END(name)
Definition: perfomance.h:79
uint16_t ItemColor
Definition: itemcolor.h:29
bool isItemSelected() const
Definition: shortcutbase.h:120
int mMouseX
Definition: viewport.h:153
const bool Equipped_true
Definition: equipped.h:29
bool isEmpty() const
Definition: dragdrop.h:195
InventoryWindow * inventoryWindow
void setItems(const size_t index, const int itemId, const ItemColor color)
Definition: shortcutbase.h:102
void setSkin(const Widget2 *const widget, Skin *const skin)
VirtShortcutContainer(Widget2 *const widget, ShortcutBase *const shortcut)
void dragItem(const Item *const item, const DragDropSourceT source, const int tag)
Definition: dragdrop.h:86
int getIndexFromGrid(const int pointX, const int pointY) const
Settings settings
Definition: settings.cpp:31
virtual void setAlpha(const float alpha)
Definition: image.cpp:286
bool isSourceItemContainer() const
Definition: dragdrop.h:240
void showDropPopup(const int x, const int y, const Item *const item)
Definition: popupmenu.cpp:1848
const bool Visible_false
Definition: visible.h:29
void removeItem(const size_t index)
Definition: shortcutbase.h:126
int getQuantity() const
Definition: item.h:104
virtual void setSkin(const Widget2 *const widget, Skin *const skin)
void widgetHidden(const Event &event)
int getSelected() const
Definition: dragdrop.h:218
PopupMenu * popupMenu
Definition: popupmenu.cpp:102
void position(const int x, const int y)
Definition: popup.cpp:234
void drawString(Graphics *const graphics, Color col, const Color &col2, const std::string &text, const int x, const int y)
Definition: font.cpp:253
Definition: item.h:48
void mouseDragged(MouseEvent &event)
void deselect()
Definition: dragdrop.h:212
void mousePressed(MouseEvent &event)
int getItem(const size_t index) const
Definition: shortcutbase.h:71
void mouseMoved(MouseEvent &event)
bool shortcut(InputEvent &event)
Definition: actions.cpp:34
void setItem(const ItemInfo &item, const ItemColor color, const bool showImage, int id, const int *const cards, const ItemOptionsList *const options)
Definition: itempopup.cpp:188
void mouseReleased(MouseEvent &event)
int mMouseY
Definition: viewport.h:154
void drawBackground(Graphics *const g)
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774
#define A_UNUSED
Definition: localconsts.h:159
void draw(Graphics *const graphics)
Image * getImage() const
Definition: item.h:86
virtual void drawImage(const Image *const image, int dstX, int dstY)=0
int getWidth(const std::string &text) const
Definition: font.cpp:333
Definition: event.h:77
ItemColor getItemColor() const
Definition: dragdrop.h:68
Definition: image.h:61
void safeDrawBackground(Graphics *const g)
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:44
int getX() const
Definition: mouseevent.h:126
ItemPopup * itemPopup
Definition: itempopup.cpp:63
void safeDraw(Graphics *const graphics)
float guiAlpha
Definition: settings.h:129
int getItemCount() const
Definition: shortcutbase.h:80
void mouseExited(MouseEvent &event)
MouseButton ::T MouseButtonT
Definition: mousebutton.h:77
ItemColor getSelectedColor() const
Definition: dragdrop.h:221
ItemColor getItemColor(const size_t index) const
Definition: shortcutbase.h:74
Viewport * viewport
Definition: viewport.cpp:35
int getItem() const
Definition: dragdrop.h:65