summaryrefslogtreecommitdiff
path: root/src/gui/widgets
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2015-10-30 21:19:30 +0300
committerAndrei Karas <akaras@inbox.ru>2015-10-30 21:19:30 +0300
commit5b6848e817bea37e286f197f8702ab29f1de3bc8 (patch)
tree2f38f5ac1c38fc1e7766d4d09535f7badecd05ea /src/gui/widgets
parentbd429077f8985c5ee45a53009d02e2f495b4ad90 (diff)
downloadmanaverse-5b6848e817bea37e286f197f8702ab29f1de3bc8.tar.gz
manaverse-5b6848e817bea37e286f197f8702ab29f1de3bc8.tar.bz2
manaverse-5b6848e817bea37e286f197f8702ab29f1de3bc8.tar.xz
manaverse-5b6848e817bea37e286f197f8702ab29f1de3bc8.zip
Add item container max columns limit.
Also fix some issues with adding items to npc item container.
Diffstat (limited to 'src/gui/widgets')
-rw-r--r--src/gui/widgets/itemcontainer.cpp67
-rw-r--r--src/gui/widgets/itemcontainer.h8
2 files changed, 63 insertions, 12 deletions
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<unsigned int>(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;