Browse Source

[3.x, RTL] Track external changes in the custom fonts set by BBCode / `push_font`.

(cherry picked from commit 3c8e71b429ced4a7fe9b4cd3d8d9fe370f6d08dd)
Pāvels Nadtočajevs 4 months ago
parent
commit
4d7586d759
2 changed files with 22 additions and 0 deletions
  1. 10 0
      scene/gui/rich_text_label.cpp
  2. 12 0
      scene/gui/rich_text_label.h

+ 10 - 0
scene/gui/rich_text_label.cpp

@@ -1848,9 +1848,17 @@ void RichTextLabel::push_font(const Ref<Font> &p_font) {
 	ItemFont *item = memnew(ItemFont);
 	ItemFont *item = memnew(ItemFont);
 
 
 	item->font = p_font;
 	item->font = p_font;
+	item->owner = get_instance_id();
+	item->font->connect("changed", this, "_invalidate_fonts", Vector<Variant>(), CONNECT_REFERENCE_COUNTED);
+
 	_add_item(item, true);
 	_add_item(item, true);
 }
 }
 
 
+void RichTextLabel::_invalidate_fonts() {
+	main->first_invalid_line = 0; //invalidate ALL
+	update();
+}
+
 void RichTextLabel::push_normal() {
 void RichTextLabel::push_normal() {
 	Ref<Font> normal_font = get_font("normal_font");
 	Ref<Font> normal_font = get_font("normal_font");
 	ERR_FAIL_COND(normal_font.is_null());
 	ERR_FAIL_COND(normal_font.is_null());
@@ -2927,6 +2935,8 @@ void RichTextLabel::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("get_effects"), &RichTextLabel::get_effects);
 	ClassDB::bind_method(D_METHOD("get_effects"), &RichTextLabel::get_effects);
 	ClassDB::bind_method(D_METHOD("install_effect", "effect"), &RichTextLabel::install_effect);
 	ClassDB::bind_method(D_METHOD("install_effect", "effect"), &RichTextLabel::install_effect);
 
 
+	ClassDB::bind_method(D_METHOD("_invalidate_fonts"), &RichTextLabel::_invalidate_fonts);
+
 	ADD_GROUP("BBCode", "bbcode_");
 	ADD_GROUP("BBCode", "bbcode_");
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bbcode_enabled"), "set_use_bbcode", "is_using_bbcode");
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bbcode_enabled"), "set_use_bbcode", "is_using_bbcode");
 	ADD_PROPERTY(PropertyInfo(Variant::STRING, "bbcode_text", PROPERTY_HINT_MULTILINE_TEXT), "set_bbcode", "get_bbcode");
 	ADD_PROPERTY(PropertyInfo(Variant::STRING, "bbcode_text", PROPERTY_HINT_MULTILINE_TEXT), "set_bbcode", "get_bbcode");

+ 12 - 0
scene/gui/rich_text_label.h

@@ -164,7 +164,17 @@ private:
 
 
 	struct ItemFont : public Item {
 	struct ItemFont : public Item {
 		Ref<Font> font;
 		Ref<Font> font;
+		ObjectID owner;
+
 		ItemFont() { type = ITEM_FONT; }
 		ItemFont() { type = ITEM_FONT; }
+		~ItemFont() {
+			if (font.is_valid()) {
+				RichTextLabel *owner_rtl = ObjectDB::get_instance<RichTextLabel>(owner);
+				if (owner_rtl) {
+					font->disconnect("changed", owner_rtl, "_invalidate_fonts");
+				}
+			}
+		}
 	};
 	};
 
 
 	struct ItemColor : public Item {
 	struct ItemColor : public Item {
@@ -348,6 +358,8 @@ private:
 	void _add_item(Item *p_item, bool p_enter = false, bool p_ensure_newline = false);
 	void _add_item(Item *p_item, bool p_enter = false, bool p_ensure_newline = false);
 	void _remove_item(Item *p_item, const int p_line, const int p_subitem_line);
 	void _remove_item(Item *p_item, const int p_line, const int p_subitem_line);
 
 
+	void _invalidate_fonts();
+
 	struct ProcessState {
 	struct ProcessState {
 		int line_width;
 		int line_width;
 	};
 	};