summaryrefslogtreecommitdiffstats
path: root/scene/gui/button.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'scene/gui/button.cpp')
-rw-r--r--scene/gui/button.cpp44
1 files changed, 34 insertions, 10 deletions
diff --git a/scene/gui/button.cpp b/scene/gui/button.cpp
index 3bf9d79c7f..55ca5fc3ee 100644
--- a/scene/gui/button.cpp
+++ b/scene/gui/button.cpp
@@ -121,6 +121,7 @@ void Button::_update_theme_item_cache() {
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));
}
Size2 Button::_get_largest_stylebox_size() const {
@@ -214,9 +215,10 @@ void Button::_notification(int p_what) {
const RID ci = get_canvas_item();
const Size2 size = get_size();
+ Ref<StyleBox> style = _get_current_stylebox();
// Draws the stylebox in the current state.
if (!flat) {
- _get_current_stylebox()->draw(ci, Rect2(Point2(), size));
+ style->draw(ci, Rect2(Point2(), size));
}
if (has_focus()) {
@@ -232,18 +234,23 @@ void Button::_notification(int p_what) {
break;
}
+ const float style_margin_left = (theme_cache.align_to_largest_stylebox) ? theme_cache.style_margin_left : style->get_margin(SIDE_LEFT);
+ const float style_margin_right = (theme_cache.align_to_largest_stylebox) ? theme_cache.style_margin_right : style->get_margin(SIDE_RIGHT);
+ const float style_margin_top = (theme_cache.align_to_largest_stylebox) ? theme_cache.style_margin_top : style->get_margin(SIDE_TOP);
+ const float style_margin_bottom = (theme_cache.align_to_largest_stylebox) ? theme_cache.style_margin_bottom : style->get_margin(SIDE_BOTTOM);
+
Size2 drawable_size_remained = size;
{ // The size after the stelybox is stripped.
- drawable_size_remained.width -= theme_cache.style_margin_left + theme_cache.style_margin_right;
- drawable_size_remained.height -= theme_cache.style_margin_top + theme_cache.style_margin_bottom;
+ drawable_size_remained.width -= style_margin_left + style_margin_right;
+ drawable_size_remained.height -= style_margin_top + style_margin_bottom;
}
const int h_separation = MAX(0, theme_cache.h_separation);
float left_internal_margin_with_h_separation = _internal_margin[SIDE_LEFT];
float right_internal_margin_with_h_separation = _internal_margin[SIDE_RIGHT];
- { // The width reserved for internal element in derived classes (and h_separation if need).
+ { // The width reserved for internal element in derived classes (and h_separation if needed).
if (_internal_margin[SIDE_LEFT] > 0.0f) {
left_internal_margin_with_h_separation += h_separation;
@@ -381,12 +388,12 @@ void Button::_notification(int p_what) {
[[fallthrough]];
case HORIZONTAL_ALIGNMENT_FILL:
case HORIZONTAL_ALIGNMENT_LEFT: {
- icon_ofs.x += theme_cache.style_margin_left;
+ icon_ofs.x += style_margin_left;
icon_ofs.x += left_internal_margin_with_h_separation;
} break;
case HORIZONTAL_ALIGNMENT_RIGHT: {
- icon_ofs.x = size.x - theme_cache.style_margin_right;
+ icon_ofs.x = size.x - style_margin_right;
icon_ofs.x -= right_internal_margin_with_h_separation;
icon_ofs.x -= icon_size.width;
} break;
@@ -399,11 +406,11 @@ void Button::_notification(int p_what) {
[[fallthrough]];
case VERTICAL_ALIGNMENT_FILL:
case VERTICAL_ALIGNMENT_TOP: {
- icon_ofs.y += theme_cache.style_margin_top;
+ icon_ofs.y += style_margin_top;
} break;
case VERTICAL_ALIGNMENT_BOTTOM: {
- icon_ofs.y = size.y - theme_cache.style_margin_bottom - icon_size.height;
+ icon_ofs.y = size.y - style_margin_bottom - icon_size.height;
} break;
}
icon_ofs = icon_ofs.floor();
@@ -442,7 +449,7 @@ void Button::_notification(int p_what) {
case HORIZONTAL_ALIGNMENT_FILL:
case HORIZONTAL_ALIGNMENT_LEFT:
case HORIZONTAL_ALIGNMENT_RIGHT: {
- text_ofs.x += theme_cache.style_margin_left;
+ text_ofs.x += style_margin_left;
text_ofs.x += left_internal_margin_with_h_separation;
if (icon_align_rtl_checked == HORIZONTAL_ALIGNMENT_LEFT) {
// Offset by the space's width that occupied by icon and h_separation together.
@@ -451,7 +458,7 @@ void Button::_notification(int p_what) {
} break;
}
- text_ofs.y = (drawable_size_remained.height - text_buf->get_size().height) / 2.0f + theme_cache.style_margin_top;
+ text_ofs.y = (drawable_size_remained.height - text_buf->get_size().height) / 2.0f + style_margin_top;
if (vertical_icon_alignment == VERTICAL_ALIGNMENT_TOP) {
text_ofs.y += custom_element_size.height - drawable_size_remained.height; // Offset by the icon's height.
}
@@ -493,6 +500,21 @@ Size2 Button::get_minimum_size_for_text_and_icon(const String &p_text, Ref<Textu
minsize.width = 0;
}
+ float left_internal_margin_with_h_separation = _internal_margin[SIDE_LEFT];
+ float right_internal_margin_with_h_separation = _internal_margin[SIDE_RIGHT];
+ { // The width reserved for internal element in derived classes (and h_separation if needed).
+
+ if (_internal_margin[SIDE_LEFT] > 0.0f) {
+ left_internal_margin_with_h_separation += theme_cache.h_separation;
+ }
+
+ if (_internal_margin[SIDE_RIGHT] > 0.0f) {
+ right_internal_margin_with_h_separation += theme_cache.h_separation;
+ }
+
+ minsize.width += left_internal_margin_with_h_separation + right_internal_margin_with_h_separation; // The size after the internal element is stripped.
+ }
+
if (!expand_icon && p_icon.is_valid()) {
Size2 icon_size = _fit_icon_size(p_icon->get_size());
if (vertical_icon_alignment == VERTICAL_ALIGNMENT_CENTER) {
@@ -828,6 +850,8 @@ void Button::_bind_methods() {
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Button, h_separation);
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Button, icon_max_width);
+
+ BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Button, align_to_largest_stylebox);
}
Button::Button(const String &p_text) {