From 81f9e6c7ee79a764a3bd55aae4811482ccecb68c Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 18 Feb 2025 04:14:36 +0300 Subject: [PATCH] refactor & debug output fixes --- src/content/ContentLoader.cpp | 4 +- src/graphics/ui/elements/InventoryView.cpp | 76 ++++++++++------------ 2 files changed, 38 insertions(+), 42 deletions(-) diff --git a/src/content/ContentLoader.cpp b/src/content/ContentLoader.cpp index 75b7b035..a9aa5b0a 100644 --- a/src/content/ContentLoader.cpp +++ b/src/content/ContentLoader.cpp @@ -429,7 +429,7 @@ void ContentLoader::loadItem( } else if (iconTypeStr == "sprite") { def.iconType = ItemIconType::SPRITE; } else if (iconTypeStr.length()) { - logger.error() << name << ": unknown icon type" << iconTypeStr; + logger.error() << name << ": unknown icon type - " << iconTypeStr; } root.at("icon").get(def.icon); root.at("placing-block").get(def.placingBlock); @@ -447,7 +447,7 @@ void ContentLoader::loadItem( } else if (usesDisplayStr == "vbar") { def.usesDisplay = ItemUsesDisplay::VBAR; } else if (usesDisplayStr.length()) { - logger.error() << name << ": unknown uses display mode " << usesDisplayStr; + logger.error() << name << ": unknown uses display mode - " << usesDisplayStr; } if (auto found = root.at("emission")) { diff --git a/src/graphics/ui/elements/InventoryView.cpp b/src/graphics/ui/elements/InventoryView.cpp index d40614fe..4421bdd3 100644 --- a/src/graphics/ui/elements/InventoryView.cpp +++ b/src/graphics/ui/elements/InventoryView.cpp @@ -212,6 +212,15 @@ void SlotView::draw(const DrawContext& pctx, const Assets& assets) { } } +static void draw_shaded_text( + Batch2D& batch, const Font& font, const std::wstring& text, int x, int y +) { + batch.setColor({0, 0, 0, 1.0f}); + font.draw(batch, text, x + 1, y + 1, nullptr, 0); + batch.resetColor(); + font.draw(batch, text, x, y, nullptr, 0); +} + void SlotView::drawItemInfo( Batch2D& batch, const ItemStack& stack, @@ -224,48 +233,35 @@ void SlotView::drawItemInfo( const auto& countStr = cache.countStr; int x = pos.x + SLOT_SIZE - countStr.length() * 8; int y = pos.y + SLOT_SIZE - 16; - - batch.setColor({0, 0, 0, 1.0f}); - font.draw(batch, countStr, x + 1, y + 1, nullptr, 0); - batch.resetColor(); - font.draw(batch, countStr, x, y, nullptr, 0); + draw_shaded_text(batch, font, countStr, x, y); } - if (auto ptr = stack.getField("uses")) { - if (!ptr->isInteger()) { - return; - } - int16_t uses = ptr->asInteger(); - switch (item.usesDisplay) { - case ItemUsesDisplay::NONE: - break; - case ItemUsesDisplay::RELATION: - { - std::wstring text = std::to_wstring(uses); - batch.setColor({0, 0, 0, 1.0f}); - font.draw(batch, text, pos.x - 2, pos.y - 2, nullptr, 0); - batch.resetColor(); - font.draw(batch, text, pos.x - 3, pos.y - 3, nullptr, 0); - } - { - std::wstring text = std::to_wstring(item.uses); - batch.setColor({0, 0, 0, 1.0f}); - font.draw(batch, text, pos.x - 2, pos.y - 2 + 12, nullptr, 0); - batch.resetColor(); - font.draw(batch, text, pos.x - 3, pos.y - 3 + 12, nullptr, 0); - } - break; - case ItemUsesDisplay::VBAR: { - batch.untexture(); - batch.setColor({0, 0, 0, 0.75f}); - batch.rect(pos.x - 2, pos.y - 2, 6, SLOT_SIZE + 4); - float t = static_cast(uses) / item.uses; - batch.setColor({(1.0f - t * 0.8f), 0.4f, t * 0.8f + 0.2f, 1.0f}); - - int height = SLOT_SIZE * t; - batch.rect(pos.x, pos.y + SLOT_SIZE - height, 2, height); - break; - } + auto usesPtr = stack.getField("uses"); + if (usesPtr == nullptr || !usesPtr->isInteger()) { + return; + } + int16_t uses = usesPtr->asInteger(); + switch (item.usesDisplay) { + case ItemUsesDisplay::NONE: + break; + case ItemUsesDisplay::RELATION: + draw_shaded_text( + batch, font, std::to_wstring(uses), pos.x - 3, pos.y - 3 + ); + draw_shaded_text( + batch, font, std::to_wstring(item.uses), pos.x - 3, pos.y + 9 + ); + break; + case ItemUsesDisplay::VBAR: { + batch.untexture(); + batch.setColor({0, 0, 0, 0.75f}); + batch.rect(pos.x - 2, pos.y - 2, 6, SLOT_SIZE + 4); + float t = static_cast(uses) / item.uses; + + int height = SLOT_SIZE * t; + batch.setColor({(1.0f - t * 0.8f), 0.4f, t * 0.8f + 0.2f, 1.0f}); + batch.rect(pos.x, pos.y + SLOT_SIZE - height, 2, height); + break; } } }