From ded755e810ef9ec94c65b0c4282d6419c84e21d0 Mon Sep 17 00:00:00 2001 From: Guillaume Melquiond Date: Sun, 28 Oct 2007 15:04:58 +0000 Subject: Fixed display of trade window. Completed trade protocol. --- ChangeLog | 4 + src/gui/trade.cpp | 245 +++++++++++++++--------------------------- src/gui/trade.h | 49 ++++----- src/net/gameserver/player.cpp | 7 ++ src/net/gameserver/player.h | 1 + src/net/tradehandler.cpp | 2 +- 6 files changed, 118 insertions(+), 190 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8cc10b0e..64854e8b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -4,6 +4,10 @@ auto-resizing into two categories: "set" and "add". * src/gui/sell.cpp, src/gui/inventorywindow.cpp, src/gui/buy.cpp: Removed useless code. Updated constant names. + * src/gui/trade.cpp, src/gui/trade.h: Converted to layout handler. + Fixed money handling. Fixed transaction handling. + * src/net/tradehandler.cpp, src/net/gameserver/player.h, + src/net/gameserver/player.cpp: Implemented missing trade messages. 2007-10-28 Eugenio Favalli diff --git a/src/gui/trade.cpp b/src/gui/trade.cpp index add7b76e..bb3d702f 100644 --- a/src/gui/trade.cpp +++ b/src/gui/trade.cpp @@ -21,12 +21,13 @@ * $Id$ */ -#include "trade.h" - #include +#include #include +#include "trade.h" + #include "button.h" #include "chat.h" #include "inventorywindow.h" @@ -35,6 +36,8 @@ #include "scrollarea.h" #include "textfield.h" +#include "widgets/layout.h" + #include "../inventory.h" #include "../item.h" #include "../localplayer.h" @@ -43,177 +46,98 @@ #include "../resources/iteminfo.h" -#include "../utils/tostring.h" +#include "../utils/gettext.h" +#include "../utils/strprintf.h" TradeWindow::TradeWindow(): Window("Trade: You"), - mMyInventory(new Inventory()), - mPartnerInventory(new Inventory()) + mMyInventory(new Inventory), + mPartnerInventory(new Inventory), + mStatus(PREPARING) { + setResizable(true); 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); + Button *mAddButton = new Button(_("Add"), "add", this); + 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.get(), 5, 3); + mMyItemContainer = new ItemContainer(mMyInventory, 4, 3); mMyItemContainer->addSelectionListener(this); - mMyItemContainer->setPosition(2, 2); - - mMyScroll = new ScrollArea(mMyItemContainer); - mMyScroll->setPosition(8, 8); + ScrollArea *mMyScroll = new ScrollArea(mMyItemContainer); - mPartnerItemContainer = new ItemContainer( - mPartnerInventory.get(), 5, 3); + mPartnerItemContainer = new ItemContainer(mPartnerInventory, 4, 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); + ScrollArea *mPartnerScroll = new ScrollArea(mPartnerItemContainer); + + mMoneyLabel = new gcn::Label(strprintf(_("You get %d GP."), 0)); + gcn::Label *mMoneyLabel2 = new gcn::Label(_("You give:")); + mMoneyField = new TextField; + 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); + ContainerPlacer place; + place = getPlacer(0, 0); + place(0, 0, mMoneyLabel2); + 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); + Layout &layout = getLayout(); + layout.extend(0, 2, 2, 1); + layout.setRowHeight(1, Layout::AUTO_SET); + layout.setRowHeight(2, 0); + layout.setColWidth(0, Layout::AUTO_SET); + layout.setColWidth(1, Layout::AUTO_SET); loadWindowState("Trade"); - - 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() { + delete mMyInventory; + delete mPartnerInventory; } void TradeWindow::setMoney(int amount) { - mMoneyLabel->setCaption("You get: " + toString(amount) + "z"); - mMoneyLabel->adjustSize(); + mMoneyLabel->setCaption(strprintf(_("You get %d GP."), amount)); + setStatus(PREPARING); } 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); - } + (own ? mMyInventory : mPartnerInventory)->addItem(id, quantity); + setStatus(PREPARING); } void TradeWindow::reset() { mMyInventory->clear(); mPartnerInventory->clear(); - mTradeButton->setEnabled(false); - mOkButton->setEnabled(true); - mOkOther = false; - mOkMe = false; - mMoneyLabel->setCaption("You get: 0z"); + mMoneyLabel->setCaption(strprintf(_("You get %d GP."), 0)); mMoneyField->setEnabled(true); mMoneyField->setText(""); + setStatus(PREPARING); } -void TradeWindow::setTradeButton(bool enabled) -{ - mTradeButton->setEnabled(enabled); -} - -void TradeWindow::receivedOk(bool own) +void TradeWindow::receivedOk() { - 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); - } - } + setStatus(ACCEPTING); } void TradeWindow::tradeItem(Item *item, int quantity) @@ -241,21 +165,21 @@ void TradeWindow::selectionChanged(const SelectionEvent &event) } // 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(); - } + 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() : "")); +} + +void TradeWindow::setStatus(Status s) +{ + if (s == mStatus) return; + mStatus = s; + + mTradeButton->setCaption + (s == PREPARING ? _("Propose trade") : _("Confirm trade")); + mTradeButton->setEnabled(s != PROPOSING); } void TradeWindow::action(const gcn::ActionEvent &event) @@ -282,6 +206,8 @@ void TradeWindow::action(const gcn::ActionEvent &event) // Choose amount of items to trade new ItemAmountWindow(AMOUNT_TRADE_ADD, this, item); } + + setStatus(PREPARING); } else if (event.getId() == "cancel") { @@ -290,15 +216,16 @@ void TradeWindow::action(const gcn::ActionEvent &event) player_node->setTrading(false); Net::GameServer::Player::acceptTrade(false); } - else if (event.getId() == "ok") + else if (event.getId() == "trade") { Net::GameServer::Player::acceptTrade(true); + setStatus(PROPOSING); } - else if (event.getId() == "trade") + else if (event.getId() == "money") { - // XXX Convert for new server - /* - MessageOut outMsg(CMSG_TRADE_OK); - */ + int v = atoi(mMoneyField->getText().c_str()); + Net::GameServer::Player::tradeMoney(v); + mMoneyField->setText(strprintf("%d", v)); + setStatus(PREPARING); } } diff --git a/src/gui/trade.h b/src/gui/trade.h index 478d2876..b5b0db6a 100644 --- a/src/gui/trade.h +++ b/src/gui/trade.h @@ -24,8 +24,6 @@ #ifndef _TMW_TRADE_H #define _TMW_TRADE_H -#include - #include #include "window.h" @@ -66,35 +64,15 @@ class TradeWindow : public Window, gcn::ActionListener, SelectionListener */ void addItem(int id, bool own, int quantity); - /** - * Remove a item from the trade window. - */ - void removeItem(int id, bool own); - /** * Reset both item containers */ void reset(); - /** - * Change quantity of an item. - */ - void changeQuantity(int index, bool own, int quantity); - - /** - * Increase quantity of an item. - */ - void increaseQuantity(int index, bool own, int quantity); - - /** - * Set trade Button disabled - */ - void setTradeButton(bool enabled); - /** * Player received ok message from server */ - void receivedOk(bool own); + void receivedOk(); /** * Send trade packet. @@ -114,9 +92,21 @@ class TradeWindow : public Window, gcn::ActionListener, SelectionListener void action(const gcn::ActionEvent &event); private: - typedef std::auto_ptr InventoryPtr; - InventoryPtr mMyInventory; - InventoryPtr mPartnerInventory; + + enum Status + { + PREPARING, /**< Players are adding items. */ + PROPOSING, /**< Local player is proposing a trade. */ + ACCEPTING, /**< Distant player is proposing a trade. */ + }; + + /** + * Sets the current status of the trade. + */ + void setStatus(Status); + + Inventory *mMyInventory; + Inventory *mPartnerInventory; ItemContainer *mMyItemContainer; ItemContainer *mPartnerItemContainer; @@ -124,11 +114,10 @@ class TradeWindow : public Window, gcn::ActionListener, SelectionListener gcn::Label *mItemNameLabel; gcn::Label *mItemDescriptionLabel; gcn::Label *mMoneyLabel; - gcn::Label *mMoneyLabel2; - gcn::Button *mAddButton, *mOkButton, *mCancelButton, *mTradeButton; - ScrollArea *mMyScroll, *mPartnerScroll; + gcn::Button *mTradeButton; gcn::TextField *mMoneyField; - bool mOkOther, mOkMe; + + Status mStatus; }; extern TradeWindow *tradeWindow; diff --git a/src/net/gameserver/player.cpp b/src/net/gameserver/player.cpp index feba5565..95f7dff9 100644 --- a/src/net/gameserver/player.cpp +++ b/src/net/gameserver/player.cpp @@ -140,6 +140,13 @@ void Net::GameServer::Player::tradeItem(int slot, int amount) Net::GameServer::connection->send(msg); } +void Net::GameServer::Player::tradeMoney(int amount) +{ + MessageOut msg(PGMSG_TRADE_SET_MONEY); + msg.writeLong(amount); + Net::GameServer::connection->send(msg); +} + void Net::GameServer::Player::tradeWithNPC(int item, int amount) { MessageOut msg(PGMSG_NPC_BUYSELL); diff --git a/src/net/gameserver/player.h b/src/net/gameserver/player.h index dcf3f267..b5f3e6d7 100644 --- a/src/net/gameserver/player.h +++ b/src/net/gameserver/player.h @@ -49,6 +49,7 @@ namespace Net void requestTrade(int id); void acceptTrade(bool accept); void tradeItem(int slot, int amount); + void tradeMoney(int amount); void tradeWithNPC(int item, int amount); } } diff --git a/src/net/tradehandler.cpp b/src/net/tradehandler.cpp index 52553e94..db07b786 100644 --- a/src/net/tradehandler.cpp +++ b/src/net/tradehandler.cpp @@ -108,7 +108,7 @@ void TradeHandler::handleMessage(MessageIn &msg) break; case GPMSG_TRADE_ACCEPT: - tradeWindow->receivedOk(false); + tradeWindow->receivedOk(); break; case GPMSG_TRADE_CANCEL: -- cgit v1.2.3-70-g09d2