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

Fixing Button text ignores breakline (\n) #2967 #24374

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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
39 changes: 39 additions & 0 deletions core/ustring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4214,6 +4214,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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we always assume, that \t == 4 spaces? Shouldn't it be configurable per usecase?


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) {

Expand Down
2 changes: 2 additions & 0 deletions core/ustring.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ class String {
String percent_encode() const;
String percent_decode() const;

String check_control_characters() const;

bool is_valid_identifier() const;
bool is_valid_integer() const;
bool is_valid_float() const;
Expand Down
6 changes: 3 additions & 3 deletions scene/gui/button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,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<Texture> &p_icon) {
Expand Down
4 changes: 3 additions & 1 deletion scene/gui/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "scene/gui/label.h"
#include "scene/gui/panel.h"
#include "scene/scene_string_names.h"

cesarpinho marked this conversation as resolved.
Show resolved Hide resolved
#ifdef TOOLS_ENABLED
#include "editor/editor_settings.h"
#endif
Expand Down Expand Up @@ -2198,8 +2199,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<Control *>(this)->call("_make_custom_tooltip", p_text);
Expand Down
6 changes: 3 additions & 3 deletions scene/gui/label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,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;
Expand All @@ -559,7 +559,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) {
Expand Down