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  // TRANSLATORS: item amount window button
202  Button *const minusAmountButton = new Button(this, _("-"), "dec", this);
203  // TRANSLATORS: item amount window button
204  Button *const plusAmountButton = new Button(this, _("+"), "inc", this);
205  // TRANSLATORS: item amount window button
206  Button *const okButton = new Button(this, _("OK"), "ok", this);
207  // TRANSLATORS: item amount window button
208  Button *const cancelButton = new Button(this, _("Cancel"), "cancel", this);
209  // TRANSLATORS: item amount window button
210  Button *const addAllButton = new Button(this, _("All"), "all", this);
211 
212  minusAmountButton->adjustSize();
213  minusAmountButton->setWidth(plusAmountButton->getWidth());
214 
215  // Set positions
216  ContainerPlacer placer(nullptr, nullptr);
217  placer = getPlacer(0, 0);
218  int n = 0;
220  {
221  placer(0, n, mItemDropDown, 8, 1);
222  n++;
223  }
224  placer(1, n, minusAmountButton, 1, 1);
225  placer(2, n, mItemAmountTextField, 3, 1);
226  placer(5, n, plusAmountButton, 1, 1);
227  placer(6, n, addAllButton, 1, 1);
228 
229  placer(0, n, mItemIcon, 1, 3);
230  placer(1, n + 1, mItemAmountSlide, 7, 1);
231 
234  {
235  Button *const minusPriceButton = new Button(
236  // TRANSLATORS: item amount window button
237  this, _("-"), "decPrice", this);
238  Button *const plusPriceButton = new Button(
239  // TRANSLATORS: item amount window button
240  this, _("+"), "incPrice", this);
241  minusPriceButton->adjustSize();
242  minusPriceButton->setWidth(plusPriceButton->getWidth());
243 
244  placer(1, n + 2, minusPriceButton, 1, 1);
245  placer(2, n + 2, mItemPriceTextField, 3, 1);
246  placer(5, n + 2, plusPriceButton, 1, 1);
247  placer(6, n + 2, mGPLabel, 1, 1);
248 
249  placer(1, n + 3, mItemPriceSlide, 7, 1);
250  placer(4, n + 5, cancelButton, 1, 1);
251  placer(5, n + 5, okButton, 1, 1);
252  }
253  else
254  {
255  placer(4, n + 2, cancelButton, 1, 1);
256  placer(5, n + 2, okButton, 1, 1);
257  }
258 
259  reflowLayout(225, 0);
260 
261  resetAmount();
262 
263  switch (usage)
264  {
266  // TRANSLATORS: amount window message
267  setCaption(_("Select amount of items to trade."));
268  break;
270  // TRANSLATORS: amount window message
271  setCaption(_("Select amount of items to drop."));
272  break;
274  // TRANSLATORS: amount window message
275  setCaption(_("Select amount of items to store."));
276  break;
278  // TRANSLATORS: amount window message
279  setCaption(_("Select amount of items to send."));
280  break;
282  // TRANSLATORS: amount window message
283  setCaption(_("Select amount of items to craft."));
284  break;
286  // TRANSLATORS: amount window message
287  setCaption(_("Select amount of items to store to cart."));
288  break;
290  // TRANSLATORS: amount window message
291  setCaption(_("Select amount of items to retrieve."));
292  break;
294  // TRANSLATORS: amount window message
295  setCaption(_("Select amount of items to retrieve from cart."));
296  break;
298  // TRANSLATORS: amount window message
299  setCaption(_("Select amount of items to split."));
300  break;
302  // TRANSLATORS: amount window message
303  setCaption(_("Add to buy shop."));
304  break;
306  // TRANSLATORS: amount window message
307  setCaption(_("Add to sell shop."));
308  break;
309  default:
310  // TRANSLATORS: amount window message
311  setCaption(_("Unknown."));
312  break;
313  }
314 
316 
318 }
319 
321 {
324 }
325 
327 {
329 }
330 
331 // Show ItemTooltip
333 {
334  Window::mouseMoved(event);
335 
336  if ((viewport == nullptr) || (itemPopup == nullptr))
337  return;
338 
339  if (event.getSource() == mItemIcon)
340  {
341  itemPopup->setItem(mItem, false);
343  }
344 }
345 
346 // Hide ItemTooltip
348 {
349  if (itemPopup != nullptr)
351 }
352 
354 {
356 }
357 
359 {
360  const std::string &eventId = event.getId();
361  if (eventId == "cancel")
362  {
363  close();
364  return;
365  }
366  else if (eventId == "ok")
367  {
368  if (mItemPriceTextField != nullptr)
369  {
370  finish(mItem,
373  mUsage);
374  }
375  else
376  {
378  {
379  finish(mItem,
381  mPrice,
382  mUsage);
383  }
384  else
385  {
386  finish(mItem,
388  0,
389  mUsage);
390  }
391  }
392  close();
393  return;
394  }
395  else if (eventId == "itemType")
396  {
397  if ((mItemDropDown == nullptr) || (mItemsModal == nullptr))
398  return;
399 
400  const int id = ItemDB::get(mItemsModal->getElementAt(
402 
403  mItem = new Item(id,
405  10000,
406  0,
409  Damaged_true,
411  Equipm_false,
413 
415  mMax = 10000;
416  else if (mMax == 0)
417  mMax = mItem->getQuantity();
418 
420  }
421 
422  int amount = mItemAmountTextField->getValue();
423 
424  if (eventId == "inc" && amount < mMax)
425  amount++;
426  else if (eventId == "dec" && amount > 1)
427  amount--;
428  else if (eventId == "all")
429  amount = mMax;
430  else if (eventId == "slide")
431  amount = CAST_S32(mItemAmountSlide->getValue());
433  mItemAmountSlide->setValue(amount);
434 
435  if ((mItemPriceTextField != nullptr) && (mItemPriceSlide != nullptr))
436  {
437  if (mPrice > 7)
438  mPrice = 7;
439  else if (mPrice < 0)
440  mPrice = 0;
441 
442  int price = 0;
443 
444  if (eventId == "incPrice")
445  {
446  mPrice++;
447  price = CAST_S32(pow(10.0, mPrice));
449  mItemPriceSlide->setValue(price);
450  }
451  else if (eventId == "decPrice")
452  {
453  mPrice--;
454  price = CAST_S32(pow(10.0, mPrice));
456  mItemPriceSlide->setValue(price);
457  }
458  else if (eventId == "slidePrice")
459  {
460  price = CAST_S32(mItemPriceSlide->getValue());
461  if (price != 0)
462  mPrice = CAST_S32(log(static_cast<float>(price)));
463  else
464  mPrice = 0;
466  mItemPriceSlide->setValue(price);
467  }
468  }
469 }
470 
472 {
474  scheduleDelete();
475 }
476 
478 {
480 }
481 
483  Window *const parent,
484  Item *const item,
485  int maxRange,
486  int tag)
487 {
488  if (item == nullptr)
489  return;
490 
491  if (maxRange == 0)
492  maxRange = item->getQuantity();
493 
494  if (usage != ItemAmountWindowUsage::ShopBuyAdd &&
496  maxRange <= 1)
497  {
498  if (usage == ItemAmountWindowUsage::CraftAdd)
499  finish(item, maxRange, tag, usage);
500  else
501  finish(item, maxRange, 0, usage);
502  }
503  else
504  {
506  usage, parent, item, maxRange);
507  if (usage == ItemAmountWindowUsage::CraftAdd)
508  window->mPrice = tag;
509  }
510 }
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:814
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:94
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:104
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:829
#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 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:498
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:82
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:99
#define A_UNUSED
Definition: localconsts.h:171
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:86
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:1380
void setCaption(const std::string &caption)
Definition: window.h:530
bool enableNewMailSystem
Definition: settings.h:163
void addSellItem(const Item *const item, const int amount, const int price)
Definition: shopwindow.cpp:515
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)