Browse Source

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.
Rémi Verschelde 1 year ago
parent
commit
6ad0a1ffef

+ 9 - 0
doc/classes/RichTextLabel.xml

@@ -249,6 +249,13 @@
 				[/codeblock]
 				[/codeblock]
 			</description>
 			</description>
 		</method>
 		</method>
+		<method name="invalidate_paragraph">
+			<return type="bool" />
+			<param index="0" name="paragraph" type="int" />
+			<description>
+				Invalidates [param paragraph] and all subsequent paragraphs cache.
+			</description>
+		</method>
 		<method name="is_menu_visible" qualifiers="const">
 		<method name="is_menu_visible" qualifiers="const">
 			<return type="bool" />
 			<return type="bool" />
 			<description>
 			<description>
@@ -497,9 +504,11 @@
 		<method name="remove_paragraph">
 		<method name="remove_paragraph">
 			<return type="bool" />
 			<return type="bool" />
 			<param index="0" name="paragraph" type="int" />
 			<param index="0" name="paragraph" type="int" />
+			<param index="1" name="no_invalidate" type="bool" default="false" />
 			<description>
 			<description>
 				Removes a paragraph of content from the label. Returns [code]true[/code] if the paragraph exists.
 				Removes a paragraph of content from the label. Returns [code]true[/code] if the paragraph exists.
 				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].
 				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].
+				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.
 			</description>
 			</description>
 		</method>
 		</method>
 		<method name="scroll_to_line">
 		<method name="scroll_to_line">

+ 7 - 0
misc/extension_api_validation/4.2-stable.expected

@@ -330,3 +330,10 @@ GH-84472
 Validate extension JSON: Error: Field 'classes/CanvasItem/methods/draw_circle/arguments': size changed value in new API, from 3 to 6.
 Validate extension JSON: Error: Field 'classes/CanvasItem/methods/draw_circle/arguments': size changed value in new API, from 3 to 6.
 
 
 Optional arguments added. Compatibility methods registered.
 Optional arguments added. Compatibility methods registered.
+
+
+GH-91098
+--------
+Validate extension JSON: Error: Field 'classes/RichTextLabel/methods/remove_paragraph/arguments': size changed value in new API, from 1 to 2.
+
+Added optional argument. Compatibility method registered.

+ 5 - 0
scene/gui/rich_text_label.compat.inc

@@ -38,9 +38,14 @@ void RichTextLabel::_add_image_bind_compat_80410(const Ref<Texture2D> &p_image,
 	add_image(p_image, p_width, p_height, p_color, p_alignment, p_region, Variant(), false, String(), false);
 	add_image(p_image, p_width, p_height, p_color, p_alignment, p_region, Variant(), false, String(), false);
 }
 }
 
 
+bool RichTextLabel::_remove_paragraph_bind_compat_91098(int p_paragraph) {
+	return remove_paragraph(p_paragraph, false);
+}
+
 void RichTextLabel::_bind_compatibility_methods() {
 void RichTextLabel::_bind_compatibility_methods() {
 	ClassDB::bind_compatibility_method(D_METHOD("push_meta", "data"), &RichTextLabel::_push_meta_bind_compat_89024);
 	ClassDB::bind_compatibility_method(D_METHOD("push_meta", "data"), &RichTextLabel::_push_meta_bind_compat_89024);
 	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()));
 	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()));
+	ClassDB::bind_compatibility_method(D_METHOD("remove_paragraph", "paragraph"), &RichTextLabel::_remove_paragraph_bind_compat_91098);
 }
 }
 
 
 #endif // DISABLE_DEPRECATED
 #endif // DISABLE_DEPRECATED

+ 40 - 3
scene/gui/rich_text_label.cpp

@@ -3342,7 +3342,7 @@ void RichTextLabel::_remove_frame(HashSet<Item *> &r_erase_list, ItemFrame *p_fr
 	}
 	}
 }
 }
 
 
