ManaPlus
itemamountwindow.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 "settings.h"
27 
28 #include "being/playerinfo.h"
29 
30 #include "input/keyboardconfig.h"
31 
32 #include "gui/viewport.h"
33 
34 #include "gui/models/itemsmodel.h"
35 
36 #include "gui/popups/itempopup.h"
37 
39 #include "gui/windows/npcdialog.h"
40 #include "gui/windows/shopwindow.h"
42 
43 #include "gui/widgets/button.h"
46 #include "gui/widgets/dropdown.h"
47 #include "gui/widgets/icon.h"
49 #include "gui/widgets/label.h"
50 #include "gui/widgets/slider.h"
51 
52 #include "net/inventoryhandler.h"
53 #include "net/mail2handler.h"
54 #include "net/npchandler.h"
55 
56 #include "resources/item/item.h"
57 
58 #include "utils/delete2.h"
59 #include "utils/gettext.h"
60 
61 #ifndef USE_SDL2
62 #include <cmath>
63 #endif // USE_SDL2
64 
65 #include "debug.h"
66 
67 void ItemAmountWindow::finish(Item *const item,
68  const int amount,
69  const int price,
70  const ItemAmountWindowUsageT usage)
71 {
72  if (item == nullptr)
73  return;
74  switch (usage)
75  {
77  if (tradeWindow != nullptr)
78  tradeWindow->tradeItem(item, amount, false);
79  break;
81  PlayerInfo::dropItem(item, amount, Sfx_true);
82  break;
84  inventoryHandler->splitItem(item, amount);
85  break;
88  item->getInvIndex(), amount, InventoryType::Storage);
89  break;
92  item->getInvIndex(), amount, InventoryType::Inventory);
93  break;
95  if (shopWindow != nullptr)
96  shopWindow->addBuyItem(item, amount, price);
97  break;
99  if (shopWindow != nullptr)
100  shopWindow->addSellItem(item, amount, price);
101  break;
104  item->getInvIndex(), amount, InventoryType::Cart);
105  break;
108  item->getInvIndex(), amount, InventoryType::Inventory);
109  break;
112  {
113  mail2Handler->addItem(item, amount);
114  }
115  else if (mailEditWindow != nullptr)
116  {
117  mailEditWindow->addItem(item, amount);
118  }
119  break;
121  {
122  NpcDialog *const dialog = npcHandler->getCurrentNpcDialog();
123  if (dialog != nullptr)
124  dialog->addCraftItem(item, amount, price); // price as slot
125  break;
126  }
127  default:
128  break;
129  }
130 }
131 
133  Window *const parent,
134  Item *const item,
135  const int maxRange) :
136  Window("", Modal_false, parent, "amount.xml"),
137  ActionListener(),
138  KeyListener(),
139  mItemAmountTextField(new IntTextField(this, 1, 0, 0, Enable_true, 0)),
140  mItemPriceTextField(nullptr),
141  mGPLabel(nullptr),
142  mItem(item),
143  mItemIcon(new Icon(this,
144  item != nullptr ? item->getImage() : nullptr, AutoRelease_false)),
145  mItemAmountSlide(new Slider(this, 1.0, maxRange, 1.0)),
146  mItemPriceSlide(nullptr),
147  mItemDropDown(nullptr),
148  mItemsModal(nullptr),
149  mPrice(0),
150  mMax(maxRange),
151  mUsage(usage),
152  mEnabledKeyboard(keyboard.isEnabled())
153 {
154  if (mItem == nullptr)
155  return;
156 
158  mMax = 10000;
159  else if (mMax == 0)
160  mMax = mItem->getQuantity();
161 
162  keyboard.setEnabled(false);
163 
167 
171 
174  {
175  mItemPriceTextField = new IntTextField(this, 1, 0, 0, Enable_true, 0);
176  mItemPriceTextField->setRange(1, 10000000);
179 
180  mItemPriceSlide = new Slider(this, 1.0, 10000000, 1.0);
182  mItemPriceSlide->setActionEventId("slidePrice");
184 
185  mGPLabel = new Label(this, " GP");
186  }
187 
189  {
190  mItemsModal = new ItemsModal;
191  mItemDropDown = new DropDown(this,
192  mItemsModal,
193  false,
194  Modal_false,
195  nullptr,
196  std::string());
197  mItemDropDown->setActionEventId("itemType");
199  }
200 
201  // Buttons
202  Button *const minusAmountButton = new Button(this,
203  // TRANSLATORS: item amount window button
204  _("-"),
205  "dec",
206  BUTTON_SKIN,
207  this);
208  Button *const plusAmountButton = new Button(this,
209  // TRANSLATORS: item amount window button
210  _("+"),
211  "inc",
212  BUTTON_SKIN,
213  this);
214  Button *const okButton = new Button(this,
215  // TRANSLATORS: item amount window button
216  _("OK"),
217  "ok",
218  BUTTON_SKIN,
219  this);
220  Button *const cancelButton = new Button(this,
221  // TRANSLATORS: item amount window button
222  _("Cancel"),
223  "cancel",
224  BUTTON_SKIN,
225  this);
226  Button *const addAllButton = new Button(this,
227  // TRANSLATORS: item amount window button
228  _("All"),
229  "all",
230  BUTTON_SKIN,
231  this);
232 
233  minusAmountButton->adjustSize();
234  minusAmountButton->setWidth(plusAmountButton->getWidth());
235 
236  // Set positions
237  ContainerPlacer placer(nullptr, nullptr);
238  placer = getPlacer(0, 0);
239  int n = 0;
241  {
242  placer(0, n, mItemDropDown, 8, 1);
243  n++;
244  }
245  placer(1, n, minusAmountButton, 1, 1);
246  placer(2, n, mItemAmountTextField, 3, 1);
247  placer(5, n, plusAmountButton, 1, 1);
248  placer(6, n, addAllButton, 1, 1);
249 
250  placer(0, n, mItemIcon, 1, 3);
251  placer(1, n + 1, mItemAmountSlide, 7, 1);
252 
255  {
256  Button *const minusPriceButton = new Button(this,
257  // TRANSLATORS: item amount window button
258  _("-"),
259  "decPrice",
260  BUTTON_SKIN,
261  this);
262  Button *const plusPriceButton = new Button(this,
263  // TRANSLATORS: item amount window button
264  _("+"),
265  "incPrice",
266  BUTTON_SKIN,
267  this);
268  minusPriceButton->adjustSize();
269  minusPriceButton->setWidth(plusPriceButton->getWidth());
270 
271  placer(1, n + 2, minusPriceButton, 1, 1);
272  placer(2, n + 2, mItemPriceTextField, 3, 1);
273  placer(5, n + 2, plusPriceButton, 1, 1);
274  placer(6, n + 2, mGPLabel, 1, 1);
275 
276  placer(1, n + 3, mItemPriceSlide, 7, 1);
277  placer(4, n + 5, cancelButton, 1, 1);
278  placer(5, n + 5, okButton, 1, 1);
279  }
280  else
281  {
282  placer(4, n + 2, cancelButton, 1, 1);
283  placer(5, n + 2, okButton, 1, 1);
284  }
285 
286  reflowLayout(225, 0);
287 
288  resetAmount();
289 
290  switch (usage)
291  {
293  // TRANSLATORS: amount window message
294  setCaption(_("Select amount of items to trade."));
295  break;
297  // TRANSLATORS: amount window message
298  setCaption(_("Select amount of items to drop."));
299  break;
301  // TRANSLATORS: amount window message
302  setCaption(_("Select amount of items to store."));
303  break;
305  // TRANSLATORS: amount window message
306  setCaption(_("Select amount of items to send."));
307  break;
309  // TRANSLATORS: amount window message
310  setCaption(_("Select amount of items to craft."));
311  break;
313  // TRANSLATORS: amount window message
314  setCaption(_("Select amount of items to store to cart."));
315  break;
317  // TRANSLATORS: amount window message
318  setCaption(_("Select amount of items to retrieve."));
319  break;
321  // TRANSLATORS: amount window message
322  setCaption(_("Select amount of items to retrieve from cart."));
323  break;
325  // TRANSLATORS: amount window message
326  setCaption(_("Select amount of items to split."));
327  break;
329  // TRANSLATORS: amount window message
330  setCaption(_("Add to buy shop."));
331  break;
333  // TRANSLATORS: amount window message
334  setCaption(_("Add to sell shop."));
335  break;
336  default:
337  // TRANSLATORS: amount window message
338  setCaption(_("Unknown."));
339  break;
340  }
341 
343 
345 }
346 
348 {
351 }
352 
354 {
356 }
357 
358 // Show ItemTooltip
360 {
361  Window::mouseMoved(event);
362 
363  if ((viewport == nullptr) || (itemPopup == nullptr))
364  return;
365 
366  if (event.getSource() == mItemIcon)
367  {
368  itemPopup->setItem(mItem, false);
370  }
371 }
372 
373 // Hide ItemTooltip
375 {
376  if (itemPopup != nullptr)
378 }
379 
381 {
383 }
384 
386 {
387  const std::string &eventId = event.getId();
388  if (eventId == "cancel")
389  {
390  close();
391  return;
392  }
393  else if (eventId == "ok")
394  {
395  if (mItemPriceTextField != nullptr)
396  {
397  finish(mItem,
400  mUsage);
401  }
402  else
403  {
405  {
406  finish(mItem,
408  mPrice,
409  mUsage);
410  }
411  else
412  {
413  finish(mItem,
415  0,
416  mUsage);
417  }
418  }
419  close();
420  return;
421  }
422  else if (eventId == "itemType")
423  {
424  if ((mItemDropDown == nullptr) || (mItemsModal == nullptr))
425  return;
426 
427  const int id = ItemDB::get(mItemsModal->getElementAt(
429 
430  mItem = new Item(id,
432  10000,
433  0,
436  Damaged_true,
438  Equipm_false,
440 
442  mMax = 10000;
443  else if (mMax == 0)
444  mMax = mItem->getQuantity();
445 
447  }
448 
449  int amount = mItemAmountTextField->getValue();
450 
451  if (eventId == "inc" && amount < mMax)
452  amount++;
453  else if (eventId == "dec" && amount > 1)
454  amount--;
455  else if (eventId == "all")
456  amount = mMax;
457  else if (eventId == "slide")
458  amount = CAST_S32(mItemAmountSlide->getValue());
460  mItemAmountSlide->setValue(amount);
461 
462  if ((mItemPriceTextField != nullptr) && (mItemPriceSlide != nullptr))
463  {
464  if (mPrice > 7)
465  mPrice = 7;
466  else if (mPrice < 0)
467  mPrice = 0;
468 
469  int price = 0;
470 
471  if (eventId == "incPrice")
472  {
473  mPrice++;
474  price = CAST_S32(pow(10.0, mPrice));
476  mItemPriceSlide->setValue(price);
477  }
478  else if (eventId == "decPrice")
479  {
480  mPrice--;
481  price = CAST_S32(pow(10.0, mPrice));
483  mItemPriceSlide->setValue(price);
484  }
485  else if (eventId == "slidePrice")
486  {
487  price = CAST_S32(mItemPriceSlide->getValue());
488  if (price != 0)
489  mPrice = CAST_S32(log(static_cast<float>(price)));
490  else
491  mPrice = 0;
493  mItemPriceSlide->setValue(price);
494  }
495  }
496 }
497 
499 {
501  scheduleDelete();
502 }
503 
505 {
507 }
508 
510  Window *const parent,
511  Item *const item,
512  int maxRange,
513  int tag)
514 {
515  if (item == nullptr)
516  return;
517 
518  if (maxRange == 0)
519  maxRange = item->getQuantity();
520 
521  if (usage != ItemAmountWindowUsage::ShopBuyAdd &&
523  maxRange <= 1)
524  {
525  if (usage == ItemAmountWindowUsage::CraftAdd)
526  finish(item, maxRange, tag, usage);
527  else
528  finish(item, maxRange, 0, usage);
529  }
530  else
531  {
533  usage, parent, item, maxRange);
534  if (usage == ItemAmountWindowUsage::CraftAdd)
535  window->mPrice = tag;
536  }
537 }
const bool AutoRelease_false
Definition: autorelease.h:30
#define fromBool(val, name)
Definition: booldefines.h:49
const std::string BUTTON_SKIN
Definition: button.h:89
#define CAST_S32
Definition: cast.h:30
Definition: button.h:102
void adjustSize()
Definition: button.cpp:799
int getSelected() const
Definition: dropdown.cpp:509
Widget * getSource() const
Definition: event.h:104
Definition: icon.h:41
void setImage(Image *const image)
Definition: icon.cpp:74
void setValue(const int value)
void setRange(const int minimum, const int maximum)
int getValue() const
void mouseExited(MouseEvent &event)
IntTextField * mItemPriceTextField
ItemsModal * mItemsModal
static void finish(Item *const item, const int amount, const int price, const ItemAmountWindowUsageT usage)
void keyReleased(KeyEvent &event)
ItemAmountWindow(const ItemAmountWindowUsageT usage, Window *const parent, Item *const item, const int maxRange)
DropDown * mItemDropDown
IntTextField * mItemAmountTextField
void action(const ActionEvent &event)
ItemAmountWindowUsageT mUsage
void mouseMoved(MouseEvent &event)
static void showWindow(const ItemAmountWindowUsageT usage, Window *const parent, Item *const item, int maxRange, const int tag)
int getId() const
Definition: iteminfo.h:68
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 getQuantity() const
Definition: item.h:105
int getInvIndex() const
Definition: item.h:165
Image * getImage() const
Definition: item.h:87
std::string getElementAt(int i)
Definition: itemsmodel.h:75
void setEnabled(const bool flag)
Definition: label.h:91
void addItem(const Item *const item, const int amount)
virtual void moveItem2(const InventoryTypeT source, const int slot, const int amount, const InventoryTypeT destination) const =0
virtual void splitItem(const Item *const item, const int amount) const =0
virtual void addItem(const Item *const item, const int amount) const =0
virtual NpcDialog * getCurrentNpcDialog() const =0
void addCraftItem(Item *const item, const int amount, const int slot)
Definition: npcdialog.cpp:1382
void position(const int x, const int y)
Definition: popup.cpp:235
bool enableNewMailSystem
Definition: settings.h:165
void addSellItem(const Item *const item, const int amount, const int price)
Definition: shopwindow.cpp:529
void addBuyItem(const Item *const item, const int amount, const int price)
Definition: shopwindow.cpp:512
Definition: slider.h:89
double getValue() const
Definition: slider.h:204
void setValue(const double value)
Definition: slider.cpp:474
void tradeItem(const Item *const item, const int quantity, const bool check) const
int mMouseX
Definition: viewport.h:154
int mMouseY
Definition: viewport.h:155
void setVisible(Visible visible)
Definition: widget.cpp:225
void setWidth(const int width)
Definition: widget.cpp:133
void addMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:292
void setHeight(const int height)
Definition: widget.cpp:140
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:596
void addKeyListener(KeyListener *const keyListener)
Definition: widget.cpp:272
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:252
int getWidth() const
Definition: widget.h:221
Definition: window.h:102
ContainerPlacer getPlacer(const int x, const int y)
Definition: window.cpp:1391
virtual void setVisible(Visible visible)
Definition: window.cpp:778
Window * getParentWindow() const
Definition: window.h:310
void reflowLayout(int w, int h)
Definition: window.cpp:1396
void setCaption(const std::string &caption)
Definition: window.h:531
void mouseMoved(MouseEvent &event)
Definition: window.cpp:967
void postInit()
Definition: window.cpp:249
virtual void scheduleDelete()
Definition: window.cpp:831
void setLocationRelativeTo(const Widget *const widget)
Definition: window.cpp:509
#define CREATEWIDGETR(type,...)
Definition: createwidget.h:36
const bool Damaged_true
Definition: damaged.h:30
#define new
Definition: debug_new.h:147
#define delete2(var)
Definition: delete2.h:25
Viewport * viewport
Definition: viewport.cpp:36
const bool Enable_true
Definition: enable.h:30
const bool Equipm_false
Definition: equipm.h:30
const bool Equipped_false
Definition: equipped.h:30
const bool Favorite_false
Definition: favorite.h:30
#define _(s)
Definition: gettext.h:35
const bool Identified_true
Definition: identified.h:30
Net::InventoryHandler * inventoryHandler
Definition: net.cpp:89
ItemAmountWindowUsage ::T ItemAmountWindowUsageT
const ItemColor ItemColor_one
Definition: itemcolor.h:30
ItemPopup * itemPopup
Definition: itempopup.cpp:64
KeyboardConfig keyboard
#define nullptr
Definition: localconsts.h:45
#define A_UNUSED
Definition: localconsts.h:160
Net::Mail2Handler * mail2Handler
Definition: net.cpp:113
MailEditWindow * mailEditWindow
const bool Modal_false
Definition: modal.h:30
const ItemInfo & get(const int id)
Definition: itemdb.cpp:792
@ Unknown
Definition: itemtype.h:30
Image * getImage(const std::string &idPath)
Definition: imageloader.cpp:86
void dropItem(const Item *const item, const int amount, const Sfx sfx)
Definition: playerinfo.cpp:354
Net::NpcHandler * npcHandler
Definition: net.cpp:93
Settings settings
Definition: settings.cpp:32
const bool Sfx_true
Definition: sfx.h:30
ShopWindow * shopWindow
Definition: shopwindow.cpp:101
TradeWindow * tradeWindow
Definition: tradewindow.cpp:65
bool Visible
Definition: visible.h:30
const bool Visible_false
Definition: visible.h:30