Skip to content

Commit 6ad0a1f

Browse files
committed
Merge pull request #91098 from bruvzg/rtl_fast_delete
[RTL] Add extra argument to `remove_paragraph` to skip cache invalidation and a method for manual cache invalidation.
2 parents 28a18cf + 360d365 commit 6ad0a1f

File tree

5 files changed

+64
-4
lines changed

5 files changed

+64
-4
lines changed

doc/classes/RichTextLabel.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,13 @@
249249
[/codeblock]
250250
</description>
251251
</method>
252+
<method name="invalidate_paragraph">
253+
<return type="bool" />
254+
<param index="0" name="paragraph" type="int" />
255+
<description>
256+
Invalidates [param paragraph] and all subsequent paragraphs cache.
257+
</description>
258+
</method>
252259
<method name="is_menu_visible" qualifiers="const">
253260
<return type="bool" />
254261
<description>
@@ -497,9 +504,11 @@
497504
<method name="remove_paragraph">
498505
<return type="bool" />
499506
<param index="0" name="paragraph" type="int" />
507+
<param index="1" name="no_invalidate" type="bool" default="false" />
500508
<description>
501509
Removes a paragraph of content from the label. Returns [code]true[/code] if the paragraph exists.
502510
The [param paragraph] argument is the index of the paragraph to remove, it can take values in the interval [code][0, get_paragraph_count() - 1][/code].
511+
If [param no_invalidate] is set to [code]true[/code], cache for the subsequent paragraphs is not invalidated. Use it for faster updates if deleted paragraph is fully self-contained (have no unclosed tags), or this call is part of the complex edit operation and [method invalidate_paragraph] will be called at the end of operation.
503512
</description>
504513
</method>
505514
<method name="scroll_to_line">

misc/extension_api_validation/4.2-stable.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,10 @@ GH-84472
330330
Validate extension JSON: Error: Field 'classes/CanvasItem/methods/draw_circle/arguments': size changed value in new API, from 3 to 6.
331331

332332
Optional arguments added. Compatibility methods registered.
333+
334+
335+
GH-91098
336+
--------
337+
Validate extension JSON: Error: Field 'classes/RichTextLabel/methods/remove_paragraph/arguments': size changed value in new API, from 1 to 2.
338+
339+
Added optional argument. Compatibility method registered.

scene/gui/rich_text_label.compat.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,14 @@ void RichTextLabel::_add_image_bind_compat_80410(const Ref<Texture2D> &p_image,
3838
add_image(p_image, p_width, p_height, p_color, p_alignment, p_region, Variant(), false, String(), false);
3939
}
4040

41+
bool RichTextLabel::_remove_paragraph_bind_compat_91098(int p_paragraph) {
42+
return remove_paragraph(p_paragraph, false);
43+
}
44+
4145
void RichTextLabel::_bind_compatibility_methods() {
4246
ClassDB::bind_compatibility_method(D_METHOD("push_meta", "data"), &RichTextLabel::_push_meta_bind_compat_89024);
4347
ClassDB::bind_compatibility_method(D_METHOD("add_image", "image", "width", "height", "color", "inline_align", "region"), &RichTextLabel::_add_image_bind_compat_80410, DEFVAL(0), DEFVAL(0), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(INLINE_ALIGNMENT_CENTER), DEFVAL(Rect2()));
48+
ClassDB::bind_compatibility_method(D_METHOD("remove_paragraph", "paragraph"), &RichTextLabel::_remove_paragraph_bind_compat_91098);
4449
}
4550

4651
#endif // DISABLE_DEPRECATED

scene/gui/rich_text_label.cpp

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3342,7 +3342,7 @@ void RichTextLabel::_remove_frame(HashSet<Item *> &r_erase_list, ItemFrame *p_fr
33423342
}
33433343
}
33443344

