refactor: enhance tooltip handling by making caption and description retrieval static

This commit is contained in:
GHOST11111100 2025-07-27 02:26:05 +03:00
parent d489dab013
commit 1e270d4adb

View File

@ -117,18 +117,24 @@ SlotView::SlotView(GUI& gui, SlotLayout layout)
setTooltipDelay(0.0f); setTooltipDelay(0.0f);
} }
// TODO: Refactor // 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"); dv::value* caption = stack.getField("caption");
if (caption != nullptr) { if (caption != nullptr) {
return util::pascal_case( return util::pascal_case(
langs::get(util::str2wstr_utf8(caption->asString())) langs::get(util::str2wstr_utf8(caption->asString()))
); );
} else { } 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 // 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"); dv::value* description = stack.getField("description");
if (description != nullptr) { 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) { void SlotView::refreshTooltip(const ItemStack& stack, const ItemDef& item) {
itemid_t itemid = stack.getItemId(); 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; return;
} }
if (itemid) { if (itemid) {
std::wstring caption = get_caption_string(stack, item);
std::wstring description = get_description_string(stack, item);
if (description.length() > 0) { if (description.length() > 0) {
tooltip = caption + L"\n" + description; tooltip = caption + L"\n" + description;
} else { } else {