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