Merge pull request #93389 from KoBeWi/MASSIVE_copy_paste

Remove code duplication in Button
This commit is contained in:
Thaddeus Crews 2025-09-22 13:28:47 -05:00
commit 2d94ad1f3a
No known key found for this signature in database
GPG key ID: 8C6E5FEB5FC03CCC
2 changed files with 25 additions and 50 deletions

View file

@ -57,73 +57,39 @@ String Button::_get_translated_text(const String &p_text) const {
void Button::_update_theme_item_cache() { void Button::_update_theme_item_cache() {
Control::_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(); const bool rtl = is_layout_rtl();
if (rtl && has_theme_stylebox(SNAME("normal_mirrored"))) { if (rtl && has_theme_stylebox(SNAME("normal_mirrored"))) {
theme_cache.max_style_size = theme_cache.normal_mirrored->get_minimum_size(); _update_style_margins(theme_cache.normal_mirrored);
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);
} else { } else {
theme_cache.max_style_size = theme_cache.normal->get_minimum_size(); _update_style_margins(theme_cache.normal);
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);
} }
if (has_theme_stylebox("hover_pressed")) { if (has_theme_stylebox("hover_pressed")) {
if (rtl && has_theme_stylebox(SNAME("hover_pressed_mirrored"))) { 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()); _update_style_margins(theme_cache.hover_pressed_mirrored);
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));
} else { } else {
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.hover_pressed->get_minimum_size()); _update_style_margins(theme_cache.hover_pressed);
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));
} }
} }
if (rtl && has_theme_stylebox(SNAME("pressed_mirrored"))) { 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()); _update_style_margins(theme_cache.pressed_mirrored);
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));
} else { } else {
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.pressed->get_minimum_size()); _update_style_margins(theme_cache.pressed);
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));
} }
if (rtl && has_theme_stylebox(SNAME("hover_mirrored"))) { 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()); _update_style_margins(theme_cache.hover_mirrored);
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));
} else { } else {
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.hover->get_minimum_size()); _update_style_margins(theme_cache.hover);
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));
} }
if (rtl && has_theme_stylebox(SNAME("disabled_mirrored"))) { 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()); _update_style_margins(theme_cache.disabled_mirrored);
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));
} else { } else {
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.disabled->get_minimum_size()); _update_style_margins(theme_cache.disabled);
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));
} }
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)); 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(); update_minimum_size();
} }
void Button::_update_style_margins(const Ref<StyleBox> &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<Texture2D> Button::get_button_icon() const { Ref<Texture2D> Button::get_button_icon() const {
return icon; return icon;
} }

View file

@ -105,6 +105,7 @@ private:
void _shape(Ref<TextParagraph> p_paragraph = Ref<TextParagraph>(), String p_text = "") const; void _shape(Ref<TextParagraph> p_paragraph = Ref<TextParagraph>(), String p_text = "") const;
void _texture_changed(); void _texture_changed();
void _update_style_margins(const Ref<StyleBox> &p_stylebox);
protected: protected:
virtual void _update_theme_item_cache() override; virtual void _update_theme_item_cache() override;