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/inventory.cpp | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) (limited to 'src/inventory.cpp') diff --git a/src/inventory.cpp b/src/inventory.cpp index c9fb4fd7..8a6e79bb 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -34,7 +34,8 @@ struct SlotUsed : public std::unary_function }; Inventory::Inventory(int size): - mSize(size) + mSize(size), + mUsed(0) { mItems = new Item*[mSize]; std::fill_n(mItems, mSize, (Item*) 0); @@ -83,6 +84,8 @@ void Inventory::setItem(int index, int id, int quantity, bool equipment) Item *item = new Item(id, quantity, equipment); item->setInvIndex(index); mItems[index] = item; + mUsed++; + distributeSlotsChangedEvent(); } else if (id > 0) { @@ -113,6 +116,11 @@ void Inventory::removeItemAt(int index) { delete mItems[index]; mItems[index] = 0; + mUsed--; + if (mUsed < 0) // Already at 0, no need to distribute event + mUsed = 0; + else + distributeSlotsChangedEvent(); } bool Inventory::contains(Item *item) const @@ -131,11 +139,6 @@ int Inventory::getFreeSlot() const return (i == mItems + mSize) ? -1 : (i - mItems); } -int Inventory::getNumberOfSlotsUsed() const -{ - return count_if(mItems, mItems + mSize, SlotUsed()); -} - int Inventory::getLastUsedSlot() const { for (int i = mSize - 1; i >= 0; i--) @@ -144,3 +147,23 @@ int Inventory::getLastUsedSlot() const return -1; } + +void Inventory::addInventoyListener(InventoryListener* listener) +{ + mInventoryListeners.push_back(listener); +} + +void Inventory::removeInventoyListener(InventoryListener* listener) +{ + mInventoryListeners.remove(listener); +} + +void Inventory::distributeSlotsChangedEvent() +{ + InventoryListenerList::const_iterator i = mInventoryListeners.begin(); + InventoryListenerList::const_iterator i_end = mInventoryListeners.end(); + for (; i != i_end; i++) + { + (*i)->slotsChanged(this); + } +} -- cgit v1.2.3-70-g09d2