Explorar o código

Add `get_item_rect` function to `ItemList`

Ninni Pipping %!s(int64=2) %!d(string=hai) anos
pai
achega
e5fdce7ca3
Modificáronse 3 ficheiros con 26 adicións e 0 borrados
  1. 10 0
      doc/classes/ItemList.xml
  2. 14 0
      scene/gui/item_list.cpp
  3. 2 0
      scene/gui/item_list.h

+ 10 - 0
doc/classes/ItemList.xml

@@ -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" />

+ 14 - 0
scene/gui/item_list.cpp

@@ -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);
 

+ 2 - 0
scene/gui/item_list.h

@@ -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;