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 #ifndef GUI_WINDOWS_TRADEWINDOW_H
25 #define GUI_WINDOWS_TRADEWINDOW_H
26 
27 #include "gui/widgets/window.h"
28 
29 #include "enums/resources/item/itemtype.h"
30 
31 #include "enums/simpletypes/damaged.h"
32 #include "enums/simpletypes/equipm.h"
33 #include "enums/simpletypes/favorite.h"
34 #include "enums/simpletypes/identified.h"
35 #include "enums/simpletypes/itemcolor.h"
36 
37 #include "listeners/actionlistener.h"
38 #include "listeners/selectionlistener.h"
39 
40 class Button;
41 class Inventory;
42 class Item;
43 class ItemContainer;
44 class Label;
45 class TextField;
46 
47 struct ItemOptionsList;
48 
49 /**
50  * Trade dialog.
51  *
52  * \ingroup Interface
53  */
54 class TradeWindow final : public Window,
55                           private ActionListener,
56                           private SelectionListener
57 {
58     public:
59         /**
60          * Constructor.
61          */
62         TradeWindow();
63 
64         A_DELETE_COPY(TradeWindow)
65 
66         /**
67          * Destructor.
68          */
69         ~TradeWindow() override final;
70 
71         /**
72          * Displays expected money in the trade window.
73          */
74         void setMoney(const int quantity);
75 
76         /**
77          * Add an item to the trade window.
78          */
79         void addItem(const int id,
80                      const ItemTypeT type,
81                      const bool own,
82                      const int quantity,
83                      const uint8_t refine,
84                      const ItemColor color,
85                      const Identified identified,
86                      const Damaged damaged,
87                      const Favorite favorite) const;
88 
89         /**
90          * Reset both item containers
91          */
92         void reset();
93 
94         /**
95          * Add an item to the trade window.
96          */
97         void addItem2(const int id,
98                       const ItemTypeT type,
99                       const int *const cards,
100                       const ItemOptionsList *const options,
101                       const int sz,
102                       const bool own,
103                       const int quantity,
104                       const uint8_t refine,
105                       const ItemColor color,
106                       const Identified identified,
107                       const Damaged damaged,
108                       const Favorite favorite,
109                       const Equipm equipment) const;
110 
111         /**
112          * Change quantity of an item.
113          */
114         void changeQuantity(const int index, const bool own,
115                             const int quantity) const;
116 
117         /**
118          * Increase quantity of an item.
119          */
120         void increaseQuantity(const int index, const bool own,
121                               const int quantity) const;
122 
123         /**
124          * Player received ok message from server
125          */
126         void receivedOk(const bool own);
127 
128         /**
129          * Send trade packet.
130          */
131         void tradeItem(const Item *const item,
132                        const int quantity,
133                        const bool check) const;
134 
135         /**
136          * Updates the labels and makes sure only one item is selected in
137          * either my inventory or partner inventory.
138          */
139         void valueChanged(const SelectionEvent &event) override final;
140 
141         /**
142          * Called when receiving actions from the widgets.
143          */
144         void action(const ActionEvent &event) override final;
145 
146         /**
147          * Closes the Trade Window, as well as telling the server that the
148          * window has been closed.
149          */
150         void close() override final;
151 
152         /**
153          * Clear auto trade items.
154          */
155         void clear() override final;
156 
157         /**
158          * Add item what will be added to trade.
159          */
160         void addAutoItem(const std::string &nick, Item *const item,
161                          const int amount);
162 
163         void addAutoMoney(const std::string &nick, const int money);
164 
165         void initTrade(const std::string &nick);
166 
getAutoTradeNick()167         std::string getAutoTradeNick() const noexcept2 A_WARN_UNUSED
168         { return mAutoAddToNick; }
169 
170         bool checkItem(const Item *const item) const A_WARN_UNUSED;
171 
172         bool isInpupFocused() const A_WARN_UNUSED;
173 
174         void completeTrade();
175 
176     private:
177         enum Status
178         {
179             PREPARING = 0, /**< Players are adding items. (1) */
180             PROPOSING, /**< Local player has confirmed the trade. (1) */
181             ACCEPTING, /**< Accepting the trade. (2) */
182             ACCEPTED  /**< Local player has accepted the trade. */
183         };
184 
185         /**
186          * Sets the current status of the trade.
187          */
188         void setStatus(const Status s);
189 
190         Inventory *mMyInventory A_NONNULLPOINTER;
191         Inventory *mPartnerInventory A_NONNULLPOINTER;
192 
193         ItemContainer *mMyItemContainer A_NONNULLPOINTER;
194         ItemContainer *mPartnerItemContainer A_NONNULLPOINTER;
195 
196         Label *mMoneyLabel A_NONNULLPOINTER;
197         Button *mAddButton A_NONNULLPOINTER;
198         Button *mOkButton A_NONNULLPOINTER;
199         Button  *mMoneyChangeButton A_NONNULLPOINTER;
200         TextField *mMoneyField A_NONNULLPOINTER;
201 
202         Item* mAutoAddItem;
203         std::string mAutoAddToNick;
204         int mGotMoney;
205         int mGotMaxMoney;
206         int mAutoMoney;
207         int mAutoAddAmount;
208         Status mStatus;
209         bool mOkOther;
210         bool mOkMe;
211 };
212 
213 extern TradeWindow *tradeWindow;
214 
215 #endif  // GUI_WINDOWS_TRADEWINDOW_H
216