summaryrefslogtreecommitdiff
path: root/src/gui/trade.cpp
blob: cf3ad1a575d2eb35effb3509ef932304ea8d8d44 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 *  The Mana World
 *  Copyright 2004 The Mana World Development Team
 *
 *  This file is part of The Mana World.
 *
 *  The Mana World is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  The Mana World is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with The Mana World; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  $Id$
 */

#include "trade.h"

#include <sstream>

#include <guichan/widgets/label.hpp>

#include "button.h"
#include "chat.h"
#include "inventorywindow.h"
#include "item_amount.h"
#include "itemcontainer.h"
#include "scrollarea.h"
#include "textfield.h"

#include "../inventory.h"
#include "../item.h"
#include "../localplayer.h"

#include "../net/gameserver/player.h"

#include "../resources/iteminfo.h"

#include "../utils/tostring.h"

TradeWindow::TradeWindow():
    Window("Trade: You"),
    mMyInventory(new Inventory()),
    mPartnerInventory(new Inventory())
{
    setDefaultSize(115, 197, 332, 209);

    mAddButton = new Button("Add", "add", this);
    mOkButton = new Button("Ok", "ok", this);
    mCancelButton = new Button("Cancel", "cancel", this);
    mTradeButton = new Button("Trade", "trade", this);

    mMyItemContainer = new ItemContainer(mMyInventory.get(), 5, 3);
    mMyItemContainer->addSelectionListener(this);
    mMyItemContainer->setPosition(2, 2);

    mMyScroll = new ScrollArea(mMyItemContainer);
    mMyScroll->setPosition(8, 8);

    mPartnerItemContainer = new ItemContainer(
        mPartnerInventory.get(), 5, 3);
    mPartnerItemContainer->addSelectionListener(this);
    mPartnerItemContainer->setPosition(2, 58);

    mPartnerScroll = new ScrollArea(mPartnerItemContainer);
    mPartnerScroll->setPosition(8, 64);

    mMoneyLabel = new gcn::Label("You get: 0z");
    mMoneyLabel2 = new gcn::Label("You give:");
    mMoneyField = new TextField();

    mAddButton->adjustSize();
    mOkButton->adjustSize();
    mCancelButton->adjustSize();
    mTradeButton->adjustSize();

    mTradeButton->setEnabled(false);

    mItemNameLabel = new gcn::Label("Name:");
    mItemDescriptionLabel = new gcn::Label("Description:");

    add(mMyScroll);
    add(mPartnerScroll);
    add(mAddButton);
    add(mOkButton);
    add(mCancelButton);
    add(mTradeButton);
    add(mItemNameLabel);
    add(mItemDescriptionLabel);
    add(mMoneyLabel2);
    add(mMoneyField);
    add(mMoneyLabel);

    gcn::Rectangle area = getChildrenArea();
    int width = area.width;
    int height = area.height;

    mMoneyField->setPosition(8 + 60, height - 20);
    mMoneyField->setWidth(50);

    mMoneyLabel->setPosition(8 + 60 + 50 + 6, height - 20);
    mMoneyLabel2->setPosition(8, height - 20);

    mCancelButton->setPosition(width - 54, height - 49);
    mTradeButton->setPosition(mCancelButton->getX() - 41, height - 49);
    mOkButton->setPosition(mTradeButton->getX() - 24, height - 49);
    mAddButton->setPosition(mOkButton->getX() - 31, height - 49);

    mMyItemContainer->setSize(width - 24 - 12 - 1,
        (INVENTORY_SIZE * 24) / (width / 24) - 1);
    mMyScroll->setSize(width - 16, (height - 76) / 2);

    mPartnerItemContainer->setSize(width - 24 - 12 - 1,
        (INVENTORY_SIZE * 24) / (width / 24) - 1);
    mPartnerScroll->setSize(width - 16, (height - 76) / 2);

    mItemNameLabel->setPosition(8,
        mPartnerScroll->getY() + mPartnerScroll->getHeight() + 4);
    mItemDescriptionLabel->setPosition(8,
        mItemNameLabel->getY() + mItemNameLabel->getHeight() + 4);
}

TradeWindow::~TradeWindow()
{
}

void TradeWindow::setMoney(int amount)
{
    mMoneyLabel->setCaption("You get: " + toString(amount) + "z");
    mMoneyLabel->adjustSize();
}

