summaryrefslogtreecommitdiff
path: root/src/inventory.cpp
diff options
context:
space:
mode:
authorJared Adams <jaxad0127@gmail.com>2010-03-24 23:10:51 -0600
committerJared Adams <jaxad0127@gmail.com>2010-03-25 11:31:55 -0600
commitbf6cb46d9b06b06470efd5ad3ebae7e274f6906f (patch)
tree281cdf6d017477f07e02e73acef175f937c18eed /src/inventory.cpp
parent83077364f8b67fb9fc57e8b04a1feff0e243848d (diff)
downloadmana-client-bf6cb46d9b06b06470efd5ad3ebae7e274f6906f.tar.gz
mana-client-bf6cb46d9b06b06470efd5ad3ebae7e274f6906f.tar.bz2
mana-client-bf6cb46d9b06b06470efd5ad3ebae7e274f6906f.tar.xz
mana-client-bf6cb46d9b06b06470efd5ad3ebae7e274f6906f.zip
Eliminate the logic methods from InventoryWindow and StorageWindow
Diffstat (limited to 'src/inventory.cpp')
-rw-r--r--src/inventory.cpp35
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);
+ }
+}