Merge pull request #33685 from Scony/improve-item-lists-add-item-methods

ItemList's add_(icon_)item method returns id of added item
This commit is contained in:
Rémi Verschelde 2021-01-15 16:24:20 +01:00 committed by GitHub
commit 1ffe3ed47a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 8 deletions

View File

@ -12,18 +12,18 @@
</tutorials> </tutorials>
<methods> <methods>
<method name="add_icon_item"> <method name="add_icon_item">
<return type="void"> <return type="int">
</return> </return>
<argument index="0" name="icon" type="Texture2D"> <argument index="0" name="icon" type="Texture2D">
</argument> </argument>
<argument index="1" name="selectable" type="bool" default="true"> <argument index="1" name="selectable" type="bool" default="true">
</argument> </argument>
<description> <description>
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.
</description> </description>
</method> </method>
<method name="add_item"> <method name="add_item">
<return type="void"> <return type="int">
</return> </return>
<argument index="0" name="text" type="String"> <argument index="0" name="text" type="String">
</argument> </argument>
@ -32,7 +32,8 @@
<argument index="2" name="selectable" type="bool" default="true"> <argument index="2" name="selectable" type="bool" default="true">
</argument> </argument>
<description> <description>
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. If selectable is [code]true[/code], the list item will be selectable.
</description> </description>
</method> </method>

View File

@ -50,7 +50,7 @@ void ItemList::_shape(int p_idx) {
} }
} }
void ItemList::add_item(const String &p_item, const Ref<Texture2D> &p_texture, bool p_selectable) { int ItemList::add_item(const String &p_item, const Ref<Texture2D> &p_texture, bool p_selectable) {
Item item; Item item;
item.icon = p_texture; item.icon = p_texture;
item.icon_transposed = false; item.icon_transposed = false;
@ -64,14 +64,16 @@ void ItemList::add_item(const String &p_item, const Ref<Texture2D> &p_texture, b
item.tooltip_enabled = true; item.tooltip_enabled = true;
item.custom_bg = Color(0, 0, 0, 0); item.custom_bg = Color(0, 0, 0, 0);
items.push_back(item); items.push_back(item);
int item_id = items.size() - 1;
_shape(items.size() - 1); _shape(items.size() - 1);
update(); update();
shape_changed = true; shape_changed = true;
return item_id;
} }
void ItemList::add_icon_item(const Ref<Texture2D> &p_item, bool p_selectable) { int ItemList::add_icon_item(const Ref<Texture2D> &p_item, bool p_selectable) {
Item item; Item item;
item.icon = p_item; item.icon = p_item;
item.icon_transposed = false; item.icon_transposed = false;
@ -85,9 +87,11 @@ void ItemList::add_icon_item(const Ref<Texture2D> &p_item, bool p_selectable) {
item.tooltip_enabled = true; item.tooltip_enabled = true;
item.custom_bg = Color(0, 0, 0, 0); item.custom_bg = Color(0, 0, 0, 0);
items.push_back(item); items.push_back(item);
int item_id = items.size() - 1;
update(); update();
shape_changed = true; shape_changed = true;
return item_id;
} }
void ItemList::set_item_text(int p_idx, const String &p_text) { void ItemList::set_item_text(int p_idx, const String &p_text) {

View File

@ -130,8 +130,8 @@ protected:
static void _bind_methods(); static void _bind_methods();
public: public:
void add_item(const String &p_item, const Ref<Texture2D> &p_texture = Ref<Texture2D>(), bool p_selectable = true); int add_item(const String &p_item, const Ref<Texture2D> &p_texture = Ref<Texture2D>(), bool p_selectable = true);
void add_icon_item(const Ref<Texture2D> &p_item, bool p_selectable = true); int add_icon_item(const Ref<Texture2D> &p_item, bool p_selectable = true);
void set_item_text(int p_idx, const String &p_text); void set_item_text(int p_idx, const String &p_text);
String get_item_text(int p_idx) const; String get_item_text(int p_idx) const;