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