Add ItemStack:get_description() to get tooltip (#8847)

master
Paul Ouellette 2019-08-24 12:38:02 -04:00 committed by sfan5
parent efbac7e446
commit 008b80fe1c
6 changed files with 33 additions and 22 deletions

View File

@ -5254,6 +5254,7 @@ an itemstring, a table or `nil`.
* `get_metadata()`: (DEPRECATED) Returns metadata (a string attached to an item * `get_metadata()`: (DEPRECATED) Returns metadata (a string attached to an item
stack). stack).
* `set_metadata(metadata)`: (DEPRECATED) Returns true. * `set_metadata(metadata)`: (DEPRECATED) Returns true.
* `get_description()`: returns the description shown in inventory list tooltips.
* `clear()`: removes all items from the stack, making it empty. * `clear()`: removes all items from the stack, making it empty.
* `replace(item)`: replace the contents of this stack. * `replace(item)`: replace the contents of this stack.
* `item` can also be an itemstring or table. * `item` can also be an itemstring or table.

View File

@ -2844,40 +2844,26 @@ void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int layer,
} }
if (layer == 1) { if (layer == 1) {
// Draw item stack
if (selected) if (selected)
item.takeItem(m_selected_amount); item.takeItem(m_selected_amount);
if (!item.empty()) { if (!item.empty()) {
// Draw item stack
drawItemStack(driver, m_font, item, drawItemStack(driver, m_font, item,
rect, &AbsoluteClippingRect, m_client, rect, &AbsoluteClippingRect, m_client,
rotation_kind); rotation_kind);
}
// Draw tooltip // Draw tooltip
std::wstring tooltip_text;
if (hovering && !m_selected_item) { if (hovering && !m_selected_item) {
const std::string &desc = item.metadata.getString("description"); std::string tooltip = item.getDescription(m_client->idef());
if (desc.empty()) if (m_tooltip_append_itemname)
tooltip_text = tooltip += "\n[" + item.name + "]";
utf8_to_wide(item.getDefinition(m_client->idef()).description); showTooltip(utf8_to_wide(tooltip), m_default_tooltip_color,
else
tooltip_text = utf8_to_wide(desc);
if (!item.name.empty()) {
if (tooltip_text.empty())
tooltip_text = utf8_to_wide(item.name);
else if (m_tooltip_append_itemname)
tooltip_text += utf8_to_wide("\n[" + item.name + "]");
}
}
if (!tooltip_text.empty()) {
showTooltip(tooltip_text, m_default_tooltip_color,
m_default_tooltip_bgcolor); m_default_tooltip_bgcolor);
} }
} }
} }
} }
}
void GUIFormSpecMenu::drawSelectedItem() void GUIFormSpecMenu::drawSelectedItem()
{ {

View File

@ -246,6 +246,14 @@ std::string ItemStack::getItemString() const
return os.str(); return os.str();
} }
std::string ItemStack::getDescription(IItemDefManager *itemdef) const
{
std::string desc = metadata.getString("description");
if (desc.empty())
desc = getDefinition(itemdef).description;
return desc.empty() ? name : desc;
}
ItemStack ItemStack::addItem(ItemStack newitem, IItemDefManager *itemdef) ItemStack ItemStack::addItem(ItemStack newitem, IItemDefManager *itemdef)
{ {

View File

@ -47,6 +47,8 @@ struct ItemStack
// Returns the string used for inventory // Returns the string used for inventory
std::string getItemString() const; std::string getItemString() const;
// Returns the tooltip
std::string getDescription(IItemDefManager *itemdef) const;
/* /*
Quantity methods Quantity methods

View File

@ -175,6 +175,16 @@ int LuaItemStack::l_set_metadata(lua_State *L)
return 1; return 1;
} }
// get_description(self)
int LuaItemStack::l_get_description(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
std::string desc = o->m_stack.getDescription(getGameDef(L)->idef());
lua_pushstring(L, desc.c_str());
return 1;
}
// clear(self) -> true // clear(self) -> true
int LuaItemStack::l_clear(lua_State *L) int LuaItemStack::l_clear(lua_State *L)
{ {
@ -470,6 +480,7 @@ const luaL_Reg LuaItemStack::methods[] = {
luamethod(LuaItemStack, get_meta), luamethod(LuaItemStack, get_meta),
luamethod(LuaItemStack, get_metadata), luamethod(LuaItemStack, get_metadata),
luamethod(LuaItemStack, set_metadata), luamethod(LuaItemStack, set_metadata),
luamethod(LuaItemStack, get_description),
luamethod(LuaItemStack, clear), luamethod(LuaItemStack, clear),
luamethod(LuaItemStack, replace), luamethod(LuaItemStack, replace),
luamethod(LuaItemStack, to_string), luamethod(LuaItemStack, to_string),

View File

@ -66,6 +66,9 @@ private:
// set_metadata(self, string) // set_metadata(self, string)
static int l_set_metadata(lua_State *L); static int l_set_metadata(lua_State *L);
// get_description(self)
static int l_get_description(lua_State *L);
// clear(self) -> true // clear(self) -> true
static int l_clear(lua_State *L); static int l_clear(lua_State *L);