Merge pull request #41464 from Waridley/button_multiline_strings

Support multiline strings in buttons
This commit is contained in:
Rémi Verschelde 2022-10-03 13:58:44 +02:00
commit f793f20d65
3 changed files with 61 additions and 40 deletions

View File

@ -34,7 +34,7 @@
#include "servers/visual_server.h" #include "servers/visual_server.h"
Size2 Button::get_minimum_size() const { Size2 Button::get_minimum_size() const {
Size2 minsize = get_font("font")->get_string_size(xl_text); Size2 minsize = get_font("font")->total_size_of_lines(xl_text.split("\n"));
if (clip_text) { if (clip_text) {
minsize.width = 0; minsize.width = 0;
} }
@ -83,6 +83,8 @@ void Button::_notification(int p_what) {
Ref<StyleBox> style = get_stylebox("normal"); Ref<StyleBox> style = get_stylebox("normal");
Vector<String> lines = xl_text.split("\n");
switch (get_draw_mode()) { switch (get_draw_mode()) {
case DRAW_NORMAL: { case DRAW_NORMAL: {
style = get_stylebox("normal"); style = get_stylebox("normal");
@ -202,7 +204,7 @@ void Button::_notification(int p_what) {
int icon_text_separation = text.empty() ? 0 : get_constant("h_separation"); int icon_text_separation = text.empty() ? 0 : get_constant("h_separation");
_size.width -= icon_text_separation + icon_ofs_region; _size.width -= icon_text_separation + icon_ofs_region;
if (!clip_text && icon_align != ALIGN_CENTER) { if (!clip_text && icon_align != ALIGN_CENTER) {
_size.width -= get_font("font")->get_string_size(xl_text).width; _size.width -= get_font("font")->total_size_of_lines(lines).width;
} }
float icon_width = _icon->get_width() * _size.height / _icon->get_height(); float icon_width = _icon->get_width() * _size.height / _icon->get_height();
float icon_height = _size.height; float icon_height = _size.height;
@ -240,8 +242,12 @@ void Button::_notification(int p_what) {
text_clip -= _internal_margin[MARGIN_RIGHT] + get_constant("hseparation"); text_clip -= _internal_margin[MARGIN_RIGHT] + get_constant("hseparation");
} }
Point2 text_ofs = (size - style->get_minimum_size() - icon_ofs - font->get_string_size(xl_text) - Point2(_internal_margin[MARGIN_RIGHT] - _internal_margin[MARGIN_LEFT], 0)) / 2.0; int num_lines = lines.size();
float line_height = font->get_height();
for (int i = 0; i < num_lines; i++) {
String line_text = lines[i];
Point2 text_ofs = (size - style->get_minimum_size() - icon_ofs - font->get_string_size(line_text) - Point2(_internal_margin[MARGIN_RIGHT] - _internal_margin[MARGIN_LEFT], 0)) / 2.0;
switch (align) { switch (align) {
case ALIGN_LEFT: { case ALIGN_LEFT: {
if (icon_align != ALIGN_LEFT) { if (icon_align != ALIGN_LEFT) {
@ -264,7 +270,7 @@ void Button::_notification(int p_what) {
text_ofs += style->get_offset(); text_ofs += style->get_offset();
} break; } break;
case ALIGN_RIGHT: { case ALIGN_RIGHT: {
int text_width = font->get_string_size(xl_text).x; int text_width = font->get_string_size(line_text).x;
if (_internal_margin[MARGIN_RIGHT] > 0) { if (_internal_margin[MARGIN_RIGHT] > 0) {
text_ofs.x = size.x - style->get_margin(MARGIN_RIGHT) - text_width - _internal_margin[MARGIN_RIGHT] - get_constant("hseparation"); text_ofs.x = size.x - style->get_margin(MARGIN_RIGHT) - text_width - _internal_margin[MARGIN_RIGHT] - get_constant("hseparation");
} else { } else {
@ -278,7 +284,9 @@ void Button::_notification(int p_what) {
} }
text_ofs.y += font->get_ascent(); text_ofs.y += font->get_ascent();
font->draw(ci, text_ofs.floor(), xl_text, color, clip_text ? text_clip : -1); text_ofs.y += line_height * (((float)i) - (((float)(num_lines - 1)) / 2.0));
font->draw(ci, text_ofs.floor(), line_text, color, clip_text ? text_clip : -1);
}
} break; } break;
} }
} }
@ -380,7 +388,7 @@ void Button::_bind_methods() {
BIND_ENUM_CONSTANT(ALIGN_CENTER); BIND_ENUM_CONSTANT(ALIGN_CENTER);
BIND_ENUM_CONSTANT(ALIGN_RIGHT); BIND_ENUM_CONSTANT(ALIGN_RIGHT);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT_INTL), "set_text", "get_text"); ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT, "", PROPERTY_USAGE_DEFAULT_INTL), "set_text", "get_text");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_button_icon", "get_button_icon"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_button_icon", "get_button_icon");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_text"), "set_clip_text", "get_clip_text"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_text"), "set_clip_text", "get_clip_text");

View File

@ -523,6 +523,18 @@ Size2 Font::get_wordwrap_string_size(const String &p_string, float p_width) cons
return Size2(p_width, h); return Size2(p_width, h);
} }
Size2 Font::total_size_of_lines(Vector<String> p_lines) {
int num_lines = p_lines.size();
Size2 size;
size.height = get_height() * num_lines;
for (int i = 0; i < num_lines; i++) {
Size2 line_size = get_string_size(p_lines[i]);
size.width = MAX(line_size.width, size.width);
}
return size;
}
void BitmapFont::set_fallback(const Ref<BitmapFont> &p_fallback) { void BitmapFont::set_fallback(const Ref<BitmapFont> &p_fallback) {
for (Ref<BitmapFont> fallback_child = p_fallback; fallback_child != nullptr; fallback_child = fallback_child->get_fallback()) { for (Ref<BitmapFont> fallback_child = p_fallback; fallback_child != nullptr; fallback_child = fallback_child->get_fallback()) {
ERR_FAIL_COND_MSG(fallback_child == this, "Can't set as fallback one of its parents to prevent crashes due to recursive loop."); ERR_FAIL_COND_MSG(fallback_child == this, "Can't set as fallback one of its parents to prevent crashes due to recursive loop.");

View File

@ -56,6 +56,7 @@ public:
virtual Size2 get_char_size(CharType p_char, CharType p_next = 0) const = 0; virtual Size2 get_char_size(CharType p_char, CharType p_next = 0) const = 0;
Size2 get_string_size(const String &p_string) const; Size2 get_string_size(const String &p_string) const;
Size2 get_wordwrap_string_size(const String &p_string, float p_width) const; Size2 get_wordwrap_string_size(const String &p_string, float p_width) const;
Size2 total_size_of_lines(Vector<String> p_lines);
virtual bool is_distance_field_hint() const = 0; virtual bool is_distance_field_hint() const = 0;