diff options
Diffstat (limited to 'scene/gui/label.cpp')
-rw-r--r-- | scene/gui/label.cpp | 121 |
1 files changed, 42 insertions, 79 deletions
diff --git a/scene/gui/label.cpp b/scene/gui/label.cpp index 7490101ee3..f49acc1b96 100644 --- a/scene/gui/label.cpp +++ b/scene/gui/label.cpp @@ -35,7 +35,6 @@ #include "core/translation.h" void Label::set_autowrap(bool p_autowrap) { - if (autowrap == p_autowrap) { return; } @@ -48,34 +47,31 @@ void Label::set_autowrap(bool p_autowrap) { minimum_size_changed(); } } -bool Label::has_autowrap() const { +bool Label::has_autowrap() const { return autowrap; } void Label::set_uppercase(bool p_uppercase) { - uppercase = p_uppercase; word_cache_dirty = true; update(); } -bool Label::is_uppercase() const { +bool Label::is_uppercase() const { return uppercase; } int Label::get_line_height() const { - return get_theme_font("font")->get_height(); } void Label::_notification(int p_what) { - if (p_what == NOTIFICATION_TRANSLATION_CHANGED) { - String new_text = tr(text); - if (new_text == xl_text) + if (new_text == xl_text) { return; //nothing new + } xl_text = new_text; regenerate_word_cache(); @@ -83,13 +79,13 @@ void Label::_notification(int p_what) { } if (p_what == NOTIFICATION_DRAW) { - if (clip) { RenderingServer::get_singleton()->canvas_item_set_clip(get_canvas_item(), true); } - if (word_cache_dirty) + if (word_cache_dirty) { regenerate_word_cache(); + } RID ci = get_canvas_item(); @@ -126,9 +122,7 @@ void Label::_notification(int p_what) { } if (lines_visible > 0) { - switch (valign) { - case VALIGN_TOP: { //nothing } break; @@ -155,22 +149,25 @@ void Label::_notification(int p_what) { } WordCache *wc = word_cache; - if (!wc) + if (!wc) { return; + } int line = 0; int line_to = lines_skipped + (lines_visible > 0 ? lines_visible : 1); FontDrawer drawer(font, font_outline_modulate); while (wc) { /* handle lines not meant to be drawn quickly */ - if (line >= line_to) + if (line >= line_to) { break; + } if (line < lines_skipped) { - - while (wc && wc->char_pos >= 0) + while (wc && wc->char_pos >= 0) { wc = wc->next; - if (wc) + } + if (wc) { wc = wc->next; + } line++; continue; } @@ -190,7 +187,6 @@ void Label::_notification(int p_what) { int taken = 0; int spaces = 0; while (to && to->char_pos >= 0) { - taken += to->pixel_width; if (to != from && to->space_count) { spaces += to->space_count; @@ -203,18 +199,14 @@ void Label::_notification(int p_what) { float x_ofs = 0; switch (align) { - case ALIGN_FILL: case ALIGN_LEFT: { - x_ofs = style->get_offset().x; } break; case ALIGN_CENTER: { - x_ofs = int(size.width - (taken + spaces * space_w)) / 2; } break; case ALIGN_RIGHT: { - x_ofs = int(size.width - style->get_margin(MARGIN_RIGHT) - (taken + spaces * space_w)); } break; } @@ -224,11 +216,9 @@ void Label::_notification(int p_what) { y_ofs += vbegin + line * vsep; while (from != to) { - // draw a word int pos = from->char_pos; if (from->char_pos < 0) { - ERR_PRINT("BUG"); return; } @@ -236,17 +226,14 @@ void Label::_notification(int p_what) { /* spacing */ x_ofs += space_w * from->space_count; if (can_fill && align == ALIGN_FILL && spaces) { - x_ofs += int((size.width - (taken + space_w * spaces)) / spaces); } } if (font_color_shadow.a > 0) { - int chars_total_shadow = chars_total; //save chars drawn float x_ofs_shadow = x_ofs; for (int i = 0; i < from->word_len; i++) { - if (visible_chars < 0 || chars_total_shadow < visible_chars) { CharType c = xl_text[i + pos]; CharType n = xl_text[i + pos + 1]; @@ -267,7 +254,6 @@ void Label::_notification(int p_what) { } } for (int i = 0; i < from->word_len; i++) { - if (visible_chars < 0 || chars_total < visible_chars) { CharType c = xl_text[i + pos]; CharType n = xl_text[i + pos + 1]; @@ -289,18 +275,15 @@ void Label::_notification(int p_what) { } if (p_what == NOTIFICATION_THEME_CHANGED) { - word_cache_dirty = true; update(); } if (p_what == NOTIFICATION_RESIZED) { - word_cache_dirty = true; } } Size2 Label::get_minimum_size() const { - Size2 min_style = get_theme_stylebox("normal")->get_minimum_size(); // don't want to mutable everything @@ -308,79 +291,78 @@ Size2 Label::get_minimum_size() const { const_cast<Label *>(this)->regenerate_word_cache(); } - if (autowrap) + if (autowrap) { return Size2(1, clip ? 1 : minsize.height) + min_style; - else { + } else { Size2 ms = minsize; - if (clip) + if (clip) { ms.width = 1; + } return ms + min_style; } } int Label::get_longest_line_width() const { - Ref<Font> font = get_theme_font("font"); real_t max_line_width = 0; real_t line_width = 0; for (int i = 0; i < xl_text.size(); i++) { - CharType current = xl_text[i]; - if (uppercase) + if (uppercase) { current = String::char_uppercase(current); + } if (current < 32) { - if (current == '\n') { - - if (line_width > max_line_width) + if (line_width > max_line_width) { max_line_width = line_width; + } line_width = 0; } } else { - real_t char_width = font->get_char_size(current, xl_text[i + 1]).width; line_width += char_width; } } - if (line_width > max_line_width) + if (line_width > max_line_width) { max_line_width = line_width; + } // ceiling to ensure autowrapping does not cut text return Math::ceil(max_line_width); } int Label::get_line_count() const { - - if (!is_inside_tree()) + if (!is_inside_tree()) { return 1; - if (word_cache_dirty) + } + if (word_cache_dirty) { const_cast<Label *>(this)->regenerate_word_cache(); + } return line_count; } int Label::get_visible_line_count() const { - int line_spacing = get_theme_constant("line_spacing"); int font_h = get_theme_font("font")->get_height() + line_spacing; int lines_visible = (get_size().height - get_theme_stylebox("normal")->get_minimum_size().height + line_spacing) / font_h; - if (lines_visible > line_count) + if (lines_visible > line_count) { lines_visible = line_count; + } - if (max_lines_visible >= 0 && lines_visible > max_lines_visible) + if (max_lines_visible >= 0 && lines_visible > max_lines_visible) { lines_visible = max_lines_visible; + } return lines_visible; } void Label::regenerate_word_cache() { - while (word_cache) { - WordCache *current = word_cache; word_cache = current->next; memdelete(current); @@ -408,11 +390,11 @@ void Label::regenerate_word_cache() { WordCache *last = nullptr; for (int i = 0; i <= xl_text.length(); i++) { - CharType current = i < xl_text.length() ? xl_text[i] : L' '; //always a space at the end, so the algo works - if (uppercase) + if (uppercase) { current = String::char_uppercase(current); + } // ranges taken from http://www.unicodemap.org/ // if your language is not well supported, consider helping improve @@ -423,7 +405,6 @@ void Label::regenerate_word_cache() { real_t char_width = 0; if (current < 33) { - if (current_word_size > 0) { WordCache *wc = memnew(WordCache); if (word_cache) { @@ -509,8 +490,9 @@ void Label::regenerate_word_cache() { } } - if (!autowrap) + if (!autowrap) { minsize.width = width; + } if (max_lines_visible > 0 && line_count > max_lines_visible) { minsize.height = (font->get_height() * max_lines_visible) + (line_spacing * (max_lines_visible - 1)); @@ -526,60 +508,53 @@ void Label::regenerate_word_cache() { } void Label::set_align(Align p_align) { - ERR_FAIL_INDEX((int)p_align, 4); align = p_align; update(); } Label::Align Label::get_align() const { - return align; } void Label::set_valign(VAlign p_align) { - ERR_FAIL_INDEX((int)p_align, 4); valign = p_align; update(); } Label::VAlign Label::get_valign() const { - return valign; } void Label::set_text(const String &p_string) { - - if (text == p_string) + if (text == p_string) { return; + } text = p_string; xl_text = tr(p_string); word_cache_dirty = true; - if (percent_visible < 1) + if (percent_visible < 1) { visible_chars = get_total_character_count() * percent_visible; + } update(); } void Label::set_clip_text(bool p_clip) { - clip = p_clip; update(); minimum_size_changed(); } bool Label::is_clipping_text() const { - return clip; } String Label::get_text() const { - return text; } void Label::set_visible_characters(int p_amount) { - visible_chars = p_amount; if (get_total_character_count() > 0) { percent_visible = (float)p_amount / (float)total_char_cache; @@ -589,19 +564,15 @@ void Label::set_visible_characters(int p_amount) { } int Label::get_visible_characters() const { - return visible_chars; } void Label::set_percent_visible(float p_percent) { - if (p_percent < 0 || p_percent >= 1) { - visible_chars = -1; percent_visible = 1; } else { - visible_chars = get_total_character_count() * p_percent; percent_visible = p_percent; } @@ -610,42 +581,36 @@ void Label::set_percent_visible(float p_percent) { } float Label::get_percent_visible() const { - return percent_visible; } void Label::set_lines_skipped(int p_lines) { - lines_skipped = p_lines; update(); } int Label::get_lines_skipped() const { - return lines_skipped; } void Label::set_max_lines_visible(int p_lines) { - max_lines_visible = p_lines; update(); } int Label::get_max_lines_visible() const { - return max_lines_visible; } int Label::get_total_character_count() const { - - if (word_cache_dirty) + if (word_cache_dirty) { const_cast<Label *>(this)->regenerate_word_cache(); + } return total_char_cache; } void Label::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_align", "align"), &Label::set_align); ClassDB::bind_method(D_METHOD("get_align"), &Label::get_align); ClassDB::bind_method(D_METHOD("set_valign", "valign"), &Label::set_valign); @@ -700,9 +665,7 @@ Label::Label(const String &p_text) { } Label::~Label() { - while (word_cache) { - WordCache *current = word_cache; word_cache = current->next; memdelete(current); |