From edfe5e4525d16a477a1c88344e3321ccf23b605e Mon Sep 17 00:00:00 2001 From: Björn Steinbrink Date: Tue, 23 Aug 2005 21:11:53 +0000 Subject: Some code cleanups in the sell dialog. Made the dialog accept items instead of looking them up in the inventory itself. --- src/game.cpp | 6 ++- src/gui/sell.cpp | 154 ++++++++++++++++++++----------------------------------- src/gui/sell.h | 5 +- 3 files changed, 63 insertions(+), 102 deletions(-) (limited to 'src') 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. -- cgit v1.2.3-70-g09d2