Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove bbcode_text from RichTextLabel #39148

Merged
merged 1 commit into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions doc/classes/RichTextLabel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</brief_description>
<description>
Rich text can contain custom text, fonts, images and some basic formatting. The label manages these as an internal tag stack. It also adapts itself to given width/heights.
[b]Note:[/b] Assignments to [member bbcode_text] clear the tag stack and reconstruct it from the property's contents. Any edits made to [member bbcode_text] will erase previous edits made from other manual sources such as [method append_bbcode] and the [code]push_*[/code] / [method pop] methods.
[b]Note:[/b] Assignments to [member text] clear the tag stack and reconstruct it from the property's contents. Any edits made to [member text] will erase previous edits made from other manual sources such as [method append_text] and the [code]push_*[/code] / [method pop] methods.
[b]Note:[/b] RichTextLabel doesn't support entangled BBCode tags. For example, instead of using [code][b]bold[i]bold italic[/b]italic[/i][/code], use [code][b]bold[i]bold italic[/i][/b][i]italic[/i][/code].
[b]Note:[/b] [code]push_*/pop[/code] functions won't affect BBCode.
[b]Note:[/b] Unlike [Label], RichTextLabel doesn't have a [i]property[/i] to horizontally align text to the center. Instead, enable [member bbcode_enabled] and surround the text in a [code][center][/code] tag as follows: [code][center]Example[/center][/code]. There is currently no built-in way to vertically align text either, but this can be emulated by relying on anchors/containers and the [member fit_content_height] property.
Expand Down Expand Up @@ -35,18 +35,18 @@
Adds raw non-BBCode-parsed text to the tag stack.
</description>
</method>
<method name="append_bbcode">
<method name="append_text">
<return type="int" enum="Error" />
<argument index="0" name="bbcode" type="String" />
<description>
Parses [code]bbcode[/code] and adds tags to the tag stack as needed. Returns the result of the parsing, [constant OK] if successful.
[b]Note:[/b] Using this method, you can't close a tag that was opened in a previous [method append_bbcode] call. This is done to improve performance, especially when updating large RichTextLabels since rebuilding the whole BBCode every time would be slower. If you absolutely need to close a tag in a future method call, append the [member bbcode_text] instead of using [method append_bbcode].
[b]Note:[/b] Using this method, you can't close a tag that was opened in a previous [method append_text] call. This is done to improve performance, especially when updating large RichTextLabels since rebuilding the whole BBCode every time would be slower. If you absolutely need to close a tag in a future method call, append the [member text] instead of using [method append_text].
</description>
</method>
<method name="clear">
<return type="void" />
<description>
Clears the tag stack and sets [member bbcode_text] to an empty string.
Clears the tag stack and sets [member text] to an empty string.
</description>
</method>
<method name="get_content_height" qualifiers="const">
Expand All @@ -67,6 +67,12 @@
Returns the total number of paragraphs (newlines or [code]p[/code] tags in the tag stack's text tags). Considers wrapped text as one paragraph.
</description>
</method>
<method name="get_parsed_text" qualifiers="const">
<return type="String" />
<description>
Returns the text without BBCode mark-up.
</description>
</method>
<method name="get_selected_text" qualifiers="const">
<return type="String" />
<description>
Expand Down Expand Up @@ -126,7 +132,7 @@
<return type="int" enum="Error" />
<argument index="0" name="bbcode" type="String" />
<description>
The assignment version of [method append_bbcode]. Clears the tag stack and inserts the new content. Returns [constant OK] if parses [code]bbcode[/code] successfully.
The assignment version of [method append_text]. Clears the tag stack and inserts the new content. Returns [constant OK] if parses [code]bbcode[/code] successfully.
</description>
</method>
<method name="parse_expressions_for_values">
Expand Down Expand Up @@ -368,10 +374,6 @@
<member name="bbcode_enabled" type="bool" setter="set_use_bbcode" getter="is_using_bbcode" default="false">
If [code]true[/code], the label uses BBCode formatting.
</member>
<member name="bbcode_text" type="String" setter="set_bbcode" getter="get_bbcode" default="&quot;&quot;">
The label's text in BBCode format. Is not representative of manual modifications to the internal tag stack. Erases changes made by other methods when edited.
[b]Note:[/b] It is unadvised to use the [code]+=[/code] operator with [code]bbcode_text[/code] (e.g. [code]bbcode_text += "some string"[/code]) as it replaces the whole text and can cause slowdowns. Use [method append_bbcode] for adding text instead, unless you absolutely need to close a tag that was opened in an earlier method call.
</member>
<member name="custom_effects" type="Array" setter="set_effects" getter="get_effects" default="[]">
The currently installed custom effects. This is an array of [RichTextEffect]s.
To add a custom effect, it's more convenient to use [method install_effect].
Expand Down Expand Up @@ -413,8 +415,8 @@
The number of spaces associated with a single tab length. Does not affect [code]\t[/code] in text tags, only indent tags.
</member>
<member name="text" type="String" setter="set_text" getter="get_text" default="&quot;&quot;">
The raw text of the label.
When set, clears the tag stack and adds a raw text tag to the top of it. Does not parse BBCodes. Does not modify [member bbcode_text].
The label's text in BBCode format. Is not representative of manual modifications to the internal tag stack. Erases changes made by other methods when edited.
[b]Note:[/b] If [member bbcode_enabled] is [code]true[/code], it is unadvised to use the [code]+=[/code] operator with [code]text[/code] (e.g. [code]text += "some string"[/code]) as it replaces the whole text and can cause slowdowns. Use [method append_text] for adding text instead, unless you absolutely need to close a tag that was opened in an earlier method call.
</member>
<member name="text_direction" type="int" setter="set_text_direction" getter="get_text_direction" enum="Control.TextDirection" default="0">
Base text writing direction.
Expand Down
14 changes: 7 additions & 7 deletions editor/editor_help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ void EditorHelp::_update_doc() {
}

class_desc->push_color(symbol_color);
class_desc->append_bbcode("[url=" + link + "]" + linktxt + "[/url]");
class_desc->append_text("[url=" + link + "]" + linktxt + "[/url]");
class_desc->pop();
class_desc->add_newline();
}
Expand Down Expand Up @@ -1180,9 +1180,9 @@ void EditorHelp::_update_doc() {
class_desc->add_text(" ");
class_desc->push_color(comment_color);
if (cd.is_script_doc) {
class_desc->append_bbcode(TTR("There is currently no description for this property."));
class_desc->append_text(TTR("There is currently no description for this property."));
} else {
class_desc->append_bbcode(TTR("There is currently no description for this property. Please help us by [color=$color][url=$url]contributing one[/url][/color]!").replace("$url", CONTRIBUTE_URL).replace("$color", link_color_text));
class_desc->append_text(TTR("There is currently no description for this property. Please help us by [color=$color][url=$url]contributing one[/url][/color]!").replace("$url", CONTRIBUTE_URL).replace("$color", link_color_text));
}
class_desc->pop();
}
Expand Down Expand Up @@ -1229,7 +1229,7 @@ void EditorHelp::_update_doc() {
class_desc->push_font(doc_font);
class_desc->push_indent(1);
if (methods_filtered[i].errors_returned.size()) {
class_desc->append_bbcode(TTR("Error codes returned:"));
class_desc->append_text(TTR("Error codes returned:"));
class_desc->add_newline();
class_desc->push_list(0, RichTextLabel::LIST_DOTS, false);
for (int j = 0; j < methods_filtered[i].errors_returned.size(); j++) {
Expand All @@ -1246,7 +1246,7 @@ void EditorHelp::_update_doc() {
}

class_desc->push_bold();
class_desc->append_bbcode(text);
class_desc->append_text(text);
class_desc->pop();
}
class_desc->pop();
Expand All @@ -1260,9 +1260,9 @@ void EditorHelp::_update_doc() {
class_desc->add_text(" ");
class_desc->push_color(comment_color);
if (cd.is_script_doc) {
class_desc->append_bbcode(TTR("There is currently no description for this method."));
class_desc->append_text(TTR("There is currently no description for this method."));
} else {
class_desc->append_bbcode(TTR("There is currently no description for this method. Please help us by [color=$color][url=$url]contributing one[/url][/color]!").replace("$url", CONTRIBUTE_URL).replace("$color", link_color_text));
class_desc->append_text(TTR("There is currently no description for this method. Please help us by [color=$color][url=$url]contributing one[/url][/color]!").replace("$url", CONTRIBUTE_URL).replace("$color", link_color_text));
}
class_desc->pop();
}
Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/asset_library_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ void EditorAssetLibraryItemDescription::configure(const String &p_title, int p_a
description->add_text(TTR("View Files"));
description->pop();
description->add_text("\n" + TTR("Description:") + "\n\n");
description->append_bbcode(p_description);
description->append_text(p_description);
set_title(p_title);
}

Expand Down
57 changes: 27 additions & 30 deletions scene/gui/rich_text_label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1374,8 +1374,8 @@ void RichTextLabel::_notification(int p_what) {
} break;
case NOTIFICATION_THEME_CHANGED:
case NOTIFICATION_ENTER_TREE: {
if (bbcode != "") {
set_bbcode(bbcode);
if (text != "") {
set_text(text);
}

main->first_invalid_line = 0; //invalidate ALL
Expand Down Expand Up @@ -2767,10 +2767,10 @@ bool RichTextLabel::is_scroll_following() const {

Error RichTextLabel::parse_bbcode(const String &p_bbcode) {
clear();
return append_bbcode(p_bbcode);
return append_text(p_bbcode);
}

Error RichTextLabel::append_bbcode(const String &p_bbcode) {
Error RichTextLabel::append_text(const String &p_bbcode) {
int pos = 0;

List<String> tag_stack;
Expand Down Expand Up @@ -3824,8 +3824,8 @@ int RichTextLabel::get_selection_to() const {
return selection.to_frame->lines[selection.to_line].char_offset + selection.to_char - 1;
}

void RichTextLabel::set_bbcode(const String &p_bbcode) {
bbcode = p_bbcode;
void RichTextLabel::set_text(const String &p_bbcode) {
text = p_bbcode;
if (is_inside_tree() && use_bbcode) {
parse_bbcode(p_bbcode);
} else { // raw text
Expand All @@ -3834,24 +3834,24 @@ void RichTextLabel::set_bbcode(const String &p_bbcode) {
}
}

String RichTextLabel::get_bbcode() const {
return bbcode;
String RichTextLabel::get_text() const {
return text;
}

void RichTextLabel::set_use_bbcode(bool p_enable) {
if (use_bbcode == p_enable) {
return;
}
use_bbcode = p_enable;
set_bbcode(bbcode);
notify_property_list_changed();
set_text(text);
}

bool RichTextLabel::is_using_bbcode() const {
return use_bbcode;
}

String RichTextLabel::get_text() {
String RichTextLabel::get_parsed_text() const {
String text = "";
Item *it = main;
while (it) {
Expand All @@ -3870,11 +3870,6 @@ String RichTextLabel::get_text() {
return text;
}

void RichTextLabel::set_text(const String &p_string) {
clear();
add_text(p_string);
}

void RichTextLabel::set_text_direction(Control::TextDirection p_text_direction) {
ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
if (text_direction != p_text_direction) {
Expand Down Expand Up @@ -3948,8 +3943,8 @@ float RichTextLabel::get_percent_visible() const {

void RichTextLabel::set_effects(Array p_effects) {
custom_effects = p_effects;
if ((bbcode != "") && use_bbcode) {
parse_bbcode(bbcode);
if ((text != "") && use_bbcode) {
parse_bbcode(text);
}
}

Expand All @@ -3963,8 +3958,8 @@ void RichTextLabel::install_effect(const Variant effect) {

if (rteffect.is_valid()) {
custom_effects.push_back(effect);
if ((bbcode != "") && use_bbcode) {
parse_bbcode(bbcode);
if ((text != "") && use_bbcode) {
parse_bbcode(text);
}
}
}
Expand All @@ -3977,14 +3972,20 @@ int RichTextLabel::get_content_height() const {
return total_height;
}

void RichTextLabel::_validate_property(PropertyInfo &property) const {
if (!use_bbcode && property.name == "bbcode_text") {
property.usage = PROPERTY_USAGE_NOEDITOR;
#ifndef DISABLE_DEPRECATED
// People will be very angry, if their texts get erased, because of #39148. (3.x -> 4.0)
// Altough some people may not used bbcode_text, so we only overwrite, if bbcode_text is not empty
bool RichTextLabel::_set(const StringName &p_name, const Variant &p_value) {
if (p_name == "bbcode_text" && !((String)p_value).is_empty()) {
set_text(p_value);
return true;
}
return false;
}
#endif

void RichTextLabel::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_text"), &RichTextLabel::get_text);
ClassDB::bind_method(D_METHOD("get_parsed_text"), &RichTextLabel::get_parsed_text);
ClassDB::bind_method(D_METHOD("add_text", "text"), &RichTextLabel::add_text);
ClassDB::bind_method(D_METHOD("set_text", "text"), &RichTextLabel::set_text);
ClassDB::bind_method(D_METHOD("add_image", "image", "width", "height", "color", "inline_align"), &RichTextLabel::add_image, DEFVAL(0), DEFVAL(0), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(INLINE_ALIGN_CENTER));
Expand Down Expand Up @@ -4062,10 +4063,9 @@ void RichTextLabel::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_selected_text"), &RichTextLabel::get_selected_text);

ClassDB::bind_method(D_METHOD("parse_bbcode", "bbcode"), &RichTextLabel::parse_bbcode);
ClassDB::bind_method(D_METHOD("append_bbcode", "bbcode"), &RichTextLabel::append_bbcode);
ClassDB::bind_method(D_METHOD("append_text", "bbcode"), &RichTextLabel::append_text);

ClassDB::bind_method(D_METHOD("set_bbcode", "text"), &RichTextLabel::set_bbcode);
ClassDB::bind_method(D_METHOD("get_bbcode"), &RichTextLabel::get_bbcode);
ClassDB::bind_method(D_METHOD("get_text"), &RichTextLabel::get_text);

ClassDB::bind_method(D_METHOD("set_visible_characters", "amount"), &RichTextLabel::set_visible_characters);
ClassDB::bind_method(D_METHOD("get_visible_characters"), &RichTextLabel::get_visible_characters);
Expand All @@ -4092,16 +4092,13 @@ void RichTextLabel::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_effects"), &RichTextLabel::get_effects);
ClassDB::bind_method(D_METHOD("install_effect", "effect"), &RichTextLabel::install_effect);

ADD_GROUP("BBCode", "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::INT, "visible_characters", PROPERTY_HINT_RANGE, "-1,128000,1"), "set_visible_characters", "get_visible_characters");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "percent_visible", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_percent_visible", "get_percent_visible");

ADD_PROPERTY(PropertyInfo(Variant::BOOL, "meta_underlined"), "set_meta_underline", "is_meta_underlined");
ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_size", PROPERTY_HINT_RANGE, "0,24,1"), "set_tab_size", "get_tab_size");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bbcode_enabled"), "set_use_bbcode", "is_using_bbcode");

ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fit_content_height"), "set_fit_content_height", "is_fit_content_height_enabled");

Expand Down
18 changes: 9 additions & 9 deletions scene/gui/rich_text_label.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ class RichTextLabel : public Control {
protected:
void _notification(int p_what);
static void _bind_methods();
void _validate_property(PropertyInfo &property) const override;

private:
struct Item;
Expand Down Expand Up @@ -452,16 +451,19 @@ class RichTextLabel : public Control {
virtual Dictionary parse_expressions_for_values(Vector<String> p_expressions);

void _draw_fbg_boxes(RID p_ci, RID p_rid, Vector2 line_off, Item *it_from, Item *it_to, int start, int end, int fbg_flag);

#ifndef DISABLE_DEPRECATED
// Kept for compatibility from 3.x to 4.0.
bool _set(const StringName &p_name, const Variant &p_value);
#endif
bool use_bbcode = false;
String bbcode;
String text;

int fixed_width = -1;

bool fit_content_height = false;

public:
String get_text();
String get_parsed_text() const;
void add_text(const String &p_text);
void add_image(const Ref<Texture2D> &p_image, const int p_width = 0, const int p_height = 0, const Color &p_color = Color(1.0, 1.0, 1.0), InlineAlign p_align = INLINE_ALIGN_CENTER);
void add_newline();
Expand Down Expand Up @@ -548,15 +550,13 @@ class RichTextLabel : public Control {
void selection_copy();

Error parse_bbcode(const String &p_bbcode);
Error append_bbcode(const String &p_bbcode);
Error append_text(const String &p_bbcode);

void set_use_bbcode(bool p_enable);
bool is_using_bbcode() const;

void set_bbcode(const String &p_bbcode);
String get_bbcode() const;

void set_text(const String &p_string);
void set_text(const String &p_bbcode);
String get_text() const;

void set_text_direction(TextDirection p_text_direction);
TextDirection get_text_direction() const;
Expand Down