Skip to content

Commit 2d94ad1

Browse files
committed
Merge pull request godotengine#93389 from KoBeWi/MASSIVE_copy_paste
Remove code duplication in Button
2 parents 89c51cb + e3ce74d commit 2d94ad1

File tree

2 files changed

+25
-50
lines changed

2 files changed

+25
-50
lines changed

scene/gui/button.cpp

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -57,73 +57,39 @@ String Button::_get_translated_text(const String &p_text) const {
5757
void Button::_update_theme_item_cache() {
5858
Control::_update_theme_item_cache();
5959

60+
theme_cache.max_style_size = Vector2();
61+
theme_cache.style_margin_left = 0;
62+
theme_cache.style_margin_right = 0;
63+
theme_cache.style_margin_top = 0;
64+
theme_cache.style_margin_bottom = 0;
65+
6066
const bool rtl = is_layout_rtl();
6167
if (rtl && has_theme_stylebox(SNAME("normal_mirrored"))) {
62-
theme_cache.max_style_size = theme_cache.normal_mirrored->get_minimum_size();
63-
theme_cache.style_margin_left = theme_cache.normal_mirrored->get_margin(SIDE_LEFT);
64-
theme_cache.style_margin_right = theme_cache.normal_mirrored->get_margin(SIDE_RIGHT);
65-
theme_cache.style_margin_top = theme_cache.normal_mirrored->get_margin(SIDE_TOP);
66-
theme_cache.style_margin_bottom = theme_cache.normal_mirrored->get_margin(SIDE_BOTTOM);
68+
_update_style_margins(theme_cache.normal_mirrored);
6769
} else {
68-
theme_cache.max_style_size = theme_cache.normal->get_minimum_size();
69-
theme_cache.style_margin_left = theme_cache.normal->get_margin(SIDE_LEFT);
70-
theme_cache.style_margin_right = theme_cache.normal->get_margin(SIDE_RIGHT);
71-
theme_cache.style_margin_top = theme_cache.normal->get_margin(SIDE_TOP);
72-
theme_cache.style_margin_bottom = theme_cache.normal->get_margin(SIDE_BOTTOM);
70+
_update_style_margins(theme_cache.normal);
7371
}
7472
if (has_theme_stylebox("hover_pressed")) {
7573
if (rtl && has_theme_stylebox(SNAME("hover_pressed_mirrored"))) {
76-
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.hover_pressed_mirrored->get_minimum_size());
77-
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.hover_pressed_mirrored->get_margin(SIDE_LEFT));
78-
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.hover_pressed_mirrored->get_margin(SIDE_RIGHT));
79-
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.hover_pressed_mirrored->get_margin(SIDE_TOP));
80-
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.hover_pressed_mirrored->get_margin(SIDE_BOTTOM));
74+
_update_style_margins(theme_cache.hover_pressed_mirrored);
8175
} else {
82-
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.hover_pressed->get_minimum_size());
83-
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.hover_pressed->get_margin(SIDE_LEFT));
84-
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.hover_pressed->get_margin(SIDE_RIGHT));
85-
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.hover_pressed->get_margin(SIDE_TOP));
86-
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.hover_pressed->get_margin(SIDE_BOTTOM));
76+
_update_style_margins(theme_cache.hover_pressed);
8777
}
8878
}
8979
if (rtl && has_theme_stylebox(SNAME("pressed_mirrored"))) {
90-
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.pressed_mirrored->get_minimum_size());
91-
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.pressed_mirrored->get_margin(SIDE_LEFT));
92-
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.pressed_mirrored->get_margin(SIDE_RIGHT));
93-
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.pressed_mirrored->get_margin(SIDE_TOP));
94-
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.pressed_mirrored->get_margin(SIDE_BOTTOM));
80+
_update_style_margins(theme_cache.pressed_mirrored);
9581
} else {
96-
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.pressed->get_minimum_size());
97-
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.pressed->get_margin(SIDE_LEFT));
98-
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.pressed->get_margin(SIDE_RIGHT));
99-
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.pressed->get_margin(SIDE_TOP));
100-
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.pressed->get_margin(SIDE_BOTTOM));
82+
_update_style_margins(theme_cache.pressed);
10183
}
10284
if (rtl && has_theme_stylebox(SNAME("hover_mirrored"))) {
103-
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.hover_mirrored->get_minimum_size());
104-
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.hover_mirrored->get_margin(SIDE_LEFT));
105-
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.hover_mirrored->get_margin(SIDE_RIGHT));
106-
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.hover_mirrored->get_margin(SIDE_TOP));
107-
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.hover_mirrored->get_margin(SIDE_BOTTOM));
85+
_update_style_margins(theme_cache.hover_mirrored);
10886
} else {
109-
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.hover->get_minimum_size());
110-
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.hover->get_margin(SIDE_LEFT));
111-
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.hover->get_margin(SIDE_RIGHT));
112-
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.hover->get_margin(SIDE_TOP));
113-
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.hover->get_margin(SIDE_BOTTOM));
87+
_update_style_margins(theme_cache.hover);
11488
}
11589
if (rtl && has_theme_stylebox(SNAME("disabled_mirrored"))) {
116-
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.disabled_mirrored->get_minimum_size());
117-
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.disabled_mirrored->get_margin(SIDE_LEFT));
118-
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.disabled_mirrored->get_margin(SIDE_RIGHT));
119-
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.disabled_mirrored->get_margin(SIDE_TOP));
120-
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.disabled_mirrored->get_margin(SIDE_BOTTOM));
90+
_update_style_margins(theme_cache.disabled_mirrored);
12191
} else {
122-
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.disabled->get_minimum_size());
123-
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.disabled->get_margin(SIDE_LEFT));
124-
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.disabled->get_margin(SIDE_RIGHT));
125-
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.disabled->get_margin(SIDE_TOP));
126-
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.disabled->get_margin(SIDE_BOTTOM));
92+
_update_style_margins(theme_cache.disabled);
12793
}
12894
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));
12995
}
@@ -717,6 +683,14 @@ void Button::_texture_changed() {
717683
update_minimum_size();
718684
}
719685

686+
void Button::_update_style_margins(const Ref<StyleBox> &p_stylebox) {
687+
theme_cache.max_style_size = theme_cache.max_style_size.max(p_stylebox->get_minimum_size());
688+
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, p_stylebox->get_margin(SIDE_LEFT));
689+
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, p_stylebox->get_margin(SIDE_RIGHT));
690+
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, p_stylebox->get_margin(SIDE_TOP));
691+
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, p_stylebox->get_margin(SIDE_BOTTOM));
692+
}
693+
720694
Ref<Texture2D> Button::get_button_icon() const {
721695
return icon;
722696
}

scene/gui/button.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class Button : public BaseButton {
105105

106106
void _shape(Ref<TextParagraph> p_paragraph = Ref<TextParagraph>(), String p_text = "") const;
107107
void _texture_changed();
108+
void _update_style_margins(const Ref<StyleBox> &p_stylebox);
108109

109110
protected:
110111
virtual void _update_theme_item_cache() override;

0 commit comments

Comments
 (0)