Add ItemStack:get_description() to get tooltip (#8847)
authorPaul Ouellette <oue.paul18@gmail.com>
Sat, 24 Aug 2019 16:38:02 +0000 (12:38 -0400)
committersfan5 <sfan5@live.de>
Sat, 24 Aug 2019 16:38:02 +0000 (18:38 +0200)
doc/lua_api.txt
src/gui/guiFormSpecMenu.cpp
src/inventory.cpp
src/inventory.h
src/script/lua_api/l_item.cpp
src/script/lua_api/l_item.h

index c7f6908adda5a192083aff24ddeb963a75250ee9..eaca09fe99d895e2463fe6a455c874e4874d4717 100644 (file)
@@ -5254,6 +5254,7 @@ an itemstring, a table or `nil`.
 * `get_metadata()`: (DEPRECATED) Returns metadata (a string attached to an item
   stack).
 * `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.
 * `replace(item)`: replace the contents of this stack.
     * `item` can also be an itemstring or table.
index e7eaefd754e1d6d2c5c0b0aba784a496836dca27..c0fa2470b64237137112fa6c6e31cd6ecbf3af66 100644 (file)
@@ -2844,37 +2844,23 @@ void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int layer,
                }
 
                if (layer == 1) {
-                       // Draw item stack
                        if (selected)
                                item.takeItem(m_selected_amount);
 
                        if (!item.empty()) {
+                               // Draw item stack
                                drawItemStack(driver, m_font, item,
                                        rect, &AbsoluteClippingRect, m_client,
                                        rotation_kind);
-                       }
-
-                       // Draw tooltip
-                       std::wstring tooltip_text;
-                       if (hovering && !m_selected_item) {
-                               const std::string &desc = item.metadata.getString("description");
-                               if (desc.empty())
-                                       tooltip_text =
-                                               utf8_to_wide(item.getDefinition(m_client->idef()).description);
-                               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 + "]");
+                               // Draw tooltip
+                               if (hovering && !m_selected_item) {
+                                       std::string tooltip = item.getDescription(m_client->idef());
+                                       if (m_tooltip_append_itemname)
+                                               tooltip += "\n[" + item.name + "]";
+                                       showTooltip(utf8_to_wide(tooltip), m_default_tooltip_color,
+                                                       m_default_tooltip_bgcolor);
                                }
                        }
-                       if (!tooltip_text.empty()) {
-                               showTooltip(tooltip_text, m_default_tooltip_color,
-                                       m_default_tooltip_bgcolor);
-                       }
                }
        }
 }
index 40dc602d0e855883a1d0a42549bdedb117c1fcda..f2cc2ede39796ee9e3bff9ee0c28e0692962b921 100644 (file)
@@ -246,6 +246,14 @@ std::string ItemStack::getItemString() const
        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)
 {
index 95a0bb43e7594fe33432c3726ff1b6c2a9b1b0a5..3f299993a27026284d1b456ea81ec04e58e333e2 100644 (file)
@@ -47,6 +47,8 @@ struct ItemStack
 
        // Returns the string used for inventory
        std::string getItemString() const;
+       // Returns the tooltip
+       std::string getDescription(IItemDefManager *itemdef) const;
 
        /*
                Quantity methods
index e41d23fd148fdd11f21ee6a526a526dc61023195..f9708b560f4f580bbbd5693702c60ca7ea9fdcdf 100644 (file)
@@ -175,6 +175,16 @@ int LuaItemStack::l_set_metadata(lua_State *L)
        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
 int LuaItemStack::l_clear(lua_State *L)
 {
@@ -470,6 +480,7 @@ const luaL_Reg LuaItemStack::methods[] = {
        luamethod(LuaItemStack, get_meta),
        luamethod(LuaItemStack, get_metadata),
        luamethod(LuaItemStack, set_metadata),
+       luamethod(LuaItemStack, get_description),
        luamethod(LuaItemStack, clear),
        luamethod(LuaItemStack, replace),
        luamethod(LuaItemStack, to_string),
index 5ff715b2a36fe9bf7ebf10cbdca9c801a542dd3f..6fab58045774b6de9d4189ad2e9fbaf86889b049 100644 (file)
@@ -66,6 +66,9 @@ private:
        // set_metadata(self, string)
        static int l_set_metadata(lua_State *L);
 
+       // get_description(self)
+       static int l_get_description(lua_State *L);
+
        // clear(self) -> true
        static int l_clear(lua_State *L);