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-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
25 
26 #include "dragdrop.h"
27 #include "settings.h"
28 
29 #include "being/playerinfo.h"
30 
31 #include "gui/viewport.h"
32 
33 #include "gui/fonts/font.h"
34 
35 #include "gui/popups/itempopup.h"
36 #include "gui/popups/popupmenu.h"
37 
38 #include "gui/models/shopitems.h"
39 
41 
42 #include "net/net.h"
43 
45 
47 
48 #include "debug.h"
49 
50 const int ITEM_ICON_SIZE = 32;
51 
52 ShopListBox::ShopListBox(const Widget2 *const widget,
53  ListModel *const listModel,
54  const ShopListBoxTypeT type) :
55  ListBox(widget, listModel, "shoplistbox.xml"),
56  mPlayerMoney(0),
57  mShopItems(nullptr),
58  mWarningColor(getThemeColor(ThemeColorId::SHOP_WARNING, 255U)),
59  mType(type),
60  mPriceCheck(true),
61  mProtectItems(false)
62 {
64  mHighlightColor = getThemeColor(ThemeColorId::HIGHLIGHT, 255U);
65  mForegroundColor = getThemeColor(ThemeColorId::LISTBOX, 255U);
67 }
68 
69 ShopListBox::ShopListBox(const Widget2 *const widget,
70  ListModel *const listModel,
71  ShopItems *const shopListModel,
72  const ShopListBoxTypeT type) :
73  ListBox(widget, listModel, "shoplistbox.xml"),
74  mPlayerMoney(0),
75  mShopItems(shopListModel),
76  mWarningColor(getThemeColor(ThemeColorId::SHOP_WARNING, 255U)),
77  mType(type),
78  mPriceCheck(true),
79  mProtectItems(false)
80 {
81  mRowHeight = std::max(getFont()->getHeight(), ITEM_ICON_SIZE);
82  mHighlightColor = getThemeColor(ThemeColorId::HIGHLIGHT, 255U);
83  mForegroundColor = getThemeColor(ThemeColorId::LISTBOX, 255U);
85 }
86 
87 void ShopListBox::setPlayersMoney(const int money)
88 {
89  mPlayerMoney = money;
90 }
91 
92 void ShopListBox::draw(Graphics *const graphics)
93 {
94  BLOCK_START("ShopListBox::draw")
95  if ((mListModel == nullptr) || (mShopItems == nullptr))
96  {
97  BLOCK_END("ShopListBox::draw")
98  return;
99  }
100 
101  if (settings.guiAlpha != mAlpha)
103 
104  const unsigned int alpha = CAST_U32(mAlpha * 255.0F);
105  Font *const font = getFont();
106 
107  const int sz = mListModel->getNumberOfElements();
108  const int fontHeigh = getFont()->getHeight();
109  const int width = mDimension.width - 2 * mPadding;
110  // Draw the list elements
111  for (int i = 0, y = 0;
112  i < sz;
113  ++i, y += mRowHeight)
114  {
115  bool needDraw(false);
116  Color temp;
117  Color* backgroundColor = &mBackgroundColor;
118 
119  ShopItem *const item = mShopItems->at(i);
120  if ((item != nullptr) &&
121  (item->getDisabled() ||
122  (mPlayerMoney < item->getPrice() && mPriceCheck) ||
124  {
125  if (i != mSelected)
126  {
127  backgroundColor = &mWarningColor;
128  backgroundColor->a = alpha;
129  }
130  else
131  {
132  temp = mWarningColor;
133  temp.r = (temp.r + mHighlightColor.r) / 2;
134  temp.g = (temp.g + mHighlightColor.g) / 2;
135  temp.b = (temp.b + mHighlightColor.b) / 2;
136  temp.a = alpha;
137  backgroundColor = &temp;
138  }
139  needDraw = true;
140  }
141  else if (i == mSelected)
142  {
143  mHighlightColor.a = alpha;
144  backgroundColor = &mHighlightColor;
145  needDraw = true;
146  }
147  else
148  {
149  mBackgroundColor.a = alpha;
150  }
151 
152  if (needDraw)
153  {
154  graphics->setColor(*backgroundColor);
155  graphics->fillRectangle(Rect(mPadding, y + mPadding,
156  width, mRowHeight));
157  }
158 
159  if ((mShopItems != nullptr) && (item != nullptr))
160  {
161  Image *const icon = item->getImage();
162  if (icon != nullptr)
163  {
164  icon->setAlpha(1.0F);
165  graphics->drawImage(icon, mPadding, y + mPadding);
166  }
167  }
168  if (mSelected == i)
169  {
170  font->drawString(graphics,
175  y + (ITEM_ICON_SIZE - fontHeigh) / 2 + mPadding);
176  }
177  else
178  {
179  font->drawString(graphics,
184  y + (ITEM_ICON_SIZE - fontHeigh) / 2 + mPadding);
185  }
186  }
187  BLOCK_END("ShopListBox::draw")
188 }
189 
190 void ShopListBox::safeDraw(Graphics *const graphics)
191 {
192  ShopListBox::draw(graphics);
193 }
194 
196 {
197  BLOCK_START("ShopListBox::adjustSize")
198  if (mListModel != nullptr)
199  {
201  + 2 * mPadding);
202  }
203  BLOCK_END("ShopListBox::adjustSize")
204 }
205 
206 void ShopListBox::setPriceCheck(const bool check)
207 {
208  mPriceCheck = check;
209 }
210 
212 {
213  if ((itemPopup == nullptr) || (mRowHeight == 0U))
214  return;
215 
216  if (mShopItems == nullptr)
217  {
218  itemPopup->hide();
219  return;
220  }
221 
222  const int index = (event.getY() - mPadding) / mRowHeight;
223 
224  if (index < 0 || index >= mShopItems->getNumberOfElements())
225  {
226  itemPopup->hide();
227  }
228  else
229  {
230  const Item *const item = mShopItems->at(index);
231  if (item != nullptr)
232  {
233  itemPopup->setItem(item, false);
235  }
236  else
237  {
239  }
240  }
241 }
242 
244 {
245  ListBox::mouseReleased(event);
246  if (event.getType() == MouseEventType::RELEASED2)
247  {
248  if (dragDrop.isEmpty())
249  return;
250  const DragDropSourceT src = dragDrop.getSource();
253  {
254  return;
255  }
258  src != DragDropSource::Cart)
259  {
260  return;
261  }
262  Inventory *inventory;
263  if (src == DragDropSource::Inventory)
264  inventory = PlayerInfo::getInventory();
265  else if (src == DragDropSource::Cart)
266  inventory = PlayerInfo::getCartInventory();
267  else
268  return;
269  if (inventory == nullptr)
270  return;
271  Item *const item = inventory->getItem(dragDrop.getTag());
273  {
276  nullptr,
277  item,
278  0,
279  0);
280  }
281  else
282  {
285  nullptr,
286  item,
287  0,
288  0);
289  }
290  }
291 
292  if (event.getButton() == MouseButton::RIGHT)
293  {
294  setSelected(std::max(0, getSelectionByMouse(event.getY())));
295 
296  if (mSelected < 0 || mSelected >= mShopItems->getNumberOfElements())
297  return;
298 
299  Item *const item = mShopItems->at(mSelected);
300  if ((popupMenu != nullptr) && (viewport != nullptr))
301  {
303  viewport->mMouseY,
304  item);
305  }
306  }
307 }
308 
310 {
311  if (itemPopup == nullptr)
312  return;
313 
314  itemPopup->hide();
315 }
#define CAST_U32
Definition: cast.h:31
Definition: color.h:76
unsigned int a
Definition: color.h:251
unsigned int b
Definition: color.h:245
unsigned int r
Definition: color.h:235
unsigned int g
Definition: color.h:240
DragDropSourceT getSource() const
Definition: dragdrop.h:84
bool isEmpty() const
Definition: dragdrop.h:196
int getTag() const
Definition: dragdrop.h:235
Definition: font.h:90
int getHeight() const
Definition: font.cpp:362
void drawString(Graphics *const graphics, Color col, const Color &col2, const std::string &text, const int x, const int y)
Definition: font.cpp:254
virtual void drawImage(const Image *const image, int dstX, int dstY)=0
virtual void fillRectangle(const Rect &rectangle)=0
virtual void setColor(const Color &color)
Definition: graphics.h:320
Item * getItem(const int index) const
Definition: inventory.cpp:83
static void showWindow(const ItemAmountWindowUsageT usage, Window *const parent, Item *const item, int maxRange, const int tag)
void setItem(const ItemInfo &item, const ItemColor color, const bool showImage, int id, const int *const cards, const ItemOptionsList *const options)
Definition: itempopup.cpp:189
Definition: item.h:50
int getId() const
Definition: item.h:81
Image * getImage() const
Definition: item.h:87
int mSelected
Definition: listbox.h:256
Color mHighlightColor
Definition: listbox.h:283
unsigned int mRowHeight
Definition: listbox.h:289
Color mForegroundSelectedColor2
Definition: listbox.h:285
void mouseReleased(MouseEvent &event)
Definition: listbox.cpp:311
ListModel * mListModel
Definition: listbox.h:261
int mPadding
Definition: listbox.h:287
static float mAlpha
Definition: listbox.h:292
void setSelected(const int selected)
Definition: listbox.cpp:399
virtual int getSelectionByMouse(const int y) const
Definition: listbox.cpp:392
Color mForegroundSelectedColor
Definition: listbox.h:284
virtual std::string getElementAt(int i)=0
virtual int getNumberOfElements()=0
MouseButtonT getButton() const
Definition: mouseevent.h:116
MouseEventTypeT getType() const
Definition: mouseevent.h:155
int getY() const
Definition: mouseevent.h:138
void showItemPopup(const int x, const int y, const Item *const item)
Definition: popupmenu.cpp:1790
void hide()
Definition: popup.cpp:265
void position(const int x, const int y)
Definition: popup.cpp:235
Definition: rect.h:74
int width
Definition: rect.h:219
float guiAlpha
Definition: settings.h:131
bool getDisabled() const
Definition: shopitem.h:157
int getNumberOfElements()
Definition: shopitems.h:99
ShopItem * at(const size_t i) const
Definition: shopitems.cpp:129
bool mPriceCheck
Definition: shoplistbox.h:110
void mouseExited(MouseEvent &event)
void draw(Graphics *const graphics)
Definition: shoplistbox.cpp:92
void safeDraw(Graphics *const graphics)
void mouseReleased(MouseEvent &event)
ShopListBox(const Widget2 *const widget, ListModel *const listModel, const ShopListBoxTypeT type)
Definition: shoplistbox.cpp:52
Color mWarningColor
Definition: shoplistbox.h:107
void setPlayersMoney(const int money)
Definition: shoplistbox.cpp:87
void setPriceCheck(const bool check)
ShopListBoxTypeT mType
Definition: shoplistbox.h:108
ShopItems * mShopItems
Definition: shoplistbox.h:105
void adjustSize()
int mPlayerMoney
Definition: shoplistbox.h:99
bool mProtectItems
Definition: shoplistbox.h:111
void mouseMoved(MouseEvent &event)
int mMouseX
Definition: viewport.h:154
int mMouseY
Definition: viewport.h:155
Color mForegroundColor2
Definition: widget2.h:113
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:45
void setVisible(Visible visible)
Definition: widget.cpp:225
Color mForegroundColor
Definition: widget.h:1086
Rect mDimension
Definition: widget.h:1101
Color mBackgroundColor
Definition: widget.h:1091
void setHeight(const int height)
Definition: widget.cpp:140
Font * getFont() const
Definition: widget.cpp:331
int getHeight() const
Definition: widget.h:240
DragDrop dragDrop
DragDropSource ::T DragDropSourceT
Viewport * viewport
Definition: viewport.cpp:36
ItemPopup * itemPopup
Definition: itempopup.cpp:64
#define nullptr
Definition: localconsts.h:45
#define A_UNUSED
Definition: localconsts.h:160
ServerTypeT getNetworkType()
Definition: net.cpp:189
Inventory * getInventory()
Definition: playerinfo.cpp:195
Inventory * getCartInventory()
Definition: playerinfo.cpp:207
bool isItemProtected(const int id)
Definition: playerinfo.cpp:515
#define BLOCK_END(name)
Definition: perfomance.h:80
#define BLOCK_START(name)
Definition: perfomance.h:79
PopupMenu * popupMenu
Definition: popupmenu.cpp:103
Settings settings
Definition: settings.cpp:32
const int ITEM_ICON_SIZE
Definition: shoplistbox.cpp:50
ShopListBoxType ::T ShopListBoxTypeT
const bool Visible_false
Definition: visible.h:30