Browse Source

Merge pull request #100432 from markdibarry/add_get_line_range_rtl

Add `get_line_range()` to `RichTextLabel`
Thaddeus Crews 8 tháng trước cách đây
mục cha
commit
4d4c229a83

+ 9 - 0
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.
 				[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>
 			</description>
 		</method>
 		</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">
 		<method name="get_menu" qualifiers="const">
 			<return type="PopupMenu" />
 			<return type="PopupMenu" />
 			<description>
 			<description>

+ 21 - 0
scene/gui/rich_text_label.cpp

@@ -5562,6 +5562,26 @@ int RichTextLabel::get_line_count() const {
 	return line_count;
 	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 {
 int RichTextLabel::get_visible_line_count() const {
 	if (!is_visible()) {
 	if (!is_visible()) {
 		return 0;
 		return 0;
@@ -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("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_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_visible_line_count"), &RichTextLabel::get_visible_line_count);
 
 
 	ClassDB::bind_method(D_METHOD("get_paragraph_count"), &RichTextLabel::get_paragraph_count);
 	ClassDB::bind_method(D_METHOD("get_paragraph_count"), &RichTextLabel::get_paragraph_count);

+ 1 - 0
scene/gui/rich_text_label.h

@@ -772,6 +772,7 @@ public:
 
 
 	void scroll_to_line(int p_line);
 	void scroll_to_line(int p_line);
 	int get_line_count() const;
 	int get_line_count() const;
+	Vector2i get_line_range(int p_line);
 	int get_visible_line_count() const;
 	int get_visible_line_count() const;
 
 
 	int get_content_height() const;
 	int get_content_height() const;