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  this);
118  // TRANSLATORS: sell dialog button
119  mQuitButton = new Button(this, _("Quit"), "quit", this);
120 
121  initButtons();
122 
123  mSellButton->setEnabled(false);
124 
130 
131  ContainerPlacer placer(nullptr, nullptr);
132  placer = getPlacer(0, 0);
133 
134  if (mIsSell == IsSell_true)
135  {
136  // TRANSLATORS: sell dialog button
137  mIncreaseButton = new Button(this, _("+"), "inc", this);
138  // TRANSLATORS: sell dialog button
139  mDecreaseButton = new Button(this, _("-"), "dec", this);
140  // TRANSLATORS: sell dialog button
141  mAddMaxButton = new Button(this, _("Max"), "max", this);
142  mSlider = new Slider(this, 1.0, 1.0);
143 
144  mQuantityLabel = new Label(this, strprintf(
145  "%d / %d", mAmountItems, mMaxItems));
147  // TRANSLATORS: sell dialog label
148  mMoneyLabel = new Label(this, strprintf(_("Price: %s / Total: %s"),
149  "", ""));
150  if (mAdvanced == Advanced_true)
151  {
152  // TRANSLATORS: sell dialog button
153  mConfirmButton = new Button(this, _("Sell"), "confirm", this);
154  mConfirmButton->setEnabled(false);
155  }
156 
159  mIncreaseButton->setEnabled(false);
160  mDecreaseButton->setEnabled(false);
161  mSlider->setEnabled(false);
162  mSlider->setActionEventId("slider");
163  mSlider->addActionListener(this);
164 
165  placer(0, 0, mScrollArea, 8, 5).setPadding(3);
166  placer(0, 5, mDecreaseButton, 1, 1);
167  placer(1, 5, mSlider, 3, 1);
168  placer(4, 5, mIncreaseButton, 1, 1);
169  placer(5, 5, mQuantityLabel, 2, 1);
170  placer(7, 5, mAddMaxButton, 1, 1);
171  placer(0, 6, mMoneyLabel, 8, 1);
172  if (mAdvanced == Advanced_true)
173  {
174  placer(5, 7, mSellButton, 1, 1);
175  placer(6, 7, mConfirmButton, 1, 1);
176  }
177  else
178  {
179  placer(6, 7, mSellButton, 1, 1);
180  }
181  placer(7, 7, mQuitButton, 1, 1);
182  }
183  else
184  {
185  placer(0, 0, mScrollArea, 8, 5).setPadding(3);
186  placer(6, 5, mSellButton, 1, 1);
187  placer(7, 5, mQuitButton, 1, 1);
188  }
189 
190  Layout &layout = getLayout();
191  layout.setRowHeight(0, LayoutType::SET);
192 
193  center();
194  loadWindowState();
195 
196  instances.push_back(this);
198  enableVisibleSound(true);
199 }
200 
202 {
204  instances.remove(this);
205 }
206 
208 {
209  mShopItems->clear();
210  if (mSlider != nullptr)
211  mSlider->setValue(0);
214 }
215 
216 void SellDialog::addItem(const Item *const item, const int price)
217 {
218  if (item == nullptr)
219  return;
220 
222  item->getId(),
223  item->getType(),
224  item->getColor(),
225  item->getQuantity(),
226  price);
227 
229 }
230 
232  const ItemTypeT type,
233  const ItemColor color,
234  const int amount,
235  const int price)
236 {
237  ShopItem *const item = mShopItems->addItem(id,
238  type,
239  color,
240  amount,
241  price);
243  return item;
244 }
245 
246 
247 void SellDialog::action(const ActionEvent &event)
248 {
249  const std::string &eventId = event.getId();
250 
251  if (eventId == "quit")
252  {
253  close();
254  return;
255  }
256 
257  const int selectedItem = mShopItemList->getSelected();
258 
259  // The following actions require a valid item selection
260  if (selectedItem == -1
261  || selectedItem >= mShopItems->getNumberOfElements())
262  {
263  return;
264  }
265 
266  if (eventId == "slider")
267  {
268  if (mSlider != nullptr)
269  {
272  }
273  }
274  else if (eventId == "inc" &&
275  mSlider != nullptr &&
277  {
278  mAmountItems++;
281  }
282  else if (eventId == "dec" &&
283  mSlider != nullptr &&
284  mAmountItems > 1)
285  {
286  mAmountItems--;
289  }
290  else if (eventId == "max" &&
291  mSlider != nullptr)
292  {
296  }
297  else if (eventId == "presell" ||
298  eventId == "sell" ||
299  eventId == "yes" ||
300  eventId == "confirm")
301  {
302  sellAction(event);
303  }
304 }
305 
307 {
308  // Reset amount of items and update labels
309  mAmountItems = 1;
310  if (mSlider != nullptr)
311  {
312  mSlider->setValue(0);
314  }
316 }
317 
318 void SellDialog::setMoney(const int amount)
319 {
320  mPlayerMoney = amount;
322 }
323 
325 {
326  const int selectedItem = mShopItemList->getSelected();
327  int income = 0;
328  ShopItem *item = nullptr;
329 
330  if (selectedItem > -1 && (mShopItems->at(selectedItem) != nullptr))
331  {
332  item = mShopItems->at(selectedItem);
333  if (item != nullptr)
334  {
335  mMaxItems = item->getQuantity();
336  if (mAmountItems > mMaxItems)
338  income = mAmountItems * mShopItems->at(selectedItem)->getPrice();
339  }
340  else
341  {
342  mMaxItems = 0;
343  mAmountItems = 0;
344  }
345  }
346  else
347  {
348  mMaxItems = 0;
349  mAmountItems = 0;
350  }
351 
352  // Update Buttons and slider
354  if (mDecreaseButton != nullptr)
356  if (mIncreaseButton != nullptr)
358  if (mSlider != nullptr)
360 
361  if (mQuantityLabel != nullptr)
362  {
363  // Update the quantity and money labels
366  }
367  if (mMoneyLabel != nullptr)
368  {
369  // TRANSLATORS: sell dialog label
370  mMoneyLabel->setCaption(strprintf(_("Price: %s / Total: %s"),
371  UnitsDb::formatCurrency(income).c_str(),
372  UnitsDb::formatCurrency(mPlayerMoney + income).c_str()));
373  }
374  if (item != nullptr)
375  item->update();
376 }
377 
379 {
380  Window::setVisible(visible);
381 
382  if (visible == Visible_true)
383  {
384  if (mShopItemList != nullptr)
386  }
387  else
388  {
389  scheduleDelete();
390  }
391 }
392 
394 {
395  FOR_EACH (DialogList::const_iterator, it, instances)
396  (*it)->close();
397 }
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:306
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:247
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:94
std::string formatCurrency(const int value)
Definition: unitsdb.cpp:323
void updateButtonsAndLabels()
Definition: selldialog.cpp:324
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:378
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:829
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
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:318
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:216
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:171
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:393
void setRowHeight(const int n, const int h)
Definition: layoutcell.cpp:127
void reset()
Definition: selldialog.cpp:207
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)