Skip to content

Commit

Permalink
Merge pull request #100432 from markdibarry/add_get_line_range_rtl
Browse files Browse the repository at this point in the history
Add `get_line_range()` to `RichTextLabel`
  • Loading branch information
Repiteo committed Dec 16, 2024
2 parents 802effe + 53a1be6 commit 4d4c229
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
9 changes: 9 additions & 0 deletions doc/classes/RichTextLabel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
</description>
</method>
<method name="get_line_range">
<return type="Vector2i" />
<param index="0" name="line" type="int" />
<description>
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.
</description>
</method>
<method name="get_menu" qualifiers="const">
<return type="PopupMenu" />
<description>
Expand Down
21 changes: 21 additions & 0 deletions scene/gui/rich_text_label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5562,6 +5562,26 @@ int RichTextLabel::get_line_count() const {
return line_count;
}

Vector2i RichTextLabel::get_line_range(int p_line) {
const_cast<RichTextLabel *>(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;
Expand Down Expand Up @@ -6455,6 +6475,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);
Expand Down
1 change: 1 addition & 0 deletions scene/gui/rich_text_label.h
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@ class RichTextLabel : public Control {

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;
Expand Down

0 comments on commit 4d4c229

Please sign in to comment.