Selaa lähdekoodia

Merge pull request #57988 from markdibarry/add_get_last_visible_character_line

Rémi Verschelde 3 vuotta sitten
vanhempi
commit
89996be091
3 muutettua tiedostoa jossa 48 lisäystä ja 0 poistoa
  1. 14 0
      doc/classes/RichTextLabel.xml
  2. 32 0
      scene/gui/rich_text_label.cpp
  3. 2 0
      scene/gui/rich_text_label.h

+ 14 - 0
doc/classes/RichTextLabel.xml

@@ -49,6 +49,20 @@
 				Clears the tag stack and sets [member text] to an empty string.
 			</description>
 		</method>
+		<method name="get_character_line">
+			<return type="int" />
+			<argument index="0" name="character" type="int" />
+			<description>
+				Returns the line number of the character position provided.
+			</description>
+		</method>
+		<method name="get_character_paragraph">
+			<return type="int" />
+			<argument index="0" name="character" type="int" />
+			<description>
+				Returns the paragraph number of the character position provided.
+			</description>
+		</method>
 		<method name="get_content_height" qualifiers="const">
 			<return type="int" />
 			<description>

+ 32 - 0
scene/gui/rich_text_label.cpp

@@ -4327,6 +4327,8 @@ void RichTextLabel::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("set_percent_visible", "percent_visible"), &RichTextLabel::set_percent_visible);
 	ClassDB::bind_method(D_METHOD("get_percent_visible"), &RichTextLabel::get_percent_visible);
 
+	ClassDB::bind_method(D_METHOD("get_character_line", "character"), &RichTextLabel::get_character_line);
+	ClassDB::bind_method(D_METHOD("get_character_paragraph", "character"), &RichTextLabel::get_character_paragraph);
 	ClassDB::bind_method(D_METHOD("get_total_character_count"), &RichTextLabel::get_total_character_count);
 
 	ClassDB::bind_method(D_METHOD("set_use_bbcode", "enable"), &RichTextLabel::set_use_bbcode);
@@ -4459,6 +4461,36 @@ int RichTextLabel::get_visible_characters() const {
 	return visible_characters;
 }
 
+int RichTextLabel::get_character_line(int p_char) {
+	int line_count = 0;
+	for (int i = 0; i < main->lines.size(); i++) {
+		if (main->lines[i].char_offset < p_char && p_char <= main->lines[i].char_offset + main->lines[i].char_count) {
+			for (int j = 0; j < main->lines[i].text_buf->get_line_count(); j++) {
+				Vector2i range = main->lines[i].text_buf->get_line_range(j);
+				if (main->lines[i].char_offset + range.x < p_char && p_char <= main->lines[i].char_offset + range.y) {
+					return line_count;
+				}
+				line_count++;
+			}
+		} else {
+			line_count += main->lines[i].text_buf->get_line_count();
+		}
+	}
+	return -1;
+}
+
+int RichTextLabel::get_character_paragraph(int p_char) {
+	int para_count = 0;
+	for (int i = 0; i < main->lines.size(); i++) {
+		if (main->lines[i].char_offset < p_char && p_char <= main->lines[i].char_offset + main->lines[i].char_count) {
+			return para_count;
+		} else {
+			para_count++;
+		}
+	}
+	return -1;
+}
+
 int RichTextLabel::get_total_character_count() const {
 	// Note: Do not use line buffer "char_count", it includes only visible characters.
 	int tc = 0;

+ 2 - 0
scene/gui/rich_text_label.h

@@ -598,6 +598,8 @@ public:
 
 	void set_visible_characters(int p_visible);
 	int get_visible_characters() const;
+	int get_character_line(int p_char);
+	int get_character_paragraph(int p_char);
 	int get_total_character_count() const;
 	int get_total_glyph_count() const;