diff --git a/doc/classes/ItemList.xml b/doc/classes/ItemList.xml index 6e5ff83a350..2c322027e75 100644 --- a/doc/classes/ItemList.xml +++ b/doc/classes/ItemList.xml @@ -12,18 +12,18 @@ - + - Adds an item to the item list with no text, only an icon. + Adds an item to the item list with no text, only an icon. Returns the index of an added item. - + @@ -32,7 +32,8 @@ - Adds an item to the item list with specified text. Specify an [code]icon[/code], or use [code]null[/code] as the [code]icon[/code] for a list item with no icon. + Adds an item to the item list with specified text. Returns the index of an added item. + Specify an [code]icon[/code], or use [code]null[/code] as the [code]icon[/code] for a list item with no icon. If selectable is [code]true[/code], the list item will be selectable. diff --git a/scene/gui/item_list.cpp b/scene/gui/item_list.cpp index 69ca96b28e7..03ce6bdd3d7 100644 --- a/scene/gui/item_list.cpp +++ b/scene/gui/item_list.cpp @@ -50,7 +50,7 @@ void ItemList::_shape(int p_idx) { } } -void ItemList::add_item(const String &p_item, const Ref &p_texture, bool p_selectable) { +int ItemList::add_item(const String &p_item, const Ref &p_texture, bool p_selectable) { Item item; item.icon = p_texture; item.icon_transposed = false; @@ -64,14 +64,16 @@ void ItemList::add_item(const String &p_item, const Ref &p_texture, b item.tooltip_enabled = true; item.custom_bg = Color(0, 0, 0, 0); items.push_back(item); + int item_id = items.size() - 1; _shape(items.size() - 1); update(); shape_changed = true; + return item_id; } -void ItemList::add_icon_item(const Ref &p_item, bool p_selectable) { +int ItemList::add_icon_item(const Ref &p_item, bool p_selectable) { Item item; item.icon = p_item; item.icon_transposed = false; @@ -85,9 +87,11 @@ void ItemList::add_icon_item(const Ref &p_item, bool p_selectable) { item.tooltip_enabled = true; item.custom_bg = Color(0, 0, 0, 0); items.push_back(item); + int item_id = items.size() - 1; update(); shape_changed = true; + return item_id; } void ItemList::set_item_text(int p_idx, const String &p_text) { diff --git a/scene/gui/item_list.h b/scene/gui/item_list.h index d08823c3986..4982a68071a 100644 --- a/scene/gui/item_list.h +++ b/scene/gui/item_list.h @@ -130,8 +130,8 @@ protected: static void _bind_methods(); public: - void add_item(const String &p_item, const Ref &p_texture = Ref(), bool p_selectable = true); - void add_icon_item(const Ref &p_item, bool p_selectable = true); + int add_item(const String &p_item, const Ref &p_texture = Ref(), bool p_selectable = true); + int add_icon_item(const Ref &p_item, bool p_selectable = true); void set_item_text(int p_idx, const String &p_text); String get_item_text(int p_idx) const;