From 5b6848e817bea37e286f197f8702ab29f1de3bc8 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Fri, 30 Oct 2015 21:19:30 +0300 Subject: Add item container max columns limit. Also fix some issues with adding items to npc item container. --- src/gui/widgets/itemcontainer.cpp | 67 ++++++++++++++++++++++++++++++++------- src/gui/widgets/itemcontainer.h | 8 ++++- src/gui/windows/npcdialog.cpp | 8 ++--- 3 files changed, 67 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/gui/widgets/itemcontainer.cpp b/src/gui/widgets/itemcontainer.cpp index 5ae542357..2ec0209ee 100644 --- a/src/gui/widgets/itemcontainer.cpp +++ b/src/gui/widgets/itemcontainer.cpp @@ -159,6 +159,7 @@ namespace ItemContainer::ItemContainer(const Widget2 *const widget, Inventory *const inventory, + const int maxColumns, const ShowEmptyRows showEmptyRows, const ForceQuantity forceQuantity) : Widget(widget), @@ -191,6 +192,7 @@ ItemContainer::ItemContainer(const Widget2 *const widget, "equippedTextPadding", 29) : 29), mPaddingItemX(mSkin ? mSkin->getOption("paddingItemX", 0) : 0), mPaddingItemY(mSkin ? mSkin->getOption("paddingItemY", 0) : 0), + mMaxColumns(maxColumns), mSelectionStatus(SEL_NONE), mForceQuantity(forceQuantity), mShowEmptyRows(showEmptyRows), @@ -258,18 +260,20 @@ void ItemContainer::draw(Graphics *graphics) if (mCellBackgroundImg) { - if (mRedraw) + if (mRedraw || graphics->getRedraw()) { mRedraw = false; mVertexes->clear(); - const unsigned int invSize = mInventory->getSize(); + const int invSize = mInventory->getSize(); const int maxRows = mShowEmptyRows == ShowEmptyRows_true ? - invSize / mGridColumns : mGridRows; + std::max(invSize / mGridColumns, mGridRows) : mGridRows; + const int maxColumns = mGridColumns > invSize ? + invSize : mGridColumns; for (int j = 0; j < maxRows; j++) { const int intY0 = j * mBoxHeight; - for (int i = 0; i < mGridColumns; i++) + for (int i = 0; i < maxColumns; i++) { int itemX = i * mBoxWidth; int itemY = intY0; @@ -387,13 +391,15 @@ void ItemContainer::safeDraw(Graphics *graphics) if (mCellBackgroundImg) { - const unsigned int invSize = mInventory->getSize(); + const int invSize = mInventory->getSize(); const int maxRows = mShowEmptyRows == ShowEmptyRows_true ? - invSize / mGridColumns : mGridRows; + std::max(invSize / mGridColumns, mGridRows) : mGridRows; + const int maxColumns = mGridColumns > invSize ? + invSize : mGridColumns; for (int j = 0; j < maxRows; j++) { const int intY0 = j * mBoxHeight; - for (int i = 0; i < mGridColumns; i++) + for (int i = 0; i < maxColumns; i++) { int itemX = i * mBoxWidth; int itemY = intY0; @@ -785,7 +791,7 @@ void ItemContainer::mouseReleased(MouseEvent &event) { mInventory->addVirtualItem( inventory->getItem(dragDrop.getTag()), - getSlotIndex(event.getX(), event.getY())); + getSlotByXY(event.getX(), event.getY())); } return; } @@ -855,8 +861,17 @@ void ItemContainer::mouseExited(MouseEvent &event A_UNUSED) void ItemContainer::widgetResized(const Event &event A_UNUSED) { - mGridColumns = std::max(1, mDimension.width / mBoxWidth); + mGridColumns = std::min(mMaxColumns, + std::max(1, mDimension.width / mBoxWidth)); + if (mGridColumns > mMaxColumns) + mGridColumns = mMaxColumns; adjustHeight(); + mRedraw = true; +} + +void ItemContainer::widgetMoved(const Event &event A_UNUSED) +{ + mRedraw = true; } void ItemContainer::adjustHeight() @@ -868,11 +883,16 @@ void ItemContainer::adjustHeight() if (mGridRows == 0 || (mLastUsedSlot + 1) % mGridColumns > 0) ++mGridRows; + const unsigned int invSize = mInventory->getSize(); const int maxRows = mShowEmptyRows == ShowEmptyRows_true ? - invSize / mGridColumns : mGridRows; - setHeight(maxRows * mBoxHeight); + std::max(invSize / mGridColumns, + static_cast(mGridRows)) : mGridRows; + if (mShowEmptyRows == ShowEmptyRows_true) + mGridRows = maxRows; + + setHeight(maxRows * mBoxHeight); updateMatrix(); } @@ -898,6 +918,13 @@ void ItemContainer::updateMatrix() Item *const item = mInventory->getItem(idx); if (!item || item->getId() == 0 || !item->isHaveTag(mTag)) + { + if (mShowEmptyRows == ShowEmptyRows_true) + sortedItems.push_back(new ItemIdPair(idx, nullptr)); + continue; + } + + if (!item->isHaveTag(mTag)) continue; if (mName.empty()) @@ -982,6 +1009,24 @@ int ItemContainer::getSlotIndex(int x, int y) const return Inventory::NO_SLOT_INDEX; } +int ItemContainer::getSlotByXY(int x, int y) const +{ + if (!mShowMatrix) + return Inventory::NO_SLOT_INDEX; + + if (x < mDimension.width && y < mDimension.height && x >= 0 && y >= 0) + { + if (x > mBoxWidth * mGridColumns) + return Inventory::NO_SLOT_INDEX; + const int idx = (y / mBoxHeight) * mGridColumns + (x / mBoxWidth); + logger->log("getSlotByXY 3: %d, %d*%d", idx, mGridRows, mGridColumns); + if (idx >= 0 && idx < mGridRows * mGridColumns) + return idx; + } + + return Inventory::NO_SLOT_INDEX; +} + void ItemContainer::setFilter(const int tag) { mTag = tag; diff --git a/src/gui/widgets/itemcontainer.h b/src/gui/widgets/itemcontainer.h index 42aeb2684..4e29cf2c5 100644 --- a/src/gui/widgets/itemcontainer.h +++ b/src/gui/widgets/itemcontainer.h @@ -60,6 +60,7 @@ class ItemContainer final : public Widget, */ ItemContainer(const Widget2 *const widget, Inventory *const inventory, + const int maxColumns = 100000, const ShowEmptyRows showEmptyRows = ShowEmptyRows_false, const ForceQuantity forceQuantity = ForceQuantity_false); @@ -96,6 +97,8 @@ class ItemContainer final : public Widget, // WidgetListener void widgetResized(const Event &event) override final; + void widgetMoved(const Event &event) override final; + /** * Returns the selected item. */ @@ -171,7 +174,7 @@ class ItemContainer final : public Widget, void distributeValueChangedEvent(); /** - * Gets the slot index based on the cursor position. + * Gets the inventory slot index based on the cursor position. * * @param x The X coordinate position. * @param y The Y coordinate position. @@ -179,6 +182,8 @@ class ItemContainer final : public Widget, */ int getSlotIndex(int x, int y) const; + int getSlotByXY(int x, int y) const; + Inventory *mInventory; Image *mSelImg; Image *mProtectedImg; @@ -207,6 +212,7 @@ class ItemContainer final : public Widget, int mEquippedTextPadding; int mPaddingItemX; int mPaddingItemY; + int mMaxColumns; SelectionState mSelectionStatus; ForceQuantity mForceQuantity; ShowEmptyRows mShowEmptyRows; diff --git a/src/gui/windows/npcdialog.cpp b/src/gui/windows/npcdialog.cpp index e94904a08..de74037f8 100644 --- a/src/gui/windows/npcdialog.cpp +++ b/src/gui/windows/npcdialog.cpp @@ -123,7 +123,8 @@ NpcDialog::NpcDialog(const BeingId npcId) : // TRANSLATORS: npc dialog button mResetButton(new Button(this, _("Reset"), "reset", this)), mInventory(new Inventory(InventoryType::NPC, 1)), - mItemContainer(new ItemContainer(this, mInventory, ShowEmptyRows_true)), + mItemContainer(new ItemContainer(this, mInventory, + 10000, ShowEmptyRows_true)), mItemScrollArea(new ScrollArea(this, mItemContainer, getOptionBool("showitemsbackground"), "npc_listbackground.xml")), mInputState(NPC_INPUT_NONE), @@ -374,7 +375,7 @@ void NpcDialog::action(const ActionEvent &event) } std::string str; - const int sz = mInventory->getNumberOfSlotsUsed(); + const int sz = mInventory->getSize(); if (sz == 0) { str = "0,0"; @@ -421,7 +422,7 @@ void NpcDialog::action(const ActionEvent &event) } std::string str; - const int sz = mInventory->getNumberOfSlotsUsed(); + const int sz = mInventory->getSize(); if (sz == 0) { str = "-1"; @@ -441,7 +442,6 @@ void NpcDialog::action(const ActionEvent &event) { str.append(";"); item = mInventory->getItem(f); - logger->log("tag=%d", item->getTag()); if (item) str.append(strprintf("%d", item->getTag())); else -- cgit v1.2.3-70-g09d2