ManaPlus
shoplistbox.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
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 
34 #include "gui/popups/itempopup.h"
35 #include "gui/popups/popupmenu.h"
36 
37 #include "gui/models/shopitems.h"
38 
40 
41 #include "net/net.h"
42 
44 
46 
47 #include "debug.h"
48 
49 const int ITEM_ICON_SIZE = 32;
50 
51 ShopListBox::ShopListBox(const Widget2 *const widget,
52  ListModel *const listModel,
53  const ShopListBoxTypeT type) :
54  ListBox(widget, listModel, "shoplistbox.xml"),
55  mPlayerMoney(0),
56  mShopItems(nullptr),
57  mWarningColor(getThemeColor(ThemeColorId::SHOP_WARNING, 255U)),
58  mType(type),
59  mPriceCheck(true),
60  mProtectItems(false)
61 {
66 }
67 
68 ShopListBox::ShopListBox(const Widget2 *const widget,
69  ListModel *const listModel,
70  ShopItems *const shopListModel,
71  const ShopListBoxTypeT type) :
72  ListBox(widget, listModel, "shoplistbox.xml"),
73  mPlayerMoney(0),
74  mShopItems(shopListModel),
76  mType(type),
77  mPriceCheck(true),
78  mProtectItems(false)
79 {
80  mRowHeight = std::max(getFont()->getHeight(), ITEM_ICON_SIZE);
84 }
85 
86 void ShopListBox::setPlayersMoney(const int money)
87 {
88  mPlayerMoney = money;
89 }
90 
91 void ShopListBox::draw(Graphics *const graphics)
92 {
93  BLOCK_START("ShopListBox::draw")
94  if ((mListModel == nullptr) || (mShopItems == nullptr))
95  {
96  BLOCK_END("ShopListBox::draw")
97  return;
98  }
99 
100  if (settings.guiAlpha != mAlpha)
102 
103  const unsigned int alpha = CAST_U32(mAlpha * 255.0F);
104  Font *const font = getFont();
105 
106  const int sz = mListModel->getNumberOfElements();
107  const int fontHeigh = getFont()->getHeight();
108  const int width = mDimension.width - 2 * mPadding;
109  // Draw the list elements
110  for (int i = 0, y = 0;
111  i < sz;
112  ++i, y += mRowHeight)
113  {
114  bool needDraw(false);
115  Color temp;
116  Color* backgroundColor = &mBackgroundColor;
117 
118  ShopItem *const item = mShopItems->at(i);
119  if ((item != nullptr) &&
120  (item->getDisabled() ||
121  (mPlayerMoney < item->getPrice() && mPriceCheck) ||
123  {
124  if (i != mSelected)
125  {
126  backgroundColor = &mWarningColor;
127  backgroundColor->a = alpha;
128  }
129  else
130  {
131  temp = mWarningColor;
132  temp.r = (temp.r + mHighlightColor.r) / 2;
133  temp.g = (temp.g + mHighlightColor.g) / 2;
134  temp.b = (temp.b + mHighlightColor.b) / 2;
135  temp.a = alpha;
136  backgroundColor = &temp;
137  }
138  needDraw = true;
139  }
140  else if (i == mSelected)
141  {
142  mHighlightColor.a = alpha;
143  backgroundColor = &mHighlightColor;
144  needDraw = true;
145  }
146  else
147  {
148  mBackgroundColor.a = alpha;
149  }
150 
151  if (needDraw)
152  {
153  graphics->setColor(*backgroundColor);
154  graphics->fillRectangle(Rect(mPadding, y + mPadding,
155  width, mRowHeight));
156  }
157 
158  if ((mShopItems != nullptr) && (item != nullptr))
159  {
160  Image *const icon = item->getImage();
161  if (icon != nullptr)
162  {
163  icon->setAlpha(1.0F);
164  graphics->drawImage(icon, mPadding, y + mPadding);
165  }
166  }
167  if (mSelected == i)
168  {
169  font->drawString(graphics,
174  y + (ITEM_ICON_SIZE - fontHeigh) / 2 + mPadding);
175  }
176  else
177  {
178  font->drawString(graphics,
183  y + (ITEM_ICON_SIZE - fontHeigh) / 2 + mPadding);
184  }
185  }
186  BLOCK_END("ShopListBox::draw")
187 }
188 
189 void ShopListBox::safeDraw(Graphics *const graphics)
190 {
191  ShopListBox::draw(graphics);
192 }
193 
195 {
196  BLOCK_START("ShopListBox::adjustSize")
197  if (mListModel != nullptr)
198  {
200  + 2 * mPadding);
201  }
202  BLOCK_END("ShopListBox::adjustSize")
203 }
204 
205 void ShopListBox::setPriceCheck(const bool check)
206 {
207  mPriceCheck = check;
208 }
209 
211 {
212  if ((itemPopup == nullptr) || (mRowHeight == 0u))
213  return;
214 
215  if (mShopItems == nullptr)
216  {
217  itemPopup->hide();
218  return;
219  }
220 
221  const int index = (event.getY() - mPadding) / mRowHeight;
222 
223  if (index < 0 || index >= mShopItems->getNumberOfElements())
224  {
225  itemPopup->hide();
226  }
227  else
228  {
229  const Item *const item = mShopItems->at(index);
230  if (item != nullptr)
231  {
232  itemPopup->setItem(item, false);
234  }
235  else
236  {
238  }
239  }
240 }
241 
243 {
244  ListBox::mouseReleased(event);
245  if (event.getType() == MouseEventType::RELEASED2)
246  {
247  if (dragDrop.isEmpty())
248  return;
249  const DragDropSourceT src = dragDrop.getSource();
252  {
253  return;
254  }
257  src != DragDropSource::Cart)
258  {
259  return;
260  }
261  Inventory *inventory;
262  if (src == DragDropSource::Inventory)
263  inventory = PlayerInfo::getInventory();
264  else if (src == DragDropSource::Cart)
265  inventory = PlayerInfo::getCartInventory();
266  else
267  return;
268  if (inventory == nullptr)
269  return;
270  Item *const item = inventory->getItem(dragDrop.getTag());
272  {
275  nullptr,
276  item,
277  0,
278  0);
279  }
280  else
281  {
284  nullptr,
285  item,
286  0,
287  0);
288  }
289  }
290 
291  if (event.getButton() == MouseButton::RIGHT)
292  {
293  setSelected(std::max(0, getSelectionByMouse(event.getY())));
294 
295  if (mSelected < 0 || mSelected >= mShopItems->getNumberOfElements())
296  return;
297 
298  Item *const item = mShopItems->at(mSelected);
299  if ((popupMenu != nullptr) && (viewport != nullptr))
300  {
302  viewport->mMouseY,
303  item);
304  }
305  }
306 }
307 
309 {
310  if (itemPopup == nullptr)
311  return;
312 
313  itemPopup->hide();
314 }
#define CAST_U32
Definition: cast.h:30
Font * getFont() const
Definition: widget.cpp:330
int mSelected
Definition: listbox.h:255
int width
Definition: rect.h:218
void setSelected(const int selected)
Definition: listbox.cpp:398
int mPadding
Definition: listbox.h:286
void setPlayersMoney(const int money)
Definition: shoplistbox.cpp:86
int getTag() const
Definition: dragdrop.h:234
Color mForegroundSelectedColor2
Definition: listbox.h:284
DragDropSource ::T DragDropSourceT
Definition: font.h:88
ShopItem * at(const size_t i) const
Definition: shopitems.cpp:128
unsigned int a
Definition: color.h:250
void setPriceCheck(const bool check)
void mouseMoved(MouseEvent &event)
void setVisible(Visible visible)
Definition: widget.cpp:224
ShopItems * mShopItems
Definition: shoplistbox.h:104
MouseButtonT getButton() const
Definition: mouseevent.h:115
unsigned int g
Definition: color.h:239
Definition: rect.h:72
Inventory * getInventory()
Definition: playerinfo.cpp:192
#define BLOCK_START(name)
Definition: perfomance.h:78
unsigned int mRowHeight
Definition: listbox.h:288
static void showWindow(const ItemAmountWindowUsageT usage, Window *const parent, Item *const item, int maxRange, const int tag)
Color mHighlightColor
Definition: listbox.h:282
void safeDraw(Graphics *const graphics)
DragDrop dragDrop
ShopListBox(const Widget2 *const widget, ListModel *const listModel, const ShopListBoxTypeT type)
Definition: shoplistbox.cpp:51
#define BLOCK_END(name)
Definition: perfomance.h:79
void mouseReleased(MouseEvent &event)
virtual int getNumberOfElements()=0
int mMouseX
Definition: viewport.h:153
const int ITEM_ICON_SIZE
Definition: shoplistbox.cpp:49
bool isEmpty() const
Definition: dragdrop.h:195
Color mForegroundColor
Definition: widget.h:1085
ListModel * mListModel
Definition: listbox.h:260
void adjustSize()
Color mForegroundSelectedColor
Definition: listbox.h:283
bool mPriceCheck
Definition: shoplistbox.h:109
ShopListBoxType ::T ShopListBoxTypeT
Settings settings
Definition: settings.cpp:31
virtual void setAlpha(const float alpha)
Definition: image.cpp:285
void draw(Graphics *const graphics)
Definition: shoplistbox.cpp:91
virtual int getSelectionByMouse(const int y) const
Definition: listbox.cpp:391
void hide()
Definition: popup.cpp:262
void mouseExited(MouseEvent &event)
Color mBackgroundColor
Definition: widget.h:1090
Inventory * getCartInventory()
Definition: playerinfo.cpp:204
virtual void setColor(const Color &color)
Definition: graphics.h:319
unsigned int b
Definition: color.h:244
const bool Visible_false
Definition: visible.h:29
int getPrice() const
Definition: shopitem.h:131
bool getDisabled() const
Definition: shopitem.h:156
PopupMenu * popupMenu
Definition: popupmenu.cpp:102
void position(const int x, const int y)
Definition: popup.cpp:232
void drawString(Graphics *const graphics, Color col, const Color &col2, const std::string &text, const int x, const int y)
Definition: font.cpp:253
ShopListBoxTypeT mType
Definition: shoplistbox.h:107
Definition: item.h:48
#define nullptr
Definition: localconsts.h:44
void mouseReleased(MouseEvent &event)
Definition: listbox.cpp:310
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
static float mAlpha
Definition: listbox.h:291
virtual std::string getElementAt(int i)=0
int mMouseY
Definition: viewport.h:154
int getY() const
Definition: mouseevent.h:137
void setHeight(const int height)
Definition: widget.cpp:139
ServerTypeT getNetworkType()
Definition: net.cpp:178
MouseEventTypeT getType() const
Definition: mouseevent.h:154
bool isItemProtected(const int id)
Definition: playerinfo.cpp:511
int getId() const
Definition: item.h:80
virtual void fillRectangle(const Rect &rectangle)=0
#define A_UNUSED
Definition: localconsts.h:171
Color mForegroundColor2
Definition: widget2.h:112
Image * getImage() const
Definition: item.h:86
virtual void drawImage(const Image *const image, int dstX, int dstY)=0
Color mWarningColor
Definition: shoplistbox.h:106
Definition: image.h:61
int getHeight() const
Definition: widget.h:239
int mPlayerMoney
Definition: shoplistbox.h:98
void showItemPopup(const int x, const int y, const Item *const item)
Definition: popupmenu.cpp:1789
Definition: color.h:74
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:44
DragDropSourceT getSource() const
Definition: dragdrop.h:83
int getHeight() const
Definition: font.cpp:360
ItemPopup * itemPopup
Definition: itempopup.cpp:63
Item * getItem(const int index) const
Definition: inventory.cpp:82
float guiAlpha
Definition: settings.h:129
bool mProtectItems
Definition: shoplistbox.h:110
Rect mDimension
Definition: widget.h:1100
unsigned int r
Definition: color.h:234
Viewport * viewport
Definition: viewport.cpp:35
int getNumberOfElements()
Definition: shopitems.h:98