Mana
Loading...
Searching...
No Matches
itemamountwindow.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
23
24#include "inventory.h"
25#include "item.h"
26#include "keyboardconfig.h"
27
28#include "gui/tradewindow.h"
29#include "gui/itempopup.h"
30#include "gui/viewport.h"
31
32#include "gui/widgets/button.h"
34#include "gui/widgets/layout.h"
35#include "gui/widgets/slider.h"
36#include "gui/widgets/icon.h"
37
38#include "net/net.h"
39#include "net/tradehandler.h"
40
41#include "utils/gettext.h"
42
43void ItemAmountWindow::finish(Item *item, int amount, Usage usage)
44{
45 switch (usage)
46 {
47 case TradeAdd:
48 Net::getTradeHandler()->addItem(item, amount);
49 break;
50 case ItemDrop:
51 item->doEvent(Event::DoDrop, amount);
52 break;
53 case StoreAdd:
54 {
55 Event event(Event::DoMove);
56 event.setItem("item", item);
57 event.setInt("amount", amount);
58 event.setInt("source", Inventory::INVENTORY);
59 event.setInt("destination", Inventory::STORAGE);
60 event.trigger(Event::ItemChannel);
61 }
62 break;
63 case StoreRemove:
64 {
65 Event event(Event::DoMove);
66 event.setItem("item", item);
67 event.setInt("amount", amount);
68 event.setInt("source", Inventory::STORAGE);
69 event.setInt("destination", Inventory::INVENTORY);
70 event.trigger(Event::ItemChannel);
71 }
72 break;
73 default:
74 break;
75 }
76}
77
79 int maxRange):
80 Window(std::string(), true, parent),
81 mItem(item),
82 mMax(maxRange),
83 mUsage(usage)
84{
85 if (!mMax)
87
88 // Save keyboard state
90 keyboard.setEnabled(false);
91
92 // Integer field
95 mItemAmountTextField->setWidth(35);
96 mItemAmountTextField->addKeyListener(this);
97
98 // Slider
99 mItemAmountSlide = new Slider(1.0, mMax);
100 mItemAmountSlide->setHeight(10);
101 mItemAmountSlide->setActionEventId("slide");
102 mItemAmountSlide->addActionListener(this);
103
104 // Item icon
105 mItemIcon = new Icon(item->getImage());
106
107 // Buttons
108 auto *minusButton = new Button(_("-"), "dec", this);
109 auto *plusButton = new Button(_("+"), "inc", this);
110 auto *okButton = new Button(_("OK"), "ok", this);
111 auto *cancelButton = new Button(_("Cancel"), "cancel", this);
112 auto *addAllButton = new Button(_("All"), "all", this);
113
114 minusButton->adjustSize();
115 minusButton->setWidth(plusButton->getWidth());
116
117 // Set positions
119 place = getPlacer(0, 0);
120 place(1, 0, minusButton);
122 place(3, 0, plusButton);
123 place(4, 0, addAllButton);
124
125 place(0, 0, mItemIcon, 1, 3);
126 place(1, 1, mItemAmountSlide, 5);
127
128 place(4, 2, cancelButton);
129 place(5, 2, okButton);
130
131 reflowLayout();
132
133 resetAmount();
134
135 switch (usage)
136 {
137 case TradeAdd:
138 setCaption(_("Select amount of items to trade."));
139 break;
140 case ItemDrop:
141 setCaption(_("Select amount of items to drop."));
142 break;
143 case StoreAdd:
144 setCaption(_("Select amount of items to store."));
145 break;
146 case StoreRemove:
147 setCaption(_("Select amount of items to retrieve."));
148 break;
149 }
150
152 setVisible(true);
153
154 mItemPopup = new ItemPopup;
155 mItemIcon->addMouseListener(this);
156}
157
162
163// Show ItemTooltip
164void ItemAmountWindow::mouseMoved(gcn::MouseEvent &event)
165{
166 if (event.getSource() == mItemIcon)
167 {
170 }
171}
172
173// Hide ItemTooltip
174void ItemAmountWindow::mouseExited(gcn::MouseEvent &event)
175{
176 mItemPopup->setVisible(false);
177}
178
183
184void ItemAmountWindow::action(const gcn::ActionEvent &event)
185{
186 int amount = mItemAmountTextField->getValue();
187
188 if (event.getId() == "cancel")
189 {
190 close();
191 }
192 else if (event.getId() == "inc" && amount < mMax)
193 {
194 amount++;
195 }
196 else if (event.getId() == "dec" && amount > 1)
197 {
198 amount--;
199 }
200 else if (event.getId() == "all")
201 {
202 amount = mMax;
203 }
204 else if (event.getId() == "slide")
205 {
206 amount = static_cast<int>(mItemAmountSlide->getValue());
207 }
208 else if (event.getId() == "ok")
209 {
210 finish(mItem, amount, mUsage);
211 close();
212 return;
213 }
215 mItemAmountSlide->setValue(amount);
216}
217
223
224void ItemAmountWindow::keyReleased(gcn::KeyEvent &keyEvent)
225{
227}
228
230 int maxRange)
231{
232 if (!maxRange)
233 maxRange = item->getQuantity();
234
235 if (maxRange <= 1)
236 {
237 finish(item, maxRange, usage);
238 }
239 else
240 {
241 new ItemAmountWindow(usage, parent, item, maxRange);
242 }
243}
Button widget.
Definition button.h:38
This class is a helper for adding widgets to nested tables in a window.
Definition layout.h:34
Definition event.h:42
@ DoDrop
Definition event.h:75
@ DoMove
Definition event.h:77
@ ItemChannel
Definition event.h:53
An icon.
Definition icon.h:36
TextBox which only accepts numbers as input.
void setRange(int minimum, int maximum)
Sets the minimum and maximum values of the text box.
int getValue()
Returns the value in the text box.
void setValue(int value)
Set the value of the text box to the specified value.
Window used for selecting the amount of items to drop, trade or store.
gcn::Slider * mItemAmountSlide
Item Amount buttons.
void close() override
Schedules the Item Amount window for deletion.
void keyReleased(gcn::KeyEvent &keyEvent) override
ItemPopup * mItemPopup
void mouseExited(gcn::MouseEvent &event) override
ItemAmountWindow(Usage usage, Window *parent, Item *item, int maxRange=0)
static void finish(Item *item, int amount, Usage usage)
void mouseMoved(gcn::MouseEvent &event) override
IntTextField * mItemAmountTextField
Item amount caption.
void resetAmount()
Sets default amount value.
void action(const gcn::ActionEvent &event) override
Called when receiving actions from widget.
static void showWindow(Usage usage, Window *parent, Item *item, int maxRange=0)
Creates the dialog, or bypass it if there aren't enough items.
A popup that displays information about an item.
Definition itempopup.h:39
void setItem(const ItemInfo &item, bool showImage=false)
Sets the info to be displayed given a particular item.
Represents one or more instances of a certain item type.
Definition item.h:35
void doEvent(Event::Type eventType)
Definition item.cpp:54
int getQuantity() const
Returns the number of items.
Definition item.h:69
Image * getImage() const
Returns the item image.
Definition item.h:54
const ItemInfo & getInfo() const
Returns information about this item type.
Definition item.h:103
bool isEnabled() const
Get the enable flag, which will stop the user from doing actions.
void setEnabled(bool flag)
Set the enable flag, which will stop the user from doing actions.
virtual void addItem(Item *item, int amount)
void position(int x, int y)
Sets the location to display the popup.
Definition popup.cpp:183
Slider widget.
Definition slider.h:32
int getMouseY() const
Returns mouse y in pixels.
Definition viewport.h:136
int getMouseX() const
Returns mouse x in pixels.
Definition viewport.h:131
A window.
Definition window.h:59
virtual void setVisible(bool visible)
Overloads window setVisible by Guichan to allow sticky window handling.
Definition window.cpp:282
Window * getParentWindow() const
Returns the parent window.
Definition window.h:232
ContainerPlacer getPlacer(int x, int y)
Returns a proxy for adding widgets in an inner table of the layout.
Definition window.cpp:743
void reflowLayout(int w=0, int h=0)
Computes the position of the widgets according to the current layout.
Definition window.cpp:748
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 setLocationRelativeTo(gcn::Widget *widget)
Sets the location relative to the given widget.
Definition window.cpp:178
KeyboardConfig keyboard
Definition client.cpp:101
Viewport * viewport
Viewport on the map.
Definition game.cpp:115
#define _(s)
Definition gettext.h:38
TradeHandler * getTradeHandler()
Definition net.cpp:120