diff --git a/scene/gui/button.cpp b/scene/gui/button.cpp index e3915b4e4b6..df7ae2c93c4 100644 --- a/scene/gui/button.cpp +++ b/scene/gui/button.cpp @@ -57,73 +57,39 @@ String Button::_get_translated_text(const String &p_text) const { void Button::_update_theme_item_cache() { Control::_update_theme_item_cache(); + theme_cache.max_style_size = Vector2(); + theme_cache.style_margin_left = 0; + theme_cache.style_margin_right = 0; + theme_cache.style_margin_top = 0; + theme_cache.style_margin_bottom = 0; + const bool rtl = is_layout_rtl(); if (rtl && has_theme_stylebox(SNAME("normal_mirrored"))) { - theme_cache.max_style_size = theme_cache.normal_mirrored->get_minimum_size(); - theme_cache.style_margin_left = theme_cache.normal_mirrored->get_margin(SIDE_LEFT); - theme_cache.style_margin_right = theme_cache.normal_mirrored->get_margin(SIDE_RIGHT); - theme_cache.style_margin_top = theme_cache.normal_mirrored->get_margin(SIDE_TOP); - theme_cache.style_margin_bottom = theme_cache.normal_mirrored->get_margin(SIDE_BOTTOM); + _update_style_margins(theme_cache.normal_mirrored); } else { - theme_cache.max_style_size = theme_cache.normal->get_minimum_size(); - theme_cache.style_margin_left = theme_cache.normal->get_margin(SIDE_LEFT); - theme_cache.style_margin_right = theme_cache.normal->get_margin(SIDE_RIGHT); - theme_cache.style_margin_top = theme_cache.normal->get_margin(SIDE_TOP); - theme_cache.style_margin_bottom = theme_cache.normal->get_margin(SIDE_BOTTOM); + _update_style_margins(theme_cache.normal); } if (has_theme_stylebox("hover_pressed")) { if (rtl && has_theme_stylebox(SNAME("hover_pressed_mirrored"))) { - theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.hover_pressed_mirrored->get_minimum_size()); - theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.hover_pressed_mirrored->get_margin(SIDE_LEFT)); - theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.hover_pressed_mirrored->get_margin(SIDE_RIGHT)); - theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.hover_pressed_mirrored->get_margin(SIDE_TOP)); - theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.hover_pressed_mirrored->get_margin(SIDE_BOTTOM)); + _update_style_margins(theme_cache.hover_pressed_mirrored); } else { - theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.hover_pressed->get_minimum_size()); - theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.hover_pressed->get_margin(SIDE_LEFT)); - theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.hover_pressed->get_margin(SIDE_RIGHT)); - theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.hover_pressed->get_margin(SIDE_TOP)); - theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.hover_pressed->get_margin(SIDE_BOTTOM)); + _update_style_margins(theme_cache.hover_pressed); } } if (rtl && has_theme_stylebox(SNAME("pressed_mirrored"))) { - theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.pressed_mirrored->get_minimum_size()); - theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.pressed_mirrored->get_margin(SIDE_LEFT)); - theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.pressed_mirrored->get_margin(SIDE_RIGHT)); - theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.pressed_mirrored->get_margin(SIDE_TOP)); - theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.pressed_mirrored->get_margin(SIDE_BOTTOM)); + _update_style_margins(theme_cache.pressed_mirrored); } else { - theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.pressed->get_minimum_size()); - theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.pressed->get_margin(SIDE_LEFT)); - theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.pressed->get_margin(SIDE_RIGHT)); - theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.pressed->get_margin(SIDE_TOP)); - theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.pressed->get_margin(SIDE_BOTTOM)); + _update_style_margins(theme_cache.pressed); } if (rtl && has_theme_stylebox(SNAME("hover_mirrored"))) { - theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.hover_mirrored->get_minimum_size()); - theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.hover_mirrored->get_margin(SIDE_LEFT)); - theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.hover_mirrored->get_margin(SIDE_RIGHT)); - theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.hover_mirrored->get_margin(SIDE_TOP)); - theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.hover_mirrored->get_margin(SIDE_BOTTOM)); + _update_style_margins(theme_cache.hover_mirrored); } else { - theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.hover->get_minimum_size()); - theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.hover->get_margin(SIDE_LEFT)); - theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.hover->get_margin(SIDE_RIGHT)); - theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.hover->get_margin(SIDE_TOP)); - theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.hover->get_margin(SIDE_BOTTOM)); + _update_style_margins(theme_cache.hover); } if (rtl && has_theme_stylebox(SNAME("disabled_mirrored"))) { - theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.disabled_mirrored->get_minimum_size()); - theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.disabled_mirrored->get_margin(SIDE_LEFT)); - theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.disabled_mirrored->get_margin(SIDE_RIGHT)); - theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.disabled_mirrored->get_margin(SIDE_TOP)); - theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.disabled_mirrored->get_margin(SIDE_BOTTOM)); + _update_style_margins(theme_cache.disabled_mirrored); } else { - theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.disabled->get_minimum_size()); - theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.disabled->get_margin(SIDE_LEFT)); - theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.disabled->get_margin(SIDE_RIGHT)); - theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.disabled->get_margin(SIDE_TOP)); - theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.disabled->get_margin(SIDE_BOTTOM)); + _update_style_margins(theme_cache.disabled); } theme_cache.max_style_size = theme_cache.max_style_size.max(Vector2(theme_cache.style_margin_left + theme_cache.style_margin_right, theme_cache.style_margin_top + theme_cache.style_margin_bottom)); } @@ -717,6 +683,14 @@ void Button::_texture_changed() { update_minimum_size(); } +void Button::_update_style_margins(const Ref &p_stylebox) { + theme_cache.max_style_size = theme_cache.max_style_size.max(p_stylebox->get_minimum_size()); + theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, p_stylebox->get_margin(SIDE_LEFT)); + theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, p_stylebox->get_margin(SIDE_RIGHT)); + theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, p_stylebox->get_margin(SIDE_TOP)); + theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, p_stylebox->get_margin(SIDE_BOTTOM)); +} + Ref Button::get_button_icon() const { return icon; } diff --git a/scene/gui/button.h b/scene/gui/button.h index 1c2b02b4863..1f7711ec85e 100644 --- a/scene/gui/button.h +++ b/scene/gui/button.h @@ -105,6 +105,7 @@ private: void _shape(Ref p_paragraph = Ref(), String p_text = "") const; void _texture_changed(); + void _update_style_margins(const Ref &p_stylebox); protected: virtual void _update_theme_item_cache() override;