Merge pull request #571 from GHOST3118/improve-items

Improve items: fix tooltip cache
This commit is contained in:
MihailRis 2025-07-27 15:08:09 +03:00 committed by GitHub
commit 4c513a2e02
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -116,31 +116,66 @@ SlotView::SlotView(GUI& gui, SlotLayout layout)
setColor(glm::vec4(0, 0, 0, 0.2f));
setTooltipDelay(0.0f);
}
// TODO: Refactor
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))
);
}
}
// TODO: Refactor
static std::wstring get_description_string(
const ItemStack& stack, const ItemDef& item
) {
dv::value* description = stack.getField("description");
if (description != nullptr) {
return langs::get(util::str2wstr_utf8(description->asString()));
} else {
return langs::get(util::str2wstr_utf8(item.description));
}
}
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) != (cCaption != 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();
if (itemid == cache.stack.getItemId()) {
if (is_same_tooltip(stack, cache.stack)) {
return;
}
if (itemid) {
dv::value* caption = stack.getField("caption");
dv::value* description = stack.getField("description");
std::wstring captionText;
std::wstring descriptionText;
if (description != nullptr) {
descriptionText = util::pascal_case( langs::get( util::str2wstr_utf8( description->asString() ) ) );
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 {
descriptionText = util::pascal_case( langs::get( util::str2wstr_utf8( item.description ) ) );
tooltip = caption;
}
if (caption != nullptr) {
captionText = util::pascal_case( langs::get( util::str2wstr_utf8( caption->asString() ) ) );
} else {
captionText = util::pascal_case( langs::get( util::str2wstr_utf8( item.caption ) ) );
}
tooltip = captionText + L"\n" + descriptionText;
} else {
tooltip.clear();
}
@ -215,7 +250,7 @@ void SlotView::draw(const DrawContext& pctx, const Assets& assets) {
cache.countStr = std::to_wstring(stack.getCount());
}
refreshTooltip(stack, item);
cache.stack.set(ItemStack(stack.getItemId(), stack.getCount()));
cache.stack.set(stack);
glm::vec4 tint(1, 1, 1, isEnabled() ? 1 : 0.5f);
glm::vec2 pos = calcPos();