Add get_item_rect function to ItemList

This commit is contained in:
Ninni Pipping 2023-05-08 09:57:49 +02:00
parent fd4a06c515
commit e5fdce7ca3
3 changed files with 26 additions and 0 deletions

View File

@ -64,6 +64,7 @@
<description>
Returns the item index at the given [param position].
When there is no item at that point, -1 will be returned if [param exact] is [code]true[/code], and the closest item index will be returned otherwise.
[b]Note:[/b] The returned value is unreliable if called right after modifying the [ItemList], before it redraws in the next frame.
</description>
</method>
<method name="get_item_custom_bg_color" qualifiers="const">
@ -115,6 +116,15 @@
Returns the metadata value of the specified index.
</description>
</method>
<method name="get_item_rect" qualifiers="const">
<return type="Rect2" />
<param index="0" name="idx" type="int" />
<param index="1" name="expand" type="bool" default="true" />
<description>
Returns the position and size of the item with the specified index, in the coordinate system of the [ItemList] node. If [param expand] is [code]true[/code] the last column expands to fill the rest of the row.
[b]Note:[/b] The returned value is unreliable if called right after modifying the [ItemList], before it redraws in the next frame.
</description>
</method>
<method name="get_item_text" qualifiers="const">
<return type="String" />
<param index="0" name="idx" type="int" />

View File

@ -294,6 +294,18 @@ Color ItemList::get_item_custom_fg_color(int p_idx) const {
return items[p_idx].custom_fg;
}
Rect2 ItemList::get_item_rect(int p_idx, bool p_expand) const {
ERR_FAIL_INDEX_V(p_idx, items.size(), Rect2());
Rect2 ret = items[p_idx].rect_cache;
ret.position += theme_cache.panel_style->get_offset();
if (p_expand && p_idx % current_columns == current_columns - 1) {
ret.size.width = get_size().width - ret.position.x;
}
return ret;
}
void ItemList::set_item_tag_icon(int p_idx, const Ref<Texture2D> &p_tag_icon) {
if (p_idx < 0) {
p_idx += get_item_count();
@ -1778,6 +1790,8 @@ void ItemList::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_item_custom_fg_color", "idx", "custom_fg_color"), &ItemList::set_item_custom_fg_color);
ClassDB::bind_method(D_METHOD("get_item_custom_fg_color", "idx"), &ItemList::get_item_custom_fg_color);
ClassDB::bind_method(D_METHOD("get_item_rect", "idx", "expand"), &ItemList::get_item_rect, DEFVAL(true));
ClassDB::bind_method(D_METHOD("set_item_tooltip_enabled", "idx", "enable"), &ItemList::set_item_tooltip_enabled);
ClassDB::bind_method(D_METHOD("is_item_tooltip_enabled", "idx"), &ItemList::is_item_tooltip_enabled);

View File

@ -211,6 +211,8 @@ public:
void set_item_custom_fg_color(int p_idx, const Color &p_custom_fg_color);
Color get_item_custom_fg_color(int p_idx) const;
Rect2 get_item_rect(int p_idx, bool p_expand = true) const;
void set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior);
TextServer::OverrunBehavior get_text_overrun_behavior() const;