3345-
bool RichTextLabel::remove_paragraph(const int p_paragraph) {
3345+
bool RichTextLabel::remove_paragraph(int p_paragraph, bool p_no_invalidate) {
33463346
_stop_thread();
33473347
MutexLock data_lock(data_mutex);
33483348

@@ -3391,8 +3391,44 @@ bool RichTextLabel::remove_paragraph(const int p_paragraph) {
33913391

33923392
selection.click_frame = nullptr;
33933393
selection.click_item = nullptr;
3394-
deselect();
3394+
selection.active = false;
3395+
3396+
if (p_no_invalidate) {
3397+
// Do not invalidate cache, only update vertical offsets of the paragraphs after deleted one and scrollbar.
3398+
int to_line = main->first_invalid_line.load() - 1;
3399+
float total_height = (p_paragraph == 0) ? 0 : _calculate_line_vertical_offset(main->lines[p_paragraph - 1]);
3400+
for (int i = p_paragraph; i < to_line; i++) {
3401+
MutexLock lock(main->lines[to_line - 1].text_buf->get_mutex());
3402+
main->lines[i].offset.y = total_height;
3403+
total_height = _calculate_line_vertical_offset(main->lines[i]);
3404+
}
3405+
updating_scroll = true;
3406+
vscroll->set_max(total_height);
3407+
updating_scroll = false;
3408+
3409+
main->first_invalid_line.store(MAX(main->first_invalid_line.load() - 1, 0));
3410+
main->first_resized_line.store(MAX(main->first_resized_line.load() - 1, 0));
3411+
main->first_invalid_font_line.store(MAX(main->first_invalid_font_line.load() - 1, 0));
3412+
} else {
3413+
// Invalidate cache after the deleted paragraph.
3414+
main->first_invalid_line.store(MIN(main->first_invalid_line.load(), p_paragraph));
3415+
main->first_resized_line.store(MIN(main->first_resized_line.load(), p_paragraph));
3416+
main->first_invalid_font_line.store(MIN(main->first_invalid_font_line.load(), p_paragraph));
3417+
}
3418+
queue_redraw();
3419+
3420+
return true;
3421+
}
3422+
3423+
bool RichTextLabel::invalidate_paragraph(int p_paragraph) {
3424+
_stop_thread();
3425+
MutexLock data_lock(data_mutex);
3426+
3427+
if (p_paragraph >= (int)main->lines.size() || p_paragraph < 0) {
3428+
return false;
3429+
}
33953430

3431+
// Invalidate cache.
33963432
main->first_invalid_line.store(MIN(main->first_invalid_line.load(), p_paragraph));
33973433
main->first_resized_line.store(MIN(main->first_resized_line.load(), p_paragraph));
33983434
main->first_invalid_font_line.store(MIN(main->first_invalid_font_line.load(), p_paragraph));
@@ -5851,7 +5887,8 @@ void RichTextLabel::_bind_methods() {
58515887
ClassDB::bind_method(D_METHOD("add_image", "image", "width", "height", "color", "inline_align", "region", "key", "pad", "tooltip", "size_in_percent"), &RichTextLabel::add_image, DEFVAL(0), DEFVAL(0), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(INLINE_ALIGNMENT_CENTER), DEFVAL(Rect2()), DEFVAL(Variant()), DEFVAL(false), DEFVAL(String()), DEFVAL(false));
58525888
ClassDB::bind_method(D_METHOD("update_image", "key", "mask", "image", "width", "height", "color", "inline_align", "region", "pad", "tooltip", "size_in_percent"), &RichTextLabel::update_image, DEFVAL(0), DEFVAL(0), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(INLINE_ALIGNMENT_CENTER), DEFVAL(Rect2()), DEFVAL(false), DEFVAL(String()), DEFVAL(false));
58535889
ClassDB::bind_method(D_METHOD("newline"), &RichTextLabel::add_newline);
5854-
ClassDB::bind_method(D_METHOD("remove_paragraph", "paragraph"), &RichTextLabel::remove_paragraph);
5890+
ClassDB::bind_method(D_METHOD("remove_paragraph", "paragraph", "no_invalidate"), &RichTextLabel::remove_paragraph, DEFVAL(false));
5891+
ClassDB::bind_method(D_METHOD("invalidate_paragraph", "paragraph"), &RichTextLabel::invalidate_paragraph);
58555892
ClassDB::bind_method(D_METHOD("push_font", "font", "font_size"), &RichTextLabel::push_font, DEFVAL(0));
58565893
ClassDB::bind_method(D_METHOD("push_font_size", "font_size"), &RichTextLabel::push_font_size);
58575894
ClassDB::bind_method(D_METHOD("push_normal"), &RichTextLabel::push_normal);

scene/gui/rich_text_label.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class RichTextLabel : public Control {
134134
#ifndef DISABLE_DEPRECATED
135135
void _push_meta_bind_compat_89024(const Variant &p_meta);
136136
void _add_image_bind_compat_80410(const Ref<Texture2D> &p_image, const int p_width, const int p_height, const Color &p_color, InlineAlignment p_alignment, const Rect2 &p_region);
137+
bool _remove_paragraph_bind_compat_91098(int p_paragraph);
137138
static void _bind_compatibility_methods();
138139
#endif
139140

@@ -664,7 +665,8 @@ class RichTextLabel : public Control {
664665
void add_image(const Ref<Texture2D> &p_image, int p_width = 0, int p_height = 0, const Color &p_color = Color(1.0, 1.0, 1.0), InlineAlignment p_alignment = INLINE_ALIGNMENT_CENTER, const Rect2 &p_region = Rect2(), const Variant &p_key = Variant(), bool p_pad = false, const String &p_tooltip = String(), bool p_size_in_percent = false);
665666
void update_image(const Variant &p_key, BitField<ImageUpdateMask> p_mask, const Ref<Texture2D> &p_image, int p_width = 0, int p_height = 0, const Color &p_color = Color(1.0, 1.0, 1.0), InlineAlignment p_alignment = INLINE_ALIGNMENT_CENTER, const Rect2 &p_region = Rect2(), bool p_pad = false, const String &p_tooltip = String(), bool p_size_in_percent = false);
666667
void add_newline();
667-
bool remove_paragraph(const int p_paragraph);
668+
bool remove_paragraph(int p_paragraph, bool p_no_invalidate = false);
669+
bool invalidate_paragraph(int p_paragraph);
668670
void push_dropcap(const String &p_string, const Ref<Font> &p_font, int p_size, const Rect2 &p_dropcap_margins = Rect2(), const Color &p_color = Color(1, 1, 1), int p_ol_size = 0, const Color &p_ol_color = Color(0, 0, 0, 0));
669671
void _push_def_font(DefaultFont p_def_font);
670672
void _push_def_font_var(DefaultFont p_def_font, const Ref<Font> &p_font, int p_size = -1);

0 commit comments

Comments
 (0)