ManaPlus
selldialog.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 
24 #include "gui/widgets/selldialog.h"
25 
27 
28 #include "enums/gui/layouttype.h"
29 
31 
32 #include "gui/models/shopitems.h"
33 
34 #include "gui/widgets/button.h"
37 #include "gui/widgets/label.h"
38 #include "gui/widgets/layout.h"
39 #include "gui/widgets/scrollarea.h"
41 #include "gui/widgets/slider.h"
42 
43 #include "utils/delete2.h"
44 #include "utils/foreach.h"
45 #include "utils/gettext.h"
46 #include "utils/stringutils.h"
47 
48 #include "resources/db/unitsdb.h"
49 
51 
52 #include "debug.h"
53 
55 
57  const Advanced advanced) :
58  // TRANSLATORS: sell dialog name
59  Window(_("Sell"), Modal_false, nullptr, "sell.xml"),
62  mSellButton(nullptr),
63  mQuitButton(nullptr),
64  mConfirmButton(nullptr),
65  mAddMaxButton(nullptr),
66  mIncreaseButton(nullptr),
67  mDecreaseButton(nullptr),
68  mShopItemList(nullptr),
69  mScrollArea(nullptr),
70  mMoneyLabel(nullptr),
71  mQuantityLabel(nullptr),
72  mSlider(nullptr),
73  mShopItems(nullptr),
74  mPlayerMoney(0),
75  mMaxItems(0),
76  mAmountItems(0),
77  mIsSell(isSell),
78  mAdvanced(advanced)
79 {
80 }
81 
83 {
85  setWindowName("Sell");
86  setResizable(true);
87  setCloseButton(true);
88  setStickyButtonLock(true);
89  setMinWidth(260);
90  setMinHeight(220);
91  setDefaultSize(260, 230, ImagePosition::CENTER, 0, 0);
92 
93  if (setupWindow != nullptr)
95 
96  // Create a ShopItems instance, that is aware of duplicate entries.
97  mShopItems = new ShopItems(true,
99 
100  if (mAdvanced == Advanced_true)
102 
104  this,
105  mShopItems,
106  mShopItems,
110  fromBool(getOptionBool("showbackground", false), Opaque),
111  "sell_background.xml");
113 
114  mSellButton = new Button(this,
115  // TRANSLATORS: sell dialog button
116  mAdvanced == Advanced_true ? _("Add") : _("Sell"),
117  "presell",
118  BUTTON_SKIN,
119  this);
120  mQuitButton = new Button(this,
121  // TRANSLATORS: sell dialog button
122  _("Quit"),
123  "quit",
124  BUTTON_SKIN,
125  this);
126 
127  initButtons();
128 
129  mSellButton->setEnabled(false);
130 
136 
137  ContainerPlacer placer(nullptr, nullptr);
138  placer = getPlacer(0, 0);
139 
140  if (mIsSell == IsSell_true)
141  {
142  mIncreaseButton = new Button(this,
143  // TRANSLATORS: sell dialog button
144  _("+"),
145  "inc",
146  BUTTON_SKIN,
147  this);
148  mDecreaseButton = new Button(this,
149  // TRANSLATORS: sell dialog button
150  _("-"),
151  "dec",
152  BUTTON_SKIN,
153  this);
154  mAddMaxButton = new Button(this,
155  // TRANSLATORS: sell dialog button
156  _("Max"),
157  "max",
158  BUTTON_SKIN,
159  this);
160  mSlider = new Slider(this, 1.0, 1.0);
161 
162  mQuantityLabel = new Label(this, strprintf(
163  "%d / %d", mAmountItems, mMaxItems));
165  // TRANSLATORS: sell dialog label
166  mMoneyLabel = new Label(this, strprintf(_("Price: %s / Total: %s"),
167  "", ""));
168  if (mAdvanced == Advanced_true)
169  {
170  mConfirmButton = new Button(this,
171  // TRANSLATORS: sell dialog button
172  _("Sell"),
173  "confirm",
174  BUTTON_SKIN,
175  this);
176  mConfirmButton->setEnabled(false);
177  }
178 
181  mIncreaseButton->setEnabled(false);
182  mDecreaseButton->setEnabled(false);
183  mSlider->setEnabled(false);
184  mSlider->setActionEventId("slider");
185  mSlider->addActionListener(this);
186 
187  placer(0, 0, mScrollArea, 8, 5).setPadding(3);
188  placer(0, 5, mDecreaseButton, 1, 1);
189  placer(1, 5, mSlider, 3, 1);
190  placer(4, 5, mIncreaseButton, 1, 1);
191  placer(5, 5, mQuantityLabel, 2, 1);
192  placer(7, 5, mAddMaxButton, 1, 1);
193  placer(0, 6, mMoneyLabel, 8, 1);
194  if (mAdvanced == Advanced_true)
195  {
196  placer(5, 7, mSellButton, 1, 1);
197  placer(6, 7, mConfirmButton, 1, 1);
198  }
199  else
200  {
201  placer(6, 7, mSellButton, 1, 1);
202  }
203  placer(7, 7, mQuitButton, 1, 1);
204  }
205  else
206  {
207  placer(0, 0, mScrollArea, 8, 5).setPadding(3);
208  placer(6, 5, mSellButton, 1, 1);
209  placer(7, 5, mQuitButton, 1, 1);
210  }
211 
212  Layout &layout = getLayout();
213  layout.setRowHeight(0, LayoutType::SET);
214 
215  center();
216  loadWindowState();
217 
218  instances.push_back(this);
220  enableVisibleSound(true);
221 }
222 
224 {
226  instances.remove(this);
227 }
228 
230 {
231  mShopItems->clear();
232  if (mSlider != nullptr)
233  mSlider->setValue(0);
236 }
237 
238 void SellDialog::addItem(const Item *const item, const int price)
239 {
240  if (item == nullptr)
241  return;
242 
244  item->getId(),
245  item->getType(),
246  item->getColor(),
247  item->getQuantity(),
248  price);
249 
251 }
252 
254  const ItemTypeT type,
255  const ItemColor color,
256  const int amount,
257  const int price)
258 {
259  ShopItem *const item = mShopItems->addItem(id,
260  type,
261  color,
262  amount,
263  price);
265  return item;
266 }
267 
268 
269 void SellDialog::action(const ActionEvent &event)
270 {
271  const std::string &eventId = event.getId();
272 
273  if (eventId == "quit")
274  {
275  close();
276  return;
277  }
278 
279  const int selectedItem = mShopItemList->getSelected();
280 
281  // The following actions require a valid item selection
282  if (selectedItem == -1
283  || selectedItem >= mShopItems->getNumberOfElements())
284  {
285  return;
286  }
287 
288  if (eventId == "slider")
289  {
290  if (mSlider != nullptr)
291  {
294  }
295  }
296  else if (eventId == "inc" &&
297  mSlider != nullptr &&
299  {
300  mAmountItems++;
303  }
304  else if (eventId == "dec" &&
305  mSlider != nullptr &&
306  mAmountItems > 1)
307  {
308  mAmountItems--;
311  }
312  else if (eventId == "max" &&
313  mSlider != nullptr)
314  {
318  }
319  else if (eventId == "presell" ||
320  eventId == "sell" ||
321  eventId == "yes" ||
322  eventId == "confirm")
323  {
324  sellAction(event);
325  }
326 }
327 
329 {
330  // Reset amount of items and update labels
331  mAmountItems = 1;
332  if (mSlider != nullptr)
333  {
334  mSlider->setValue(0);
336  }
338 }
339 
340 void SellDialog::setMoney(const int amount)
341 {
342  mPlayerMoney = amount;
344 }
345 
347 {
348  const int selectedItem = mShopItemList->getSelected();
349  int income = 0;
350  ShopItem *item = nullptr;
351 
352  if (selectedItem > -1 && (mShopItems->at(selectedItem) != nullptr))
353  {
354  item = mShopItems->at(selectedItem);
355  if (item != nullptr)
356  {
357  mMaxItems = item->getQuantity();
358  if (mAmountItems > mMaxItems)
360  income = mAmountItems * mShopItems->at(selectedItem)->getPrice();
361  }
362  else
363  {
364  mMaxItems = 0;
365  mAmountItems = 0;
366  }
367  }
368  else
369  {
370  mMaxItems = 0;
371  mAmountItems = 0;
372  }
373 
374  // Update Buttons and slider
376  if (mDecreaseButton != nullptr)
378  if (mIncreaseButton != nullptr)
380  if (mSlider != nullptr)
382 
383  if (mQuantityLabel != nullptr)
384  {
385  // Update the quantity and money labels
388  }
389  if (mMoneyLabel != nullptr)
390  {
391  // TRANSLATORS: sell dialog label
392  mMoneyLabel->setCaption(strprintf(_("Price: %s / Total: %s"),
393  UnitsDb::formatCurrency(income).c_str(),
394  UnitsDb::formatCurrency(mPlayerMoney + income).c_str()));
395  }
396  if (item != nullptr)
397  item->update();
398 }
399 
401 {
402  Window::setVisible(visible);
403 
404  if (visible == Visible_true)
405  {
406  if (mShopItemList != nullptr)
408  }
409  else
410  {
411  scheduleDelete();
412  }
413 }
414 
416 {
417  FOR_EACH (DialogList::const_iterator, it, instances)
418  (*it)->close();
419 }
const bool Advanced_true
Definition: advanced.h:30
bool Advanced
Definition: advanced.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
@ CENTER
Definition: graphics.h:132
Definition: item.h:50
int getQuantity() const
Definition: item.h:105
int getInvIndex() const
Definition: item.h:165
int getId() const
Definition: item.h:81
ItemColor getColor() const
Definition: item.h:181
ItemTypeT getType() const
Definition: item.h:225
Definition: label.h:91
void setAlignment(Graphics::Alignment alignment)
Definition: label.h:153
void setCaption(const std::string &caption)
Definition: label.cpp:264
void setRowHeight(const int n, const int h)
Definition: layoutcell.cpp:128
Definition: layout.h:45
void setDistributeMousePressed(const bool b)
Definition: listbox.h:141
int getSelected() const
Definition: listbox.h:168
void setSelected(const int selected)
Definition: listbox.cpp:399
void addSelectionListener(SelectionListener *const selectionListener)
Definition: listbox.cpp:435
void setHorizontalScrollPolicy(const ScrollPolicy hPolicy)
SellDialog(const IsSell isSell, const Advanced advanced)
Definition: selldialog.cpp:56
virtual void initButtons()
Definition: selldialog.h:133
ScrollArea * mScrollArea
Definition: selldialog.h:143
int mMaxItems
Definition: selldialog.h:150
virtual void sellAction(const ActionEvent &event)=0
Advanced mAdvanced
Definition: selldialog.h:154
void addItem(const Item *const item, const int price)
Definition: selldialog.cpp:238
void updateButtonsAndLabels()
Definition: selldialog.cpp:346
void reset()
Definition: selldialog.cpp:229
ShopItems * mShopItems
Definition: selldialog.h:147
Button * mQuitButton
Definition: selldialog.h:137
void setVisible(Visible visible)
Definition: selldialog.cpp:400
Slider * mSlider
Definition: selldialog.h:146
Label * mQuantityLabel
Definition: selldialog.h:145
void valueChanged(const SelectionEvent &event)
Definition: selldialog.cpp:328
Button * mDecreaseButton
Definition: selldialog.h:141
Button * mIncreaseButton
Definition: selldialog.h:140
void postInit()
Definition: selldialog.cpp:82
Button * mAddMaxButton
Definition: selldialog.h:139
static void closeAll()
Definition: selldialog.cpp:415
void setMoney(const int amount)
Definition: selldialog.cpp:340
void action(const ActionEvent &event)
Definition: selldialog.cpp:269
Button * mSellButton
Definition: selldialog.h:136
Button * mConfirmButton
Definition: selldialog.h:138
int mAmountItems
Definition: selldialog.h:151
IsSell mIsSell
Definition: selldialog.h:153
std::list< SellDialog * > DialogList
Definition: selldialog.h:123
static DialogList instances
Definition: selldialog.h:124
ShopListBox * mShopItemList
Definition: selldialog.h:142
int mPlayerMoney
Definition: selldialog.h:149
Label * mMoneyLabel
Definition: selldialog.h:144
void registerWindowForReset(Window *const window)
void update()
Definition: shopitem.cpp:119
int getPrice() const
Definition: shopitem.h:132
ShopItem * addItem2(const int inventoryIndex, const int id, const ItemTypeT type, const ItemColor color, const int amount, const int price)
Definition: shopitems.cpp:99
ShopItem * addItem(const int id, const ItemTypeT type, const ItemColor color, const int amount, const int price)
Definition: shopitems.cpp:59
int getNumberOfElements()
Definition: shopitems.h:99
void setMergeDuplicates(const bool b)
Definition: shopitems.h:142
void clear()
Definition: shopitems.cpp:174
ShopItem * at(const size_t i) const
Definition: shopitems.cpp:129
void setPlayersMoney(const int money)
Definition: shoplistbox.cpp:87
void setPriceCheck(const bool check)
void adjustSize()
void setProtectItems(bool p)
Definition: shoplistbox.h:89
Definition: slider.h:89
double getValue() const
Definition: slider.h:204
void setScale(const double scaleStart, const double scaleEnd)
Definition: slider.cpp:468
void setValue(const double value)
Definition: slider.cpp:474
void setWidth(const int width)
Definition: widget.cpp:133
void setEnabled(const bool enabled)
Definition: widget.h:352
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:596
virtual void requestFocus()
Definition: widget.cpp:204
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:252
int getWidth() const
Definition: widget.h:221
Definition: window.h:102
void center()
Definition: window.cpp:1417
void setResizable(const bool resize)
Definition: window.cpp:627
ContainerPlacer getPlacer(const int x, const int y)
Definition: window.cpp:1391
virtual void close()
Definition: window.cpp:902
virtual void setVisible(Visible visible)
Definition: window.cpp:778
bool getOptionBool(const std::string &name, const bool def) const
Definition: window.cpp:1466
Layout & getLayout()
Definition: window.cpp:1365
void setWindowName(const std::string &name)
Definition: window.h:355
void setMinHeight(const int height)
Definition: window.cpp:604
void postInit()
Definition: window.cpp:249
void setMinWidth(const int width)
Definition: window.cpp:591
void enableVisibleSound(bool b)
Definition: window.h:481
void setCloseButton(const bool flag)
Definition: window.cpp:749
virtual void scheduleDelete()
Definition: window.cpp:831
void setStickyButtonLock(const bool sticky)
Definition: window.cpp:772
void setDefaultSize()
Definition: window.cpp:1198
void loadWindowState()
Definition: window.cpp:1087
#define CREATEWIDGETR(type,...)
Definition: createwidget.h:36
const std::string DEFAULT_CURRENCY
Definition: currency.h:27
#define delete2(var)
Definition: delete2.h:25
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
#define _(s)
Definition: gettext.h:35
const bool IsSell_true
Definition: issell.h:30
bool IsSell
Definition: issell.h:30
uint16_t ItemColor
Definition: itemcolor.h:30
ItemType ::T ItemTypeT
Definition: itemtype.h:43
#define nullptr
Definition: localconsts.h:45
#define A_UNUSED
Definition: localconsts.h:160
const bool Modal_false
Definition: modal.h:30
std::string formatCurrency(const int value)
Definition: unitsdb.cpp:324
bool Opaque
Definition: opaque.h:30
SetupWindow * setupWindow
Definition: setupwindow.cpp:64
std::string strprintf(const char *const format,...)
bool Visible
Definition: visible.h:30
const bool Visible_true
Definition: visible.h:30