summaryrefslogtreecommitdiffstats
path: root/scene/gui/rich_text_label.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'scene/gui/rich_text_label.cpp')
-rw-r--r--scene/gui/rich_text_label.cpp142
1 files changed, 108 insertions, 34 deletions
diff --git a/scene/gui/rich_text_label.cpp b/scene/gui/rich_text_label.cpp
index fbc374e89e..b6dd953d50 100644
--- a/scene/gui/rich_text_label.cpp
+++ b/scene/gui/rich_text_label.cpp
@@ -239,7 +239,8 @@ void RichTextLabel::_update_line_font(ItemFrame *p_frame, int p_line, const Ref<
RID t = l.text_buf->get_rid();
int spans = TS->shaped_get_span_count(t);
for (int i = 0; i < spans; i++) {
- ItemText *it = reinterpret_cast<ItemText *>((uint64_t)TS->shaped_get_span_meta(t, i));
+ Item *it_span = items.get_or_null(TS->shaped_get_span_meta(t, i));
+ ItemText *it = reinterpret_cast<ItemText *>(it_span);
if (it) {
Ref<Font> font = p_base_font;
int font_size = p_base_font_size;
@@ -307,7 +308,7 @@ float RichTextLabel::_resize_line(ItemFrame *p_frame, int p_line, const Ref<Font
Size2 img_size = img->size;
if (img->size_in_percent) {
img_size = _get_image_size(img->image, p_width * img->rq_size.width / 100.f, p_width * img->rq_size.height / 100.f, img->region);
- l.text_buf->resize_object((uint64_t)it, img_size, img->inline_align);
+ l.text_buf->resize_object(it->rid, img_size, img->inline_align);
}
} break;
case ITEM_TABLE: {
@@ -455,9 +456,9 @@ float RichTextLabel::_resize_line(ItemFrame *p_frame, int p_line, const Ref<Font
}
int row_idx = (table->align_to_row < 0) ? table->rows_baseline.size() - 1 : table->align_to_row;
if (table->rows_baseline.size() != 0 && row_idx < (int)table->rows_baseline.size() - 1) {
- l.text_buf->resize_object((uint64_t)it, Size2(table->total_width, table->total_height), table->inline_align, Math::round(table->rows_baseline[row_idx]));
+ l.text_buf->resize_object(it->rid, Size2(table->total_width, table->total_height), table->inline_align, Math::round(table->rows_baseline[row_idx]));
} else {
- l.text_buf->resize_object((uint64_t)it, Size2(table->total_width, table->total_height), table->inline_align);
+ l.text_buf->resize_object(it->rid, Size2(table->total_width, table->total_height), table->inline_align);
}
} break;
default:
@@ -578,7 +579,7 @@ float RichTextLabel::_shape_line(ItemFrame *p_frame, int p_line, const Ref<Font>
}
remaining_characters -= tx.length();
- l.text_buf->add_string(tx, font, font_size, lang, (uint64_t)it);
+ l.text_buf->add_string(tx, font, font_size, lang, it->rid);
txt += tx;
l.char_count += tx.length();
} break;
@@ -588,7 +589,7 @@ float RichTextLabel::_shape_line(ItemFrame *p_frame, int p_line, const Ref<Font>
if (img->size_in_percent) {
img_size = _get_image_size(img->image, p_width * img->rq_size.width / 100.f, p_width * img->rq_size.height / 100.f, img->region);
}
- l.text_buf->add_object((uint64_t)it, img_size, img->inline_align, 1);
+ l.text_buf->add_object(it->rid, img_size, img->inline_align, 1);
txt += String::chr(0xfffc);
l.char_count++;
remaining_characters--;
@@ -753,9 +754,9 @@ float RichTextLabel::_shape_line(ItemFrame *p_frame, int p_line, const Ref<Font>
}
int row_idx = (table->align_to_row < 0) ? table->rows_baseline.size() - 1 : table->align_to_row;
if (table->rows_baseline.size() != 0 && row_idx < (int)table->rows_baseline.size() - 1) {
- l.text_buf->add_object((uint64_t)it, Size2(table->total_width, table->total_height), table->inline_align, t_char_count, Math::round(table->rows_baseline[row_idx]));
+ l.text_buf->add_object(it->rid, Size2(table->total_width, table->total_height), table->inline_align, t_char_count, Math::round(table->rows_baseline[row_idx]));
} else {
- l.text_buf->add_object((uint64_t)it, Size2(table->total_width, table->total_height), table->inline_align, t_char_count);
+ l.text_buf->add_object(it->rid, Size2(table->total_width, table->total_height), table->inline_align, t_char_count);
}
txt += String::chr(0xfffc).repeat(t_char_count);
} break;
@@ -932,7 +933,7 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
// Draw inlined objects.
Array objects = TS->shaped_text_get_objects(rid);
for (int i = 0; i < objects.size(); i++) {
- Item *it = reinterpret_cast<Item *>((uint64_t)objects[i]);
+ Item *it = items.get_or_null(objects[i]);
if (it != nullptr) {
Rect2 rect = TS->shaped_text_get_object_rect(rid, objects[i]);
//draw_rect(rect, Color(1,0,0), false, 2); //DEBUG_RECTS
@@ -1601,7 +1602,7 @@ float RichTextLabel::_find_click_in_line(ItemFrame *p_frame, int p_line, const V
Array objects = TS->shaped_text_get_objects(rid);
for (int i = 0; i < objects.size(); i++) {
- Item *it = reinterpret_cast<Item *>((uint64_t)objects[i]);
+ Item *it = items.get_or_null(objects[i]);
if (it != nullptr) {
Rect2 rect = TS->shaped_text_get_object_rect(rid, objects[i]);
rect.position += p_ofs + off;
@@ -2805,7 +2806,7 @@ void RichTextLabel::_thread_function(void *p_userdata) {
set_current_thread_safe_for_nodes(true);
_process_line_caches();
updating.store(false);
- call_deferred(SNAME("thread_end"));
+ callable_mp(this, &RichTextLabel::_thread_end).call_deferred();
}
void RichTextLabel::_thread_end() {
@@ -3054,6 +3055,19 @@ void RichTextLabel::_invalidate_current_line(ItemFrame *p_frame) {
}
}
+void RichTextLabel::_texture_changed(RID p_item) {
+ Item *it = items.get_or_null(p_item);
+ if (it && it->type == ITEM_IMAGE) {
+ ItemImage *img = reinterpret_cast<ItemImage *>(it);
+ Size2 new_size = _get_image_size(img->image, img->rq_size.width, img->rq_size.height, img->region);
+ if (img->size != new_size) {
+ main->first_invalid_line.store(0);
+ img->size = new_size;
+ }
+ }
+ queue_redraw();
+}
+
void RichTextLabel::add_text(const String &p_text) {
_stop_thread();
MutexLock data_lock(data_mutex);
@@ -3090,6 +3104,8 @@ void RichTextLabel::add_text(const String &p_text) {
} else {
//append item condition
ItemText *item = memnew(ItemText);
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->text = line;
_add_item(item, false);
}
@@ -3097,6 +3113,8 @@ void RichTextLabel::add_text(const String &p_text) {
if (eol) {
ItemNewline *item = memnew(ItemNewline);
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->line = current_frame->lines.size();
_add_item(item, false);
current_frame->lines.resize(current_frame->lines.size() + 1);
@@ -3151,7 +3169,7 @@ void RichTextLabel::_add_item(Item *p_item, bool p_enter, bool p_ensure_newline)
queue_redraw();
}
-void RichTextLabel::_remove_item(Item *p_item, const int p_line, const int p_subitem_line) {
+void RichTextLabel::_remove_item(Item *p_item, const int p_line) {
int size = p_item->subitems.size();
if (size == 0) {
p_item->parent->subitems.erase(p_item);
@@ -3160,7 +3178,7 @@ void RichTextLabel::_remove_item(Item *p_item, const int p_line, const int p_sub
current_frame->lines.remove_at(p_line);
if (p_line < (int)current_frame->lines.size() && current_frame->lines[p_line].from) {
for (List<Item *>::Element *E = current_frame->lines[p_line].from->E; E; E = E->next()) {
- if (E->get()->line > p_subitem_line) {
+ if (E->get()->line > p_line) {
E->get()->line--;
}
}
@@ -3169,11 +3187,12 @@ void RichTextLabel::_remove_item(Item *p_item, const int p_line, const int p_sub
} else {
// First, remove all child items for the provided item.
while (p_item->subitems.size()) {
- _remove_item(p_item->subitems.front()->get(), p_line, p_subitem_line);
+ _remove_item(p_item->subitems.front()->get(), p_line);
}
// Then remove the provided item itself.
p_item->parent->subitems.erase(p_item);
}
+ items.free(p_item->rid);
memdelete(p_item);
}
@@ -3231,6 +3250,8 @@ void RichTextLabel::add_image(const Ref<Texture2D> &p_image, int p_width, int p_
ERR_FAIL_COND(p_height < 0);
ItemImage *item = memnew(ItemImage);
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
if (p_region.has_area()) {
Ref<AtlasTexture> atlas_tex = memnew(AtlasTexture);
@@ -3250,6 +3271,8 @@ void RichTextLabel::add_image(const Ref<Texture2D> &p_image, int p_width, int p_
item->key = p_key;
item->tooltip = p_tooltip;
+ item->image->connect_changed(callable_mp(this, &RichTextLabel::_texture_changed).bind(item->rid), CONNECT_REFERENCE_COUNTED);
+
_add_item(item, false);
}
@@ -3278,6 +3301,9 @@ void RichTextLabel::update_image(const Variant &p_key, BitField<ImageUpdateMask>
item->region = p_region;
}
if (p_mask & UPDATE_TEXTURE) {
+ if (item->image.is_valid()) {
+ item->image->disconnect_changed(callable_mp(this, &RichTextLabel::_texture_changed));
+ }
if (item->region.has_area()) {
Ref<AtlasTexture> atlas_tex = memnew(AtlasTexture);
atlas_tex->set_atlas(p_image);
@@ -3286,6 +3312,7 @@ void RichTextLabel::update_image(const Variant &p_key, BitField<ImageUpdateMask>
} else {
item->image = p_image;
}
+ item->image->connect_changed(callable_mp(this, &RichTextLabel::_texture_changed).bind(item->rid), CONNECT_REFERENCE_COUNTED);
}
if (p_mask & UPDATE_COLOR) {
item->color = p_color;
@@ -3345,6 +3372,8 @@ void RichTextLabel::add_newline() {
return;
}
ItemNewline *item = memnew(ItemNewline);
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->line = current_frame->lines.size();
_add_item(item, false);
current_frame->lines.resize(current_frame->lines.size() + 1);
@@ -3377,7 +3406,10 @@ bool RichTextLabel::remove_paragraph(const int p_paragraph) {
for (int i = subitem_to_remove.size() - 1; i >= 0; i--) {
List<Item *>::Element *subitem = subitem_to_remove[i];
had_newline = had_newline || subitem->get()->type == ITEM_NEWLINE;
- _remove_item(subitem->get(), subitem->get()->line, p_paragraph);
+ if (subitem->get() == current) {
+ pop();
+ }
+ _remove_item(subitem->get(), p_paragraph);
}
if (!had_newline) {
@@ -3410,7 +3442,8 @@ void RichTextLabel::push_dropcap(const String &p_string, const Ref<Font> &p_font
ERR_FAIL_COND(p_size <= 0);
ItemDropcap *item = memnew(ItemDropcap);
-
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->text = p_string;
item->font = p_font;
item->font_size = p_size;
@@ -3427,7 +3460,8 @@ void RichTextLabel::_push_def_font_var(DefaultFont p_def_font, const Ref<Font> &
ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemFont *item = memnew(ItemFont);
-
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->def_font = p_def_font;
item->variation = true;
item->font = p_font;
@@ -3442,7 +3476,8 @@ void RichTextLabel::_push_def_font(DefaultFont p_def_font) {
ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemFont *item = memnew(ItemFont);
-
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->def_font = p_def_font;
item->def_size = true;
_add_item(item, true);
@@ -3455,7 +3490,8 @@ void RichTextLabel::push_font(const Ref<Font> &p_font, int p_size) {
ERR_FAIL_COND(current->type == ITEM_TABLE);
ERR_FAIL_COND(p_font.is_null());
ItemFont *item = memnew(ItemFont);
-
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->font = p_font;
item->font_size = p_size;
_add_item(item, true);
@@ -3499,7 +3535,8 @@ void RichTextLabel::push_font_size(int p_font_size) {
ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemFontSize *item = memnew(ItemFontSize);
-
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->font_size = p_font_size;
_add_item(item, true);
}
@@ -3510,7 +3547,8 @@ void RichTextLabel::push_outline_size(int p_ol_size) {
ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemOutlineSize *item = memnew(ItemOutlineSize);
-
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->outline_size = p_ol_size;
_add_item(item, true);
}
@@ -3521,7 +3559,8 @@ void RichTextLabel::push_color(const Color &p_color) {
ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemColor *item = memnew(ItemColor);
-
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->color = p_color;
_add_item(item, true);
}
@@ -3532,7 +3571,8 @@ void RichTextLabel::push_outline_color(const Color &p_color) {
ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemOutlineColor *item = memnew(ItemOutlineColor);
-
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->color = p_color;
_add_item(item, true);
}
@@ -3543,6 +3583,8 @@ void RichTextLabel::push_underline() {
ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemUnderline *item = memnew(ItemUnderline);
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
_add_item(item, true);
}
@@ -3553,6 +3595,8 @@ void RichTextLabel::push_strikethrough() {
ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemStrikethrough *item = memnew(ItemStrikethrough);
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
_add_item(item, true);
}
@@ -3564,6 +3608,8 @@ void RichTextLabel::push_paragraph(HorizontalAlignment p_alignment, Control::Tex
ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemParagraph *item = memnew(ItemParagraph);
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->alignment = p_alignment;
item->direction = p_direction;
item->language = p_language;
@@ -3581,6 +3627,8 @@ void RichTextLabel::push_indent(int p_level) {
ERR_FAIL_COND(p_level < 0);
ItemIndent *item = memnew(ItemIndent);
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->level = p_level;
_add_item(item, true, true);
}
@@ -3593,7 +3641,8 @@ void RichTextLabel::push_list(int p_level, ListType p_list, bool p_capitalize, c
ERR_FAIL_COND(p_level < 0);
ItemList *item = memnew(ItemList);
-
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->list_type = p_list;
item->level = p_level;
item->capitalize = p_capitalize;
@@ -3607,7 +3656,8 @@ void RichTextLabel::push_meta(const Variant &p_meta) {
ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemMeta *item = memnew(ItemMeta);
-
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->meta = p_meta;
_add_item(item, true);
}
@@ -3618,7 +3668,8 @@ void RichTextLabel::push_language(const String &p_language) {
ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemLanguage *item = memnew(ItemLanguage);
-
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->language = p_language;
_add_item(item, true);
}
@@ -3629,7 +3680,8 @@ void RichTextLabel::push_hint(const String &p_string) {
ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemHint *item = memnew(ItemHint);
-
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->description = p_string;
_add_item(item, true);
}
@@ -3641,7 +3693,8 @@ void RichTextLabel::push_table(int p_columns, InlineAlignment p_alignment, int p
ERR_FAIL_COND(current->type == ITEM_TABLE);
ERR_FAIL_COND(p_columns < 1);
ItemTable *item = memnew(ItemTable);
-
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->columns.resize(p_columns);
item->total_width = 0;
item->inline_align = p_alignment;
@@ -3659,6 +3712,8 @@ void RichTextLabel::push_fade(int p_start_index, int p_length) {
ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemFade *item = memnew(ItemFade);
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->starting_index = p_start_index;
item->length = p_length;
_add_item(item, true);
@@ -3670,6 +3725,8 @@ void RichTextLabel::push_shake(int p_strength = 10, float p_rate = 24.0f, bool p
ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemShake *item = memnew(ItemShake);
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->strength = p_strength;
item->rate = p_rate;
item->connected = p_connected;
@@ -3682,6 +3739,8 @@ void RichTextLabel::push_wave(float p_frequency = 1.0f, float p_amplitude = 10.0
ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemWave *item = memnew(ItemWave);
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->frequency = p_frequency;
item->amplitude = p_amplitude;
item->connected = p_connected;
@@ -3694,6 +3753,8 @@ void RichTextLabel::push_tornado(float p_frequency = 1.0f, float p_radius = 10.0
ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemTornado *item = memnew(ItemTornado);
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->frequency = p_frequency;
item->radius = p_radius;
item->connected = p_connected;
@@ -3706,6 +3767,8 @@ void RichTextLabel::push_rainbow(float p_saturation, float p_value, float p_freq
ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemRainbow *item = memnew(ItemRainbow);
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->frequency = p_frequency;
item->saturation = p_saturation;
item->value = p_value;
@@ -3717,6 +3780,8 @@ void RichTextLabel::push_pulse(const Color &p_color, float p_frequency, float p_
MutexLock data_lock(data_mutex);
ItemPulse *item = memnew(ItemPulse);
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->color = p_color;
item->frequency = p_frequency;
item->ease = p_ease;
@@ -3729,7 +3794,8 @@ void RichTextLabel::push_bgcolor(const Color &p_color) {
ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemBGColor *item = memnew(ItemBGColor);
-
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->color = p_color;
_add_item(item, true);
}
@@ -3740,7 +3806,8 @@ void RichTextLabel::push_fgcolor(const Color &p_color) {
ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemFGColor *item = memnew(ItemFGColor);
-
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->color = p_color;
_add_item(item, true);
}
@@ -3751,6 +3818,8 @@ void RichTextLabel::push_customfx(Ref<RichTextEffect> p_custom_effect, Dictionar
ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemCustomFX *item = memnew(ItemCustomFX);
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->custom_effect = p_custom_effect;
item->char_fx_transform->environment = p_environment;
_add_item(item, true);
@@ -3764,6 +3833,8 @@ void RichTextLabel::push_context() {
ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemContext *item = memnew(ItemContext);
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
_add_item(item, true);
}
@@ -3832,6 +3903,8 @@ void RichTextLabel::push_cell() {
ERR_FAIL_COND(current->type != ITEM_TABLE);
ItemFrame *item = memnew(ItemFrame);
+ item->owner = get_instance_id();
+ item->rid = items.make_rid(item);
item->parent_frame = current_frame;
_add_item(item, true);
current_frame = item;
@@ -4532,7 +4605,7 @@ void RichTextLabel::append_text(const String &p_bbcode) {
if (subtag_a.size() == 2) {
if (subtag_a[0] == "font" || subtag_a[0] == "f") {
- String fnt = subtag_a[1];
+ const String &fnt = subtag_a[1];
Ref<Font> font = ResourceLoader::load(fnt, "Font");
if (font.is_valid()) {
f = font;
@@ -4776,7 +4849,7 @@ void RichTextLabel::append_text(const String &p_bbcode) {
if (subtag_a.size() == 2) {
if (subtag_a[0] == "name" || subtag_a[0] == "n") {
- String fnt = subtag_a[1];
+ const String &fnt = subtag_a[1];
Ref<Font> font_data = ResourceLoader::load(fnt, "Font");
if (font_data.is_valid()) {
font = font_data;
@@ -5945,8 +6018,6 @@ void RichTextLabel::_bind_methods() {
ClassDB::bind_method(D_METHOD("is_menu_visible"), &RichTextLabel::is_menu_visible);
ClassDB::bind_method(D_METHOD("menu_option", "option"), &RichTextLabel::menu_option);
- ClassDB::bind_method(D_METHOD("_thread_end"), &RichTextLabel::_thread_end);
-
#ifndef DISABLE_DEPRECATED
ClassDB::bind_compatibility_method(D_METHOD("push_font", "font", "font_size"), &RichTextLabel::push_font);
ClassDB::bind_compatibility_method(D_METHOD("set_table_column_expand", "column", "expand", "ratio"), &RichTextLabel::set_table_column_expand);
@@ -6387,6 +6458,8 @@ Dictionary RichTextLabel::parse_expressions_for_values(Vector<String> p_expressi
RichTextLabel::RichTextLabel(const String &p_text) {
main = memnew(ItemFrame);
+ main->owner = get_instance_id();
+ main->rid = items.make_rid(main);
main->index = 0;
current = main;
main->lines.resize(1);
@@ -6417,5 +6490,6 @@ RichTextLabel::RichTextLabel(const String &p_text) {
RichTextLabel::~RichTextLabel() {
_stop_thread();
+ items.free(main->rid);
memdelete(main);
}