From 1e270d4adbbf4ec6bb6e314f15120d8ff224a364 Mon Sep 17 00:00:00 2001 From: GHOST11111100 Date: Sun, 27 Jul 2025 02:26:05 +0300 Subject: [PATCH] refactor: enhance tooltip handling by making caption and description retrieval static --- src/graphics/ui/elements/InventoryView.cpp | 38 +++++++++++++++++----- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/graphics/ui/elements/InventoryView.cpp b/src/graphics/ui/elements/InventoryView.cpp index be58dd50..563d36a7 100644 --- a/src/graphics/ui/elements/InventoryView.cpp +++ b/src/graphics/ui/elements/InventoryView.cpp @@ -117,18 +117,24 @@ SlotView::SlotView(GUI& gui, SlotLayout layout) setTooltipDelay(0.0f); } // TODO: Refactor -std::wstring get_caption_string(const ItemStack& stack, const ItemDef& item) { +static std::wstring get_caption_string( + const ItemStack& stack, const ItemDef& item +) { dv::value* caption = stack.getField("caption"); if (caption != nullptr) { return util::pascal_case( langs::get(util::str2wstr_utf8(caption->asString())) ); } else { - return util::pascal_case(langs::get(util::str2wstr_utf8(item.caption))); + return util::pascal_case( + langs::get(util::str2wstr_utf8(item.caption)) + ); } } // TODO: Refactor -std::wstring get_description_string(const ItemStack& stack, const ItemDef& item) { +static std::wstring get_description_string( + const ItemStack& stack, const ItemDef& item +) { dv::value* description = stack.getField("description"); if (description != nullptr) { @@ -138,17 +144,33 @@ std::wstring get_description_string(const ItemStack& stack, const ItemDef& item) } } +static bool is_same_tooltip(const ItemStack& stack, const ItemStack& cache) { + if (stack.getItemId() != cache.getItemId()) { + return false; + } + auto caption = stack.getField("caption"); + auto cCaption = cache.getField("caption"); + auto description = stack.getField("description"); + auto cDescription = cache.getField("description"); + + if (((caption != nullptr) != (description != nullptr)) || + ((description != nullptr) != (cDescription != nullptr))) { + return false; + } + return (caption ? caption->asString() == cCaption->asString() : true) && + (description ? description->asString() == cDescription->asString() + : true); +} + void SlotView::refreshTooltip(const ItemStack& stack, const ItemDef& item) { itemid_t itemid = stack.getItemId(); - std::wstring caption = get_caption_string(stack, item); - std::wstring cached_caption = get_caption_string(cache.stack, item); - std::wstring description = get_description_string(stack, item); - std::wstring cached_description = get_description_string(cache.stack, item); - if (itemid == cache.stack.getItemId() && cached_caption == caption && cached_description == description) { + if (is_same_tooltip(stack, cache.stack)) { return; } if (itemid) { + std::wstring caption = get_caption_string(stack, item); + std::wstring description = get_description_string(stack, item); if (description.length() > 0) { tooltip = caption + L"\n" + description; } else {