-bool RichTextLabel::remove_paragraph(const int p_paragraph) {
+bool RichTextLabel::remove_paragraph(int p_paragraph, bool p_no_invalidate) {
 	_stop_thread();
 	_stop_thread();
 	MutexLock data_lock(data_mutex);
 	MutexLock data_lock(data_mutex);
 
 
@@ -3391,8 +3391,44 @@ bool RichTextLabel::remove_paragraph(const int p_paragraph) {
 
 
 	selection.click_frame = nullptr;
 	selection.click_frame = nullptr;
 	selection.click_item = nullptr;
 	selection.click_item = nullptr;
-	deselect();
+	selection.active = false;
+
+	if (p_no_invalidate) {
+		// Do not invalidate cache, only update vertical offsets of the paragraphs after deleted one and scrollbar.
+		int to_line = main->first_invalid_line.load() - 1;
+		float total_height = (p_paragraph == 0) ? 0 : _calculate_line_vertical_offset(main->lines[p_paragraph - 1]);
+		for (int i = p_paragraph; i < to_line; i++) {
+			MutexLock lock(main->lines[to_line - 1].text_buf->get_mutex());
+			main->lines[i].offset.y = total_height;
+			total_height = _calculate_line_vertical_offset(main->lines[i]);
+		}
+		updating_scroll = true;
+		vscroll->set_max(total_height);
+		updating_scroll = false;
+
+		main->first_invalid_line.store(MAX(main->first_invalid_line.load() - 1, 0));
+		main->first_resized_line.store(MAX(main->first_resized_line.load() - 1, 0));
+		main->first_invalid_font_line.store(MAX(main->first_invalid_font_line.load() - 1, 0));
+	} else {
+		// Invalidate cache after the deleted paragraph.
+		main->first_invalid_line.store(MIN(main->first_invalid_line.load(), p_paragraph));
+		main->first_resized_line.store(MIN(main->first_resized_line.load(), p_paragraph));
+		main->first_invalid_font_line.store(MIN(main->first_invalid_font_line.load(), p_paragraph));
+	}
+	queue_redraw();
+
+	return true;
+}
+
+bool RichTextLabel::invalidate_paragraph(int p_paragraph) {
+	_stop_thread();
+	MutexLock data_lock(data_mutex);
+
+	if (p_paragraph >= (int)main->lines.size() || p_paragraph < 0) {
+		return false;
+	}
 
 
+	// Invalidate cache.
 	main->first_invalid_line.store(MIN(main->first_invalid_line.load(), p_paragraph));
 	main->first_invalid_line.store(MIN(main->first_invalid_line.load(), p_paragraph));
 	main->first_resized_line.store(MIN(main->first_resized_line.load(), p_paragraph));
 	main->first_resized_line.store(MIN(main->first_resized_line.load(), p_paragraph));
 	main->first_invalid_font_line.store(MIN(main->first_invalid_font_line.load(), p_paragraph));
 	main->first_invalid_font_line.store(MIN(main->first_invalid_font_line.load(), p_paragraph));
@@ -5851,7 +5887,8 @@ void RichTextLabel::_bind_methods() {
 	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));
 	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));
 	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));
 	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));
 	ClassDB::bind_method(D_METHOD("newline"), &RichTextLabel::add_newline);
 	ClassDB::bind_method(D_METHOD("newline"), &RichTextLabel::add_newline);
-	ClassDB::bind_method(D_METHOD("remove_paragraph", "paragraph"), &RichTextLabel::remove_paragraph);
+	ClassDB::bind_method(D_METHOD("remove_paragraph", "paragraph", "no_invalidate"), &RichTextLabel::remove_paragraph, DEFVAL(false));
+	ClassDB::bind_method(D_METHOD("invalidate_paragraph", "paragraph"), &RichTextLabel::invalidate_paragraph);
 	ClassDB::bind_method(D_METHOD("push_font", "font", "font_size"), &RichTextLabel::push_font, DEFVAL(0));
 	ClassDB::bind_method(D_METHOD("push_font", "font", "font_size"), &RichTextLabel::push_font, DEFVAL(0));
 	ClassDB::bind_method(D_METHOD("push_font_size", "font_size"), &RichTextLabel::push_font_size);
 	ClassDB::bind_method(D_METHOD("push_font_size", "font_size"), &RichTextLabel::push_font_size);
 	ClassDB::bind_method(D_METHOD("push_normal"), &RichTextLabel::push_normal);
 	ClassDB::bind_method(D_METHOD("push_normal"), &RichTextLabel::push_normal);

+ 3 - 1
scene/gui/rich_text_label.h

@@ -134,6 +134,7 @@ protected:
 #ifndef DISABLE_DEPRECATED
 #ifndef DISABLE_DEPRECATED
 	void _push_meta_bind_compat_89024(const Variant &p_meta);
 	void _push_meta_bind_compat_89024(const Variant &p_meta);
 	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);
 	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);
+	bool _remove_paragraph_bind_compat_91098(int p_paragraph);
 	static void _bind_compatibility_methods();
 	static void _bind_compatibility_methods();
 #endif
 #endif
 
 
@@ -664,7 +665,8 @@ public:
 	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);
 	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);
 	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);
 	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);
 	void add_newline();
 	void add_newline();
-	bool remove_paragraph(const int p_paragraph);
+	bool remove_paragraph(int p_paragraph, bool p_no_invalidate = false);
+	bool invalidate_paragraph(int p_paragraph);
 	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));
 	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));
 	void _push_def_font(DefaultFont p_def_font);
 	void _push_def_font(DefaultFont p_def_font);
 	void _push_def_font_var(DefaultFont p_def_font, const Ref<Font> &p_font, int p_size = -1);
 	void _push_def_font_var(DefaultFont p_def_font, const Ref<Font> &p_font, int p_size = -1);