From 38c827f67ba233ee11a964eff76714bbbedce4e9 Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Tue, 12 May 2009 21:29:28 +0200 Subject: Only scroll down the inventory as far as necessary ItemContainer now adjusts its number of rows to the last used slot. --- src/gui/itemcontainer.cpp | 23 +++++++++++++++++++++-- src/gui/itemcontainer.h | 13 +++++++------ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/gui/itemcontainer.cpp b/src/gui/itemcontainer.cpp index 6cbdabb2..26382cd2 100644 --- a/src/gui/itemcontainer.cpp +++ b/src/gui/itemcontainer.cpp @@ -57,6 +57,7 @@ ItemContainer::ItemContainer(Inventory *inventory, bool forceQuantity): mGridRows(1), mSelectedIndex(-1), mHighlightedIndex(-1), + mLastUsedSlot(-1), mSelectionStatus(SEL_NONE), mForceQuantity(forceQuantity), mSwapItems(false), @@ -82,6 +83,19 @@ ItemContainer::~ItemContainer() delete mItemPopup; } +void ItemContainer::logic() +{ + gcn::Widget::logic(); + + const int lastUsedSlot = mInventory->getLastUsedSlot(); + + if (lastUsedSlot != mLastUsedSlot) + { + mLastUsedSlot = lastUsedSlot; + adjustHeight(); + } +} + void ItemContainer::draw(gcn::Graphics *graphics) { Graphics *g = static_cast(graphics); @@ -316,8 +330,13 @@ void ItemContainer::mouseExited(gcn::MouseEvent &event) void ItemContainer::widgetResized(const gcn::Event &event) { mGridColumns = std::max(1, getWidth() / BOX_WIDTH); - mGridRows = mInventory->getSize() / mGridColumns; - if (mGridRows == 0 || mInventory->getSize() % mGridColumns > 0) + adjustHeight(); +} + +void ItemContainer::adjustHeight() +{ + mGridRows = (mLastUsedSlot + 1) / mGridColumns; + if (mGridRows == 0 || (mLastUsedSlot + 1) % mGridColumns > 0) ++mGridRows; setHeight(mGridRows * BOX_HEIGHT); diff --git a/src/gui/itemcontainer.h b/src/gui/itemcontainer.h index f446a647..2cfd06b2 100644 --- a/src/gui/itemcontainer.h +++ b/src/gui/itemcontainer.h @@ -64,6 +64,11 @@ class ItemContainer : public gcn::Widget, */ virtual ~ItemContainer(); + /** + * Necessary for checking how full the inventory is. + */ + void logic(); + /** * Draws the items. */ @@ -146,15 +151,10 @@ class ItemContainer : public gcn::Widget, */ void setSelectedIndex(int index); - /** - * Find the current item index by the most recently used item ID - */ - void refindSelectedItem(); - /** * Determine and set the height of the container. */ - void recalculateHeight(); + void adjustHeight(); /** * Sends out selection events to the list of selection listeners. @@ -174,6 +174,7 @@ class ItemContainer : public gcn::Widget, int mGridColumns, mGridRows; Image *mSelImg; int mSelectedIndex, mHighlightedIndex; + int mLastUsedSlot; SelectionState mSelectionStatus; bool mForceQuantity; bool mSwapItems; -- cgit v1.2.3-70-g09d2