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/gui/windows/npcselldialog.cpp | 120 ++++++++++++++++++++++++++++++++++++++ src/gui/windows/npcselldialog.h | 49 ++++++++++++++++ src/gui/windows/selldialog.cpp | 59 +------------------ src/gui/windows/selldialog.h | 11 ++-- 4 files changed, 179 insertions(+), 60 deletions(-) create mode 100644 src/gui/windows/npcselldialog.cpp create mode 100644 src/gui/windows/npcselldialog.h (limited to 'src/gui/windows') 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; -- cgit v1.2.3-70-g09d2