diff --git a/core/ustring.cpp b/core/ustring.cpp index fbe3fcb1b2f0..56ee16f6b266 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -4410,6 +4410,45 @@ String String::unquote() const { return substr(1, length() - 2); } +String String::check_control_characters() const { + String s = *this; + String slash = "\\"; + String tab = "\t"; + size_t tab_pos = s.find(tab, 0); + + while (tab_pos != npos) { + String str_begin = s.substr(0, tab_pos); + String str_end = s.substr(tab_pos + 1, s.length()); + + s = str_begin + " " + str_end; + + tab_pos = s.find(tab, tab_pos + 1); + } + + size_t slash_pos = s.find(slash, 0); + + while (slash_pos != npos) { + + if (slash_pos + 1 != s.length()) { + String str_begin = s.substr(0, slash_pos); + String str_end = s.substr(slash_pos + 2, s.length()); + + char chr = s.ord_at(slash_pos + 1); + + if (chr == 'n') { + s = str_begin + "\n" + str_end; + } + + if (chr == 't') { + s = str_begin + " " + str_end; + } + } + slash_pos = s.find(slash, slash_pos + 1); + } + + return s; +} + #ifdef TOOLS_ENABLED String TTR(const String &p_text) { if (TranslationServer::get_singleton()) { diff --git a/core/ustring.h b/core/ustring.h index ee7e3b1e1661..db8f47ab7069 100644 --- a/core/ustring.h +++ b/core/ustring.h @@ -339,6 +339,7 @@ class String { String percent_encode() const; String percent_decode() const; + String check_control_characters() const; String property_name_encode() const; bool is_valid_identifier() const; diff --git a/scene/gui/button.cpp b/scene/gui/button.cpp index 1f8487c17304..f436fe2df754 100644 --- a/scene/gui/button.cpp +++ b/scene/gui/button.cpp @@ -238,15 +238,15 @@ void Button::set_text(const String &p_text) { if (text == p_text) return; - text = p_text; - xl_text = tr(p_text); + text = p_text.check_control_characters(); + xl_text = tr(p_text.check_control_characters()); update(); _change_notify("text"); minimum_size_changed(); } String Button::get_text() const { - return text; + return text.check_control_characters(); } void Button::set_icon(const Ref &p_icon) { diff --git a/scene/gui/control.cpp b/scene/gui/control.cpp index b4dc37c74fba..bfbdca39aad2 100644 --- a/scene/gui/control.cpp +++ b/scene/gui/control.cpp @@ -2209,8 +2209,9 @@ void Control::set_tooltip(const String &p_tooltip) { String Control::get_tooltip(const Point2 &p_pos) const { - return data.tooltip; + return data.tooltip.check_control_characters(); } + Control *Control::make_custom_tooltip(const String &p_text) const { if (get_script_instance()) { return const_cast(this)->call("_make_custom_tooltip", p_text); diff --git a/scene/gui/label.cpp b/scene/gui/label.cpp index bedcef2df2e8..bbf1f5f9c9a5 100644 --- a/scene/gui/label.cpp +++ b/scene/gui/label.cpp @@ -552,8 +552,8 @@ void Label::set_text(const String &p_string) { if (text == p_string) return; - text = p_string; - xl_text = tr(p_string); + text = p_string.check_control_characters(); + xl_text = tr(p_string.check_control_characters()); word_cache_dirty = true; if (percent_visible < 1) visible_chars = get_total_character_count() * percent_visible; @@ -574,7 +574,7 @@ bool Label::is_clipping_text() const { String Label::get_text() const { - return text; + return text.check_control_characters(); } void Label::set_visible_characters(int p_amount) {