Skip to content

Commit

Permalink
[Impl] Improve icon rendering of inventory items
Browse files Browse the repository at this point in the history
InventoryItem:
- If an inventory item' icon is not found, the icon will be render as
its ID and a part of the missing texture;
- If an inventory icon is a tag, its ID (not including the number sign)
is also displayed.
- These changes are suggested in #18.

Signed-off-by: IoeCmcomc <[email protected]>
  • Loading branch information
IoeCmcomc committed Jun 28, 2024
1 parent ff88000 commit ce81dd3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
2 changes: 0 additions & 2 deletions resource/minecraft/minecraft.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -1575,8 +1575,6 @@
<file>texture/inv_item/stripped_cherry_log.png</file>
<file>texture/inv_item/stripped_cherry_wood.png</file>
<file>texture/inv_item/short_grass.png</file>
</qresource>
<qresource prefix="/">
<file>texture/item/armadillo_scute.png</file>
<file>texture/item/armadillo_spawn_egg.png</file>
<file>texture/item/wolf_armor.png</file>
Expand Down
39 changes: 29 additions & 10 deletions src/inventoryitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,23 @@ QPixmap InventoryItem::loadPixmap(QString id) const {
}

if (isTag()) {
Glhp::removePrefix(id, "#"_QL1);
Glhp::removePrefix(id);

QPixmap iconpix(32, 32);
iconpix.fill(Qt::transparent);
{
QPainter painter(&iconpix);
QFont font = painter.font();
font.setPixelSize(32);
font.setPixelSize(16);
painter.setFont(font);
painter.drawText(QRect(0, 0, 32, 32), Qt::AlignCenter,
painter.drawText(QRect(0, 0, 32, 16), Qt::AlignCenter,
QStringLiteral("#"));
font.setPixelSize(10);
painter.setFont(font);
painter.drawText(QRect(0, 16, 32, 16),
Qt::AlignCenter | Qt::TextWrapAnywhere,
id);
painter.end();
}
return iconpix;
Expand All @@ -81,7 +89,7 @@ QPixmap InventoryItem::loadPixmap(QString id) const {
QPixmap iconpix;
QString iconpath;

Glhp::removePrefix(id, "minecraft:"_QL1);
Glhp::removePrefix(id);
const auto &&MCRItemInfo = Game::getInfo(QStringLiteral("item"));

if (id.endsWith(QLatin1String("banner_pattern"))) {
Expand All @@ -103,16 +111,27 @@ QPixmap InventoryItem::loadPixmap(QString id) const {
iconpix = QPixmap(iconpath);
}

if (!iconpix) { // Draw missing texture
const static QColor magenta = QColor(248, 0, 248);
iconpix = QPixmap(16, 16);
iconpix.fill(magenta);
if (!iconpix) {
const static QColor magenta = QColor(248, 0, 248);
const static int borderWidth = 4;
iconpix = QPixmap(32, 32);
iconpix.fill(Qt::white);
{
QPainter painter(&iconpix);
const static QBrush brush(Qt::black);
painter.fillRect(0, 0, 8, 8, brush);
painter.fillRect(8, 8, 8, 8, brush);
painter.end();
// Draw the top and bottom parts of the missing texture
painter.fillRect(0, 0, 16, borderWidth, brush);
painter.fillRect(16, 0, 16, borderWidth, magenta);
painter.fillRect(0, 32 - borderWidth, 16, borderWidth, magenta);
painter.fillRect(16, 32 - borderWidth, 16, borderWidth, brush);
// Draw the id text in the middle
QFont font = painter.font();
font.setPixelSize(10);
painter.setFont(font);
painter.drawText(QRect(0, borderWidth, 32, 32 - borderWidth * 2),
Qt::AlignCenter | Qt::TextWrapAnywhere,
id);
// painter.end();
}
}
static const int pixmapLength = 32;
Expand Down

0 comments on commit ce81dd3

Please sign in to comment.