/**************************************************************************/ /* button.cpp */ /**************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /**************************************************************************/ /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ /* */ /* 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 "button.h" #include "core/string/translation.h" #include "scene/theme/theme_db.h" #include "servers/rendering_server.h" Size2 Button::get_minimum_size() const { Ref _icon = icon; if (_icon.is_null() && has_theme_icon(SNAME("icon"))) { _icon = theme_cache.icon; } return get_minimum_size_for_text_and_icon("", _icon); } void Button::_set_internal_margin(Side p_side, float p_value) { _internal_margin[p_side] = p_value; } void Button::_queue_update_size_cache() { } void Button::_notification(int p_what) { switch (p_what) { case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: { queue_redraw(); } break; case NOTIFICATION_TRANSLATION_CHANGED: { xl_text = atr(text); _shape(); update_minimum_size(); queue_redraw(); } break; case NOTIFICATION_THEME_CHANGED: { _shape(); update_minimum_size(); queue_redraw(); } break; case NOTIFICATION_DRAW: { RID ci = get_canvas_item(); Size2 size = get_size(); Color color; Color color_icon(1, 1, 1, 1); Ref style = theme_cache.normal; bool rtl = is_layout_rtl(); const bool is_clipped = clip_text || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING; switch (get_draw_mode()) { case DRAW_NORMAL: { if (rtl && has_theme_stylebox(SNAME("normal_mirrored"))) { style = theme_cache.normal_mirrored; } else { style = theme_cache.normal; } if (!flat) { style->draw(ci, Rect2(Point2(0, 0), size)); } // Focus colors only take precedence over normal state. if (has_focus()) { color = theme_cache.font_focus_color; if (has_theme_color(SNAME("icon_focus_color"))) { color_icon = theme_cache.icon_focus_color; } } else { color = theme_cache.font_color; if (has_theme_color(SNAME("icon_normal_color"))) { color_icon = theme_cache.icon_normal_color; } } } break; case DRAW_HOVER_PRESSED: { // Edge case for CheckButton and CheckBox. if (has_theme_stylebox("hover_pressed")) { if (rtl && has_theme_stylebox(SNAME("hover_pressed_mirrored"))) { style = theme_cache.hover_pressed_mirrored; } else { style = theme_cache.hover_pressed; } if (!flat) { style->draw(ci, Rect2(Point2(0, 0), size)); } if (has_theme_color(SNAME("font_hover_pressed_color"))) { color = theme_cache.font_hover_pressed_color; } if (has_theme_color(SNAME("icon_hover_pressed_color"))) { color_icon = theme_cache.icon_hover_pressed_color; } break; } [[fallthrough]]; } case DRAW_PRESSED: { if (rtl && has_theme_stylebox(SNAME("pressed_mirrored"))) { style = theme_cache.pressed_mirrored; } else { style = theme_cache.pressed; } if (!flat) { style->draw(ci, Rect2(Point2(0, 0), size)); } if (has_theme_color(SNAME("font_pressed_color"))) { color = theme_cache.font_pressed_color; } else { color = theme_cache.font_color; } if (has_theme_color(SNAME("icon_pressed_color"))) { color_icon = theme_cache.icon_pressed_color; } } break; case DRAW_HOVER: { if (rtl && has_theme_stylebox(SNAME("hover_mirrored"))) { style = theme_cache.hover_mirrored; } else { style = theme_cache.hover; } if (!flat) { style->draw(ci, Rect2(Point2(0, 0), size)); } color = theme_cache.font_hover_color; if (has_theme_color(SNAME("icon_hover_color"))) { color_icon = theme_cache.icon_hover_color; } } break; case DRAW_DISABLED: { if (rtl && has_theme_stylebox(SNAME("disabled_mirrored"))) { style = theme_cache.disabled_mirrored; } else { style = theme_cache.disabled; } if (!flat) { style->draw(ci, Rect2(Point2(0, 0), size)); } color = theme_cache.font_disabled_color; if (has_theme_color(SNAME("icon_disabled_color"))) { color_icon = theme_cache.icon_disabled_color; } else { color_icon.a = 0.4; } } break; } if (has_focus()) { Ref style2 = theme_cache.focus; style2->draw(ci, Rect2(Point2(), size)); } Ref _icon; if (icon.is_null() && has_theme_icon(SNAME("icon"))) { _icon = theme_cache.icon; } else { _icon = icon; } Rect2 icon_region; HorizontalAlignment icon_align_rtl_checked = horizontal_icon_alignment; HorizontalAlignment align_rtl_checked = alignment; // Swap icon and text alignment sides if right-to-left layout is set. if (rtl) { if (horizontal_icon_alignment == HORIZONTAL_ALIGNMENT_RIGHT) { icon_align_rtl_checked = HORIZONTAL_ALIGNMENT_LEFT; } else if (horizontal_icon_alignment == HORIZONTAL_ALIGNMENT_LEFT) { icon_align_rtl_checked = HORIZONTAL_ALIGNMENT_RIGHT; } if (alignment == HORIZONTAL_ALIGNMENT_RIGHT) { align_rtl_checked = HORIZONTAL_ALIGNMENT_LEFT; } else if (alignment == HORIZONTAL_ALIGNMENT_LEFT) { align_rtl_checked = HORIZONTAL_ALIGNMENT_RIGHT; } } if (!_icon.is_null()) { int valign = size.height - style->get_minimum_size().y; int voffset = 0; Size2 icon_size = _icon->get_size(); // Fix vertical size. if (vertical_icon_alignment != VERTICAL_ALIGNMENT_CENTER) { valign -= text_buf->get_size().height; } float icon_ofs_region = 0.0; Point2 style_offset; if (icon_align_rtl_checked == HORIZONTAL_ALIGNMENT_LEFT) { style_offset.x = style->get_margin(SIDE_LEFT); if (_internal_margin[SIDE_LEFT] > 0) { icon_ofs_region = _internal_margin[SIDE_LEFT] + theme_cache.h_separation; } } else if (icon_align_rtl_checked == HORIZONTAL_ALIGNMENT_CENTER) { style_offset.x = 0.0; } else if (icon_align_rtl_checked == HORIZONTAL_ALIGNMENT_RIGHT) { style_offset.x = -style->get_margin(SIDE_RIGHT); if (_internal_margin[SIDE_RIGHT] > 0) { icon_ofs_region = -_internal_margin[SIDE_RIGHT] - theme_cache.h_separation; } } style_offset.y = style->get_margin(SIDE_TOP); if (expand_icon) { Size2 _size = get_size() - style->get_offset() * 2; int icon_text_separation = text.is_empty() ? 0 : theme_cache.h_separation; _size.width -= icon_text_separation + icon_ofs_region; if (!is_clipped && icon_align_rtl_checked != HORIZONTAL_ALIGNMENT_CENTER) { _size.width -= text_buf->get_size().width; } if (vertical_icon_alignment != VERTICAL_ALIGNMENT_CENTER) { _size.height -= text_buf->get_size().height; } float icon_width = _icon->get_width() * _size.height / _icon->get_height(); float icon_height = _size.height; if (icon_width > _size.width) { icon_width = _size.width; icon_height = _icon->get_height() * icon_width / _icon->get_width(); } icon_size = Size2(icon_width, icon_height); } icon_size = _fit_icon_size(icon_size); if (vertical_icon_alignment == VERTICAL_ALIGNMENT_TOP) { voffset = -(valign - icon_size.y) / 2; } if (vertical_icon_alignment == VERTICAL_ALIGNMENT_BOTTOM) { voffset = (valign - icon_size.y) / 2 + text_buf->get_size().y; } if (icon_align_rtl_checked == HORIZONTAL_ALIGNMENT_LEFT) { icon_region = Rect2(style_offset + Point2(icon_ofs_region, voffset + Math::floor((valign - icon_size.y) * 0.5)), icon_size); } else if (icon_align_rtl_checked == HORIZONTAL_ALIGNMENT_CENTER) { icon_region = Rect2(style_offset + Point2(icon_ofs_region + Math::floor((size.x - icon_size.x) * 0.5), voffset + Math::floor((valign - icon_size.y) * 0.5)), icon_size); } else { icon_region = Rect2(style_offset + Point2(icon_ofs_region + size.x - icon_size.x, voffset + Math::floor((valign - icon_size.y) * 0.5)), icon_size); } if (icon_region.size.width > 0) { Rect2 icon_region_rounded = Rect2(icon_region.position.round(), icon_region.size.round()); draw_texture_rect(_icon, icon_region_rounded, false, color_icon); } } Point2 icon_ofs = !_icon.is_null() ? Point2(icon_region.size.width + theme_cache.h_separation, 0) : Point2(); if (align_rtl_checked == HORIZONTAL_ALIGNMENT_CENTER && icon_align_rtl_checked == HORIZONTAL_ALIGNMENT_CENTER) { icon_ofs.x = 0.0; } int text_clip = size.width - style->get_minimum_size().width - icon_ofs.width; if (_internal_margin[SIDE_LEFT] > 0) { text_clip -= _internal_margin[SIDE_LEFT] + theme_cache.h_separation; } if (_internal_margin[SIDE_RIGHT] > 0) { text_clip -= _internal_margin[SIDE_RIGHT] + theme_cache.h_separation; } text_buf->set_width(is_clipped ? text_clip : -1); int text_width = MAX(1, is_clipped ? MIN(text_clip, text_buf->get_size().x) : text_buf->get_size().x); Point2 text_ofs = (size - style->get_minimum_size() - icon_ofs - text_buf->get_size() - Point2(_internal_margin[SIDE_RIGHT] - _internal_margin[SIDE_LEFT], 0)) / 2.0; if (vertical_icon_alignment == VERTICAL_ALIGNMENT_TOP) { text_ofs.y += icon_region.size.height / 2; } if (vertical_icon_alignment == VERTICAL_ALIGNMENT_BOTTOM) { text_ofs.y -= icon_region.size.height / 2; } text_buf->set_alignment(align_rtl_checked); text_buf->set_width(text_width); switch (align_rtl_checked) { case HORIZONTAL_ALIGNMENT_FILL: case HORIZONTAL_ALIGNMENT_LEFT: { if (icon_align_rtl_checked != HORIZONTAL_ALIGNMENT_LEFT) { icon_ofs.x = 0.0; } if (_internal_margin[SIDE_LEFT] > 0) { text_ofs.x = style->get_margin(SIDE_LEFT) + icon_ofs.x + _internal_margin[SIDE_LEFT] + theme_cache.h_separation; } else { text_ofs.x = style->get_margin(SIDE_LEFT) + icon_ofs.x; } text_ofs.y += style->get_offset().y; } break; case HORIZONTAL_ALIGNMENT_CENTER: { if (text_ofs.x < 0) { text_ofs.x = 0; } if (icon_align_rtl_checked == HORIZONTAL_ALIGNMENT_LEFT) { text_ofs += icon_ofs; } text_ofs += style->get_offset(); } break; case HORIZONTAL_ALIGNMENT_RIGHT: { if (_internal_margin[SIDE_RIGHT] > 0) { text_ofs.x = size.x - style->get_margin(SIDE_RIGHT) - text_width - _internal_margin[SIDE_RIGHT] - theme_cache.h_separation; } else { text_ofs.x = size.x - style->get_margin(SIDE_RIGHT) - text_width; } text_ofs.y += style->get_offset().y; if (icon_align_rtl_checked == HORIZONTAL_ALIGNMENT_RIGHT) { text_ofs.x -= icon_ofs.x; } } break; } Color font_outline_color = theme_cache.font_outline_color; int outline_size = theme_cache.outline_size; if (outline_size > 0 && font_outline_color.a > 0) { text_buf->draw_outline(ci, text_ofs, outline_size, font_outline_color); } text_buf->draw(ci, text_ofs, color); } break; } } Size2 Button::_fit_icon_size(const Size2 &p_size) const { int max_width = theme_cache.icon_max_width; Size2 icon_size = p_size; if (max_width > 0 && icon_size.width > max_width) { icon_size.height = icon_size.height * max_width / icon_size.width; icon_size.width = max_width; } return icon_size; } Size2 Button::get_minimum_size_for_text_and_icon(const String &p_text, Ref p_icon) const { Ref paragraph; if (p_text.is_empty()) { paragraph = text_buf; } else { paragraph.instantiate(); const_cast