diff options
author | Björn Steinbrink <B.Steinbrink@gmx.de> | 2005-08-23 21:11:53 +0000 |
---|---|---|
committer | Björn Steinbrink <B.Steinbrink@gmx.de> | 2005-08-23 21:11:53 +0000 |
commit | edfe5e4525d16a477a1c88344e3321ccf23b605e (patch) | |
tree | cf6be7f81ef24ef6d56c0291fbcb80df4899ece8 /src/gui/sell.cpp | |
parent | d39602e433ce887e9f921e7c472a8e5af16fa7ec (diff) | |
download | mana-edfe5e4525d16a477a1c88344e3321ccf23b605e.tar.gz mana-edfe5e4525d16a477a1c88344e3321ccf23b605e.tar.bz2 mana-edfe5e4525d16a477a1c88344e3321ccf23b605e.tar.xz mana-edfe5e4525d16a477a1c88344e3321ccf23b605e.zip |
Some code cleanups in the sell dialog. Made the dialog accept items instead of looking them up in the inventory itself.
Diffstat (limited to 'src/gui/sell.cpp')
-rw-r--r-- | src/gui/sell.cpp | 154 |
1 files changed, 56 insertions, 98 deletions
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); } } |