[RTL] Add optional push_meta argument to control how meta underline is drawn.

This commit is contained in:
bruvzg 2024-02-29 21:21:13 +02:00
parent 7d2ca2d8ac
commit 2a3de7adc3
No known key found for this signature in database
GPG key ID: 7960FCF39844EC38
5 changed files with 67 additions and 6 deletions

View file

@ -428,8 +428,10 @@
<method name="push_meta"> <method name="push_meta">
<return type="void" /> <return type="void" />
<param index="0" name="data" type="Variant" /> <param index="0" name="data" type="Variant" />
<param index="1" name="underline_mode" type="int" enum="RichTextLabel.MetaUnderline" default="1" />
<description> <description>
Adds a meta tag to the tag stack. Similar to the BBCode [code skip-lint][url=something]{text}[/url][/code], but supports non-[String] metadata types. Adds a meta tag to the tag stack. Similar to the BBCode [code skip-lint][url=something]{text}[/url][/code], but supports non-[String] metadata types.
If [member meta_underlined] is [code]true[/code], meta tags display an underline. This behavior can be customized with [param underline_mode].
[b]Note:[/b] Meta tags do nothing by default when clicked. To assign behavior when clicked, connect [signal meta_clicked] to a function that is called when the meta tag is clicked. [b]Note:[/b] Meta tags do nothing by default when clicked. To assign behavior when clicked, connect [signal meta_clicked] to a function that is called when the meta tag is clicked.
</description> </description>
</method> </method>
@ -723,6 +725,15 @@
<constant name="MENU_MAX" value="2" enum="MenuItems"> <constant name="MENU_MAX" value="2" enum="MenuItems">
Represents the size of the [enum MenuItems] enum. Represents the size of the [enum MenuItems] enum.
</constant> </constant>
<constant name="META_UNDERLINE_NEVER" value="0" enum="MetaUnderline">
Meta tag does not display an underline, even if [member meta_underlined] is [code]true[/code].
</constant>
<constant name="META_UNDERLINE_ALWAYS" value="1" enum="MetaUnderline">
If [member meta_underlined] is [code]true[/code], meta tag always display an underline.
</constant>
<constant name="META_UNDERLINE_ON_HOVER" value="2" enum="MetaUnderline">
If [member meta_underlined] is [code]true[/code], meta tag display an underline when the mouse cursor is over it.
</constant>
<constant name="UPDATE_TEXTURE" value="1" enum="ImageUpdateMask" is_bitfield="true"> <constant name="UPDATE_TEXTURE" value="1" enum="ImageUpdateMask" is_bitfield="true">
If this bit is set, [method update_image] changes image texture. If this bit is set, [method update_image] changes image texture.
</constant> </constant>

View file

@ -224,3 +224,10 @@ Validate extension JSON: JSON file: Field was added in a way that breaks compati
Added a return value for add_bone. Added a return value for add_bone.
Should not affect existing regular use - the return value would just be unused. Should not affect existing regular use - the return value would just be unused.
Compatibility method registered. Compatibility method registered.
GH-89024
--------
Validate extension JSON: Error: Field 'classes/RichTextLabel/methods/push_meta/arguments': size changed value in new API, from 1 to 2.
Added optional argument. Compatibility method registered.

View file

@ -30,11 +30,16 @@
#ifndef DISABLE_DEPRECATED #ifndef DISABLE_DEPRECATED
void RichTextLabel::_push_meta_bind_compat_89024(const Variant &p_meta) {
push_meta(p_meta, RichTextLabel::MetaUnderline::META_UNDERLINE_ALWAYS);
}
void RichTextLabel::_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 RichTextLabel::_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) {
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);
} }
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("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()));
} }

View file

