diff options
author | Jared Adams <jaxad0127@gmail.com> | 2010-03-24 23:10:51 -0600 |
---|---|---|
committer | Jared Adams <jaxad0127@gmail.com> | 2010-03-25 11:31:55 -0600 |
commit | bf6cb46d9b06b06470efd5ad3ebae7e274f6906f (patch) | |
tree | 281cdf6d017477f07e02e73acef175f937c18eed /src/inventory.cpp | |
parent | 83077364f8b67fb9fc57e8b04a1feff0e243848d (diff) | |
download | mana-bf6cb46d9b06b06470efd5ad3ebae7e274f6906f.tar.gz mana-bf6cb46d9b06b06470efd5ad3ebae7e274f6906f.tar.bz2 mana-bf6cb46d9b06b06470efd5ad3ebae7e274f6906f.tar.xz mana-bf6cb46d9b06b06470efd5ad3ebae7e274f6906f.zip |
Eliminate the logic methods from InventoryWindow and StorageWindow
Diffstat (limited to 'src/inventory.cpp')
-rw-r--r-- | src/inventory.cpp | 35 |
1 files changed, 29 insertions, 6 deletions
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<Item*, bool> }; 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); + } +} |