summaryrefslogtreecommitdiff
path: root/src/gui/widgets/itemcontainer.cpp
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2015-10-30 17:19:59 +0300
committerAndrei Karas <akaras@inbox.ru>2015-10-30 18:19:54 +0300
commite196607ec6a16204579f8e37e1c88c133bf1db37 (patch)
tree37fa0856b49d702b7f3058aa009581f5a7549ecc /src/gui/widgets/itemcontainer.cpp
parent41049787e0b39550675c682e1c5740e5ff054df6 (diff)
downloadmanaverse-e196607ec6a16204579f8e37e1c88c133bf1db37.tar.gz
manaverse-e196607ec6a16204579f8e37e1c88c133bf1db37.tar.bz2
manaverse-e196607ec6a16204579f8e37e1c88c133bf1db37.tar.xz
manaverse-e196607ec6a16204579f8e37e1c88c133bf1db37.zip
Add support in themes to draw each inventory cell background.
By default no images for cell background in any theme.
Diffstat (limited to 'src/gui/widgets/itemcontainer.cpp')
-rw-r--r--src/gui/widgets/itemcontainer.cpp163
1 files changed, 160 insertions, 3 deletions
diff --git a/src/gui/widgets/itemcontainer.cpp b/src/gui/widgets/itemcontainer.cpp
index fec14b814..9d8a7e264 100644
--- a/src/gui/widgets/itemcontainer.cpp
+++ b/src/gui/widgets/itemcontainer.cpp
@@ -23,6 +23,7 @@
#include "gui/widgets/itemcontainer.h"
#include "dragdrop.h"
+#include "graphicsvertexes.h"
#include "itemshortcut.h"
#include "being/playerinfo.h"
@@ -43,6 +44,7 @@
#include "net/inventoryhandler.h"
#include "net/tradehandler.h"
+#include "utils/delete2.h"
#include "utils/gettext.h"
#include "utils/stringutils.h"
@@ -165,9 +167,11 @@ ItemContainer::ItemContainer(const Widget2 *const widget,
mInventory(inventory),
mSelImg(Theme::getImageFromThemeXml("item_selection.xml", "")),
mProtectedImg(Theme::getImageFromTheme("lock.png")),
+ mCellBackgroundImg(Theme::getImageFromThemeXml("inventory_cell.xml", "")),
mName(),
mShowMatrix(nullptr),
mSkin(theme ? theme->load("itemcontainer.xml", "") : nullptr),
+ mVertexes(new ImageCollection),
mEquipedColor(getThemeColor(ThemeColorId::ITEM_EQUIPPED)),
mEquipedColor2(getThemeColor(ThemeColorId::ITEM_EQUIPPED_OUTLINE)),
mUnEquipedColor(getThemeColor(ThemeColorId::ITEM_NOT_EQUIPPED)),
@@ -188,7 +192,8 @@ ItemContainer::ItemContainer(const Widget2 *const widget,
mPaddingItemY(mSkin ? mSkin->getOption("paddingItemY", 0) : 0),
mSelectionStatus(SEL_NONE),
mForceQuantity(forceQuantity),
- mDescItems(false)
+ mDescItems(false),
+ mRedraw(true)
{
setFocusable(true);
addKeyListener(this);
@@ -217,6 +222,7 @@ ItemContainer::~ItemContainer()
theme->unload(mSkin);
delete []mShowMatrix;
+ delete2(mVertexes);
}
void ItemContainer::logic()
@@ -248,6 +254,33 @@ void ItemContainer::draw(Graphics *graphics)
BLOCK_START("ItemContainer::draw")
Font *const font = getFont();
+ if (mCellBackgroundImg)
+ {
+ if (mRedraw)
+ {
+ mRedraw = false;
+ mVertexes->clear();
+
+ const unsigned int invSize = mInventory->getSize();
+ const int maxRows = invSize / mGridColumns;
+ for (int j = 0; j < maxRows; j++)
+ {
+ const int intY0 = j * mBoxHeight;
+ for (int i = 0; i < mGridColumns; i++)
+ {
+ int itemX = i * mBoxWidth;
+ int itemY = intY0;
+ graphics->calcTileCollection(mVertexes,
+ mCellBackgroundImg,
+ itemX + mPaddingItemX,
+ itemY + mPaddingItemY);
+ }
+ }
+ graphics->finalize(mVertexes);
+ }
+ graphics->drawTileCollection(mVertexes);
+ }
+
for (int j = 0; j < mGridRows; j++)
{
const int intY0 = j * mBoxHeight;
@@ -342,7 +375,120 @@ void ItemContainer::draw(Graphics *graphics)
void ItemContainer::safeDraw(Graphics *graphics)
{
- ItemContainer::draw(graphics);
+ if (!mInventory || !mShowMatrix)
+ return;
+
+ BLOCK_START("ItemContainer::draw")
+ Font *const font = getFont();
+
+ if (mCellBackgroundImg)
+ {
+ const unsigned int invSize = mInventory->getSize();
+ const int maxRows = invSize / mGridColumns;
+ for (int j = 0; j < maxRows; j++)
+ {
+ const int intY0 = j * mBoxHeight;
+ for (int i = 0; i < mGridColumns; i++)
+ {
+ int itemX = i * mBoxWidth;
+ int itemY = intY0;
+ graphics->drawImage(mCellBackgroundImg,
+ itemX + mPaddingItemX,
+ itemY + mPaddingItemY);
+ }
+ }
+ }
+
+ for (int j = 0; j < mGridRows; j++)
+ {
+ const int intY0 = j * mBoxHeight;
+ int itemIndex = j * mGridColumns - 1;
+ for (int i = 0; i < mGridColumns; i++)
+ {
+ int itemX = i * mBoxWidth;
+ int itemY = intY0;
+ itemIndex ++;
+ if (mShowMatrix[itemIndex] < 0)
+ continue;
+
+ const Item *const item = mInventory->getItem(
+ mShowMatrix[itemIndex]);
+
+ if (!item || item->getId() == 0)
+ continue;
+
+ Image *const image = item->getImage();
+ if (image)
+ {
+ if (mShowMatrix[itemIndex] == mSelectedIndex)
+ {
+ if (mSelImg)
+ graphics->drawImage(mSelImg, itemX, itemY);
+ }
+ image->setAlpha(1.0F); // ensure the image if fully drawn...
+ graphics->drawImage(image,
+ itemX + mPaddingItemX,
+ itemY + mPaddingItemY);
+ if (mProtectedImg && PlayerInfo::isItemProtected(
+ item->getId()))
+ {
+ graphics->drawImage(mProtectedImg,
+ itemX + mPaddingItemX,
+ itemY + mPaddingItemY);
+ }
+ }
+ }
+ }
+
+ for (int j = 0; j < mGridRows; j++)
+ {
+ const int intY0 = j * mBoxHeight;
+ int itemIndex = j * mGridColumns - 1;
+ for (int i = 0; i < mGridColumns; i++)
+ {
+ int itemX = i * mBoxWidth;
+ int itemY = intY0;
+ itemIndex ++;
+ if (mShowMatrix[itemIndex] < 0)
+ continue;
+
+ const Item *const item = mInventory->getItem(
+ mShowMatrix[itemIndex]);
+
+ if (!item || item->getId() == 0)
+ continue;
+
+ // Draw item caption
+ std::string caption;
+ if (item->getQuantity() > 1 || mForceQuantity)
+ {
+ caption = toString(item->getQuantity());
+ }
+ else if (item->isEquipped() == Equipped_true)
+ {
+ // TRANSLATORS: Text under equipped items (should be small)
+ caption = _("Eq.");
+ }
+
+ if (item->isEquipped() == Equipped_true)
+ {
+ font->drawString(graphics,
+ mEquipedColor, mEquipedColor2,
+ caption,
+ itemX + (mBoxWidth - font->getWidth(caption)) / 2,
+ itemY + mEquippedTextPadding);
+ }
+ else
+ {
+ font->drawString(graphics,
+ mUnEquipedColor, mUnEquipedColor2,
+ caption,
+ itemX + (mBoxWidth - font->getWidth(caption)) / 2,
+ itemY + mEquippedTextPadding);
+ }
+ }
+ }
+ BLOCK_END("ItemContainer::draw")
}
void ItemContainer::selectNone()
@@ -716,7 +862,9 @@ void ItemContainer::adjustHeight()
if (mGridRows == 0 || (mLastUsedSlot + 1) % mGridColumns > 0)
++mGridRows;
- setHeight(mGridRows * mBoxHeight);
+ const unsigned int invSize = mInventory->getSize();
+ const int maxRows = invSize / mGridColumns;
+ setHeight(maxRows * mBoxHeight);
updateMatrix();
}
@@ -726,6 +874,7 @@ void ItemContainer::updateMatrix()
if (!mInventory)
return;
+ mRedraw = true;
delete []mShowMatrix;
mShowMatrix = new int[static_cast<size_t>(mGridRows * mGridColumns)];
@@ -837,3 +986,11 @@ void ItemContainer::setSortType(const int sortType)
mSortType = sortType;
updateMatrix();
}
+
+void ItemContainer::setCellBackgroundImage(const std::string &xmlName)
+{
+ if (mCellBackgroundImg)
+ mCellBackgroundImg->decRef();
+ mCellBackgroundImg = Theme::getImageFromThemeXml(xmlName, "");
+ mRedraw = true;
+}