diff options
Diffstat (limited to 'src/gui/trade.cpp')
-rw-r--r-- | src/gui/trade.cpp | 234 |
1 files changed, 190 insertions, 44 deletions
diff --git a/src/gui/trade.cpp b/src/gui/trade.cpp index 0592c485..98214a79 100644 --- a/src/gui/trade.cpp +++ b/src/gui/trade.cpp @@ -1,21 +1,21 @@ /* * The Mana World - * Copyright 2004 The Mana World Development Team + * Copyright (C) 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 + * This program 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, + * This program 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 + * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ @@ -24,8 +24,6 @@ #include <guichan/font.hpp> #include <guichan/widgets/label.hpp> -#include "trade.h" - #include "button.h" #include "chat.h" #include "inventorywindow.h" @@ -33,42 +31,68 @@ #include "itemcontainer.h" #include "scrollarea.h" #include "textfield.h" +#include "trade.h" #include "widgets/layout.h" #include "../inventory.h" #include "../item.h" #include "../localplayer.h" +#include "../units.h" +#ifdef TMWSERV_SUPPORT #include "../net/gameserver/player.h" - -#include "../resources/iteminfo.h" +#else +#include "../net/messageout.h" +#include "../net/ea/protocol.h" +#endif #include "../utils/gettext.h" +#include "../utils/stringutils.h" #include "../utils/strprintf.h" +#ifdef TMWSERV_SUPPORT TradeWindow::TradeWindow(): - Window("Trade: You"), - mMyInventory(new Inventory), - mPartnerInventory(new Inventory), - mStatus(PREPARING) +#else +TradeWindow::TradeWindow(Network *network): +#endif + Window(_("Trade: You")), +#ifdef EATHENA_SUPPORT + mNetwork(network), +#endif + mMyInventory(new Inventory(INVENTORY_SIZE)), + mPartnerInventory(new Inventory(INVENTORY_SIZE)) +#ifdef TMWSERV_SUPPORT + , mStatus(PREPARING) +#endif { setWindowName("Trade"); setResizable(true); setDefaultSize(115, 197, 332, 209); Button *mAddButton = new Button(_("Add"), "add", this); +#ifdef EATHENA_SUPPORT + mOkButton = new Button(_("Ok"), "ok", this); +#endif Button *mCancelButton = new Button(_("Cancel"), "cancel", this); mTradeButton = new Button(_("Propose trade"), "trade", this); mTradeButton->setWidth(8 + std::max( mTradeButton->getFont()->getWidth(_("Propose trade")), mTradeButton->getFont()->getWidth(_("Confirm trade")))); - mMyItemContainer = new ItemContainer(mMyInventory, 4, 3); +#ifdef TMWSERV_SUPPORT + mMyItemContainer = new ItemContainer(mMyInventory.get(), 4, 3, 0); +#else + mMyItemContainer = new ItemContainer(mMyInventory.get(), 4, 3, 2); +#endif mMyItemContainer->addSelectionListener(this); ScrollArea *mMyScroll = new ScrollArea(mMyItemContainer); - mPartnerItemContainer = new ItemContainer(mPartnerInventory, 4, 3); +#ifdef TMWSERV_SUPPORT + mPartnerItemContainer = new ItemContainer(mPartnerInventory.get(), 4, 3, 0); +#else + mPartnerItemContainer = new ItemContainer(mPartnerInventory.get(), 4, 3, 2); +#endif mPartnerItemContainer->addSelectionListener(this); ScrollArea *mPartnerScroll = new ScrollArea(mPartnerItemContainer); @@ -78,10 +102,6 @@ TradeWindow::TradeWindow(): mMoneyField->setWidth(40); Button *mMoneyChange = new Button(_("Change"), "money", this); - mItemNameLabel = new gcn::Label(strprintf(_("Name: %s"), "")); - mItemDescriptionLabel = new gcn::Label( - strprintf(_("Description: %s"), "")); - place(1, 0, mMoneyLabel); place(0, 1, mMyScroll).setPadding(3); place(1, 1, mPartnerScroll).setPadding(3); @@ -91,11 +111,12 @@ TradeWindow::TradeWindow(): place(1, 0, mMoneyField); place(2, 0, mMoneyChange).setHAlign(LayoutCell::LEFT); place = getPlacer(0, 2); - place(0, 0, mItemNameLabel, 4); - place(0, 1, mItemDescriptionLabel, 4); - place(0, 2, mAddButton); - place(2, 2, mTradeButton); - place(3, 2, mCancelButton); + place(0, 0, mAddButton); +#ifdef EATHENA_SUPPORT + place(1, 0, mOkButton); +#endif + place(2, 0, mTradeButton); + place(3, 0, mCancelButton); Layout &layout = getLayout(); layout.extend(0, 2, 2, 1); layout.setRowHeight(1, Layout::AUTO_SET); @@ -108,69 +129,151 @@ TradeWindow::TradeWindow(): TradeWindow::~TradeWindow() { - delete mMyInventory; - delete mPartnerInventory; } void TradeWindow::setMoney(int amount) { - mMoneyLabel->setCaption(strprintf(_("You get %d GP."), amount)); + mMoneyLabel->setCaption(strprintf(_("You get %s."), + Units::formatCurrency(amount).c_str())); + mMoneyLabel->adjustSize(); +#ifdef TMWSERV_SUPPORT setStatus(PREPARING); +#endif } +#ifdef TMWSERV_SUPPORT void TradeWindow::addItem(int id, bool own, int quantity) { (own ? mMyInventory : mPartnerInventory)->addItem(id, quantity); setStatus(PREPARING); } +#endif + +#ifdef EATHENA_SUPPORT +void TradeWindow::addItem(int id, bool own, int quantity, bool equipment) +{ + if (own) + { + mMyInventory->addItem(id, quantity, equipment); + } + else + { + mPartnerInventory->addItem(id, quantity, equipment); + } +} + +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); +} +#endif void TradeWindow::reset() { mMyInventory->clear(); mPartnerInventory->clear(); - mMoneyLabel->setCaption(strprintf(_("You get %d GP."), 0)); +#ifdef EATHENA_SUPPORT + mTradeButton->setEnabled(false); + mOkButton->setEnabled(true); + mOkOther = false; + mOkMe = false; +#endif + mMoneyLabel->setCaption(strprintf(_("You get %s."), "")); mMoneyField->setEnabled(true); mMoneyField->setText(""); +#ifdef TMWSERV_SUPPORT setStatus(PREPARING); +#endif } +#ifdef TMWSERV_SUPPORT + void TradeWindow::receivedOk() { setStatus(ACCEPTING); } +#else + +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); + } + } +} + +#endif + void TradeWindow::tradeItem(Item *item, int quantity) { +#ifdef TMWSERV_SUPPORT Net::GameServer::Player::tradeItem(item->getInvIndex(), quantity); addItem(item->getId(), true, quantity); item->increaseQuantity(-quantity); +#else + MessageOut outMsg(mNetwork); + outMsg.writeInt16(CMSG_TRADE_ITEM_ADD_REQUEST); + outMsg.writeInt16(item->getInvIndex()); + outMsg.writeInt32(quantity); +#endif } void TradeWindow::valueChanged(const gcn::SelectionEvent &event) { - Item *item; + const 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())) - { + (item = mMyItemContainer->getSelectedItem())) mPartnerItemContainer->selectNone(); - } - else if ((item = mPartnerItemContainer->getItem())) - { + else if ((item = mPartnerItemContainer->getSelectedItem())) mMyItemContainer->selectNone(); - } - - // Update name and description - ItemInfo const *info = item ? &item->getInfo() : NULL; - mItemNameLabel->setCaption(strprintf(_("Name: %s"), - info ? info->getName().c_str() : "")); - mItemDescriptionLabel->setCaption(strprintf(_("Description: %s"), - info ? info->getDescription().c_str() : "")); } +#ifdef TMWSERV_SUPPORT void TradeWindow::setStatus(Status s) { if (s == mStatus) return; @@ -180,44 +283,86 @@ void TradeWindow::setStatus(Status s) (s == PREPARING ? _("Propose trade") : _("Confirm trade")); mTradeButton->setEnabled(s != PROPOSING); } +#endif void TradeWindow::action(const gcn::ActionEvent &event) { - Item *item = inventoryWindow->getItem(); + Item *item = inventoryWindow->getSelectedItem(); if (event.getId() == "add") { if (!item) return; + if (mMyInventory->getFreeSlot() < 1) + 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) { + if (item->getQuantity() == 1) + { tradeItem(item, 1); } - else { + else + { // Choose amount of items to trade new ItemAmountWindow(AMOUNT_TRADE_ADD, this, item); } +#ifdef TMWSERV_SUPPORT setStatus(PREPARING); +#endif } else if (event.getId() == "cancel") { setVisible(false); reset(); player_node->setTrading(false); +#ifdef TMWSERV_SUPPORT Net::GameServer::Player::acceptTrade(false); +#else + MessageOut outMsg(mNetwork); + outMsg.writeInt16(CMSG_TRADE_CANCEL_REQUEST); +#endif + } +#ifdef EATHENA_SUPPORT + else if (event.getId() == "ok") + { + std::stringstream tempMoney(mMoneyField->getText()); + int tempInt; + if (tempMoney >> tempInt) + { + mMoneyField->setText(toString(tempInt)); + + MessageOut outMsg(mNetwork); + outMsg.writeInt16(CMSG_TRADE_ITEM_ADD_REQUEST); + outMsg.writeInt16(0); + outMsg.writeInt32(tempInt); + } + else + { + mMoneyField->setText(""); + } + mMoneyField->setEnabled(false); + MessageOut outMsg(mNetwork); + outMsg.writeInt16(CMSG_TRADE_ADD_COMPLETE); } +#endif else if (event.getId() == "trade") { +#ifdef TMWSERV_SUPPORT Net::GameServer::Player::acceptTrade(true); setStatus(PROPOSING); +#else + MessageOut outMsg(mNetwork); + outMsg.writeInt16(CMSG_TRADE_OK); +#endif } +#ifdef TMWSERV_SUPPORT else if (event.getId() == "money") { int v = atoi(mMoneyField->getText().c_str()); @@ -225,4 +370,5 @@ void TradeWindow::action(const gcn::ActionEvent &event) mMoneyField->setText(strprintf("%d", v)); setStatus(PREPARING); } +#endif } |