Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Add icon_max_width to SpinBox

Kasper Arnklit Frandsen18a86e3880c1
diff --git a/doc/classes/SpinBox.xml b/doc/classes/SpinBox.xml
index 8ab2c2150fc..8f7befaa165 100644
--- a/doc/classes/SpinBox.xml
+++ b/doc/classes/SpinBox.xml
@@ -122,6 +122,9 @@
<theme_item name="field_and_buttons_separation" data_type="constant" type="int" default="2">
Width of the horizontal separation between the text input field ([LineEdit]) and the buttons.
</theme_item>
+ <theme_item name="icon_max_width" data_type="constant" type="int" default="0">
+ The maximum allowed width of the [SpinBox]'s up and down icons. The height is adjusted according to the icon's ratio.
+ </theme_item>
<theme_item name="set_min_buttons_width_from_icons" data_type="constant" type="int" default="1" deprecated="This property exists only for compatibility with older themes. Setting [theme_item buttons_width] to a negative value has the same effect.">
If not [code]0[/code], the minimum button width corresponds to the widest of all icons set on those buttons, even if [theme_item buttons_width] is smaller.
</theme_item>
diff --git a/scene/gui/spin_box.cpp b/scene/gui/spin_box.cpp
index ed382f4634e..8d7b0bba0ee 100644
--- a/scene/gui/spin_box.cpp
+++ b/scene/gui/spin_box.cpp
@@ -457,19 +457,22 @@ inline void SpinBox::_compute_sizes() {
}
inline int SpinBox::_get_widest_button_icon_width() {
- int max = 0;
+ int width = 0;
#ifndef DISABLE_DEPRECATED
- max = MAX(max, theme_cache.updown_icon->get_width());
+ width = MAX(width, theme_cache.updown_icon->get_width());
#endif
- max = MAX(max, theme_cache.up_icon->get_width());
- max = MAX(max, theme_cache.up_hover_icon->get_width());
- max = MAX(max, theme_cache.up_pressed_icon->get_width());
- max = MAX(max, theme_cache.up_disabled_icon->get_width());
- max = MAX(max, theme_cache.down_icon->get_width());
- max = MAX(max, theme_cache.down_hover_icon->get_width());
- max = MAX(max, theme_cache.down_pressed_icon->get_width());
- max = MAX(max, theme_cache.down_disabled_icon->get_width());
- return max;
+ width = MAX(width, theme_cache.up_icon->get_width());
+ width = MAX(width, theme_cache.up_hover_icon->get_width());
+ width = MAX(width, theme_cache.up_pressed_icon->get_width());
+ width = MAX(width, theme_cache.up_disabled_icon->get_width());
+ width = MAX(width, theme_cache.down_icon->get_width());
+ width = MAX(width, theme_cache.down_hover_icon->get_width());
+ width = MAX(width, theme_cache.down_pressed_icon->get_width());
+ width = MAX(width, theme_cache.down_disabled_icon->get_width());
+ if (theme_cache.icon_max_width > 0) {
+ width = MIN(width, theme_cache.icon_max_width);
+ }
+ return width;
}
void SpinBox::_notification(int p_what) {
@@ -517,11 +520,14 @@ void SpinBox::_notification(int p_what) {
down_icon_modulate = theme_cache.down_hover_icon_modulate;
}
+ Size2 up_icon_size = _fit_icon_size(up_icon->get_size()).round();
+ Size2 down_icon_size = _fit_icon_size(down_icon->get_size()).round();
+
// Compute center icon positions once we know which one is used.
- int up_icon_left = sizing_cache.buttons_left + (sizing_cache.buttons_width - up_icon->get_width()) / 2;
- int up_icon_top = (sizing_cache.button_up_height - up_icon->get_height()) / 2;
- int down_icon_left = sizing_cache.buttons_left + (sizing_cache.buttons_width - down_icon->get_width()) / 2;
- int down_icon_top = sizing_cache.second_button_top + (sizing_cache.button_down_height - down_icon->get_height()) / 2;
+ int up_icon_left = sizing_cache.buttons_left + (sizing_cache.buttons_width - up_icon_size.width) / 2;
+ int up_icon_top = (sizing_cache.button_up_height - up_icon_size.height) / 2;
+ int down_icon_left = sizing_cache.buttons_left + (sizing_cache.buttons_width - down_icon_size.width) / 2;
+ int down_icon_top = sizing_cache.second_button_top + (sizing_cache.button_down_height - down_icon_size.height) / 2;
// Draw separators.
draw_style_box(theme_cache.up_down_buttons_separator, Rect2(sizing_cache.buttons_left, sizing_cache.buttons_separator_top, sizing_cache.buttons_width, sizing_cache.buttons_vertical_separation));
@@ -541,8 +547,8 @@ void SpinBox::_notification(int p_what) {
}
#endif
// Draw arrows.
- draw_texture(up_icon, Point2i(up_icon_left, up_icon_top), up_icon_modulate);
- draw_texture(down_icon, Point2i(down_icon_left, down_icon_top), down_icon_modulate);
+ draw_texture_rect(up_icon, Rect2(Point2i(up_icon_left, up_icon_top), up_icon_size));
+ draw_texture_rect(down_icon, Rect2(Point2i(down_icon_left, down_icon_top), down_icon_size));
} break;
@@ -588,6 +594,18 @@ void SpinBox::_notification(int p_what) {
}
}
+Size2 SpinBox::_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;
+}
+
void SpinBox::set_horizontal_alignment(HorizontalAlignment p_alignment) {
line_edit->set_horizontal_alignment(p_alignment);
}
@@ -821,6 +839,7 @@ void SpinBox::_bind_methods() {
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, SpinBox, buttons_vertical_separation);
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, SpinBox, field_and_buttons_separation);
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, SpinBox, buttons_width);
+ BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, SpinBox, icon_max_width);
#ifndef DISABLE_DEPRECATED
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, SpinBox, set_min_buttons_width_from_icons);
diff --git a/scene/gui/spin_box.h b/scene/gui/spin_box.h
index e4ee32da8bd..a52ee6bc696 100644
--- a/scene/gui/spin_box.h
+++ b/scene/gui/spin_box.h
@@ -148,6 +148,7 @@ class SpinBox : public Range {
int buttons_vertical_separation = 0;
int field_and_buttons_separation = 0;
int buttons_width = 0;
+ int icon_max_width = 0;
#ifndef DISABLE_DEPRECATED
Ref<Texture2D> updown_icon;
bool is_updown_assigned = false;
@@ -165,6 +166,7 @@ protected:
void _validate_property(PropertyInfo &p_property) const;
void _notification(int p_what);
+ Size2 _fit_icon_size(const Size2 &p_size) const;
static void _bind_methods();
public: