From e196607ec6a16204579f8e37e1c88c133bf1db37 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Fri, 30 Oct 2015 17:19:59 +0300 Subject: Add support in themes to draw each inventory cell background. By default no images for cell background in any theme. --- src/gui/widgets/itemcontainer.cpp | 163 +++++++++++++++++++++++++++++++++++++- src/gui/widgets/itemcontainer.h | 5 ++ 2 files changed, 165 insertions(+), 3 deletions(-) (limited to 'src/gui') 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(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; +} diff --git a/src/gui/widgets/itemcontainer.h b/src/gui/widgets/itemcontainer.h index f87eac55d..eb422e269 100644 --- a/src/gui/widgets/itemcontainer.h +++ b/src/gui/widgets/itemcontainer.h @@ -131,6 +131,8 @@ class ItemContainer final : public Widget, void unsetInventory() { mInventory = nullptr; } + void setCellBackgroundImage(const std::string &xmlName); + private: enum Direction { @@ -176,10 +178,12 @@ class ItemContainer final : public Widget, Inventory *mInventory; Image *mSelImg; Image *mProtectedImg; + Image *mCellBackgroundImg; std::string mName; int *mShowMatrix; Skin *mSkin; + ImageCollection *mVertexes; Color mEquipedColor; Color mEquipedColor2; Color mUnEquipedColor; @@ -202,6 +206,7 @@ class ItemContainer final : public Widget, SelectionState mSelectionStatus; bool mForceQuantity; bool mDescItems; + bool mRedraw; }; #endif // GUI_WIDGETS_ITEMCONTAINER_H -- cgit v1.2.3-70-g09d2