summaryrefslogtreecommitdiff
path: root/src/gui
diff options
context:
space:
mode:
authorFate <fate-tmw@googlemail.com>2008-11-28 21:49:25 -0700
committerFate <fate-tmw@googlemail.com>2008-11-28 21:49:25 -0700
commitb08958152524cb5c26bab8b8a9943f71acfbded3 (patch)
tree5de0b5e7cfaa1e7c4fc1351289e469d7e792565d /src/gui
parent4aeedb10f22d0dc307c9f733d0f1c79b70b9c200 (diff)
downloadmana-b08958152524cb5c26bab8b8a9943f71acfbded3.tar.gz
mana-b08958152524cb5c26bab8b8a9943f71acfbded3.tar.bz2
mana-b08958152524cb5c26bab8b8a9943f71acfbded3.tar.xz
mana-b08958152524cb5c26bab8b8a9943f71acfbded3.zip
Changed inventory container protocol to guarantee that we have no stale item references lying around
Diffstat (limited to 'src/gui')
-rw-r--r--src/gui/itemcontainer.cpp43
-rw-r--r--src/gui/itemcontainer.h6
2 files changed, 26 insertions, 23 deletions
diff --git a/src/gui/itemcontainer.cpp b/src/gui/itemcontainer.cpp
index 5342e3fe..1e12a1ae 100644
--- a/src/gui/itemcontainer.cpp
+++ b/src/gui/itemcontainer.cpp
@@ -39,9 +39,11 @@
const int ItemContainer::gridWidth = 36; // item icon width + 4
const int ItemContainer::gridHeight = 42; // item icon height + 10
+static const int NO_ITEM = -1;
+
ItemContainer::ItemContainer(Inventory *inventory):
mInventory(inventory),
- mSelectedItem(NULL)
+ mSelectedItemIndex(NO_ITEM)
{
ResourceManager *resman = ResourceManager::getInstance();
@@ -82,13 +84,6 @@ void ItemContainer::draw(gcn::Graphics *graphics)
columns = 1;
}
- // Reset selected item when quantity not above 0 (should probably be made
- // sure somewhere else)
- if (mSelectedItem && mSelectedItem->getQuantity() <= 0)
- {
- selectNone();
- }
-
/*
* eAthena seems to start inventory from the 3rd slot. Still a mystery to
* us why, make sure not to copy this oddity to our own server.
@@ -104,7 +99,7 @@ void ItemContainer::draw(gcn::Graphics *graphics)
int itemY = ((i - 2) / columns) * gridHeight;
// Draw selection image below selected item
- if (mSelectedItem == item)
+ if (mSelectedItemIndex == i)
{
static_cast<Graphics*>(graphics)->drawImage(
mSelImg, itemX, itemY);
@@ -147,19 +142,30 @@ void ItemContainer::recalculateHeight()
Item *ItemContainer::getSelectedItem() const
{
- return mSelectedItem;
+ if (mSelectedItemIndex == NO_ITEM)
+ return NULL;
+ return mInventory->getItem(mSelectedItemIndex);
}
void ItemContainer::selectNone()
{
- setSelectedItem(NULL);
+ setSelectedItemIndex(NO_ITEM);
}
-void ItemContainer::setSelectedItem(Item *item)
+void ItemContainer::setSelectedItemIndex(int index)
{
- if (mSelectedItem != item)
+ int newSelectedItemIndex;
+
+ // mMaxItems is broken because of eAthena's odd inventory layout and the client's refusal
+ // to handle it properly, so we work around the issue right here.
+ if (index < 0 || index > mMaxItems + 1)
+ newSelectedItemIndex = NO_ITEM;
+ else
+ newSelectedItemIndex = index;
+
+ if (mSelectedItemIndex != newSelectedItemIndex)
{
- mSelectedItem = item;
+ mSelectedItemIndex = newSelectedItemIndex;
distributeValueChangedEvent();
}
}
@@ -189,13 +195,10 @@ void ItemContainer::mousePressed(gcn::MouseEvent &event)
itemShortcut->setItemSelected(-1);
// Fix for old server, it should be: if (index >= mMaxItems)
- if (index > mMaxItems + 1)
- {
- setSelectedItem(NULL);
- return;
- }
+ setSelectedItemIndex(index);
+
Item *item = mInventory->getItem(index);
- setSelectedItem(item);
+
if (item)
itemShortcut->setItemSelected(item->getId());
}
diff --git a/src/gui/itemcontainer.h b/src/gui/itemcontainer.h
index 60b62f08..fd77fea2 100644
--- a/src/gui/itemcontainer.h
+++ b/src/gui/itemcontainer.h
@@ -105,9 +105,9 @@ class ItemContainer : public gcn::Widget, public gcn::MouseListener,
private:
/**
- * Sets the currently selected item.
+ * Sets the currently selected item. Invalid (e.g., negative) indices set `no item'.
*/
- void setSelectedItem(Item *item);
+ void setSelectedItemIndex(int index);
/**
* Determine and set the height of the container.
@@ -121,7 +121,7 @@ class ItemContainer : public gcn::Widget, public gcn::MouseListener,
Inventory *mInventory;
Image *mSelImg;
- Item *mSelectedItem;
+ int mSelectedItemIndex;
int mMaxItems;