void TradeWindow::addItem(int id, bool own, int quantity)
{
    if (own) {
        mMyInventory->addItem(id, quantity);
    } else {
        mPartnerInventory->addItem(id, quantity);
    }
}

void TradeWindow::removeItem(int id, bool own)
{
    if (own) {
        mMyInventory->removeItem(id);
    } else {
        mPartnerInventory->removeItem(id);
    }
}

void TradeWindow::changeQuantity(int index, bool own, int quantity)
{
    if (own) {
        mMyInventory->getItem(index)->setQuantity(quantity);
    } else {
        mPartnerInventory->getItem(index)->setQuantity(quantity);
    }
}

void TradeWindow::increaseQuantity(int index, bool own, int quantity)
{
    if (own) {
        mMyInventory->getItem(index)->increaseQuantity(quantity);
    } else {
        mPartnerInventory->getItem(index)->increaseQuantity(quantity);
    }
}

void TradeWindow::reset()
{
    mMyInventory->clear();
    mPartnerInventory->clear();
    mTradeButton->setEnabled(false);
    mOkButton->setEnabled(true);
    mOkOther = false;
    mOkMe = false;
    mMoneyLabel->setCaption("You get: 0z");
    mMoneyField->setEnabled(true);
    mMoneyField->setText("");
}

void TradeWindow::setTradeButton(bool enabled)
{
    mTradeButton->setEnabled(enabled);
}

void TradeWindow::receivedOk(bool own)
{
    if (own) {
        mOkMe = true;
        if (mOkOther) {
            mTradeButton->setEnabled(true);
            mOkButton->setEnabled(false);
        } else {
            mTradeButton->setEnabled(false);
            mOkButton->setEnabled(false);
        }
    } else {
        mOkOther = true;
        if (mOkMe) {
            mTradeButton->setEnabled(true);
            mOkButton->setEnabled(false);
        } else {
            mTradeButton->setEnabled(false);
            mOkButton->setEnabled(true);
        }
    }
}

void TradeWindow::tradeItem(Item *item, int quantity)
{
    Net::GameServer::Player::tradeItem(item->getInvIndex(), quantity);
    addItem(item->getId(), true, quantity);
    item->increaseQuantity(-quantity);
}

void TradeWindow::selectionChanged(const SelectionEvent &event)
{
    Item *item;

    /* If an item is selected in one container, make sure no item is selected
     * in the other container.
     */
    if (event.getSource() == mMyItemContainer &&
            (item = mMyItemContainer->getItem()))
    {
        mPartnerItemContainer->selectNone();
    }
    else if ((item = mPartnerItemContainer->getItem()))
    {
        mMyItemContainer->selectNone();
    }

    // Update name and description
    if (!item)
    {
        mItemNameLabel->setCaption("Name:");
        mItemDescriptionLabel->setCaption("Description:");
    }
    else
    {
        std::string SomeText;
        SomeText = "Name: " + item->getInfo().getName();
        mItemNameLabel->setCaption(SomeText);
        mItemNameLabel->adjustSize();
        SomeText = "Description: " + item->getInfo().getDescription();
        mItemDescriptionLabel->setCaption(SomeText);
        mItemDescriptionLabel->adjustSize();
    }
}

void TradeWindow::action(const gcn::ActionEvent &event)
{
    Item *item = inventoryWindow->getItem();

    if (event.getId() == "add")
    {
        if (!item)
        {
            return;
        }

        if (mMyInventory->contains(item)) {
            chatWindow->chatLog("Failed adding item. You can not "
                    "overlap one kind of item on the window.", BY_SERVER);
            return;
        }

        if (item->getQuantity() == 1) {
            tradeItem(item, 1);
        }
        else {
            // Choose amount of items to trade
            new ItemAmountWindow(AMOUNT_TRADE_ADD, this, item);
        }
    }
    else if (event.getId() == "cancel")
    {
        setVisible(false);
        reset();
        player_node->setTrading(false);
        Net::GameServer::Player::acceptTrade(false);
    }
    else if (event.getId() == "ok")
    {
        Net::GameServer::Player::acceptTrade(true);
    }
    else if (event.getId() == "trade")
    {
        // XXX Convert for new server
        /*
        MessageOut outMsg(CMSG_TRADE_OK);
        */
    }
}