From bf6cb46d9b06b06470efd5ad3ebae7e274f6906f Mon Sep 17 00:00:00 2001 From: Jared Adams Date: Wed, 24 Mar 2010 23:10:51 -0600 Subject: Eliminate the logic methods from InventoryWindow and StorageWindow --- src/gui/inventorywindow.cpp | 104 +++++++++++++++++++------------------------- src/gui/inventorywindow.h | 26 +++++------ src/gui/storagewindow.cpp | 32 +++++--------- src/gui/storagewindow.h | 10 ++--- 4 files changed, 71 insertions(+), 101 deletions(-) (limited to 'src/gui') diff --git a/src/gui/inventorywindow.cpp b/src/gui/inventorywindow.cpp index 701558a9..6291d524 100644 --- a/src/gui/inventorywindow.cpp +++ b/src/gui/inventorywindow.cpp @@ -53,9 +53,8 @@ #include -InventoryWindow::InventoryWindow(int invSize): +InventoryWindow::InventoryWindow(): Window(_("Inventory")), - mMaxSlots(invSize), mSplit(false), mItemDesc(false) { @@ -90,10 +89,6 @@ InventoryWindow::InventoryWindow(int invSize): gcn::ScrollArea *invenScroll = new ScrollArea(mItems); invenScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); - mTotalWeight = -1; - mMaxWeight = -1; - mUsedSlots = -1; - mSlotsLabel = new Label(_("Slots:")); mWeightLabel = new Label(_("Weight:")); @@ -113,45 +108,17 @@ InventoryWindow::InventoryWindow(int invSize): Layout &layout = getLayout(); layout.setRowHeight(1, Layout::AUTO_SET); + player_node->getInventory()->addInventoyListener(this); + loadWindowState(); + updateWeight(); + slotsChanged(player_node->getInventory()); } InventoryWindow::~InventoryWindow() { } -void InventoryWindow::logic() -{ - if (!isVisible()) - return; - - Window::logic(); - - // It would be nicer if this update could be event based, needs some - // redesign of InventoryWindow and ItemContainer probably. - updateButtons(); - - const int usedSlots = player_node->getInventory()->getNumberOfSlotsUsed(); - - if (mMaxWeight != player_node->getMaxWeight() || - mTotalWeight != player_node->getTotalWeight() || - mUsedSlots != usedSlots) - { - mTotalWeight = player_node->getTotalWeight(); - mMaxWeight = player_node->getMaxWeight(); - mUsedSlots = usedSlots; - - // Adjust progress bars - mSlotsBar->setProgress((float) mUsedSlots / mMaxSlots); - mWeightBar->setProgress((float) mTotalWeight / mMaxWeight); - - mSlotsBar->setText(strprintf("%d/%d", mUsedSlots, mMaxSlots)); - mWeightBar->setText(strprintf("%s/%s", - Units::formatWeight(mTotalWeight).c_str(), - Units::formatWeight(mMaxWeight).c_str())); - } -} - void InventoryWindow::action(const gcn::ActionEvent &event) { if (event.getId() == "outfit") @@ -255,39 +222,29 @@ void InventoryWindow::keyReleased(gcn::KeyEvent &event) void InventoryWindow::valueChanged(const gcn::SelectionEvent &event) { - if (mSplit && Net::getInventoryHandler()->canSplit(mItems->getSelectedItem())) - { - Item *item = mItems->getSelectedItem(); + Item *item = mItems->getSelectedItem(); - if (item) - ItemAmountWindow::showWindow(ItemAmountWindow::ItemSplit, this, item, - (item->getQuantity() - 1)); + if (mSplit && Net::getInventoryHandler()-> + canSplit(mItems->getSelectedItem()) && item) + { + ItemAmountWindow::showWindow(ItemAmountWindow::ItemSplit, this, item, + (item->getQuantity() - 1)); } -} - -void InventoryWindow::setSplitAllowed(bool allowed) -{ - mSplitButton->setVisible(allowed); -} - -void InventoryWindow::updateButtons() -{ - const Item *selectedItem = mItems->getSelectedItem(); - - if (!selectedItem || selectedItem->getQuantity() == 0) + if (!item || item->getQuantity() == 0) { mUseButton->setEnabled(false); mDropButton->setEnabled(false); + return; } mUseButton->setEnabled(true); mDropButton->setEnabled(true); - if (selectedItem->isEquipment()) + if (item->isEquipment()) { - if (selectedItem->isEquipped()) + if (item->isEquipped()) mUseButton->setCaption(_("Unequip")); else mUseButton->setCaption(_("Equip")); @@ -297,13 +254,40 @@ void InventoryWindow::updateButtons() mUseButton->setCaption(_("Use")); } - if (selectedItem->getQuantity() > 1) + if (item->getQuantity() > 1) mDropButton->setCaption(_("Drop...")); else mDropButton->setCaption(_("Drop")); - if (Net::getInventoryHandler()->canSplit(selectedItem)) + if (Net::getInventoryHandler()->canSplit(item)) mSplitButton->setEnabled(true); else mSplitButton->setEnabled(false); } + + +void InventoryWindow::setSplitAllowed(bool allowed) +{ + mSplitButton->setVisible(allowed); +} + +void InventoryWindow::updateWeight() +{ + int total = player_node->getTotalWeight(); + int max = player_node->getMaxWeight(); + + // Adjust progress bar + mWeightBar->setProgress((float) total / max); + mWeightBar->setText(strprintf("%s/%s", Units::formatWeight(total).c_str(), + Units::formatWeight(max).c_str())); +} + +void InventoryWindow::slotsChanged(Inventory* inventory) +{ + const int usedSlots = player_node->getInventory()->getNumberOfSlotsUsed(); + const int maxSlots = player_node->getInventory()->getSize(); + + mSlotsBar->setProgress((float) usedSlots / maxSlots); + + mSlotsBar->setText(strprintf("%d/%d", usedSlots, maxSlots)); +} diff --git a/src/gui/inventorywindow.h b/src/gui/inventorywindow.h index fdc5e55a..cfea130f 100644 --- a/src/gui/inventorywindow.h +++ b/src/gui/inventorywindow.h @@ -47,25 +47,20 @@ class TextBox; class InventoryWindow : public Window, public gcn::ActionListener, public gcn::KeyListener, - public gcn::SelectionListener + public gcn::SelectionListener, + public InventoryListener { public: /** * Constructor. */ - InventoryWindow(int invSize = Net::getInventoryHandler() - ->getSize(Net::InventoryHandler::INVENTORY)); + InventoryWindow(); /** * Destructor. */ ~InventoryWindow(); - /** - * Logic (updates buttons and weight information). - */ - void logic(); - /** * Called when receiving actions from the widgets. */ @@ -100,17 +95,20 @@ class InventoryWindow : public Window, * Sets whether the split button should be shown. */ void setSplitAllowed(bool allowed); + + /** + * Updates the weight bar. + */ + void updateWeight(); - private: - void updateButtons(); /**< Updates button states. */ + void slotsChanged(Inventory* inventory); + private: ItemContainer *mItems; std::string mWeight; std::string mSlots; - int mUsedSlots; - int mTotalWeight; - int mMaxWeight; + gcn::Button *mUseButton; gcn::Button *mDropButton; gcn::Button *mSplitButton; @@ -121,8 +119,6 @@ class InventoryWindow : public Window, ProgressBar *mWeightBar; ProgressBar *mSlotsBar; - int mMaxSlots; - bool mSplit; bool mItemDesc; }; diff --git a/src/gui/storagewindow.cpp b/src/gui/storagewindow.cpp index 579725c4..c07653b1 100644 --- a/src/gui/storagewindow.cpp +++ b/src/gui/storagewindow.cpp @@ -104,6 +104,9 @@ StorageWindow::StorageWindow(Inventory *inventory): instances.push_back(this); setVisible(true); + + mInventory->addInventoyListener(this); + slotsChanged(mInventory); } StorageWindow::~StorageWindow() @@ -111,26 +114,6 @@ StorageWindow::~StorageWindow() instances.remove(this); } -void StorageWindow::logic() -{ - if (!isVisible()) - return; - - Window::logic(); - - const int usedSlots = mInventory->getNumberOfSlotsUsed(); - - if (mUsedSlots != usedSlots) - { - mUsedSlots = usedSlots; - - mSlotsBar->setProgress((float) mUsedSlots / mInventory->getSize()); - - mSlotsBar->setText(strprintf("%d/%d", mUsedSlots, - mInventory->getSize())); - } -} - void StorageWindow::action(const gcn::ActionEvent &event) { if (event.getId() == "store") @@ -202,6 +185,15 @@ Item *StorageWindow::getSelectedItem() const return mItems->getSelectedItem(); } +void StorageWindow::slotsChanged(Inventory* inventory) +{ + const int usedSlots = mInventory->getNumberOfSlotsUsed(); + + mSlotsBar->setProgress((float) usedSlots / mInventory->getSize()); + + mSlotsBar->setText(strprintf("%d/%d", usedSlots, mInventory->getSize())); +} + void StorageWindow::addStore(Item *item, int amount) { Net::getInventoryHandler()->moveItem(Net::InventoryHandler::INVENTORY, diff --git a/src/gui/storagewindow.h b/src/gui/storagewindow.h index 046b7613..8e09768d 100644 --- a/src/gui/storagewindow.h +++ b/src/gui/storagewindow.h @@ -43,7 +43,8 @@ class TextBox; * \ingroup Interface */ class StorageWindow : public Window, gcn::ActionListener, - gcn::SelectionListener + gcn::SelectionListener, + public InventoryListener { public: /** @@ -56,11 +57,6 @@ class StorageWindow : public Window, gcn::ActionListener, */ ~StorageWindow(); - /** - * Logic (updates buttons and weight information). - */ - void logic(); - /** * Called when receiving actions from the widgets. */ @@ -79,6 +75,8 @@ class StorageWindow : public Window, gcn::ActionListener, */ void close(); + void slotsChanged(Inventory* inventory); + /** * Add the specified ammount of the specified item to storage */ -- cgit v1.2.3-70-g09d2