@ -1223,8 +1223,25 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
for (int i = 0; i < gl_size; i++) { for (int i = 0; i < gl_size; i++) {
bool selected = selection.active && (sel_start != -1) && (glyphs[i].start >= sel_start) && (glyphs[i].end <= sel_end); bool selected = selection.active && (sel_start != -1) && (glyphs[i].start >= sel_start) && (glyphs[i].end <= sel_end);
Item *it = _get_item_at_pos(it_from, it_to, glyphs[i].start); Item *it = _get_item_at_pos(it_from, it_to, glyphs[i].start);
bool has_ul = _find_underline(it);
if (!has_ul && underline_meta) {
ItemMeta *meta = nullptr;
if (_find_meta(it, nullptr, &meta) && meta) {
switch (meta->underline) {
case META_UNDERLINE_ALWAYS: {
has_ul = true;
} break;
case META_UNDERLINE_NEVER: {
has_ul = false;
} break;
case META_UNDERLINE_ON_HOVER: {
has_ul = (meta == meta_hovering);
} break;
}
}
}
Color font_color = _find_color(it, p_base_color); Color font_color = _find_color(it, p_base_color);
if (_find_underline(it) || (_find_meta(it, nullptr) && underline_meta)) { if (has_ul) {
if (ul_started && font_color != ul_color_prev) { if (ul_started && font_color != ul_color_prev) {
float y_off = TS->shaped_text_get_underline_position(rid); float y_off = TS->shaped_text_get_underline_position(rid);
float underline_width = MAX(1.0, TS->shaped_text_get_underline_thickness(rid) * theme_cache.base_scale); float underline_width = MAX(1.0, TS->shaped_text_get_underline_thickness(rid) * theme_cache.base_scale);
@ -2242,6 +2259,7 @@ void RichTextLabel::gui_input(const Ref<InputEvent> &p_event) {
Variant meta; Variant meta;
ItemMeta *item_meta; ItemMeta *item_meta;
ItemMeta *prev_meta = meta_hovering;
if (c_item && !outside && _find_meta(c_item, &meta, &item_meta)) { if (c_item && !outside && _find_meta(c_item, &meta, &item_meta)) {
if (meta_hovering != item_meta) { if (meta_hovering != item_meta) {
if (meta_hovering) { if (meta_hovering) {
@ -2250,11 +2268,17 @@ void RichTextLabel::gui_input(const Ref<InputEvent> &p_event) {
meta_hovering = item_meta; meta_hovering = item_meta;
current_meta = meta; current_meta = meta;
emit_signal(SNAME("meta_hover_started"), meta); emit_signal(SNAME("meta_hover_started"), meta);
if ((item_meta && item_meta->underline == META_UNDERLINE_ON_HOVER) || (prev_meta && prev_meta->underline == META_UNDERLINE_ON_HOVER)) {
queue_redraw();
}
} }
} else if (meta_hovering) { } else if (meta_hovering) {
meta_hovering = nullptr; meta_hovering = nullptr;
emit_signal(SNAME("meta_hover_ended"), current_meta); emit_signal(SNAME("meta_hover_ended"), current_meta);
current_meta = false; current_meta = false;
if (prev_meta->underline == META_UNDERLINE_ON_HOVER) {
queue_redraw();
}
} }
} }
} }
@ -3650,7 +3674,7 @@ void RichTextLabel::push_list(int p_level, ListType p_list, bool p_capitalize, c
_add_item(item, true, true); _add_item(item, true, true);
} }
void RichTextLabel::push_meta(const Variant &p_meta) { void RichTextLabel::push_meta(const Variant &p_meta, MetaUnderline p_underline_mode) {
_stop_thread(); _stop_thread();
MutexLock data_lock(data_mutex); MutexLock data_lock(data_mutex);
@ -3659,6 +3683,7 @@ void RichTextLabel::push_meta(const Variant &p_meta) {
item->owner = get_instance_id(); item->owner = get_instance_id();
item->rid = items.make_rid(item); item->rid = items.make_rid(item);
item->meta = p_meta; item->meta = p_meta;
item->underline = p_underline_mode;
_add_item(item, true); _add_item(item, true);
} }
@ -4573,14 +4598,14 @@ void RichTextLabel::append_text(const String &p_bbcode) {
end = p_bbcode.length(); end = p_bbcode.length();
} }
String url = p_bbcode.substr(brk_end + 1, end - brk_end - 1).unquote(); String url = p_bbcode.substr(brk_end + 1, end - brk_end - 1).unquote();
push_meta(url); push_meta(url, META_UNDERLINE_ALWAYS);
pos = brk_end + 1; pos = brk_end + 1;
tag_stack.push_front(tag); tag_stack.push_front(tag);
} else if (tag.begins_with("url=")) { } else if (tag.begins_with("url=")) {
String url = tag.substr(4, tag.length()).unquote(); String url = tag.substr(4, tag.length()).unquote();
push_meta(url); push_meta(url, META_UNDERLINE_ALWAYS);
pos = brk_end + 1; pos = brk_end + 1;
tag_stack.push_front("url"); tag_stack.push_front("url");
} else if (tag.begins_with("hint=")) { } else if (tag.begins_with("hint=")) {
@ -5891,7 +5916,7 @@ void RichTextLabel::_bind_methods() {
ClassDB::bind_method(D_METHOD("push_paragraph", "alignment", "base_direction", "language", "st_parser", "justification_flags", "tab_stops"), &RichTextLabel::push_paragraph, DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(""), DEFVAL(TextServer::STRUCTURED_TEXT_DEFAULT), DEFVAL(TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_SKIP_LAST_LINE | TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE), DEFVAL(PackedFloat32Array())); ClassDB::bind_method(D_METHOD("push_paragraph", "alignment", "base_direction", "language", "st_parser", "justification_flags", "tab_stops"), &RichTextLabel::push_paragraph, DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(""), DEFVAL(TextServer::STRUCTURED_TEXT_DEFAULT), DEFVAL(TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_SKIP_LAST_LINE | TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE), DEFVAL(PackedFloat32Array()));
ClassDB::bind_method(D_METHOD("push_indent", "level"), &RichTextLabel::push_indent); ClassDB::bind_method(D_METHOD("push_indent", "level"), &RichTextLabel::push_indent);
ClassDB::bind_method(D_METHOD("push_list", "level", "type", "capitalize", "bullet"), &RichTextLabel::push_list, DEFVAL(String::utf8(""))); ClassDB::bind_method(D_METHOD("push_list", "level", "type", "capitalize", "bullet"), &RichTextLabel::push_list, DEFVAL(String::utf8("")));
ClassDB::bind_method(D_METHOD("push_meta", "data"), &RichTextLabel::push_meta); ClassDB::bind_method(D_METHOD("push_meta", "data", "underline_mode"), &RichTextLabel::push_meta, DEFVAL(META_UNDERLINE_ALWAYS));
ClassDB::bind_method(D_METHOD("push_hint", "description"), &RichTextLabel::push_hint); ClassDB::bind_method(D_METHOD("push_hint", "description"), &RichTextLabel::push_hint);
ClassDB::bind_method(D_METHOD("push_language", "language"), &RichTextLabel::push_language); ClassDB::bind_method(D_METHOD("push_language", "language"), &RichTextLabel::push_language);
ClassDB::bind_method(D_METHOD("push_underline"), &RichTextLabel::push_underline); ClassDB::bind_method(D_METHOD("push_underline"), &RichTextLabel::push_underline);
@ -6081,6 +6106,10 @@ void RichTextLabel::_bind_methods() {
BIND_ENUM_CONSTANT(MENU_SELECT_ALL); BIND_ENUM_CONSTANT(MENU_SELECT_ALL);
BIND_ENUM_CONSTANT(MENU_MAX); BIND_ENUM_CONSTANT(MENU_MAX);
BIND_ENUM_CONSTANT(META_UNDERLINE_NEVER);
BIND_ENUM_CONSTANT(META_UNDERLINE_ALWAYS);
BIND_ENUM_CONSTANT(META_UNDERLINE_ON_HOVER);
BIND_BITFIELD_FLAG(UPDATE_TEXTURE); BIND_BITFIELD_FLAG(UPDATE_TEXTURE);
BIND_BITFIELD_FLAG(UPDATE_SIZE); BIND_BITFIELD_FLAG(UPDATE_SIZE);
BIND_BITFIELD_FLAG(UPDATE_COLOR); BIND_BITFIELD_FLAG(UPDATE_COLOR);

View file

@ -51,6 +51,12 @@ public:
LIST_DOTS LIST_DOTS
}; };
enum MetaUnderline {
META_UNDERLINE_NEVER,
META_UNDERLINE_ALWAYS,
META_UNDERLINE_ON_HOVER,
};
enum ItemType { enum ItemType {
ITEM_FRAME, ITEM_FRAME,
ITEM_TEXT, ITEM_TEXT,
@ -117,6 +123,7 @@ protected:
static void _bind_methods(); static void _bind_methods();
#ifndef DISABLE_DEPRECATED #ifndef DISABLE_DEPRECATED
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);
static void _bind_compatibility_methods(); static void _bind_compatibility_methods();
#endif #endif
@ -271,6 +278,7 @@ private:
struct ItemMeta : public Item { struct ItemMeta : public Item {
Variant meta; Variant meta;
MetaUnderline underline = META_UNDERLINE_ALWAYS;
ItemMeta() { type = ITEM_META; } ItemMeta() { type = ITEM_META; }
}; };
@ -668,7 +676,7 @@ public:
void push_paragraph(HorizontalAlignment p_alignment, Control::TextDirection p_direction = Control::TEXT_DIRECTION_INHERITED, const String &p_language = "", TextServer::StructuredTextParser p_st_parser = TextServer::STRUCTURED_TEXT_DEFAULT, BitField<TextServer::JustificationFlag> p_jst_flags = TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_SKIP_LAST_LINE | TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE, const PackedFloat32Array &p_tab_stops = PackedFloat32Array()); void push_paragraph(HorizontalAlignment p_alignment, Control::TextDirection p_direction = Control::TEXT_DIRECTION_INHERITED, const String &p_language = "", TextServer::StructuredTextParser p_st_parser = TextServer::STRUCTURED_TEXT_DEFAULT, BitField<TextServer::JustificationFlag> p_jst_flags = TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_SKIP_LAST_LINE | TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE, const PackedFloat32Array &p_tab_stops = PackedFloat32Array());
void push_indent(int p_level); void push_indent(int p_level);
void push_list(int p_level, ListType p_list, bool p_capitalize, const String &p_bullet = String::utf8("")); void push_list(int p_level, ListType p_list, bool p_capitalize, const String &p_bullet = String::utf8(""));
void push_meta(const Variant &p_meta); void push_meta(const Variant &p_meta, MetaUnderline p_underline_mode = META_UNDERLINE_ALWAYS);
void push_hint(const String &p_string); void push_hint(const String &p_string);
void push_table(int p_columns, InlineAlignment p_alignment = INLINE_ALIGNMENT_TOP, int p_align_to_row = -1); void push_table(int p_columns, InlineAlignment p_alignment = INLINE_ALIGNMENT_TOP, int p_align_to_row = -1);
void push_fade(int p_start_index, int p_length); void push_fade(int p_start_index, int p_length);
@ -824,6 +832,7 @@ public:
VARIANT_ENUM_CAST(RichTextLabel::ListType); VARIANT_ENUM_CAST(RichTextLabel::ListType);
VARIANT_ENUM_CAST(RichTextLabel::MenuItems); VARIANT_ENUM_CAST(RichTextLabel::MenuItems);
VARIANT_ENUM_CAST(RichTextLabel::MetaUnderline);
VARIANT_BITFIELD_CAST(RichTextLabel::ImageUpdateMask); VARIANT_BITFIELD_CAST(RichTextLabel::ImageUpdateMask);
#endif // RICH_TEXT_LABEL_H #endif // RICH_TEXT_LABEL_H