From 3b4748f6b5865393322bd76f210e75f6a27fe5b6 Mon Sep 17 00:00:00 2001 From: Nikola Bunjevac Date: Fri, 5 May 2023 21:10:27 +0200 Subject: [PATCH] Fix RichTextLabel character line and paragraph index getters (cherry picked from commit e2536d0643cf23fe1854c7eac0ecdb0295bb82e6) --- scene/gui/rich_text_label.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/scene/gui/rich_text_label.cpp b/scene/gui/rich_text_label.cpp index 8fb2024e9cf..d131477e1b5 100644 --- a/scene/gui/rich_text_label.cpp +++ b/scene/gui/rich_text_label.cpp @@ -5720,10 +5720,12 @@ int RichTextLabel::get_character_line(int p_char) { 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()); - if (main->lines[i].char_offset < p_char && p_char <= main->lines[i].char_offset + main->lines[i].char_count) { + int char_offset = main->lines[i].char_offset; + int char_count = main->lines[i].char_count; + if (char_offset <= p_char && p_char < char_offset + 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) { + if (char_offset + range.x <= p_char && p_char <= char_offset + range.y) { return line_count; } line_count++; @@ -5738,13 +5740,11 @@ int RichTextLabel::get_character_line(int p_char) { int RichTextLabel::get_character_paragraph(int p_char) { _validate_line_caches(); - int para_count = 0; int to_line = main->first_invalid_line.load(); for (int i = 0; i < to_line; 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++; + int char_offset = main->lines[i].char_offset; + if (char_offset <= p_char && p_char < char_offset + main->lines[i].char_count) { + return i; } } return -1;