diff --git a/doc/classes/RichTextLabel.xml b/doc/classes/RichTextLabel.xml index d75d3043e02..47c95c97ff5 100644 --- a/doc/classes/RichTextLabel.xml +++ b/doc/classes/RichTextLabel.xml @@ -112,6 +112,15 @@ [b]Note:[/b] If [member threaded] is enabled, this method returns a value for the loaded part of the document. Use [method is_finished] or [signal finished] to determine whether document is fully loaded. + + + + + Returns the indexes of the first and last visible characters for the given [param line], as a [Vector2i]. + [b]Note:[/b] If [member visible_characters_behavior] is set to [constant TextServer.VC_CHARS_BEFORE_SHAPING] only visible wrapped lines are counted. + [b]Note:[/b] If [member threaded] is enabled, this method returns a value for the loaded part of the document. Use [method is_finished] or [signal finished] to determine whether document is fully loaded. + + diff --git a/scene/gui/rich_text_label.cpp b/scene/gui/rich_text_label.cpp index be54ef7c94e..99e3330a81d 100644 --- a/scene/gui/rich_text_label.cpp +++ b/scene/gui/rich_text_label.cpp @@ -5562,6 +5562,26 @@ int RichTextLabel::get_line_count() const { return line_count; } +Vector2i RichTextLabel::get_line_range(int p_line) { + const_cast(this)->_validate_line_caches(); + + int line_count = 0; + int to_line = main->first_invalid_line.load(); + for (int i = 0; i < to_line; i++) { + MutexLock lock(main->lines[i].text_buf->get_mutex()); + int lc = main->lines[i].text_buf->get_line_count(); + + if (p_line < line_count + lc) { + Vector2i char_offset = Vector2i(main->lines[i].char_offset, main->lines[i].char_offset); + Vector2i line_range = main->lines[i].text_buf->get_line_range(p_line - line_count); + return char_offset + line_range; + } + + line_count += lc; + } + return Vector2i(); +} + int RichTextLabel::get_visible_line_count() const { if (!is_visible()) { return 0; @@ -6450,6 +6470,7 @@ void RichTextLabel::_bind_methods() { ClassDB::bind_method(D_METHOD("is_using_bbcode"), &RichTextLabel::is_using_bbcode); ClassDB::bind_method(D_METHOD("get_line_count"), &RichTextLabel::get_line_count); + ClassDB::bind_method(D_METHOD("get_line_range", "line"), &RichTextLabel::get_line_range); ClassDB::bind_method(D_METHOD("get_visible_line_count"), &RichTextLabel::get_visible_line_count); ClassDB::bind_method(D_METHOD("get_paragraph_count"), &RichTextLabel::get_paragraph_count); diff --git a/scene/gui/rich_text_label.h b/scene/gui/rich_text_label.h index d9aac546ca0..c6ea7adcc57 100644 --- a/scene/gui/rich_text_label.h +++ b/scene/gui/rich_text_label.h @@ -771,6 +771,7 @@ public: void scroll_to_line(int p_line); int get_line_count() const; + Vector2i get_line_range(int p_line); int get_visible_line_count() const; int get_content_height() const;