From d34decaf8b2b4cbffca970e9059e1a2b4f463055 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Mon, 22 Sep 2014 14:04:40 +0300 Subject: Move npc sell code from SellDialog into NpcSellDialog. --- src/CMakeLists.txt | 2 + src/Makefile.am | 2 + src/gui/windows/npcselldialog.cpp | 120 ++++++++++++++++++++++++++++++++++++++ src/gui/windows/npcselldialog.h | 49 ++++++++++++++++ src/gui/windows/selldialog.cpp | 59 +------------------ src/gui/windows/selldialog.h | 11 ++-- src/net/ea/buysellhandler.cpp | 4 +- 7 files changed, 185 insertions(+), 62 deletions(-) create mode 100644 src/gui/windows/npcselldialog.cpp create mode 100644 src/gui/windows/npcselldialog.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7a8591193..f8f63f26c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -382,6 +382,8 @@ SET(SRCS gui/windows/npcdialog.h gui/windows/npcpostdialog.cpp gui/windows/npcpostdialog.h + gui/windows/npcselldialog.cpp + gui/windows/npcselldialog.h gui/windows/okdialog.cpp gui/windows/okdialog.h gui/windows/outfitwindow.cpp diff --git a/src/Makefile.am b/src/Makefile.am index 55b944f67..693901ce3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -504,6 +504,8 @@ manaplus_SOURCES += gui/widgets/avatarlistbox.cpp \ gui/windows/npcdialog.h \ gui/windows/npcpostdialog.cpp \ gui/windows/npcpostdialog.h \ + gui/windows/npcselldialog.cpp \ + gui/windows/npcselldialog.h \ gui/windows/okdialog.cpp \ gui/windows/okdialog.h \ gui/windows/outfitwindow.cpp \ diff --git a/src/gui/windows/npcselldialog.cpp b/src/gui/windows/npcselldialog.cpp new file mode 100644 index 000000000..b975bb6d1 --- /dev/null +++ b/src/gui/windows/npcselldialog.cpp @@ -0,0 +1,120 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2014 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/windows/npcselldialog.h" + +#include "shopitem.h" +#include "units.h" + +#include "being/playerinfo.h" + +#include "gui/windows/confirmdialog.h" +#include "gui/windows/setupwindow.h" +#include "gui/windows/tradewindow.h" + +#include "gui/models/shopitems.h" + +#include "gui/widgets/button.h" +#include "gui/widgets/containerplacer.h" +#include "gui/widgets/label.h" +#include "gui/widgets/layout.h" +#include "gui/widgets/layouttype.h" +#include "gui/widgets/scrollarea.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/delete2.h" +#include "utils/gettext.h" + +#include "debug.h" + +NpcSellDialog::NpcSellDialog(const int npcId) : + SellDialog(npcId) +{ +} + +void NpcSellDialog::sellAction(const ActionEvent &event) +{ + if (mAmountItems <= 0 || mAmountItems > mMaxItems) + return; + + const std::string &eventId = event.getId(); + const int selectedItem = mShopItemList->getSelected(); + ShopItem *const item = mShopItems->at(selectedItem); + if (!item || PlayerInfo::isItemProtected(item->getId())) + return; + if (eventId == "presell") + { + const ItemInfo &info = ItemDB::get(item->getId()); + if (info.isProtected()) + { + ConfirmDialog *const dialog = new ConfirmDialog( + // TRANSLATORS: sell confirmation header + _("sell item"), + // TRANSLATORS: sell confirmation message + strprintf(_("Do you really want to sell %s?"), + info.getName().c_str()), SOUND_REQUEST, false, true); + dialog->postInit(); + dialog->addActionListener(this); + return; + } + } + // Attempt sell + 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. + const int itemIndex = item->getCurrentInvIndex(); + const int sellCount = item->sellCurrentDuplicate(mAmountItems); + npcHandler->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); + + Rect scroll; + scroll.y = mShopItemList->getRowHeight() * (selectedItem + 1); + scroll.height = mShopItemList->getRowHeight(); + mShopItemList->showPart(scroll); + } +} diff --git a/src/gui/windows/npcselldialog.h b/src/gui/windows/npcselldialog.h new file mode 100644 index 000000000..a79220a85 --- /dev/null +++ b/src/gui/windows/npcselldialog.h @@ -0,0 +1,49 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2014 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 GUI_WINDOWS_NPCSELLDIALOG_H +#define GUI_WINDOWS_NPCSELLDIALOG_H + +#include "gui/windows/selldialog.h" + +/** + * The sell dialog. + * + * \ingroup Interface + */ +class NpcSellDialog final : public SellDialog +{ + public: + /** + * Constructor. + * + * @see Window::Window + */ + explicit NpcSellDialog(const int npcId); + + A_DELETE_COPY(NpcSellDialog) + + protected: + void sellAction(const ActionEvent &event) override final; +}; + +#endif // GUI_WINDOWS_NPCSELLDIALOG_H diff --git a/src/gui/windows/selldialog.cpp b/src/gui/windows/selldialog.cpp index fc9ec4b66..cd8c03bdc 100644 --- a/src/gui/windows/selldialog.cpp +++ b/src/gui/windows/selldialog.cpp @@ -247,60 +247,7 @@ void SellDialog::action(const ActionEvent &event) { if (mNpcId != -1) { - ShopItem *const item = mShopItems->at(selectedItem); - if (PlayerInfo::isItemProtected(item->getId())) - return; - if (eventId == "presell") - { - const ItemInfo &info = ItemDB::get(item->getId()); - if (info.isProtected()) - { - ConfirmDialog *const dialog = new ConfirmDialog( - // TRANSLATORS: sell confirmation header - _("sell item"), - // TRANSLATORS: sell confirmation message - strprintf(_("Do you really want to sell %s?"), - info.getName().c_str()), SOUND_REQUEST, false, true); - dialog->postInit(); - dialog->addActionListener(this); - return; - } - } - // Attempt sell - 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. - const int itemIndex = item->getCurrentInvIndex(); - const int sellCount = item->sellCurrentDuplicate(mAmountItems); - npcHandler->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); - - Rect scroll; - scroll.y = mShopItemList->getRowHeight() * (selectedItem + 1); - scroll.height = mShopItemList->getRowHeight(); - mShopItemList->showPart(scroll); - } + sellAction(event); } else { @@ -368,8 +315,8 @@ void SellDialog::updateButtonsAndLabels() mQuantityLabel->setCaption(strprintf("%d / %d", mAmountItems, mMaxItems)); // TRANSLATORS: sell dialog label mMoneyLabel->setCaption(strprintf(_("Price: %s / Total: %s"), - Units::formatCurrency(income).c_str(), - Units::formatCurrency(mPlayerMoney + income).c_str())); + Units::formatCurrency(income).c_str(), + Units::formatCurrency(mPlayerMoney + income).c_str())); if (item) item->update(); } diff --git a/src/gui/windows/selldialog.h b/src/gui/windows/selldialog.h index 0cfffbf38..81aaabbb2 100644 --- a/src/gui/windows/selldialog.h +++ b/src/gui/windows/selldialog.h @@ -41,9 +41,9 @@ class Slider; * * \ingroup Interface */ -class SellDialog final : public Window, - private ActionListener, - private SelectionListener +class SellDialog notfinal : public Window, + public ActionListener, + private SelectionListener { public: /** @@ -113,7 +113,7 @@ class SellDialog final : public Window, */ static void closeAll(); - private: + protected: typedef std::list DialogList; static DialogList instances; @@ -122,6 +122,9 @@ class SellDialog final : public Window, */ void updateButtonsAndLabels(); + virtual void sellAction(const ActionEvent &event A_UNUSED) + { } + std::string mNick; Button *mSellButton; diff --git a/src/net/ea/buysellhandler.cpp b/src/net/ea/buysellhandler.cpp index 6bdc33305..fe2f7ffe0 100644 --- a/src/net/ea/buysellhandler.cpp +++ b/src/net/ea/buysellhandler.cpp @@ -31,7 +31,7 @@ #include "gui/windows/chatwindow.h" #include "gui/windows/buyselldialog.h" -#include "gui/windows/selldialog.h" +#include "gui/windows/npcselldialog.h" #include "gui/windows/buydialog.h" #include "gui/windows/shopwindow.h" @@ -146,7 +146,7 @@ void BuySellHandler::processNpcSell(Net::MessageIn &msg) const const int n_items = (msg.getLength() - 4) / 10; if (n_items > 0) { - SellDialog *const dialog = new SellDialog(mNpcId); + SellDialog *const dialog = new NpcSellDialog(mNpcId); dialog->setMoney(PlayerInfo::getAttribute(Attributes::MONEY)); for (int k = 0; k < n_items; k++) -- cgit v1.2.3-60-g2f50