From 3f6fdafd417720c5e942eab1d3837471573dd522 Mon Sep 17 00:00:00 2001 From: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com> Date: Tue, 29 Aug 2023 11:26:05 +0200 Subject: [PATCH] Make `TextureButton` and `Button` update on texture change --- scene/gui/button.cpp | 14 ++++++++++++ scene/gui/button.h | 2 ++ scene/gui/texture_button.cpp | 44 +++++++++++++++++++++++++++--------- scene/gui/texture_button.h | 3 +++ 4 files changed, 52 insertions(+), 11 deletions(-) diff --git a/scene/gui/button.cpp b/scene/gui/button.cpp index 5e2349da9..663f066c1 100644 --- a/scene/gui/button.cpp +++ b/scene/gui/button.cpp @@ -32,6 +32,7 @@ #include "core/string/translation.h" #include "servers/rendering_server.h" +#include "scene/scene_string_names.h" Size2 Button::get_minimum_size() const { Size2 minsize = get_theme_font("font")->total_size_of_lines(xl_text.split("\n")); @@ -309,7 +310,13 @@ void Button::set_icon(const Ref &p_icon) { if (icon == p_icon) { return; } + if (icon.is_valid()) { + icon->disconnect(SceneStringNames::get_singleton()->changed, this, "_texture_changed"); + } icon = p_icon; + if (icon.is_valid()) { + icon->connect(SceneStringNames::get_singleton()->changed, this, "_texture_changed"); + } update(); _change_notify("icon"); minimum_size_changed(); @@ -319,6 +326,11 @@ Ref Button::get_icon() const { return icon; } +void Button::_texture_changed() { + update(); + minimum_size_changed(); +} + void Button::set_expand_icon(bool p_expand_icon) { expand_icon = p_expand_icon; update(); @@ -384,6 +396,8 @@ void Button::_bind_methods() { ClassDB::bind_method(D_METHOD("set_expand_icon", "enabled"), &Button::set_expand_icon); ClassDB::bind_method(D_METHOD("is_expand_icon"), &Button::is_expand_icon); + ClassDB::bind_method(D_METHOD("_texture_changed"), &Button::_texture_changed); + BIND_ENUM_CONSTANT(ALIGN_LEFT); BIND_ENUM_CONSTANT(ALIGN_CENTER); BIND_ENUM_CONSTANT(ALIGN_RIGHT); diff --git a/scene/gui/button.h b/scene/gui/button.h index 286e3dc80..ffb2fb119 100644 --- a/scene/gui/button.h +++ b/scene/gui/button.h @@ -53,6 +53,8 @@ private: TextAlign icon_align; float _internal_margin[4]; + void _texture_changed(); + protected: void _set_internal_margin(Margin p_margin, float p_value); void _notification(int p_what); diff --git a/scene/gui/texture_button.cpp b/scene/gui/texture_button.cpp index c3398578e..eab0469c7 100644 --- a/scene/gui/texture_button.cpp +++ b/scene/gui/texture_button.cpp @@ -29,8 +29,11 @@ /*************************************************************************/ #include "texture_button.h" + #include "core/typedefs.h" #include "scene/resources/bit_map.h" +#include "scene/scene_string_names.h" + #include Size2 TextureButton::get_minimum_size() const { @@ -273,6 +276,8 @@ void TextureButton::_bind_methods() { ClassDB::bind_method(D_METHOD("get_expand"), &TextureButton::get_expand); ClassDB::bind_method(D_METHOD("get_stretch_mode"), &TextureButton::get_stretch_mode); + ClassDB::bind_method(D_METHOD("_texture_changed"), &TextureButton::_texture_changed); + ADD_GROUP("Textures", "texture_"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_normal", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_normal_texture", "get_normal_texture"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_pressed", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_pressed_texture", "get_pressed_texture"); @@ -295,25 +300,21 @@ void TextureButton::_bind_methods() { } void TextureButton::set_normal_texture(const Ref &p_normal) { - normal = p_normal; - update(); - minimum_size_changed(); + _set_texture(&normal, p_normal); } void TextureButton::set_pressed_texture(const Ref &p_pressed) { - pressed = p_pressed; - update(); - minimum_size_changed(); + _set_texture(&pressed, p_pressed); } + void TextureButton::set_hover_texture(const Ref &p_hover) { - hover = p_hover; - update(); - minimum_size_changed(); + _set_texture(&hover, p_hover); } + void TextureButton::set_disabled_texture(const Ref &p_disabled) { - disabled = p_disabled; - update(); + _set_texture(&disabled, p_disabled); } + void TextureButton::set_click_mask(const Ref &p_click_mask) { click_mask = p_click_mask; update(); @@ -344,6 +345,27 @@ void TextureButton::set_focused_texture(const Ref &p_focused) { focused = p_focused; }; +void TextureButton::_set_texture(Ref *p_destination, const Ref &p_texture) { + DEV_ASSERT(p_destination); + Ref &destination = *p_destination; + if (destination == p_texture) { + return; + } + if (destination.is_valid()) { + destination->disconnect(SceneStringNames::get_singleton()->changed, this, "_texture_changed"); + } + destination = p_texture; + if (destination.is_valid()) { + destination->connect(SceneStringNames::get_singleton()->changed, this, "_texture_changed", varray(), CONNECT_REFERENCE_COUNTED); + } + _texture_changed(); +} + +void TextureButton::_texture_changed() { + update(); + minimum_size_changed(); +} + bool TextureButton::get_expand() const { return expand; } diff --git a/scene/gui/texture_button.h b/scene/gui/texture_button.h index e8f2933de..3f356ed9d 100644 --- a/scene/gui/texture_button.h +++ b/scene/gui/texture_button.h @@ -66,6 +66,9 @@ private: bool hflip; bool vflip; + void _set_texture(Ref *p_destination, const Ref &p_texture); + void _texture_changed(); + protected: virtual Size2 get_minimum_size() const; virtual bool has_point(const Point2 &p_point) const;