diff options
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | src/game.cpp | 6 | ||||
-rw-r--r-- | src/gui/sell.cpp | 154 | ||||
-rw-r--r-- | src/gui/sell.h | 5 |
4 files changed, 67 insertions, 102 deletions
@@ -1,5 +1,9 @@ 2005-08-23 Björn Steinbrink <B.Steinbrink@gmx.de> + * src/game.cpp, src/gui/sell.cpp, src/gui/sell.h: Made the sell dialog + accepts Items instead of looking them up in the inventory. + * src/gui/sell.cpp: Some code cleanups, simplifications and removal of + duplicate code. * src/gui/buy.cpp, src/gui/sell.cpp, src/gui/shop.h: Avoid possible buffer overflows by using a std::string instead of char[30]. * src/gui/buy.cpp: Some code cleanups, simplifications and removal of diff --git a/src/game.cpp b/src/game.cpp index f4241d0d..3dd254ef 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1606,8 +1606,10 @@ void do_parse() sellDialog->reset(); sellDialog->setVisible(true); for (int k = 0; k < n_items; k++) { - sellDialog->addItem( - RFIFOW(4 + 10 * k), RFIFOL(4 + 10 * k + 2)); + Item *item = inventory->getItem(RFIFOW(4 + 10 * k)); + if (item) { + sellDialog->addItem(item, RFIFOL(4 + 10 * k + 2)); + } } } else { diff --git a/src/gui/sell.cpp b/src/gui/sell.cpp index 8140c013..ac40b22c 100644 --- a/src/gui/sell.cpp +++ b/src/gui/sell.cpp @@ -35,7 +35,6 @@ #include "slider.h" #include "../game.h" -#include "../inventory.h" #include "../item.h" #include "../resources/iteminfo.h" @@ -44,7 +43,8 @@ SellDialog::SellDialog(): - Window("Sell") + Window("Sell"), + m_maxItems(0), m_amountItems(0) { itemList = new ListBox(this); scrollArea = new ScrollArea(itemList); @@ -102,9 +102,6 @@ SellDialog::SellDialog(): add(quitButton); setLocationRelativeTo(getParent()); - - m_maxItems = 0; - m_amountItems = 0; } SellDialog::~SellDialog() @@ -132,10 +129,8 @@ void SellDialog::reset() decreaseButton->setEnabled(false); } -void SellDialog::addItem(short index, int price) +void SellDialog::addItem(Item *item, int price) { - Item *item = inventory->getItem(index); - if (!item) return; @@ -146,7 +141,7 @@ void SellDialog::addItem(short index, int price) item_shop.name = ss.str(); item_shop.price = price; - item_shop.index = index; + item_shop.index = item->getInvIndex(); item_shop.id = item->getId();; item_shop.quantity = item->getQuantity(); @@ -157,101 +152,61 @@ void SellDialog::addItem(short index, int price) void SellDialog::action(const std::string& eventId) { int selectedItem = itemList->getSelected(); - std::stringstream oss; - if (eventId == "item") - { - if (selectedItem > -1) - { + if (eventId == "item") { + m_amountItems = 0; + slider->setValue(0); + decreaseButton->setEnabled(false); + sellButton->setEnabled(false); + + quantityLabel->setCaption("0"); + quantityLabel->adjustSize(); + + if (selectedItem > -1) { slider->setEnabled(true); increaseButton->setEnabled(true); - decreaseButton->setEnabled(false); - m_maxItems = shopInventory[selectedItem].quantity; - m_amountItems = 0; - slider->setValue(0); - oss << m_amountItems; - quantityLabel->setCaption(oss.str()); - quantityLabel->adjustSize(); - } - else - { - slider->setValue(0); + } else { slider->setEnabled(false); increaseButton->setEnabled(false); - decreaseButton->setEnabled(false); - sellButton->setEnabled(false); - m_amountItems = 0; - } + } } - else if (eventId == "slider" && selectedItem > -1) + else if (eventId == "quit") { - m_amountItems = (int)(slider->getValue() * m_maxItems); + setVisible(false); + current_npc = 0; + } - oss << m_amountItems; - quantityLabel->setCaption(oss.str()); - quantityLabel->adjustSize(); + // The following actions require a valid item selection + if (selectedItem == -1 || selectedItem >= int(shopInventory.size())) { + return; + } - if (m_amountItems > 0) - { - sellButton->setEnabled(true); - decreaseButton->setEnabled(true); - } - else - { - sellButton->setEnabled(false); - decreaseButton->setEnabled(false); - } + bool updateButtonsAndLabels = false; - if (m_amountItems == m_maxItems) - { - increaseButton->setEnabled(false); - } - else - { - increaseButton->setEnabled(true); - } + if (eventId == "slider") { + m_amountItems = (int)(slider->getValue() * m_maxItems); + + updateButtonsAndLabels = true; } - else if (eventId == "+" && selectedItem > -1) - { + else if (eventId == "+") { assert(m_amountItems < m_maxItems); m_amountItems++; slider->setValue(double(m_amountItems)/double(m_maxItems)); - decreaseButton->setEnabled(true); - sellButton->setEnabled(true); - if (m_amountItems == m_maxItems) - { - increaseButton->setEnabled(false); - } - - oss << m_amountItems; - quantityLabel->setCaption(oss.str()); - quantityLabel->adjustSize(); + updateButtonsAndLabels = true; } - else if (eventId == "-" && selectedItem > -1) - { + else if (eventId == "-") { assert(m_amountItems > 0); m_amountItems--; slider->setValue(double(m_amountItems)/double(m_maxItems)); - increaseButton->setEnabled(true); - if (m_amountItems == 0) - { - decreaseButton->setEnabled(false); - sellButton->setEnabled(false); - } - - oss << m_amountItems; - quantityLabel->setCaption(oss.str()); - quantityLabel->adjustSize(); + updateButtonsAndLabels = true; } - else if (eventId == "sell" && selectedItem > -1) - { + else if (eventId == "sell") { // Attempt sell assert(m_amountItems > 0 && m_amountItems <= m_maxItems); - assert(selectedItem >= 0 && selectedItem < int(shopInventory.size())); WFIFOW(0) = net_w_value(0x00c9); WFIFOW(2) = net_w_value(8); @@ -259,30 +214,33 @@ void SellDialog::action(const std::string& eventId) WFIFOW(6) = net_w_value(m_amountItems); WFIFOSET(8); - if (m_amountItems == m_maxItems) - { - slider->setEnabled(false); - increaseButton->setEnabled(false); + m_maxItems -= m_amountItems; + m_amountItems = 0; + slider->setValue(0); + slider->setEnabled(m_maxItems != 0); + + // All were sold + if (!m_maxItems) { itemList->setSelected(-1); - shopInventory.erase(shopInventory.begin() += selectedItem); - } - else - { - m_maxItems = shopInventory[selectedItem].quantity - m_amountItems; + shopInventory.erase(shopInventory.begin() + selectedItem); } - decreaseButton->setEnabled(false); - sellButton->setEnabled(false); + updateButtonsAndLabels = true; + } - m_amountItems = 0; - slider->setValue(0); - quantityLabel->setCaption("O"); + // If anything changed, we need to update the buttons and labels + if (updateButtonsAndLabels) { + std::stringstream oss; + + // Update labels + oss << m_amountItems; + quantityLabel->setCaption(oss.str()); quantityLabel->adjustSize(); - } - else if (eventId == "quit") - { - setVisible(false); - current_npc = 0; + + // Update Buttons + sellButton->setEnabled(m_amountItems > 0); + decreaseButton->setEnabled(m_amountItems > 0); + increaseButton->setEnabled(m_amountItems < m_maxItems); } } diff --git a/src/gui/sell.h b/src/gui/sell.h index 9d1325a0..5cf5479e 100644 --- a/src/gui/sell.h +++ b/src/gui/sell.h @@ -33,8 +33,9 @@ #include "../guichanfwd.h" -struct ITEM_SHOP; +class Item; +struct ITEM_SHOP; /** * The sell dialog. @@ -65,7 +66,7 @@ class SellDialog : public Window, public gcn::ActionListener, /** * Adds an item to the inventory. */ - void addItem(short id, int price); + void addItem(Item *item, int price); /** * Called when receiving actions from the widgets. |