/*************************************************************************/ /* label.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "label.h" #include "core/print_string.h" #include "core/project_settings.h" #include "core/translation.h" void Label::set_autowrap(bool p_autowrap) { if (autowrap == p_autowrap) { return; } autowrap = p_autowrap; word_cache_dirty = true; update(); if (clip) { minimum_size_changed(); } } 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 { 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) return; //nothing new xl_text = new_text; regenerate_word_cache(); update(); } if (p_what == NOTIFICATION_DRAW) { if (clip) { VisualServer::get_singleton()->canvas_item_set_clip(get_canvas_item(), true); } if (word_cache_dirty) regenerate_word_cache(); RID ci = get_canvas_item(); Size2 string_size; Size2 size = get_size(); Ref style = get_theme_stylebox("normal"); Ref font = get_theme_font("font"); Color font_color = get_theme_color("font_color"); Color font_color_shadow = get_theme_color("font_color_shadow"); bool use_outline = get_theme_constant("shadow_as_outline"); Point2 shadow_ofs(get_theme_constant("shadow_offset_x"), get_theme_constant("shadow_offset_y")); int line_spacing = get_theme_constant("line_spacing"); Color font_outline_modulate = get_theme_color("font_outline_modulate"); style->draw(ci, Rect2(Point2(0, 0), get_size())); VisualServer::get_singleton()->canvas_item_set_distance_field_mode(get_canvas_item(), font.is_valid() && font->is_distance_field_hint()); int font_h = font->get_height() + line_spacing; int lines_visible = (size.y + line_spacing) / font_h; real_t space_w = font->get_char_size(' ').width; int chars_total = 0; int vbegin = 0, vsep = 0; if (lines_visible > line_count) { lines_visible = line_count; } if (max_lines_visible >= 0 && lines_visible > max_lines_visible) { lines_visible = max_lines_visible; } if (lines_visible > 0) { switch (valign) { case VALIGN_TOP: { //nothing } break; case VALIGN_CENTER: { vbegin = (size.y - (lines_visible * font_h - line_spacing)) / 2; vsep = 0; } break; case VALIGN_BOTTOM: { vbegin = size.y - (lines_visible * font_h - line_spacing); vsep = 0; } break; case VALIGN_FILL: { vbegin = 0; if (lines_visible > 1) { vsep = (size.y - (lines_visible * font_h - line_spacing)) / (lines_visible - 1); } else { vsep = 0; } } break; } } WordCache *wc = word_cache; 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) break; if (line < lines_skipped) { while (wc && wc->char_pos >= 0) wc = wc->next; if (wc) wc = wc->next; line++; continue; } /* handle lines normally */ if (wc->char_pos < 0) { //empty line wc = wc->next; line++; continue; } WordCache *from = wc; WordCache *to = wc; 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; } to = to->next; } bool can_fill = to && to->char_pos == WordCache::CHAR_WRAPLINE; 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; } float y_ofs = style->get_offset().y; y_ofs += (line - lines_skipped) * font_h + font->get_ascent(); 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; } if (from->space_count) { /* 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]; if (uppercase) { c = String::char_uppercase(c); n = String::char_uppercase(n); } float move = drawer.draw_char(ci, Point2(x_ofs_shadow, y_ofs) + shadow_ofs, c, n, font_color_shadow); if (use_outline) { drawer.draw_char(ci, Point2(x_ofs_shadow, y_ofs) + Vector2(-shadow_ofs.x, shadow_ofs.y), c, n, font_color_shadow); drawer.draw_char(ci, Point2(x_ofs_shadow, y_ofs) + Vector2(shadow_ofs.x, -shadow_ofs.y), c, n, font_color_shadow); drawer.draw_char(ci, Point2(x_ofs_shadow, y_ofs) + Vector2(-shadow_ofs.x, -shadow_ofs.y), c, n, font_color_shadow); } x_ofs_shadow += move; chars_total_shadow++; } } } 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]; if (uppercase) { c = String::char_uppercase(c); n = String::char_uppercase(n); } x_ofs += drawer.draw_char(ci, Point2(x_ofs, y_ofs), c, n, font_color); chars_total++; } } from = from->next; } wc = to ? to->next : 0; line++; } } 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 if (word_cache_dirty) { const_cast