From 8edbf5c3879850bb8546a1cb96ba713ef35c1d41 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Thu, 12 May 2011 01:09:56 +0300 Subject: Renaming login, sell, trade (cpp, h) files to correct names. --- src/gui/buydialog.cpp | 2 +- src/gui/charselectdialog.cpp | 2 +- src/gui/itemamountwindow.cpp | 2 +- src/gui/login.cpp | 235 ------------------------ src/gui/login.h | 93 ---------- src/gui/logindialog.cpp | 235 ++++++++++++++++++++++++ src/gui/logindialog.h | 93 ++++++++++ src/gui/popupmenu.cpp | 4 +- src/gui/register.cpp | 2 +- src/gui/sell.cpp | 351 ----------------------------------- src/gui/sell.h | 146 --------------- src/gui/selldialog.cpp | 351 +++++++++++++++++++++++++++++++++++ src/gui/selldialog.h | 146 +++++++++++++++ src/gui/serverdialog.cpp | 2 +- src/gui/shopwindow.cpp | 4 +- src/gui/trade.cpp | 422 ------------------------------------------- src/gui/trade.h | 172 ------------------ src/gui/tradewindow.cpp | 422 +++++++++++++++++++++++++++++++++++++++++++ src/gui/tradewindow.h | 172 ++++++++++++++++++ 19 files changed, 1428 insertions(+), 1428 deletions(-) delete mode 100644 src/gui/login.cpp delete mode 100644 src/gui/login.h create mode 100644 src/gui/logindialog.cpp create mode 100644 src/gui/logindialog.h delete mode 100644 src/gui/sell.cpp delete mode 100644 src/gui/sell.h create mode 100644 src/gui/selldialog.cpp create mode 100644 src/gui/selldialog.h delete mode 100644 src/gui/trade.cpp delete mode 100644 src/gui/trade.h create mode 100644 src/gui/tradewindow.cpp create mode 100644 src/gui/tradewindow.h (limited to 'src/gui') diff --git a/src/gui/buydialog.cpp b/src/gui/buydialog.cpp index fe504413c..00602e6f2 100644 --- a/src/gui/buydialog.cpp +++ b/src/gui/buydialog.cpp @@ -26,7 +26,7 @@ #include "units.h" #include "gui/setup.h" -#include "gui/trade.h" +#include "gui/tradewindow.h" #include "gui/widgets/button.h" #include "gui/widgets/label.h" diff --git a/src/gui/charselectdialog.cpp b/src/gui/charselectdialog.cpp index 0e12f024a..bf4225df9 100644 --- a/src/gui/charselectdialog.cpp +++ b/src/gui/charselectdialog.cpp @@ -32,7 +32,7 @@ #include "gui/changepassworddialog.h" #include "gui/charcreatedialog.h" #include "gui/confirmdialog.h" -#include "gui/login.h" +#include "gui/logindialog.h" #include "gui/okdialog.h" #include "gui/sdlinput.h" #include "gui/textdialog.h" diff --git a/src/gui/itemamountwindow.cpp b/src/gui/itemamountwindow.cpp index 751578d9b..5506f1690 100644 --- a/src/gui/itemamountwindow.cpp +++ b/src/gui/itemamountwindow.cpp @@ -25,7 +25,7 @@ #include "item.h" #include "keyboardconfig.h" -#include "gui/trade.h" +#include "gui/tradewindow.h" #include "net/inventoryhandler.h" #include "gui/itempopup.h" #include "net/net.h" diff --git a/src/gui/login.cpp b/src/gui/login.cpp deleted file mode 100644 index d910d37bc..000000000 --- a/src/gui/login.cpp +++ /dev/null @@ -1,235 +0,0 @@ -/* - * The ManaPlus Client - * Copyright (C) 2004-2009 The Mana World Development Team - * Copyright (C) 2009-2010 The Mana Developers - * Copyright (C) 2011 The ManaPlus Developers - * - * This file is part of The ManaPlus Client. - * - * 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. - * - * 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 this program. If not, see . - */ - -#include "gui/login.h" - -#include "client.h" -#include "configuration.h" - -#include "gui/okdialog.h" -#include "gui/sdlinput.h" - -#include "gui/widgets/button.h" -#include "gui/widgets/checkbox.h" -#include "gui/widgets/dropdown.h" -#include "gui/widgets/label.h" -#include "gui/widgets/layout.h" -#include "gui/widgets/passwordfield.h" -#include "gui/widgets/textfield.h" - -#include "net/logindata.h" -#include "net/loginhandler.h" -#include "net/net.h" - -#include "utils/gettext.h" -#include "utils/stringutils.h" - -static const int MAX_SERVER_LIST_SIZE = 15; -static const int LOGIN_DIALOG_WIDTH = 300; -static const int LOGIN_DIALOG_HEIGHT = 140; -static const int FIELD_WIDTH = LOGIN_DIALOG_WIDTH - 70; - -std::string LoginDialog::savedPassword = ""; -std::string LoginDialog::savedPasswordKey = ""; - - -const char *UPDATE_TYPE_TEXT[3] = -{ - N_("Normal"), - N_("Auto Close"), - N_("Skip"), -}; - -class UpdateTypeModel : public gcn::ListModel -{ -public: - virtual ~UpdateTypeModel() - { } - - virtual int getNumberOfElements() - { - return 3; - } - - virtual std::string getElementAt(int i) - { - if (i >= getNumberOfElements() || i < 0) - return _("???"); - - return gettext(UPDATE_TYPE_TEXT[i]); - } -}; - -LoginDialog::LoginDialog(LoginData *loginData, std::string serverName, - std::string *updateHost): - Window(_("Login")), - mLoginData(loginData), - mUpdateHost(updateHost) -{ - gcn::Label *serverLabel1 = new Label(_("Server:")); - gcn::Label *serverLabel2 = new Label(serverName); - serverLabel2->adjustSize(); - gcn::Label *userLabel = new Label(_("Name:")); - gcn::Label *passLabel = new Label(_("Password:")); - mCustomUpdateHost = new CheckBox(_("Custom update host"), - loginData->updateType & LoginData::Upd_Custom, this, "customhost"); - - mUpdateHostText = new TextField(serverConfig.getValue( - "customUpdateHost", "")); - - mUpdateHostText->adjustSize(); - - mUserField = new TextField(mLoginData->username); - mPassField = new PasswordField(mLoginData->password); - - if (mPassField->getText().empty() && LoginDialog::savedPassword != "") - mPassField->setText(LoginDialog::savedPassword); - - mKeepCheck = new CheckBox(_("Remember username"), mLoginData->remember); - mUpdateTypeLabel = new Label(_("Update:")); - mUpdateTypeModel = new UpdateTypeModel(); - mUpdateTypeDropDown = new DropDown(mUpdateTypeModel); - mUpdateTypeDropDown->setActionEventId("updatetype"); - mUpdateTypeDropDown->setSelected((loginData->updateType - | LoginData::Upd_Custom) ^ LoginData::Upd_Custom); - - if (!mCustomUpdateHost->isSelected()) - mUpdateHostText->setVisible(false); - - mRegisterButton = new Button(_("Register"), "register", this); - mServerButton = new Button(_("Change Server"), "server", this); - mLoginButton = new Button(_("Login"), "login", this); - - mUserField->setActionEventId("login"); - mPassField->setActionEventId("login"); - - mUserField->addKeyListener(this); - mPassField->addKeyListener(this); - mUserField->addActionListener(this); - mPassField->addActionListener(this); - - place(0, 0, serverLabel1); - place(1, 0, serverLabel2, 8).setPadding(1); - - place(0, 1, userLabel); - place(0, 2, passLabel); - place(1, 1, mUserField, 8).setPadding(1); - place(1, 2, mPassField, 8).setPadding(1); - place(0, 6, mUpdateTypeLabel, 1); - place(1, 6, mUpdateTypeDropDown, 8); - place(0, 7, mCustomUpdateHost, 9); - place(0, 8, mUpdateHostText, 9); - place(0, 9, mKeepCheck, 9); - place(0, 10, mRegisterButton).setHAlign(LayoutCell::LEFT); - place(2, 10, mServerButton); - place(3, 10, mLoginButton); - - reflowLayout(); - - addKeyListener(this); - - center(); - setVisible(true); - - if (mUserField->getText().empty()) - mUserField->requestFocus(); - else - mPassField->requestFocus(); - - mLoginButton->setEnabled(canSubmit()); - mRegisterButton->setEnabled(Net::getLoginHandler() - ->isRegistrationEnabled()); -} - -LoginDialog::~LoginDialog() -{ - delete mUpdateTypeModel; - mUpdateTypeModel = 0; -} - -void LoginDialog::action(const gcn::ActionEvent &event) -{ - if (event.getId() == "login" && canSubmit()) - { - mLoginData->username = mUserField->getText(); - mLoginData->password = mPassField->getText(); - mLoginData->remember = mKeepCheck->isSelected(); - int updateType = mUpdateTypeDropDown->getSelected(); - - if (mCustomUpdateHost->isSelected()) - { - updateType |= LoginData::Upd_Custom; - serverConfig.setValue("customUpdateHost", - mUpdateHostText->getText()); - - mLoginData->updateHost = mUpdateHostText->getText(); - *mUpdateHost = mUpdateHostText->getText(); - } - mLoginData->updateType = updateType; - serverConfig.setValue("updateType", updateType); - - mLoginData->registerLogin = false; - - mRegisterButton->setEnabled(false); - mServerButton->setEnabled(false); - mLoginButton->setEnabled(false); - - if (mLoginData->remember) - LoginDialog::savedPassword = mPassField->getText(); - - Client::setState(STATE_LOGIN_ATTEMPT); - } - else if (event.getId() == "server") - { - Client::setState(STATE_SWITCH_SERVER); - } - else if (event.getId() == "register") - { - mLoginData->username = mUserField->getText(); - mLoginData->password = mPassField->getText(); - - Client::setState(STATE_REGISTER_PREP); - } - else if (event.getId() == "customhost") - { - mUpdateHostText->setVisible(mCustomUpdateHost->isSelected()); - } -} - -void LoginDialog::keyPressed(gcn::KeyEvent &keyEvent) -{ - gcn::Key key = keyEvent.getKey(); - - if (key.getValue() == Key::ESCAPE) - action(gcn::ActionEvent(NULL, mServerButton->getActionEventId())); - else if (key.getValue() == Key::ENTER) - action(gcn::ActionEvent(NULL, mLoginButton->getActionEventId())); - else - mLoginButton->setEnabled(canSubmit()); -} - -bool LoginDialog::canSubmit() const -{ - return !mUserField->getText().empty() && - !mPassField->getText().empty() && - Client::getState() == STATE_LOGIN; -} diff --git a/src/gui/login.h b/src/gui/login.h deleted file mode 100644 index 9a65e9718..000000000 --- a/src/gui/login.h +++ /dev/null @@ -1,93 +0,0 @@ -/* - * The ManaPlus Client - * Copyright (C) 2004-2009 The Mana World Development Team - * Copyright (C) 2009-2010 The Mana Developers - * Copyright (C) 2011 The ManaPlus Developers - * - * This file is part of The ManaPlus Client. - * - * 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. - * - * 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 this program. If not, see . - */ - -#ifndef LOGIN_H -#define LOGIN_H - -#include "gui/widgets/window.h" - -#include -#include -#include - -#include -#include - -class LoginData; -class UpdateTypeModel; - -/** - * The login dialog. - * - * \ingroup Interface - */ -class LoginDialog : public Window, public gcn::ActionListener, - public gcn::KeyListener -{ - public: - /** - * Constructor - * - * @see Window::Window - */ - LoginDialog(LoginData *loginData, std::string serverName, - std::string *updateHost); - - ~LoginDialog(); - - /** - * Called when receiving actions from the widgets. - */ - void action(const gcn::ActionEvent &event); - - /** - * Called when a key is pressed in one of the text fields. - */ - void keyPressed(gcn::KeyEvent &keyEvent); - - static std::string savedPasswordKey; - static std::string savedPassword; - - private: - /** - * Returns whether submit can be enabled. This is true in the login - * state, when all necessary fields have some text. - */ - bool canSubmit() const; - - gcn::TextField *mUserField; - gcn::TextField *mPassField; - gcn::CheckBox *mKeepCheck; - gcn::Label *mUpdateTypeLabel; - gcn::DropDown *mUpdateTypeDropDown; - gcn::Button *mServerButton; - gcn::Button *mLoginButton; - gcn::Button *mRegisterButton; - gcn::CheckBox *mCustomUpdateHost; - gcn::TextField *mUpdateHostText; - - LoginData *mLoginData; - std::string *mUpdateHost; - UpdateTypeModel *mUpdateTypeModel; -}; - -#endif diff --git a/src/gui/logindialog.cpp b/src/gui/logindialog.cpp new file mode 100644 index 000000000..e32caeb13 --- /dev/null +++ b/src/gui/logindialog.cpp @@ -0,0 +1,235 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * 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. + * + * 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 this program. If not, see . + */ + +#include "gui/logindialog.h" + +#include "client.h" +#include "configuration.h" + +#include "gui/okdialog.h" +#include "gui/sdlinput.h" + +#include "gui/widgets/button.h" +#include "gui/widgets/checkbox.h" +#include "gui/widgets/dropdown.h" +#include "gui/widgets/label.h" +#include "gui/widgets/layout.h" +#include "gui/widgets/passwordfield.h" +#include "gui/widgets/textfield.h" + +#include "net/logindata.h" +#include "net/loginhandler.h" +#include "net/net.h" + +#include "utils/gettext.h" +#include "utils/stringutils.h" + +static const int MAX_SERVER_LIST_SIZE = 15; +static const int LOGIN_DIALOG_WIDTH = 300; +static const int LOGIN_DIALOG_HEIGHT = 140; +static const int FIELD_WIDTH = LOGIN_DIALOG_WIDTH - 70; + +std::string LoginDialog::savedPassword = ""; +std::string LoginDialog::savedPasswordKey = ""; + + +const char *UPDATE_TYPE_TEXT[3] = +{ + N_("Normal"), + N_("Auto Close"), + N_("Skip"), +}; + +class UpdateTypeModel : public gcn::ListModel +{ +public: + virtual ~UpdateTypeModel() + { } + + virtual int getNumberOfElements() + { + return 3; + } + + virtual std::string getElementAt(int i) + { + if (i >= getNumberOfElements() || i < 0) + return _("???"); + + return gettext(UPDATE_TYPE_TEXT[i]); + } +}; + +LoginDialog::LoginDialog(LoginData *loginData, std::string serverName, + std::string *updateHost): + Window(_("Login")), + mLoginData(loginData), + mUpdateHost(updateHost) +{ + gcn::Label *serverLabel1 = new Label(_("Server:")); + gcn::Label *serverLabel2 = new Label(serverName); + serverLabel2->adjustSize(); + gcn::Label *userLabel = new Label(_("Name:")); + gcn::Label *passLabel = new Label(_("Password:")); + mCustomUpdateHost = new CheckBox(_("Custom update host"), + loginData->updateType & LoginData::Upd_Custom, this, "customhost"); + + mUpdateHostText = new TextField(serverConfig.getValue( + "customUpdateHost", "")); + + mUpdateHostText->adjustSize(); + + mUserField = new TextField(mLoginData->username); + mPassField = new PasswordField(mLoginData->password); + + if (mPassField->getText().empty() && LoginDialog::savedPassword != "") + mPassField->setText(LoginDialog::savedPassword); + + mKeepCheck = new CheckBox(_("Remember username"), mLoginData->remember); + mUpdateTypeLabel = new Label(_("Update:")); + mUpdateTypeModel = new UpdateTypeModel(); + mUpdateTypeDropDown = new DropDown(mUpdateTypeModel); + mUpdateTypeDropDown->setActionEventId("updatetype"); + mUpdateTypeDropDown->setSelected((loginData->updateType + | LoginData::Upd_Custom) ^ LoginData::Upd_Custom); + + if (!mCustomUpdateHost->isSelected()) + mUpdateHostText->setVisible(false); + + mRegisterButton = new Button(_("Register"), "register", this); + mServerButton = new Button(_("Change Server"), "server", this); + mLoginButton = new Button(_("Login"), "login", this); + + mUserField->setActionEventId("login"); + mPassField->setActionEventId("login"); + + mUserField->addKeyListener(this); + mPassField->addKeyListener(this); + mUserField->addActionListener(this); + mPassField->addActionListener(this); + + place(0, 0, serverLabel1); + place(1, 0, serverLabel2, 8).setPadding(1); + + place(0, 1, userLabel); + place(0, 2, passLabel); + place(1, 1, mUserField, 8).setPadding(1); + place(1, 2, mPassField, 8).setPadding(1); + place(0, 6, mUpdateTypeLabel, 1); + place(1, 6, mUpdateTypeDropDown, 8); + place(0, 7, mCustomUpdateHost, 9); + place(0, 8, mUpdateHostText, 9); + place(0, 9, mKeepCheck, 9); + place(0, 10, mRegisterButton).setHAlign(LayoutCell::LEFT); + place(2, 10, mServerButton); + place(3, 10, mLoginButton); + + reflowLayout(); + + addKeyListener(this); + + center(); + setVisible(true); + + if (mUserField->getText().empty()) + mUserField->requestFocus(); + else + mPassField->requestFocus(); + + mLoginButton->setEnabled(canSubmit()); + mRegisterButton->setEnabled(Net::getLoginHandler() + ->isRegistrationEnabled()); +} + +LoginDialog::~LoginDialog() +{ + delete mUpdateTypeModel; + mUpdateTypeModel = 0; +} + +void LoginDialog::action(const gcn::ActionEvent &event) +{ + if (event.getId() == "login" && canSubmit()) + { + mLoginData->username = mUserField->getText(); + mLoginData->password = mPassField->getText(); + mLoginData->remember = mKeepCheck->isSelected(); + int updateType = mUpdateTypeDropDown->getSelected(); + + if (mCustomUpdateHost->isSelected()) + { + updateType |= LoginData::Upd_Custom; + serverConfig.setValue("customUpdateHost", + mUpdateHostText->getText()); + + mLoginData->updateHost = mUpdateHostText->getText(); + *mUpdateHost = mUpdateHostText->getText(); + } + mLoginData->updateType = updateType; + serverConfig.setValue("updateType", updateType); + + mLoginData->registerLogin = false; + + mRegisterButton->setEnabled(false); + mServerButton->setEnabled(false); + mLoginButton->setEnabled(false); + + if (mLoginData->remember) + LoginDialog::savedPassword = mPassField->getText(); + + Client::setState(STATE_LOGIN_ATTEMPT); + } + else if (event.getId() == "server") + { + Client::setState(STATE_SWITCH_SERVER); + } + else if (event.getId() == "register") + { + mLoginData->username = mUserField->getText(); + mLoginData->password = mPassField->getText(); + + Client::setState(STATE_REGISTER_PREP); + } + else if (event.getId() == "customhost") + { + mUpdateHostText->setVisible(mCustomUpdateHost->isSelected()); + } +} + +void LoginDialog::keyPressed(gcn::KeyEvent &keyEvent) +{ + gcn::Key key = keyEvent.getKey(); + + if (key.getValue() == Key::ESCAPE) + action(gcn::ActionEvent(NULL, mServerButton->getActionEventId())); + else if (key.getValue() == Key::ENTER) + action(gcn::ActionEvent(NULL, mLoginButton->getActionEventId())); + else + mLoginButton->setEnabled(canSubmit()); +} + +bool LoginDialog::canSubmit() const +{ + return !mUserField->getText().empty() && + !mPassField->getText().empty() && + Client::getState() == STATE_LOGIN; +} diff --git a/src/gui/logindialog.h b/src/gui/logindialog.h new file mode 100644 index 000000000..ade45c211 --- /dev/null +++ b/src/gui/logindialog.h @@ -0,0 +1,93 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * 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. + * + * 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 this program. If not, see . + */ + +#ifndef LOGINDIALOG_H +#define LOGINDIALOG_H + +#include "gui/widgets/window.h" + +#include +#include +#include + +#include +#include + +class LoginData; +class UpdateTypeModel; + +/** + * The login dialog. + * + * \ingroup Interface + */ +class LoginDialog : public Window, public gcn::ActionListener, + public gcn::KeyListener +{ + public: + /** + * Constructor + * + * @see Window::Window + */ + LoginDialog(LoginData *loginData, std::string serverName, + std::string *updateHost); + + ~LoginDialog(); + + /** + * Called when receiving actions from the widgets. + */ + void action(const gcn::ActionEvent &event); + + /** + * Called when a key is pressed in one of the text fields. + */ + void keyPressed(gcn::KeyEvent &keyEvent); + + static std::string savedPasswordKey; + static std::string savedPassword; + + private: + /** + * Returns whether submit can be enabled. This is true in the login + * state, when all necessary fields have some text. + */ + bool canSubmit() const; + + gcn::TextField *mUserField; + gcn::TextField *mPassField; + gcn::CheckBox *mKeepCheck; + gcn::Label *mUpdateTypeLabel; + gcn::DropDown *mUpdateTypeDropDown; + gcn::Button *mServerButton; + gcn::Button *mLoginButton; + gcn::Button *mRegisterButton; + gcn::CheckBox *mCustomUpdateHost; + gcn::TextField *mUpdateHostText; + + LoginData *mLoginData; + std::string *mUpdateHost; + UpdateTypeModel *mUpdateTypeModel; +}; + +#endif diff --git a/src/gui/popupmenu.cpp b/src/gui/popupmenu.cpp index 73fd360fa..9048c53b2 100644 --- a/src/gui/popupmenu.cpp +++ b/src/gui/popupmenu.cpp @@ -44,11 +44,11 @@ #include "gui/itemamountwindow.h" #include "gui/ministatus.h" #include "gui/outfitwindow.h" -#include "gui/sell.h" +#include "gui/selldialog.h" #include "gui/socialwindow.h" #include "gui/textcommandeditor.h" #include "gui/textdialog.h" -#include "gui/trade.h" +#include "gui/tradewindow.h" #include "gui/viewport.h" #include "gui/windowmenu.h" diff --git a/src/gui/register.cpp b/src/gui/register.cpp index 9ebc82fb7..4bce1b8b8 100644 --- a/src/gui/register.cpp +++ b/src/gui/register.cpp @@ -26,7 +26,7 @@ #include "configuration.h" #include "log.h" -#include "gui/login.h" +#include "gui/logindialog.h" #include "gui/okdialog.h" #include "gui/widgets/button.h" diff --git a/src/gui/sell.cpp b/src/gui/sell.cpp deleted file mode 100644 index 1a1fa60e0..000000000 --- a/src/gui/sell.cpp +++ /dev/null @@ -1,351 +0,0 @@ -/* - * The ManaPlus Client - * Copyright (C) 2004-2009 The Mana World Development Team - * Copyright (C) 2009-2010 The Mana Developers - * Copyright (C) 2011 The ManaPlus Developers - * - * This file is part of The ManaPlus Client. - * - * 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. - * - * 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 this program. If not, see . - */ - -#include "gui/sell.h" - -#include "shopitem.h" -#include "units.h" - -#include "gui/setup.h" -#include "gui/trade.h" - -#include "gui/widgets/button.h" -#include "gui/widgets/label.h" -#include "gui/widgets/layout.h" -#include "gui/widgets/scrollarea.h" -#include "gui/widgets/shopitems.h" -#include "gui/widgets/shoplistbox.h" -#include "gui/widgets/slider.h" - -#include "net/buysellhandler.h" -#include "net/net.h" -#include "net/npchandler.h" - -#include "resources/iteminfo.h" - -#include "utils/gettext.h" -#include "utils/stringutils.h" - -SellDialog::DialogList SellDialog::instances; - -SellDialog::SellDialog(int npcId): - Window(_("Sell")), - mNpcId(npcId), mMaxItems(0), mAmountItems(0), mNick("") -{ - init(); -} - -SellDialog::SellDialog(std::string nick): - Window(_("Sell")), - mNpcId(-1), mMaxItems(0), mAmountItems(0), mNick(nick) -{ - init(); -} - -void SellDialog::init() -{ - setWindowName("Sell"); - //setupWindow->registerWindowForReset(this); - setResizable(true); - setCloseButton(true); - setMinWidth(260); - setMinHeight(230); - setDefaultSize(260, 230, ImageRect::CENTER); - - // Create a ShopItems instance, that is aware of duplicate entries. - mShopItems = new ShopItems(true); - - mShopItemList = new ShopListBox(mShopItems, mShopItems); - mScrollArea = new ScrollArea(mShopItemList); - mScrollArea->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); - - mSlider = new Slider(1.0); - - mQuantityLabel = new Label(strprintf("%d / %d", mAmountItems, mMaxItems)); - mQuantityLabel->setAlignment(gcn::Graphics::CENTER); - mMoneyLabel = new Label(strprintf(_("Price: %s / Total: %s"), - "", "")); - - mIncreaseButton = new Button(_("+"), "inc", this); - mDecreaseButton = new Button(_("-"), "dec", this); - mSellButton = new Button(_("Sell"), "sell", this); - mQuitButton = new Button(_("Quit"), "quit", this); - mAddMaxButton = new Button(_("Max"), "max", this); - - mDecreaseButton->adjustSize(); - mDecreaseButton->setWidth(mIncreaseButton->getWidth()); - - mIncreaseButton->setEnabled(false); - mDecreaseButton->setEnabled(false); - mSellButton->setEnabled(false); - mSlider->setEnabled(false); - - mShopItemList->setPriceCheck(false); - mShopItemList->addSelectionListener(this); - mSlider->setActionEventId("slider"); - mSlider->addActionListener(this); - - ContainerPlacer place; - place = getPlacer(0, 0); - - place(0, 0, mScrollArea, 8, 5).setPadding(3); - place(0, 5, mDecreaseButton); - place(1, 5, mSlider, 3); - place(4, 5, mIncreaseButton); - place(5, 5, mQuantityLabel, 2); - place(7, 5, mAddMaxButton); - place(0, 6, mMoneyLabel, 8); - place(6, 7, mSellButton); - place(7, 7, mQuitButton); - - Layout &layout = getLayout(); - layout.setRowHeight(0, Layout::AUTO_SET); - - center(); - loadWindowState(); - - instances.push_back(this); - setVisible(true); -} - -SellDialog::~SellDialog() -{ - delete mShopItems; - mShopItems = 0; - - instances.remove(this); -} - -void SellDialog::reset() -{ - mShopItems->clear(); - mSlider->setValue(0); - - // Reset previous selected item to prevent failing asserts - mShopItemList->setSelected(-1); - - updateButtonsAndLabels(); -} - -void SellDialog::addItem(const Item *item, int price) -{ - if (!item) - return; - - mShopItems->addItem2(item->getInvIndex(), item->getId(), - item->getColor(), item->getQuantity(), price); - - mShopItemList->adjustSize(); -} - -void SellDialog::addItem(int id, unsigned char color, int amount, int price) -{ - mShopItems->addItem(id, color, amount, price); - mShopItemList->adjustSize(); -} - - -void SellDialog::action(const gcn::ActionEvent &event) -{ - if (event.getId() == "quit") - { - close(); - return; - } - - int selectedItem = mShopItemList->getSelected(); - - // The following actions require a valid item selection - if (selectedItem == -1 || - selectedItem >= static_cast(mShopItems->getNumberOfElements())) - { - return; - } - - if (event.getId() == "slider") - { - mAmountItems = static_cast(mSlider->getValue()); - updateButtonsAndLabels(); - } - else if (event.getId() == "inc" && mAmountItems < mMaxItems) - { - mAmountItems++; - mSlider->setValue(mAmountItems); - updateButtonsAndLabels(); - } - else if (event.getId() == "dec" && mAmountItems > 1) - { - mAmountItems--; - mSlider->setValue(mAmountItems); - updateButtonsAndLabels(); - } - else if (event.getId() == "max") - { - mAmountItems = mMaxItems; - mSlider->setValue(mAmountItems); - updateButtonsAndLabels(); - } - else if (event.getId() == "sell" && mAmountItems > 0 - && mAmountItems <= mMaxItems) - { - if (mNpcId != -1) - { - // Attempt sell - ShopItem *item = mShopItems->at(selectedItem); - int sellCount, itemIndex; - mPlayerMoney += - mAmountItems * mShopItems->at(selectedItem)->getPrice(); - mMaxItems -= mAmountItems; - while (mAmountItems > 0) - { - // This order is important, item->getCurrentInvIndex() would return - // the inventory index of the next Duplicate otherwise. - itemIndex = item->getCurrentInvIndex(); - sellCount = item->sellCurrentDuplicate(mAmountItems); - -#ifdef MANASERV_SUPPORT - // For Manaserv, the Item id is to be given as index. - if ((Net::getNetworkType() == ServerInfo::MANASERV)) - itemIndex = item->getId(); -#endif - - Net::getNpcHandler()->sellItem(mNpcId, itemIndex, sellCount); - mAmountItems -= sellCount; - } - - mPlayerMoney += - mAmountItems * mShopItems->at(selectedItem)->getPrice(); - mAmountItems = 1; - mSlider->setValue(0); - - if (mMaxItems) - { - updateButtonsAndLabels(); - } - else - { - // All were sold - mShopItemList->setSelected(-1); - delete mShopItems->at(selectedItem); - mShopItems->erase(selectedItem); - - gcn::Rectangle scroll; - scroll.y = mShopItemList->getRowHeight() * (selectedItem + 1); - scroll.height = mShopItemList->getRowHeight(); - mShopItemList->showPart(scroll); - } - } - else - { - ShopItem *item = mShopItems->at(selectedItem); - - Net::getBuySellHandler()->sendSellRequest(mNick, - item, mAmountItems); - - if (tradeWindow) - tradeWindow->addAutoItem(mNick, item, mAmountItems); - } - } -} - -void SellDialog::valueChanged(const gcn::SelectionEvent &event _UNUSED_) -{ - // Reset amount of items and update labels - mAmountItems = 1; - mSlider->setValue(0); - - updateButtonsAndLabels(); - mSlider->gcn::Slider::setScale(1, mMaxItems); -} - -void SellDialog::setMoney(int amount) -{ - mPlayerMoney = amount; - mShopItemList->setPlayersMoney(amount); -} - -void SellDialog::updateButtonsAndLabels() -{ - int selectedItem = mShopItemList->getSelected(); - int income = 0; - ShopItem *item = 0; - - if (selectedItem > -1 && mShopItems->at(selectedItem)) - { - item = mShopItems->at(selectedItem); - if (item) - { - mMaxItems = item->getQuantity(); - if (mAmountItems > mMaxItems) - mAmountItems = mMaxItems; - income = mAmountItems * mShopItems->at(selectedItem)->getPrice(); - } - else - { - mMaxItems = 0; - mAmountItems = 0; - } - } - else - { - mMaxItems = 0; - mAmountItems = 0; - } - - // Update Buttons and slider - mSellButton->setEnabled(mAmountItems > 0); - mDecreaseButton->setEnabled(mAmountItems > 1); - mIncreaseButton->setEnabled(mAmountItems < mMaxItems); - mSlider->setEnabled(mMaxItems > 1); - - // Update the quantity and money labels - mQuantityLabel->setCaption(strprintf("%d / %d", mAmountItems, mMaxItems)); - mMoneyLabel->setCaption(strprintf(_("Price: %s / Total: %s"), - Units::formatCurrency(income).c_str(), - Units::formatCurrency(mPlayerMoney + income).c_str())); - if (item) - item->update(); -} - -void SellDialog::setVisible(bool visible) -{ - Window::setVisible(visible); - - if (visible) - { - if (mShopItemList) - mShopItemList->requestFocus(); - } - else - { - scheduleDelete(); - } -} - -void SellDialog::closeAll() -{ - DialogList::iterator it = instances.begin(); - DialogList::iterator it_end = instances.end(); - - for (; it != it_end; ++it) - (*it)->close(); -} diff --git a/src/gui/sell.h b/src/gui/sell.h deleted file mode 100644 index 86343a953..000000000 --- a/src/gui/sell.h +++ /dev/null @@ -1,146 +0,0 @@ -/* - * The ManaPlus Client - * Copyright (C) 2004-2009 The Mana World Development Team - * Copyright (C) 2009-2010 The Mana Developers - * Copyright (C) 2011 The ManaPlus Developers - * - * This file is part of The ManaPlus Client. - * - * 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. - * - * 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 this program. If not, see . - */ - -#ifndef SELL_H -#define SELL_H - -#include "gui/widgets/window.h" - -#include -#include - -#include - -#ifdef __GNUC__ -#define _UNUSED_ __attribute__ ((unused)) -#else -#define _UNUSED_ -#endif - -class Item; -class ShopItems; -class ShopListBox; - -/** - * The sell dialog. - * - * \ingroup Interface - */ -class SellDialog : public Window, gcn::ActionListener, gcn::SelectionListener -{ - public: - /** - * Constructor. - * - * @see Window::Window - */ - SellDialog(int npcId); - - /** - * Constructor. - */ - SellDialog(std::string nick); - - /** - * Destructor - */ - virtual ~SellDialog(); - - void init(); - - /** - * Resets the dialog, clearing inventory. - */ - void reset(); - - /** - * Adds an item to the inventory. - */ - void addItem(const Item *item, int price); - - /** - * Called when receiving actions from the widgets. - */ - void action(const gcn::ActionEvent &event); - - /** - * Updates labels according to selected item. - * - * @see SelectionListener::selectionChanged - */ - void valueChanged(const gcn::SelectionEvent &event); - - /** - * Gives Player's Money amount - */ - void setMoney(int amount); - - /** - * Sets the visibility of this window. - */ - void setVisible(bool visible); - - void addItem(int id, unsigned char color, int amount, int price); - - /** - * Returns true if any instances exist. - */ - static bool isActive() - { return !instances.empty(); } - - /** - * Closes all instances. - */ - static void closeAll(); - - private: - typedef std::list DialogList; - static DialogList instances; - - /** - * Updates the state of buttons and labels. - */ - void updateButtonsAndLabels(); - - int mNpcId; - - gcn::Button *mSellButton; - gcn::Button *mQuitButton; - gcn::Button *mAddMaxButton; - gcn::Button *mIncreaseButton; - gcn::Button *mDecreaseButton; - ShopListBox *mShopItemList; - gcn::ScrollArea *mScrollArea; - gcn::Label *mMoneyLabel; - gcn::Label *mQuantityLabel; - gcn::Slider *mSlider; - - ShopItems *mShopItems; - int mPlayerMoney; - - int mMaxItems; - int mAmountItems; - - std::string mNick; -}; - -#endif diff --git a/src/gui/selldialog.cpp b/src/gui/selldialog.cpp new file mode 100644 index 000000000..e1ed7c1b0 --- /dev/null +++ b/src/gui/selldialog.cpp @@ -0,0 +1,351 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * 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. + * + * 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 this program. If not, see . + */ + +#include "gui/selldialog.h" + +#include "shopitem.h" +#include "units.h" + +#include "gui/setup.h" +#include "gui/tradewindow.h" + +#include "gui/widgets/button.h" +#include "gui/widgets/label.h" +#include "gui/widgets/layout.h" +#include "gui/widgets/scrollarea.h" +#include "gui/widgets/shopitems.h" +#include "gui/widgets/shoplistbox.h" +#include "gui/widgets/slider.h" + +#include "net/buysellhandler.h" +#include "net/net.h" +#include "net/npchandler.h" + +#include "resources/iteminfo.h" + +#include "utils/gettext.h" +#include "utils/stringutils.h" + +SellDialog::DialogList SellDialog::instances; + +SellDialog::SellDialog(int npcId): + Window(_("Sell")), + mNpcId(npcId), mMaxItems(0), mAmountItems(0), mNick("") +{ + init(); +} + +SellDialog::SellDialog(std::string nick): + Window(_("Sell")), + mNpcId(-1), mMaxItems(0), mAmountItems(0), mNick(nick) +{ + init(); +} + +void SellDialog::init() +{ + setWindowName("Sell"); + //setupWindow->registerWindowForReset(this); + setResizable(true); + setCloseButton(true); + setMinWidth(260); + setMinHeight(230); + setDefaultSize(260, 230, ImageRect::CENTER); + + // Create a ShopItems instance, that is aware of duplicate entries. + mShopItems = new ShopItems(true); + + mShopItemList = new ShopListBox(mShopItems, mShopItems); + mScrollArea = new ScrollArea(mShopItemList); + mScrollArea->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); + + mSlider = new Slider(1.0); + + mQuantityLabel = new Label(strprintf("%d / %d", mAmountItems, mMaxItems)); + mQuantityLabel->setAlignment(gcn::Graphics::CENTER); + mMoneyLabel = new Label(strprintf(_("Price: %s / Total: %s"), + "", "")); + + mIncreaseButton = new Button(_("+"), "inc", this); + mDecreaseButton = new Button(_("-"), "dec", this); + mSellButton = new Button(_("Sell"), "sell", this); + mQuitButton = new Button(_("Quit"), "quit", this); + mAddMaxButton = new Button(_("Max"), "max", this); + + mDecreaseButton->adjustSize(); + mDecreaseButton->setWidth(mIncreaseButton->getWidth()); + + mIncreaseButton->setEnabled(false); + mDecreaseButton->setEnabled(false); + mSellButton->setEnabled(false); + mSlider->setEnabled(false); + + mShopItemList->setPriceCheck(false); + mShopItemList->addSelectionListener(this); + mSlider->setActionEventId("slider"); + mSlider->addActionListener(this); + + ContainerPlacer place; + place = getPlacer(0, 0); + + place(0, 0, mScrollArea, 8, 5).setPadding(3); + place(0, 5, mDecreaseButton); + place(1, 5, mSlider, 3); + place(4, 5, mIncreaseButton); + place(5, 5, mQuantityLabel, 2); + place(7, 5, mAddMaxButton); + place(0, 6, mMoneyLabel, 8); + place(6, 7, mSellButton); + place(7, 7, mQuitButton); + + Layout &layout = getLayout(); + layout.setRowHeight(0, Layout::AUTO_SET); + + center(); + loadWindowState(); + + instances.push_back(this); + setVisible(true); +} + +SellDialog::~SellDialog() +{ + delete mShopItems; + mShopItems = 0; + + instances.remove(this); +} + +void SellDialog::reset() +{ + mShopItems->clear(); + mSlider->setValue(0); + + // Reset previous selected item to prevent failing asserts + mShopItemList->setSelected(-1); + + updateButtonsAndLabels(); +} + +void SellDialog::addItem(const Item *item, int price) +{ + if (!item) + return; + + mShopItems->addItem2(item->getInvIndex(), item->getId(), + item->getColor(), item->getQuantity(), price); + + mShopItemList->adjustSize(); +} + +void SellDialog::addItem(int id, unsigned char color, int amount, int price) +{ + mShopItems->addItem(id, color, amount, price); + mShopItemList->adjustSize(); +} + + +void SellDialog::action(const gcn::ActionEvent &event) +{ + if (event.getId() == "quit") + { + close(); + return; + } + + int selectedItem = mShopItemList->getSelected(); + + // The following actions require a valid item selection + if (selectedItem == -1 || + selectedItem >= static_cast(mShopItems->getNumberOfElements())) + { + return; + } + + if (event.getId() == "slider") + { + mAmountItems = static_cast(mSlider->getValue()); + updateButtonsAndLabels(); + } + else if (event.getId() == "inc" && mAmountItems < mMaxItems) + { + mAmountItems++; + mSlider->setValue(mAmountItems); + updateButtonsAndLabels(); + } + else if (event.getId() == "dec" && mAmountItems > 1) + { + mAmountItems--; + mSlider->setValue(mAmountItems); + updateButtonsAndLabels(); + } + else if (event.getId() == "max") + { + mAmountItems = mMaxItems; + mSlider->setValue(mAmountItems); + updateButtonsAndLabels(); + } + else if (event.getId() == "sell" && mAmountItems > 0 + && mAmountItems <= mMaxItems) + { + if (mNpcId != -1) + { + // Attempt sell + ShopItem *item = mShopItems->at(selectedItem); + int sellCount, itemIndex; + mPlayerMoney += + mAmountItems * mShopItems->at(selectedItem)->getPrice(); + mMaxItems -= mAmountItems; + while (mAmountItems > 0) + { + // This order is important, item->getCurrentInvIndex() would return + // the inventory index of the next Duplicate otherwise. + itemIndex = item->getCurrentInvIndex(); + sellCount = item->sellCurrentDuplicate(mAmountItems); + +#ifdef MANASERV_SUPPORT + // For Manaserv, the Item id is to be given as index. + if ((Net::getNetworkType() == ServerInfo::MANASERV)) + itemIndex = item->getId(); +#endif + + Net::getNpcHandler()->sellItem(mNpcId, itemIndex, sellCount); + mAmountItems -= sellCount; + } + + mPlayerMoney += + mAmountItems * mShopItems->at(selectedItem)->getPrice(); + mAmountItems = 1; + mSlider->setValue(0); + + if (mMaxItems) + { + updateButtonsAndLabels(); + } + else + { + // All were sold + mShopItemList->setSelected(-1); + delete mShopItems->at(selectedItem); + mShopItems->erase(selectedItem); + + gcn::Rectangle scroll; + scroll.y = mShopItemList->getRowHeight() * (selectedItem + 1); + scroll.height = mShopItemList->getRowHeight(); + mShopItemList->showPart(scroll); + } + } + else + { + ShopItem *item = mShopItems->at(selectedItem); + + Net::getBuySellHandler()->sendSellRequest(mNick, + item, mAmountItems); + + if (tradeWindow) + tradeWindow->addAutoItem(mNick, item, mAmountItems); + } + } +} + +void SellDialog::valueChanged(const gcn::SelectionEvent &event _UNUSED_) +{ + // Reset amount of items and update labels + mAmountItems = 1; + mSlider->setValue(0); + + updateButtonsAndLabels(); + mSlider->gcn::Slider::setScale(1, mMaxItems); +} + +void SellDialog::setMoney(int amount) +{ + mPlayerMoney = amount; + mShopItemList->setPlayersMoney(amount); +} + +void SellDialog::updateButtonsAndLabels() +{ + int selectedItem = mShopItemList->getSelected(); + int income = 0; + ShopItem *item = 0; + + if (selectedItem > -1 && mShopItems->at(selectedItem)) + { + item = mShopItems->at(selectedItem); + if (item) + { + mMaxItems = item->getQuantity(); + if (mAmountItems > mMaxItems) + mAmountItems = mMaxItems; + income = mAmountItems * mShopItems->at(selectedItem)->getPrice(); + } + else + { + mMaxItems = 0; + mAmountItems = 0; + } + } + else + { + mMaxItems = 0; + mAmountItems = 0; + } + + // Update Buttons and slider + mSellButton->setEnabled(mAmountItems > 0); + mDecreaseButton->setEnabled(mAmountItems > 1); + mIncreaseButton->setEnabled(mAmountItems < mMaxItems); + mSlider->setEnabled(mMaxItems > 1); + + // Update the quantity and money labels + mQuantityLabel->setCaption(strprintf("%d / %d", mAmountItems, mMaxItems)); + mMoneyLabel->setCaption(strprintf(_("Price: %s / Total: %s"), + Units::formatCurrency(income).c_str(), + Units::formatCurrency(mPlayerMoney + income).c_str())); + if (item) + item->update(); +} + +void SellDialog::setVisible(bool visible) +{ + Window::setVisible(visible); + + if (visible) + { + if (mShopItemList) + mShopItemList->requestFocus(); + } + else + { + scheduleDelete(); + } +} + +void SellDialog::closeAll() +{ + DialogList::iterator it = instances.begin(); + DialogList::iterator it_end = instances.end(); + + for (; it != it_end; ++it) + (*it)->close(); +} diff --git a/src/gui/selldialog.h b/src/gui/selldialog.h new file mode 100644 index 000000000..e40dd50c2 --- /dev/null +++ b/src/gui/selldialog.h @@ -0,0 +1,146 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * 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. + * + * 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 this program. If not, see . + */ + +#ifndef SELLDIALOG_H +#define SELLDIALOG_H + +#include "gui/widgets/window.h" + +#include +#include + +#include + +#ifdef __GNUC__ +#define _UNUSED_ __attribute__ ((unused)) +#else +#define _UNUSED_ +#endif + +class Item; +class ShopItems; +class ShopListBox; + +/** + * The sell dialog. + * + * \ingroup Interface + */ +class SellDialog : public Window, gcn::ActionListener, gcn::SelectionListener +{ + public: + /** + * Constructor. + * + * @see Window::Window + */ + SellDialog(int npcId); + + /** + * Constructor. + */ + SellDialog(std::string nick); + + /** + * Destructor + */ + virtual ~SellDialog(); + + void init(); + + /** + * Resets the dialog, clearing inventory. + */ + void reset(); + + /** + * Adds an item to the inventory. + */ + void addItem(const Item *item, int price); + + /** + * Called when receiving actions from the widgets. + */ + void action(const gcn::ActionEvent &event); + + /** + * Updates labels according to selected item. + * + * @see SelectionListener::selectionChanged + */ + void valueChanged(const gcn::SelectionEvent &event); + + /** + * Gives Player's Money amount + */ + void setMoney(int amount); + + /** + * Sets the visibility of this window. + */ + void setVisible(bool visible); + + void addItem(int id, unsigned char color, int amount, int price); + + /** + * Returns true if any instances exist. + */ + static bool isActive() + { return !instances.empty(); } + + /** + * Closes all instances. + */ + static void closeAll(); + + private: + typedef std::list DialogList; + static DialogList instances; + + /** + * Updates the state of buttons and labels. + */ + void updateButtonsAndLabels(); + + int mNpcId; + + gcn::Button *mSellButton; + gcn::Button *mQuitButton; + gcn::Button *mAddMaxButton; + gcn::Button *mIncreaseButton; + gcn::Button *mDecreaseButton; + ShopListBox *mShopItemList; + gcn::ScrollArea *mScrollArea; + gcn::Label *mMoneyLabel; + gcn::Label *mQuantityLabel; + gcn::Slider *mSlider; + + ShopItems *mShopItems; + int mPlayerMoney; + + int mMaxItems; + int mAmountItems; + + std::string mNick; +}; + +#endif diff --git a/src/gui/serverdialog.cpp b/src/gui/serverdialog.cpp index 3f4e2650f..48b364264 100644 --- a/src/gui/serverdialog.cpp +++ b/src/gui/serverdialog.cpp @@ -29,7 +29,7 @@ #include "main.h" #include "gui/gui.h" -#include "gui/login.h" +#include "gui/logindialog.h" #include "gui/okdialog.h" #include "gui/sdlinput.h" #include "gui/theme.h" diff --git a/src/gui/shopwindow.cpp b/src/gui/shopwindow.cpp index 60d48a4c7..72553ea01 100644 --- a/src/gui/shopwindow.cpp +++ b/src/gui/shopwindow.cpp @@ -24,9 +24,9 @@ #include "gui/buydialog.h" #include "gui/itemamountwindow.h" -#include "gui/sell.h" +#include "gui/selldialog.h" #include "gui/setup.h" -#include "gui/trade.h" +#include "gui/tradewindow.h" #include "gui/widgets/button.h" #include "gui/widgets/chattab.h" diff --git a/src/gui/trade.cpp b/src/gui/trade.cpp deleted file mode 100644 index 38aaecb83..000000000 --- a/src/gui/trade.cpp +++ /dev/null @@ -1,422 +0,0 @@ -/* - * The ManaPlus Client - * Copyright (C) 2004-2009 The Mana World Development Team - * Copyright (C) 2009-2010 The Mana Developers - * Copyright (C) 2011 The ManaPlus Developers - * - * This file is part of The ManaPlus Client. - * - * 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. - * - * 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 this program. If not, see . - */ - -#include "gui/trade.h" - -#include "inventory.h" -#include "item.h" -#include "localplayer.h" -#include "playerinfo.h" -#include "units.h" - -#include "gui/inventorywindow.h" -#include "gui/itemamountwindow.h" -#include "gui/setup.h" -#include "gui/theme.h" - -#include "gui/widgets/button.h" -#include "gui/widgets/chattab.h" -#include "gui/widgets/itemcontainer.h" -#include "gui/widgets/label.h" -#include "gui/widgets/scrollarea.h" -#include "gui/widgets/textfield.h" -#include "gui/widgets/layout.h" - -#include "net/inventoryhandler.h" -#include "net/net.h" -#include "net/tradehandler.h" - -#include "utils/gettext.h" -#include "utils/stringutils.h" - -#include - -#include - -#define CAPTION_PROPOSE _("Propose trade") -#define CAPTION_CONFIRMED _("Confirmed. Waiting...") -#define CAPTION_ACCEPT _("Agree trade") -#define CAPTION_ACCEPTED _("Agreed. Waiting...") - -TradeWindow::TradeWindow(): - Window(_("Trade: You")), - mMyInventory(new Inventory(Inventory::TRADE)), - mPartnerInventory(new Inventory(Inventory::TRADE)), - mStatus(PROPOSING), - mAutoAddItem(0), - mAutoAddToNick(""), - mGotMoney(0), - mAutoMoney(0) -{ - logger->log1("TradeWindow::TradeWindow nick"); - - setWindowName("Trade"); - setResizable(true); - setCloseButton(true); - setDefaultSize(386, 180, ImageRect::CENTER); - setMinWidth(386); - setMinHeight(180); - - if (setupWindow) - setupWindow->registerWindowForReset(this); - - std::string longestName = getFont()->getWidth(_("OK")) > - getFont()->getWidth(_("Trade")) ? - _("OK") : _("Trade"); - - mAddButton = new Button(_("Add"), "add", this); - mOkButton = new Button("", "", this); // Will be filled in later - - int width = std::max(mOkButton->getFont()->getWidth(CAPTION_PROPOSE), - mOkButton->getFont()->getWidth(CAPTION_CONFIRMED)); - width = std::max(width, mOkButton->getFont()->getWidth(CAPTION_ACCEPT)); - width = std::max(width, mOkButton->getFont()->getWidth(CAPTION_ACCEPTED)); - - mOkButton->setWidth(8 + width); - - mMyItemContainer = new ItemContainer(mMyInventory.get()); - mMyItemContainer->addSelectionListener(this); - - ScrollArea *myScroll = new ScrollArea(mMyItemContainer); - myScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); - - mPartnerItemContainer = new ItemContainer(mPartnerInventory.get()); - mPartnerItemContainer->addSelectionListener(this); - - ScrollArea *partnerScroll = new ScrollArea(mPartnerItemContainer); - partnerScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); - - mMoneyLabel = new Label(strprintf(_("You get %s"), "")); - gcn::Label *mMoneyLabel2 = new Label(_("You give:")); - - mMoneyField = new TextField; - mMoneyField->setWidth(40); - mMoneyChangeButton = new Button(_("Change"), "money", this); - - place(1, 0, mMoneyLabel); - place(0, 1, myScroll).setPadding(3); - place(1, 1, partnerScroll).setPadding(3); - ContainerPlacer place; - place = getPlacer(0, 0); - place(0, 0, mMoneyLabel2); - place(1, 0, mMoneyField, 2); - place(3, 0, mMoneyChangeButton).setHAlign(LayoutCell::LEFT); - place = getPlacer(0, 2); - place(0, 0, mAddButton); - place(1, 0, mOkButton); - 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(); - - reset(); -} - -TradeWindow::~TradeWindow() -{ -} - -void TradeWindow::setMoney(int amount) -{ - if (amount < mGotMoney) - mMoneyLabel->setForegroundColor(Theme::getThemeColor(Theme::WARNING)); - mMoneyLabel->setForegroundColor(Theme::getThemeColor(Theme::TEXT)); - - mGotMoney = amount; - mMoneyLabel->setCaption(strprintf(_("You get %s"), - Units::formatCurrency(amount).c_str())); - mMoneyLabel->adjustSize(); -} - -void TradeWindow::addItem(int id, bool own, int quantity, - int refine, unsigned char color) -{ - if (own) - mMyInventory->addItem(id, quantity, refine, color); - else - mPartnerInventory->addItem(id, quantity, refine, color); -} - -void TradeWindow::addItem2(int id, bool own, int quantity, - int refine, unsigned char color, bool equipment) -{ - if (own) - mMyInventory->addItem(id, quantity, refine, color, equipment); - else - mPartnerInventory->addItem(id, quantity, refine, color, equipment); -} - -void TradeWindow::changeQuantity(int index, bool own, int quantity) -{ - if (own) - { - if (mMyInventory->getItem(index)) - mMyInventory->getItem(index)->setQuantity(quantity); - } - else - { - if (mPartnerInventory->getItem(index)) - mPartnerInventory->getItem(index)->setQuantity(quantity); - } -} - -void TradeWindow::increaseQuantity(int index, bool own, int quantity) -{ - if (own) - { - if (mMyInventory->getItem(index)) - mMyInventory->getItem(index)->increaseQuantity(quantity); - } - else - { - if (mPartnerInventory->getItem(index)) - mPartnerInventory->getItem(index)->increaseQuantity(quantity); - } -} - -void TradeWindow::reset() -{ - mMyInventory->clear(); - mPartnerInventory->clear(); - mOkOther = false; - mOkMe = false; - setMoney(0); - mMoneyField->setEnabled(true); - mMoneyField->setText(""); - mMoneyLabel->setForegroundColor(Theme::getThemeColor(Theme::TEXT)); - mAddButton->setEnabled(true); - mMoneyChangeButton->setEnabled(true); - mGotMoney = 0; - setStatus(PREPARING); -} - -void TradeWindow::receivedOk(bool own) -{ - if (own) - mOkMe = true; - else - mOkOther = true; - - if (mOkMe && mOkOther) - { - //mOkMe = false; - //mOkOther = false; - setStatus(ACCEPTING); - } -} - -void TradeWindow::tradeItem(Item *item, int quantity) -{ - Net::getTradeHandler()->addItem(item, quantity); -} - -void TradeWindow::valueChanged(const gcn::SelectionEvent &event) -{ - if (!mMyItemContainer || !mPartnerItemContainer) - return; - - /* If an item is selected in one container, make sure no item is selected - * in the other container. - */ - if (event.getSource() == mMyItemContainer && - mMyItemContainer->getSelectedItem()) - { - mPartnerItemContainer->selectNone(); - } - else if (mPartnerItemContainer->getSelectedItem()) - { - mMyItemContainer->selectNone(); - } -} - -void TradeWindow::setStatus(Status s) -{ - if (s == mStatus) - return; - mStatus = s; - - switch (s) - { - case PREPARING: - mOkButton->setCaption(CAPTION_PROPOSE); - mOkButton->setActionEventId("ok"); - break; - case PROPOSING: - mOkButton->setCaption(CAPTION_CONFIRMED); - mOkButton->setActionEventId(""); - break; - case ACCEPTING: - mOkButton->setCaption(CAPTION_ACCEPT); - mOkButton->setActionEventId("trade"); - break; - case ACCEPTED: - mOkButton->setCaption(CAPTION_ACCEPTED); - mOkButton->setActionEventId(""); - break; - default: - break; - } - - mOkButton->setEnabled((s != PROPOSING && s != ACCEPTED)); -} - -void TradeWindow::action(const gcn::ActionEvent &event) -{ - if (!inventoryWindow) - return; - - Item *item = inventoryWindow->getSelectedItem(); - - if (event.getId() == "add") - { - if (mStatus != PREPARING) - return; - - if (!inventoryWindow->isVisible()) - { - inventoryWindow->setVisible(true); - return; - } - - if (!item) - return; - - if (mMyInventory->getFreeSlot() == -1) - return; - - if (mMyInventory->contains(item)) - { - if (localChatTab) - { - localChatTab->chatLog(_("Failed adding item. You can not " - "overlap one kind of item on the window."), BY_SERVER); - } - return; - } - - // Choose amount of items to trade - ItemAmountWindow::showWindow(ItemAmountWindow::TradeAdd, this, item); - - setStatus(PREPARING); - } - else if (event.getId() == "cancel") - { - setVisible(false); - reset(); - PlayerInfo::setTrading(false); - - Net::getTradeHandler()->cancel(); - } - else if (event.getId() == "ok") - { - mMoneyField->setEnabled(false); - mAddButton->setEnabled(false); - mMoneyChangeButton->setEnabled(false); - receivedOk(true); - setStatus(PROPOSING); - Net::getTradeHandler()->confirm(); - } - else if (event.getId() == "trade") - { - receivedOk(true); - setStatus(ACCEPTED); - Net::getTradeHandler()->finish(); - } - else if (event.getId() == "money") - { - if (mStatus != PREPARING) - return; - - int v = atoi(mMoneyField->getText().c_str()); - int curMoney = PlayerInfo::getAttribute(MONEY); - if (v > curMoney) - { - if (localChatTab) - { - localChatTab->chatLog(_("You don't have enough money."), - BY_SERVER); - } - v = curMoney; - } - Net::getTradeHandler()->setMoney(v); - mMoneyField->setText(strprintf("%d", v)); - } -} - -void TradeWindow::close() -{ - Net::getTradeHandler()->cancel(); - clear(); -} - -void TradeWindow::clear() -{ - mAutoAddItem = 0; - mAutoAddToNick = ""; - mAutoMoney = 0; - mAutoAddAmount = 0; - mGotMoney = 0; - mMoneyLabel->setForegroundColor(Theme::getThemeColor(Theme::TEXT)); -} - -void TradeWindow::addAutoItem(std::string nick, Item* item, int amount) -{ - mAutoAddToNick = nick; - mAutoAddItem = item; - mAutoAddAmount = amount; -} - -void TradeWindow::addAutoMoney(std::string nick, int money) -{ - mAutoAddToNick = nick; - mAutoMoney = money; -} - -void TradeWindow::initTrade(std::string nick) -{ - if (!player_node) - return; - - if (!mAutoAddToNick.empty() && mAutoAddToNick == nick) - { - if (mAutoAddItem && mAutoAddItem->getQuantity()) - { - Inventory *inv = PlayerInfo::getInventory(); - if (inv) - { - Item *item = inv->findItem(mAutoAddItem->getId()); - if (item) - tradeItem(item, mAutoAddItem->getQuantity()); - } - } - if (mAutoMoney) - { - Net::getTradeHandler()->setMoney(mAutoMoney); - mMoneyField->setText(strprintf("%d", mAutoMoney)); - } - } - clear(); -} diff --git a/src/gui/trade.h b/src/gui/trade.h deleted file mode 100644 index 67515c013..000000000 --- a/src/gui/trade.h +++ /dev/null @@ -1,172 +0,0 @@ -/* - * The ManaPlus Client - * Copyright (C) 2004-2009 The Mana World Development Team - * Copyright (C) 2009-2010 The Mana Developers - * Copyright (C) 2011 The ManaPlus Developers - * - * This file is part of The ManaPlus Client. - * - * 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. - * - * 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 this program. If not, see . - */ - -#ifndef TRADE_H -#define TRADE_H - -#include "guichanfwd.h" - -#include "gui/widgets/window.h" - -#include -#include - -#include - -class Inventory; -class Item; -class ItemContainer; -class ScrollArea; - -/** - * Trade dialog. - * - * \ingroup Interface - */ -class TradeWindow : public Window, gcn::ActionListener, gcn::SelectionListener -{ - public: - /** - * Constructor. - */ - TradeWindow(); - - /** - * Destructor. - */ - ~TradeWindow(); - - /** - * Displays expected money in the trade window. - */ - void setMoney(int quantity); - - /** - * Add an item to the trade window. - */ - void addItem(int id, bool own, int quantity, - int refine, unsigned char color); - - /** - * Reset both item containers - */ - void reset(); - - /** - * Add an item to the trade window. - */ - void addItem2(int id, bool own, int quantity, int refine, - unsigned char color, bool equipment); - - /** - * 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); - - /** - * Player received ok message from server - */ - void receivedOk(bool own); - - /** - * Send trade packet. - */ - void tradeItem(Item *item, int quantity); - - /** - * Updates the labels and makes sure only one item is selected in - * either my inventory or partner inventory. - */ - void valueChanged(const gcn::SelectionEvent &event); - - /** - * Called when receiving actions from the widgets. - */ - void action(const gcn::ActionEvent &event); - - /** - * Closes the Trade Window, as well as telling the server that the - * window has been closed. - */ - void close(); - - /** - * Clear auto trade items. - */ - void clear(); - - /** - * Add item what will be added to trade. - */ - void addAutoItem(std::string nick, Item* item, int amount); - - void addAutoMoney(std::string nick, int money); - - void initTrade(std::string nick); - - std::string getAutoTradeNick() - { return mAutoAddToNick; } - - private: - enum Status - { - PREPARING = 0, /**< Players are adding items. (1) */ - PROPOSING, /**< Local player has confirmed the trade. (1) */ - ACCEPTING, /**< Accepting the trade. (2) */ - ACCEPTED /**< Local player has accepted the trade. */ - }; - - /** - * Sets the current status of the trade. - */ - void setStatus(Status s); - - typedef const std::auto_ptr InventoryPtr; - InventoryPtr mMyInventory; - InventoryPtr mPartnerInventory; - - ItemContainer *mMyItemContainer; - ItemContainer *mPartnerItemContainer; - - gcn::Label *mMoneyLabel; - gcn::Button *mAddButton; - gcn::Button *mOkButton; - gcn::Button *mMoneyChangeButton; - gcn::TextField *mMoneyField; - - Status mStatus; - bool mOkOther, mOkMe; - Item* mAutoAddItem; - std::string mAutoAddToNick; - int mGotMoney; - int mAutoMoney; - int mAutoAddAmount; -}; - -extern TradeWindow *tradeWindow; - -#endif diff --git a/src/gui/tradewindow.cpp b/src/gui/tradewindow.cpp new file mode 100644 index 000000000..279f6c178 --- /dev/null +++ b/src/gui/tradewindow.cpp @@ -0,0 +1,422 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * 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. + * + * 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 this program. If not, see . + */ + +#include "gui/tradewindow.h" + +#include "inventory.h" +#include "item.h" +#include "localplayer.h" +#include "playerinfo.h" +#include "units.h" + +#include "gui/inventorywindow.h" +#include "gui/itemamountwindow.h" +#include "gui/setup.h" +#include "gui/theme.h" + +#include "gui/widgets/button.h" +#include "gui/widgets/chattab.h" +#include "gui/widgets/itemcontainer.h" +#include "gui/widgets/label.h" +#include "gui/widgets/scrollarea.h" +#include "gui/widgets/textfield.h" +#include "gui/widgets/layout.h" + +#include "net/inventoryhandler.h" +#include "net/net.h" +#include "net/tradehandler.h" + +#include "utils/gettext.h" +#include "utils/stringutils.h" + +#include + +#include + +#define CAPTION_PROPOSE _("Propose trade") +#define CAPTION_CONFIRMED _("Confirmed. Waiting...") +#define CAPTION_ACCEPT _("Agree trade") +#define CAPTION_ACCEPTED _("Agreed. Waiting...") + +TradeWindow::TradeWindow(): + Window(_("Trade: You")), + mMyInventory(new Inventory(Inventory::TRADE)), + mPartnerInventory(new Inventory(Inventory::TRADE)), + mStatus(PROPOSING), + mAutoAddItem(0), + mAutoAddToNick(""), + mGotMoney(0), + mAutoMoney(0) +{ + logger->log1("TradeWindow::TradeWindow nick"); + + setWindowName("Trade"); + setResizable(true); + setCloseButton(true); + setDefaultSize(386, 180, ImageRect::CENTER); + setMinWidth(386); + setMinHeight(180); + + if (setupWindow) + setupWindow->registerWindowForReset(this); + + std::string longestName = getFont()->getWidth(_("OK")) > + getFont()->getWidth(_("Trade")) ? + _("OK") : _("Trade"); + + mAddButton = new Button(_("Add"), "add", this); + mOkButton = new Button("", "", this); // Will be filled in later + + int width = std::max(mOkButton->getFont()->getWidth(CAPTION_PROPOSE), + mOkButton->getFont()->getWidth(CAPTION_CONFIRMED)); + width = std::max(width, mOkButton->getFont()->getWidth(CAPTION_ACCEPT)); + width = std::max(width, mOkButton->getFont()->getWidth(CAPTION_ACCEPTED)); + + mOkButton->setWidth(8 + width); + + mMyItemContainer = new ItemContainer(mMyInventory.get()); + mMyItemContainer->addSelectionListener(this); + + ScrollArea *myScroll = new ScrollArea(mMyItemContainer); + myScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); + + mPartnerItemContainer = new ItemContainer(mPartnerInventory.get()); + mPartnerItemContainer->addSelectionListener(this); + + ScrollArea *partnerScroll = new ScrollArea(mPartnerItemContainer); + partnerScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); + + mMoneyLabel = new Label(strprintf(_("You get %s"), "")); + gcn::Label *mMoneyLabel2 = new Label(_("You give:")); + + mMoneyField = new TextField; + mMoneyField->setWidth(40); + mMoneyChangeButton = new Button(_("Change"), "money", this); + + place(1, 0, mMoneyLabel); + place(0, 1, myScroll).setPadding(3); + place(1, 1, partnerScroll).setPadding(3); + ContainerPlacer place; + place = getPlacer(0, 0); + place(0, 0, mMoneyLabel2); + place(1, 0, mMoneyField, 2); + place(3, 0, mMoneyChangeButton).setHAlign(LayoutCell::LEFT); + place = getPlacer(0, 2); + place(0, 0, mAddButton); + place(1, 0, mOkButton); + 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(); + + reset(); +} + +TradeWindow::~TradeWindow() +{ +} + +void TradeWindow::setMoney(int amount) +{ + if (amount < mGotMoney) + mMoneyLabel->setForegroundColor(Theme::getThemeColor(Theme::WARNING)); + mMoneyLabel->setForegroundColor(Theme::getThemeColor(Theme::TEXT)); + + mGotMoney = amount; + mMoneyLabel->setCaption(strprintf(_("You get %s"), + Units::formatCurrency(amount).c_str())); + mMoneyLabel->adjustSize(); +} + +void TradeWindow::addItem(int id, bool own, int quantity, + int refine, unsigned char color) +{ + if (own) + mMyInventory->addItem(id, quantity, refine, color); + else + mPartnerInventory->addItem(id, quantity, refine, color); +} + +void TradeWindow::addItem2(int id, bool own, int quantity, + int refine, unsigned char color, bool equipment) +{ + if (own) + mMyInventory->addItem(id, quantity, refine, color, equipment); + else + mPartnerInventory->addItem(id, quantity, refine, color, equipment); +} + +void TradeWindow::changeQuantity(int index, bool own, int quantity) +{ + if (own) + { + if (mMyInventory->getItem(index)) + mMyInventory->getItem(index)->setQuantity(quantity); + } + else + { + if (mPartnerInventory->getItem(index)) + mPartnerInventory->getItem(index)->setQuantity(quantity); + } +} + +void TradeWindow::increaseQuantity(int index, bool own, int quantity) +{ + if (own) + { + if (mMyInventory->getItem(index)) + mMyInventory->getItem(index)->increaseQuantity(quantity); + } + else + { + if (mPartnerInventory->getItem(index)) + mPartnerInventory->getItem(index)->increaseQuantity(quantity); + } +} + +void TradeWindow::reset() +{ + mMyInventory->clear(); + mPartnerInventory->clear(); + mOkOther = false; + mOkMe = false; + setMoney(0); + mMoneyField->setEnabled(true); + mMoneyField->setText(""); + mMoneyLabel->setForegroundColor(Theme::getThemeColor(Theme::TEXT)); + mAddButton->setEnabled(true); + mMoneyChangeButton->setEnabled(true); + mGotMoney = 0; + setStatus(PREPARING); +} + +void TradeWindow::receivedOk(bool own) +{ + if (own) + mOkMe = true; + else + mOkOther = true; + + if (mOkMe && mOkOther) + { + //mOkMe = false; + //mOkOther = false; + setStatus(ACCEPTING); + } +} + +void TradeWindow::tradeItem(Item *item, int quantity) +{ + Net::getTradeHandler()->addItem(item, quantity); +} + +void TradeWindow::valueChanged(const gcn::SelectionEvent &event) +{ + if (!mMyItemContainer || !mPartnerItemContainer) + return; + + /* If an item is selected in one container, make sure no item is selected + * in the other container. + */ + if (event.getSource() == mMyItemContainer && + mMyItemContainer->getSelectedItem()) + { + mPartnerItemContainer->selectNone(); + } + else if (mPartnerItemContainer->getSelectedItem()) + { + mMyItemContainer->selectNone(); + } +} + +void TradeWindow::setStatus(Status s) +{ + if (s == mStatus) + return; + mStatus = s; + + switch (s) + { + case PREPARING: + mOkButton->setCaption(CAPTION_PROPOSE); + mOkButton->setActionEventId("ok"); + break; + case PROPOSING: + mOkButton->setCaption(CAPTION_CONFIRMED); + mOkButton->setActionEventId(""); + break; + case ACCEPTING: + mOkButton->setCaption(CAPTION_ACCEPT); + mOkButton->setActionEventId("trade"); + break; + case ACCEPTED: + mOkButton->setCaption(CAPTION_ACCEPTED); + mOkButton->setActionEventId(""); + break; + default: + break; + } + + mOkButton->setEnabled((s != PROPOSING && s != ACCEPTED)); +} + +void TradeWindow::action(const gcn::ActionEvent &event) +{ + if (!inventoryWindow) + return; + + Item *item = inventoryWindow->getSelectedItem(); + + if (event.getId() == "add") + { + if (mStatus != PREPARING) + return; + + if (!inventoryWindow->isVisible()) + { + inventoryWindow->setVisible(true); + return; + } + + if (!item) + return; + + if (mMyInventory->getFreeSlot() == -1) + return; + + if (mMyInventory->contains(item)) + { + if (localChatTab) + { + localChatTab->chatLog(_("Failed adding item. You can not " + "overlap one kind of item on the window."), BY_SERVER); + } + return; + } + + // Choose amount of items to trade + ItemAmountWindow::showWindow(ItemAmountWindow::TradeAdd, this, item); + + setStatus(PREPARING); + } + else if (event.getId() == "cancel") + { + setVisible(false); + reset(); + PlayerInfo::setTrading(false); + + Net::getTradeHandler()->cancel(); + } + else if (event.getId() == "ok") + { + mMoneyField->setEnabled(false); + mAddButton->setEnabled(false); + mMoneyChangeButton->setEnabled(false); + receivedOk(true); + setStatus(PROPOSING); + Net::getTradeHandler()->confirm(); + } + else if (event.getId() == "trade") + { + receivedOk(true); + setStatus(ACCEPTED); + Net::getTradeHandler()->finish(); + } + else if (event.getId() == "money") + { + if (mStatus != PREPARING) + return; + + int v = atoi(mMoneyField->getText().c_str()); + int curMoney = PlayerInfo::getAttribute(MONEY); + if (v > curMoney) + { + if (localChatTab) + { + localChatTab->chatLog(_("You don't have enough money."), + BY_SERVER); + } + v = curMoney; + } + Net::getTradeHandler()->setMoney(v); + mMoneyField->setText(strprintf("%d", v)); + } +} + +void TradeWindow::close() +{ + Net::getTradeHandler()->cancel(); + clear(); +} + +void TradeWindow::clear() +{ + mAutoAddItem = 0; + mAutoAddToNick = ""; + mAutoMoney = 0; + mAutoAddAmount = 0; + mGotMoney = 0; + mMoneyLabel->setForegroundColor(Theme::getThemeColor(Theme::TEXT)); +} + +void TradeWindow::addAutoItem(std::string nick, Item* item, int amount) +{ + mAutoAddToNick = nick; + mAutoAddItem = item; + mAutoAddAmount = amount; +} + +void TradeWindow::addAutoMoney(std::string nick, int money) +{ + mAutoAddToNick = nick; + mAutoMoney = money; +} + +void TradeWindow::initTrade(std::string nick) +{ + if (!player_node) + return; + + if (!mAutoAddToNick.empty() && mAutoAddToNick == nick) + { + if (mAutoAddItem && mAutoAddItem->getQuantity()) + { + Inventory *inv = PlayerInfo::getInventory(); + if (inv) + { + Item *item = inv->findItem(mAutoAddItem->getId()); + if (item) + tradeItem(item, mAutoAddItem->getQuantity()); + } + } + if (mAutoMoney) + { + Net::getTradeHandler()->setMoney(mAutoMoney); + mMoneyField->setText(strprintf("%d", mAutoMoney)); + } + } + clear(); +} diff --git a/src/gui/tradewindow.h b/src/gui/tradewindow.h new file mode 100644 index 000000000..6865434b6 --- /dev/null +++ b/src/gui/tradewindow.h @@ -0,0 +1,172 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * 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. + * + * 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 this program. If not, see . + */ + +#ifndef TRADEWINDOW_H +#define TRADEWINDOW_H + +#include "guichanfwd.h" + +#include "gui/widgets/window.h" + +#include +#include + +#include + +class Inventory; +class Item; +class ItemContainer; +class ScrollArea; + +/** + * Trade dialog. + * + * \ingroup Interface + */ +class TradeWindow : public Window, gcn::ActionListener, gcn::SelectionListener +{ + public: + /** + * Constructor. + */ + TradeWindow(); + + /** + * Destructor. + */ + ~TradeWindow(); + + /** + * Displays expected money in the trade window. + */ + void setMoney(int quantity); + + /** + * Add an item to the trade window. + */ + void addItem(int id, bool own, int quantity, + int refine, unsigned char color); + + /** + * Reset both item containers + */ + void reset(); + + /** + * Add an item to the trade window. + */ + void addItem2(int id, bool own, int quantity, int refine, + unsigned char color, bool equipment); + + /** + * 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); + + /** + * Player received ok message from server + */ + void receivedOk(bool own); + + /** + * Send trade packet. + */ + void tradeItem(Item *item, int quantity); + + /** + * Updates the labels and makes sure only one item is selected in + * either my inventory or partner inventory. + */ + void valueChanged(const gcn::SelectionEvent &event); + + /** + * Called when receiving actions from the widgets. + */ + void action(const gcn::ActionEvent &event); + + /** + * Closes the Trade Window, as well as telling the server that the + * window has been closed. + */ + void close(); + + /** + * Clear auto trade items. + */ + void clear(); + + /** + * Add item what will be added to trade. + */ + void addAutoItem(std::string nick, Item* item, int amount); + + void addAutoMoney(std::string nick, int money); + + void initTrade(std::string nick); + + std::string getAutoTradeNick() + { return mAutoAddToNick; } + + private: + enum Status + { + PREPARING = 0, /**< Players are adding items. (1) */ + PROPOSING, /**< Local player has confirmed the trade. (1) */ + ACCEPTING, /**< Accepting the trade. (2) */ + ACCEPTED /**< Local player has accepted the trade. */ + }; + + /** + * Sets the current status of the trade. + */ + void setStatus(Status s); + + typedef const std::auto_ptr InventoryPtr; + InventoryPtr mMyInventory; + InventoryPtr mPartnerInventory; + + ItemContainer *mMyItemContainer; + ItemContainer *mPartnerItemContainer; + + gcn::Label *mMoneyLabel; + gcn::Button *mAddButton; + gcn::Button *mOkButton; + gcn::Button *mMoneyChangeButton; + gcn::TextField *mMoneyField; + + Status mStatus; + bool mOkOther, mOkMe; + Item* mAutoAddItem; + std::string mAutoAddToNick; + int mGotMoney; + int mAutoMoney; + int mAutoAddAmount; +}; + +extern TradeWindow *tradeWindow; + +#endif -- cgit v1.2.3-70-g09d2