Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_line_range() to RichTextLabel #100432

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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);
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 @@ -771,6 +771,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
Loading