Make TextureButton and Button update on texture change

This commit is contained in:
Ninni Pipping
2023-05-17 14:49:59 +02:00
parent 294b1a731a
commit d4ac3b6ded
4 changed files with 53 additions and 31 deletions

View File

@ -30,6 +30,7 @@
#include "texture_button.h"
#include "core/core_string_names.h"
#include "core/typedefs.h"
#include <stdlib.h>
@ -294,42 +295,19 @@ void TextureButton::_bind_methods() {
}
void TextureButton::set_texture_normal(const Ref<Texture2D> &p_normal) {
if (normal == p_normal) {
return;
}
normal = p_normal;
queue_redraw();
update_minimum_size();
_set_texture(&normal, p_normal);
}
void TextureButton::set_texture_pressed(const Ref<Texture2D> &p_pressed) {
if (pressed == p_pressed) {
return;
}
pressed = p_pressed;
queue_redraw();
update_minimum_size();
_set_texture(&pressed, p_pressed);
}
void TextureButton::set_texture_hover(const Ref<Texture2D> &p_hover) {
if (hover == p_hover) {
return;
}
hover = p_hover;
queue_redraw();
update_minimum_size();
_set_texture(&hover, p_hover);
}
void TextureButton::set_texture_disabled(const Ref<Texture2D> &p_disabled) {
if (disabled == p_disabled) {
return;
}
disabled = p_disabled;
queue_redraw();
_set_texture(&disabled, p_disabled);
}
void TextureButton::set_click_mask(const Ref<BitMap> &p_click_mask) {
@ -337,8 +315,7 @@ void TextureButton::set_click_mask(const Ref<BitMap> &p_click_mask) {
return;
}
click_mask = p_click_mask;
queue_redraw();
update_minimum_size();
_texture_changed();
}
Ref<Texture2D> TextureButton::get_texture_normal() const {
@ -369,6 +346,28 @@ void TextureButton::set_texture_focused(const Ref<Texture2D> &p_focused) {
focused = p_focused;
};
void TextureButton::_set_texture(Ref<Texture2D> *p_destination, const Ref<Texture2D> &p_texture) {
DEV_ASSERT(p_destination);
Ref<Texture2D> &destination = *p_destination;
if (destination == p_texture) {
return;
}
if (destination.is_valid()) {
destination->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &TextureButton::_texture_changed));
}
destination = p_texture;
if (destination.is_valid()) {
// Pass `CONNECT_REFERENCE_COUNTED` to avoid early disconnect in case the same texture is assigned to different "slots".
destination->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &TextureButton::_texture_changed), CONNECT_REFERENCE_COUNTED);
}
_texture_changed();
}
void TextureButton::_texture_changed() {
queue_redraw();
update_minimum_size();
}
bool TextureButton::get_ignore_texture_size() const {
return ignore_texture_size;
}