Mana
Loading...
Searching...
No Matches
selldialog.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "gui/selldialog.h"
23
24#include "client.h"
25#include "playerinfo.h"
26#include "shopitem.h"
27#include "units.h"
28
29#include "gui/setup.h"
30
31#include "gui/widgets/button.h"
32#include "gui/widgets/label.h"
33#include "gui/widgets/layout.h"
37#include "gui/widgets/slider.h"
38
39#include "net/net.h"
40#include "net/npchandler.h"
41
42#include "resources/iteminfo.h"
43
44#include "utils/gettext.h"
45#include "utils/stringutils.h"
46
48
50 Window(_("Sell")),
51 mNpcId(npcId)
52{
53 setWindowName("Sell");
54 //setupWindow->registerWindowForReset(this);
55 setResizable(true);
56 setCloseButton(true);
57 setMinWidth(260);
58 setMinHeight(230);
60
61 // Create a ShopItems instance, that is aware of duplicate entries.
62 mShopItems = new ShopItems(true);
63
66 mScrollArea->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER);
67
68 mSlider = new Slider(1.0);
69
71 mQuantityLabel->setAlignment(gcn::Graphics::CENTER);
72 mMoneyLabel = new Label(strprintf(_("Price: %s / Total: %s"),
73 "", ""));
74
75 mIncreaseButton = new Button(_("+"), "inc", this);
76 mDecreaseButton = new Button(_("-"), "dec", this);
77 mSellButton = new Button(_("Sell"), "sell", this);
78 mQuitButton = new Button(_("Quit"), "quit", this);
79 mAddMaxButton = new Button(_("Max"), "max", this);
80
81 mDecreaseButton->adjustSize();
82 mDecreaseButton->setWidth(mIncreaseButton->getWidth());
83
84 mIncreaseButton->setEnabled(false);
85 mDecreaseButton->setEnabled(false);
86 mSellButton->setEnabled(false);
87 mSlider->setEnabled(false);
88
90 mShopItemList->addSelectionListener(this);
91 mSlider->setActionEventId("slider");
92 mSlider->addActionListener(this);
93
95 place = getPlacer(0, 0);
96
97 place(0, 0, mScrollArea, 8, 5).setPadding(3);
99 place(1, 5, mSlider, 3);
100 place(4, 5, mIncreaseButton);
101 place(5, 5, mQuantityLabel, 2);
102 place(7, 5, mAddMaxButton);
103 place(0, 6, mMoneyLabel, 8);
104 place(6, 7, mSellButton);
105 place(7, 7, mQuitButton);
106
107 Layout &layout = getLayout();
109
110 center();
112
113 instances.push_back(this);
114 setVisible(true);
115
117}
118
128
130{
131 mShopItems->clear();
132 mSlider->setValue(0);
133
134 // Reset previous selected item to prevent failing asserts
135 mShopItemList->setSelected(-1);
136
138}
139
140void SellDialog::addItem(const Item *item, int price)
141{
142 if (!item)
143 return;
144
145 mShopItems->addItem(item->getInvIndex(), item->getId(),
146 item->getQuantity(), price);
147
149}
150
151void SellDialog::action(const gcn::ActionEvent &event)
152{
153 if (event.getId() == "quit")
154 {
155 close();
156 return;
157 }
158
159 int selectedItem = mShopItemList->getSelected();
160
161 // The following actions require a valid item selection
162 if (selectedItem == -1 ||
163 selectedItem >= (int) mShopItems->getNumberOfElements())
164 {
165 return;
166 }
167
168 if (event.getId() == "slider")
169 {
170 mAmountItems = (int) mSlider->getValue();
172 }
173 else if (event.getId() == "inc" && mAmountItems < mMaxItems)
174 {
175 mAmountItems++;
176 mSlider->setValue(mAmountItems);
178 }
179 else if (event.getId() == "dec" && mAmountItems > 1)
180 {
181 mAmountItems--;
182 mSlider->setValue(mAmountItems);
184 }
185 else if (event.getId() == "max")
186 {
188 mSlider->setValue(mAmountItems);
190 }
191 else if (event.getId() == "sell" && mAmountItems > 0
193 {
194 // Attempt sell
195 ShopItem *item = mShopItems->at(selectedItem);
196 int sellCount, itemIndex;
197 mPlayerMoney +=
198 mAmountItems * mShopItems->at(selectedItem)->getPrice();
200 while (mAmountItems > 0)
201 {
202 // This order is important, item->getCurrentInvIndex() would return
203 // the inventory index of the next Duplicate otherwise.
204 itemIndex = item->getCurrentInvIndex();
205 sellCount = item->sellCurrentDuplicate(mAmountItems);
206
207 // For Manaserv, the Item id is to be given as index.
209 itemIndex = item->getId();
210
211 Net::getNpcHandler()->sellItem(mNpcId, itemIndex, sellCount);
212 mAmountItems -= sellCount;
213 }
214
215 mAmountItems = 1;
216 mSlider->setValue(0);
217
218 if (!mMaxItems)
219 {
220 // All were sold
221 mShopItemList->setSelected(-1);
222 delete mShopItems->at(selectedItem);
223 mShopItems->erase(selectedItem);
224
225 gcn::Rectangle scroll;
226 scroll.y = mShopItemList->getRowHeight() * (selectedItem + 1);
227 scroll.height = mShopItemList->getRowHeight();
228 mShopItemList->showPart(scroll);
229 }
230 else
231 {
232 mSlider->gcn::Slider::setScale(1, mMaxItems);
233 // Update only when there are items left, the entry doesn't exist
234 // otherwise and can't be updated
236 }
237 }
238}
239
240void SellDialog::valueChanged(const gcn::SelectionEvent &event)
241{
242 // Reset amount of items and update labels
243 mAmountItems = 1;
244 mSlider->setValue(0);
245
247 mSlider->gcn::Slider::setScale(1, mMaxItems);
248}
249
250void SellDialog::mouseClicked(gcn::MouseEvent &mouseEvent)
251{
252 if (mouseEvent.getSource() == mShopItemList &&
253 isDoubleClick(mShopItemList->getSelected()))
254 {
255 action(gcn::ActionEvent(mSellButton, mSellButton->getActionEventId()));
256 }
257}
258
259void SellDialog::setMoney(int amount)
260{
261 mPlayerMoney = amount;
263
265}
266
268{
269 int selectedItem = mShopItemList->getSelected();
270 int income = 0;
271
272 if (selectedItem > -1)
273 {
274 mMaxItems = mShopItems->at(selectedItem)->getQuantity();
276 {
278 }
279
280 income = mAmountItems * mShopItems->at(selectedItem)->getPrice();
281 }
282 else
283 {
284 mMaxItems = 0;
285 mAmountItems = 0;
286 }
287
288 // Update Buttons and slider
289 mSellButton->setEnabled(mAmountItems > 0);
290 mDecreaseButton->setEnabled(mAmountItems > 1);
292 mSlider->setEnabled(mMaxItems > 1);
293
294 // Update the quantity and money labels
295 mQuantityLabel->setCaption(strprintf("%d / %d", mAmountItems, mMaxItems));
296 mMoneyLabel->setCaption(strprintf(_("Price: %s / Total: %s"),
297 Units::formatCurrency(income).c_str(),
298 Units::formatCurrency(mPlayerMoney + income).c_str()));
299}
300
301void SellDialog::setVisible(bool visible)
302{
303 Window::setVisible(visible);
304
305 if (visible)
306 {
307 mShopItemList->requestFocus();
308 }
309 else
310 {
312 }
313}
314
316{
317 for (auto &instance : instances)
318 instance->close();
319}
Button widget.
Definition button.h:38
This class is a helper for adding widgets to nested tables in a window.
Definition layout.h:34
Represents one or more instances of a certain item type.
Definition item.h:35
int getQuantity() const
Returns the number of items.
Definition item.h:69
int getInvIndex() const
Returns the inventory index of this item.
Definition item.h:94
int getId() const
Returns the item id.
Definition item.h:49
Label widget.
Definition label.h:34
void setRowHeight(int n, int h)
Definition layout.h:221
LayoutCell & setPadding(int p)
Sets the padding around the cell content.
Definition layout.h:179
This class is an helper for setting the position of widgets.
Definition layout.h:281
@ AUTO_SET
Uses the share as the new size.
Definition layout.h:304
virtual void sellItem(int beingId, int itemId, int amount)=0
A scroll area.
Definition scrollarea.h:38
void action(const gcn::ActionEvent &event) override
Called when receiving actions from the widgets.
gcn::Slider * mSlider
Definition selldialog.h:107
void mouseClicked(gcn::MouseEvent &mouseEvent) override
Allows for quick-selling by extending double-click events.
std::list< SellDialog * > DialogList
Definition selldialog.h:88
void updateButtonsAndLabels()
Updates the state of buttons and labels.
void addItem(const Item *item, int price)
Adds an item to the inventory.
void reset()
Resets the dialog, clearing inventory.
ShopItems * mShopItems
Definition selldialog.h:109
gcn::Button * mIncreaseButton
Definition selldialog.h:101
gcn::Button * mSellButton
Definition selldialog.h:98
gcn::Label * mQuantityLabel
Definition selldialog.h:106
void setVisible(bool visible) override
Sets the visibility of this window.
gcn::ScrollArea * mScrollArea
Definition selldialog.h:104
void valueChanged(const gcn::SelectionEvent &event) override
Updates labels according to selected item.
static void closeAll()
Closes all instances.
int mAmountItems
Definition selldialog.h:113
SellDialog(int npcId)
static DialogList instances
Definition selldialog.h:89
~SellDialog() override
gcn::Button * mDecreaseButton
Definition selldialog.h:102
ShopListBox * mShopItemList
Definition selldialog.h:103
int mPlayerMoney
Definition selldialog.h:110
gcn::Button * mAddMaxButton
Definition selldialog.h:100
void setMoney(int amount)
Gives Player's Money amount.
gcn::Button * mQuitButton
Definition selldialog.h:99
gcn::Label * mMoneyLabel
Definition selldialog.h:105
Represents an item in a shop inventory.
Definition shopitem.h:33
int getCurrentInvIndex() const
Gets the inventory index of the currently topmost duplicate.
Definition shopitem.h:70
int sellCurrentDuplicate(int quantity)
Reduces the quantity of the topmost duplicate by the specified amount.
Definition shopitem.cpp:49
int getPrice() const
Gets the price of the item.
Definition shopitem.h:94
This class handles the list of items available in a shop.
Definition shopitems.h:41
void addItem(int id, int amount, int price)
Adds an item to the list.
Definition shopitems.cpp:48
ShopItem * at(int i) const
Returns the item number i in the shop.
Definition shopitems.cpp:72
int getNumberOfElements() override
Returns the number of items in the shop.
Definition shopitems.cpp:38
void erase(int i)
Removes an element from the shop.
Definition shopitems.cpp:77
void clear()
Clears the list of items in the shop.
Definition shopitems.cpp:82
A list box, meant to be used inside a scroll area.
Definition shoplistbox.h:37
void setPriceCheck(bool check)
Set on/off the disabling of too expensive items.
unsigned int getRowHeight() const override
Returns the height of a row.
Definition shoplistbox.h:56
void setPlayersMoney(int money)
gives information about the current player's money
void adjustSize()
Adjust List draw size.
Slider widget.
Definition slider.h:32
static std::string formatCurrency(int value)
Formats the given number in the correct currency format.
Definition units.cpp:216
A window.
Definition window.h:59
void center()
Positions the window in the center of it's parent.
Definition window.cpp:768
virtual void setVisible(bool visible)
Overloads window setVisible by Guichan to allow sticky window handling.
Definition window.cpp:282
void setMinHeight(int height)
Sets the minimum height of the window.
Definition window.cpp:197
virtual void close()
Overrideable functionality for when the window is to close.
Definition window.cpp:327
Layout & getLayout()
Gets the layout handler for this window.
Definition window.cpp:714
void setWindowName(const std::string &name)
Sets the name of the window.
Definition window.h:272
ContainerPlacer getPlacer(int x, int y)
Returns a proxy for adding widgets in an inner table of the layout.
Definition window.cpp:743
void setResizable(bool resize)
Sets whether or not the window can be resized.
Definition window.cpp:212
LayoutCell & place(int x, int y, gcn::Widget *, int w=1, int h=1)
Adds a widget to the window and sets it at given cell.
Definition window.cpp:737
void scheduleDelete()
Schedule this window for deletion.
Definition window.cpp:299
void setCloseButton(bool flag)
Sets whether or not the window has a close button.
Definition window.cpp:262
void setDefaultSize()
Set the default win pos and size to the current ones.
Definition window.cpp:545
void loadWindowState()
Reads the position (and the size for resizable windows) in the configuration based on the given strin...
Definition window.cpp:467
void setMinWidth(int width)
Sets the minimum width of the window.
Definition window.cpp:192
bool isDoubleClick(int selected)
Returns whether this call and the last call were done for the same selected index and within a short ...
Definition client.cpp:126
#define _(s)
Definition gettext.h:38
ServerType getNetworkType()
Definition net.cpp:200
NpcHandler * getNpcHandler()
Definition net.cpp:100
void setBuySellState(BuySellState buySellState)
Sets which buy, sell, or related interaction the player is currently involved in.
BuySellState getBuySellState()
Returns the current buy, sell, or related interaction the player is involved in.
@ BUYSELL_NONE
Definition playerinfo.h:66
@ BUYSELL_SELLING
Definition playerinfo.h:69
std::string strprintf(char const *format,...)
A safe version of sprintf that returns a std::string of the result.