From d0a98e1ddcb68a9b0057d863ec09317936c92b2e 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 b4e8b388511..85542879ae6 100644 --- a/scene/gui/button.cpp +++ b/scene/gui/button.cpp @@ -31,6 +31,7 @@ #include "button.h" #include "core/translation.h" +#include "scene/scene_string_names.h" #include "servers/visual_server.h" Size2 Button::get_minimum_size() const { @@ -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 82e5ba81985..c3ad26202d8 100644 --- a/scene/gui/button.h +++ b/scene/gui/button.h @@ -54,6 +54,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 e193ec5262e..b8c653846db 100644 --- a/scene/gui/texture_button.cpp +++ b/scene/gui/texture_button.cpp @@ -29,7 +29,10 @@ /**************************************************************************/ #include "texture_button.h" + #include "core/typedefs.h" +#include "scene/scene_string_names.h" + #include Size2 TextureButton::get_minimum_size() const { @@ -272,6 +275,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"); @@ -294,25 +299,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(); @@ -343,6 +344,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 7f9bbeb8013..81b19b3402f 100644 --- a/scene/gui/texture_button.h +++ b/scene/gui/texture_button.h @@ -64,6 +64,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;