From 2fb12d513bb7e10375893e1ae22da3538b20fa27 Mon Sep 17 00:00:00 2001 From: Björn Steinbrink Date: Tue, 31 Jan 2006 02:59:23 +0000 Subject: Created a ListModel for ShopItems. --- src/Makefile.am | 1 + src/gui/buy.cpp | 38 +++++++++++++++++--------------------- src/gui/buy.h | 15 ++++++++------- src/gui/sell.cpp | 42 ++++++++++++++++++++---------------------- src/gui/shop.h | 22 ++++++++++++++++++++++ 5 files changed, 68 insertions(+), 50 deletions(-) (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 131555f8..947c76b9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -87,6 +87,7 @@ tmw_SOURCES = graphic/spriteset.cpp \ gui/sell.h \ gui/setup.cpp \ gui/setup.h \ + gui/shop.cpp \ gui/shop.h \ gui/skill.cpp \ gui/skill.h \ diff --git a/src/gui/buy.cpp b/src/gui/buy.cpp index 65f2e525..71f502f2 100644 --- a/src/gui/buy.cpp +++ b/src/gui/buy.cpp @@ -46,7 +46,9 @@ BuyDialog::BuyDialog(Network *network): Window("Buy"), mNetwork(network), m_money(0), m_amountItems(0), m_maxItems(0) { - itemList = new ListBox(this); + mShopItems = new ShopItems; + + itemList = new ListBox(mShopItems); scrollArea = new ScrollArea(itemList); slider = new Slider(1.0); quantityLabel = new gcn::Label("0"); @@ -114,6 +116,11 @@ BuyDialog::BuyDialog(Network *network): setLocationRelativeTo(getParent()); } +BuyDialog::~BuyDialog() +{ + delete mShopItems; +} + void BuyDialog::setMoney(int amount) { m_money = amount; @@ -121,7 +128,7 @@ void BuyDialog::setMoney(int amount) void BuyDialog::reset() { - shopInventory.clear(); + mShopItems->clear(); m_money = 0; slider->setValue(0.0); m_amountItems = 0; @@ -149,7 +156,7 @@ void BuyDialog::addItem(short id, int price) item_shop.price = price; item_shop.id = id; - shopInventory.push_back(item_shop); + mShopItems->push_back(item_shop); itemList->adjustSize(); } @@ -173,7 +180,7 @@ void BuyDialog::action(const std::string& eventId) // If no item was selected, none can be bought, otherwise // calculate how many the player can afford m_maxItems = (itemList->getSelected() == -1) ? 0 : - m_money / shopInventory[selectedItem].price; + m_money / mShopItems->at(selectedItem).price; // When at least one item can be bought, enable the slider and the // increase button @@ -186,7 +193,7 @@ void BuyDialog::action(const std::string& eventId) } // The following actions require a valid selection - if (selectedItem < 0 || selectedItem >= int(shopInventory.size())) { + if (selectedItem < 0 || selectedItem >= int(mShopItems->size())) { return; } @@ -225,10 +232,10 @@ void BuyDialog::action(const std::string& eventId) outMsg.writeInt16(CMSG_NPC_BUY_REQUEST); outMsg.writeInt16(8); outMsg.writeInt16(m_amountItems); - outMsg.writeInt16(shopInventory[selectedItem].id); + outMsg.writeInt16(mShopItems->at(selectedItem).id); // update money ! - m_money -= m_amountItems * shopInventory[selectedItem].price; + m_money -= m_amountItems * mShopItems->at(selectedItem).price; // Update number of items that can be bought at max m_maxItems -= m_amountItems; @@ -258,7 +265,7 @@ void BuyDialog::action(const std::string& eventId) quantityLabel->adjustSize(); oss.str(""); - oss << "Price : " << m_amountItems * shopInventory[selectedItem].price << " GP"; + oss << "Price : " << m_amountItems * mShopItems->at(selectedItem).price << " GP"; moneyLabel->setCaption(oss.str()); moneyLabel->adjustSize(); } @@ -268,23 +275,12 @@ void BuyDialog::mouseClick(int x, int y, int button, int count) { Window::mouseClick(x, y, button, count); -// shopInventory[selectedItem]; int selectedItem = itemList->getSelected(); if (selectedItem > -1) { itemDescLabel->setCaption("Description: " + - itemDb->getItemInfo(shopInventory[selectedItem].id)->getDescription()); + itemDb->getItemInfo(mShopItems->at(selectedItem).id)->getDescription()); itemEffectLabel->setCaption("Effect: " + - itemDb->getItemInfo(shopInventory[selectedItem].id)->getEffect()); + itemDb->getItemInfo(mShopItems->at(selectedItem).id)->getEffect()); } } - -int BuyDialog::getNumberOfElements() -{ - return shopInventory.size(); -} - -std::string BuyDialog::getElementAt(int i) -{ - return shopInventory[i].name; -} diff --git a/src/gui/buy.h b/src/gui/buy.h index 6a1c9829..bf84d24e 100644 --- a/src/gui/buy.h +++ b/src/gui/buy.h @@ -24,25 +24,21 @@ #ifndef _TMW_BUY_H #define _TMW_BUY_H -#include - #include -#include -#include "shop.h" #include "window.h" #include "../guichanfwd.h" class Network; +class ShopItems; /** * The buy dialog. * * \ingroup Interface */ -class BuyDialog : public Window, public gcn::ActionListener, - public gcn::ListModel +class BuyDialog : public Window, public gcn::ActionListener { public: /** @@ -52,6 +48,11 @@ class BuyDialog : public Window, public gcn::ActionListener, */ BuyDialog(Network *network); + /** + * Destructor + */ + ~BuyDialog(); + /** * Resets the dialog, clearing shop inventory. */ @@ -101,7 +102,7 @@ class BuyDialog : public Window, public gcn::ActionListener, gcn::Label *quantityLabel; gcn::Slider *slider; - std::vector shopInventory; + ShopItems *mShopItems; int m_money; int m_amountItems; diff --git a/src/gui/sell.cpp b/src/gui/sell.cpp index f89055b4..89aec044 100644 --- a/src/gui/sell.cpp +++ b/src/gui/sell.cpp @@ -49,7 +49,9 @@ SellDialog::SellDialog(Network *network): mNetwork(network), m_maxItems(0), m_amountItems(0) { - itemList = new ListBox(this); + mShopItems = new ShopItems(); + + itemList = new ListBox(mShopItems); scrollArea = new ScrollArea(itemList); slider = new Slider(1.0); quantityLabel = new gcn::Label("0"); @@ -117,9 +119,14 @@ SellDialog::SellDialog(Network *network): setLocationRelativeTo(getParent()); } +SellDialog::~SellDialog() +{ + delete mShopItems; +} + void SellDialog::reset() { - shopInventory.clear(); + mShopItems->clear(); slider->setValue(0.0); m_amountItems = 0; @@ -152,7 +159,7 @@ void SellDialog::addItem(Item *item, int price) item_shop.id = item->getId(); item_shop.quantity = item->getQuantity(); - shopInventory.push_back(item_shop); + mShopItems->push_back(item_shop); itemList->adjustSize(); } @@ -174,7 +181,7 @@ void SellDialog::action(const std::string& eventId) if (selectedItem > -1) { slider->setEnabled(true); increaseButton->setEnabled(true); - m_maxItems = shopInventory[selectedItem].quantity; + m_maxItems = mShopItems->at(selectedItem).quantity; } else { slider->setEnabled(false); increaseButton->setEnabled(false); @@ -187,7 +194,7 @@ void SellDialog::action(const std::string& eventId) } // The following actions require a valid item selection - if (selectedItem == -1 || selectedItem >= int(shopInventory.size())) { + if (selectedItem == -1 || selectedItem >= int(mShopItems->size())) { return; } @@ -220,7 +227,7 @@ void SellDialog::action(const std::string& eventId) MessageOut outMsg(mNetwork); outMsg.writeInt16(CMSG_NPC_SELL_REQUEST); outMsg.writeInt16(8); - outMsg.writeInt16(shopInventory[selectedItem].index); + outMsg.writeInt16(mShopItems->at(selectedItem).index); outMsg.writeInt16(m_amountItems); m_maxItems -= m_amountItems; @@ -231,10 +238,12 @@ void SellDialog::action(const std::string& eventId) // All were sold if (!m_maxItems) { itemList->setSelected(-1); - shopInventory.erase(shopInventory.begin() + selectedItem); + mShopItems->erase(mShopItems->begin() + selectedItem); } - updateButtonsAndLabels = true; + // Update only when there are items left, the entry doesn't exist + // otherwise and can't be updated + updateButtonsAndLabels = bool(m_maxItems); } // If anything changed, we need to update the buttons and labels @@ -246,7 +255,7 @@ void SellDialog::action(const std::string& eventId) quantityLabel->setCaption(oss.str()); quantityLabel->adjustSize(); oss.str(""); - oss << "Price: " << m_amountItems * shopInventory[selectedItem].price; + oss << "Price: " << m_amountItems * mShopItems->at(selectedItem).price; moneyLabel->setCaption(oss.str()); moneyLabel->adjustSize(); @@ -261,23 +270,12 @@ void SellDialog::mouseClick(int x, int y, int button, int count) { Window::mouseClick(x, y, button, count); -// shopInventory[selectedItem]; int selectedItem = itemList->getSelected(); if (selectedItem > -1) { itemDescLabel->setCaption("Description: " + - itemDb->getItemInfo(shopInventory[selectedItem].id)->getDescription()); + itemDb->getItemInfo(mShopItems->at(selectedItem).id)->getDescription()); itemEffectLabel->setCaption("Effect: " + - itemDb->getItemInfo(shopInventory[selectedItem].id)->getEffect()); + itemDb->getItemInfo(mShopItems->at(selectedItem).id)->getEffect()); } } - -int SellDialog::getNumberOfElements() -{ - return shopInventory.size(); -} - -std::string SellDialog::getElementAt(int i) -{ - return shopInventory[i].name; -} diff --git a/src/gui/shop.h b/src/gui/shop.h index e256f869..fb1f33fd 100644 --- a/src/gui/shop.h +++ b/src/gui/shop.h @@ -25,6 +25,9 @@ #define _SHOP_H #include +#include + +#include struct ITEM_SHOP { std::string name; @@ -34,4 +37,23 @@ struct ITEM_SHOP { int quantity; }; +class ShopItems : public std::vector, public gcn::ListModel +{ + public: + /** + * Destructor + */ + virtual ~ShopItems() {}; + + /** + * Returns the number of items in the shop. + */ + int getNumberOfElements(); + + /** + * Returns the name of item number i in the shop. + */ + std::string getElementAt(int i); +}; + #endif -- cgit v1.2.3-70-g09d2